# panjf2000/ants architecture

> Architecture diagram of the GitHub repository panjf2000/ants, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/panjf2000/ants
- Repository: https://github.com/panjf2000/ants

## Overview

ants is a Go goroutine-pool library. Callers configure a pool, submit tasks or arguments, and receive bounded concurrent execution; the library also supports capacity tuning, worker reclamation, panic handling, shutdown, and reboot. Multiple-pool variants add task distribution across pools. The graph preserves the documented capabilities and sampled worker/queue internals. Several source files were not excerpted, so links involving those components are limited to README evidence or marked unverified.

Main entry points: [`ants.go`](https://github.com/panjf2000/ants/blob/dev/ants.go) (Pool lifecycle), [`pool.go`](https://github.com/panjf2000/ants/blob/dev/pool.go) (Pool API), [`worker_queue.go`](https://github.com/panjf2000/ants/blob/dev/worker_queue.go) (Worker queue), [`pool_func.go`](https://github.com/panjf2000/ants/blob/dev/pool_func.go) (Function API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["Pool APIs"]
  node_pool_api["Pool API<br/>[pool.go]"]
  node_func_api["Function API<br/>[pool_func.go]"]
  node_generic_api["Generic API"]
  node_options["Pool options<br/>[options.go]"]
end

subgraph group_runtime["Pool Runtime"]
  node_pool_core["Pool lifecycle<br/>[ants.go]"]
end

subgraph group_workers["Workers And Queues"]
  node_task_worker["Task worker<br/>[worker.go]"]
  node_func_worker["Function worker<br/>[worker_func.go]"]
  node_generic_worker["Generic worker"]
  node_queue_iface["Worker queue<br/>[worker_queue.go]"]
  node_stack_queue["Stack queue<br/>[worker_stack.go]"]
  node_loop_queue["Ring queue"]
end

subgraph group_scaling["Multi-Pool And Sync"]
  node_multi_pool["Task multipool<br/>[multipool.go]"]
  node_multi_func["Function multipool<br/>[multipool_func.go]"]
  node_multi_generic["Generic multipool"]
  node_spinlock["Spin lock<br/>[spinlock.go]"]
  node_sync_package["Sync package<br/>[sync.go]"]
end

node_caller(("Go caller"))

node_caller -->|"creates pool"| node_pool_api
node_caller -->|"submits tasks"| node_pool_api
node_caller -->|"submits arguments"| node_func_api
node_caller -->|"configures pool"| node_options
node_caller -->|"tunes capacity"| node_pool_api
node_caller -->|"releases or reboots"| node_pool_api
node_pool_api -.->|"uses lifecycle"| node_pool_core
node_func_api -.->|"uses lifecycle"| node_pool_core
node_generic_api -.->|"uses lifecycle"| node_pool_core
node_pool_core -.->|"manages workers"| node_queue_iface
node_queue_iface -->|"selects stack"| node_stack_queue
node_queue_iface -->|"selects ring"| node_loop_queue
node_stack_queue -->|"finishes workers"| node_task_worker
node_loop_queue -->|"finishes workers"| node_task_worker
node_task_worker -->|"updates runtime"| node_pool_core
node_func_worker -->|"updates runtime"| node_pool_core
node_generic_worker -->|"updates runtime"| node_pool_core
node_multi_pool -->|"dispatches tasks"| node_pool_api
node_multi_func -->|"dispatches arguments"| node_func_api
node_multi_generic -.->|"dispatches arguments"| node_generic_api
node_pool_core -.->|"uses synchronization"| node_spinlock

click node_pool_api "https://github.com/panjf2000/ants/blob/dev/pool.go"
click node_func_api "https://github.com/panjf2000/ants/blob/dev/pool_func.go"
click node_generic_api "https://github.com/panjf2000/ants/blob/dev/pool_func_generic.go"
click node_options "https://github.com/panjf2000/ants/blob/dev/options.go"
click node_pool_core "https://github.com/panjf2000/ants/blob/dev/ants.go"
click node_task_worker "https://github.com/panjf2000/ants/blob/dev/worker.go"
click node_func_worker "https://github.com/panjf2000/ants/blob/dev/worker_func.go"
click node_generic_worker "https://github.com/panjf2000/ants/blob/dev/worker_func_generic.go"
click node_queue_iface "https://github.com/panjf2000/ants/blob/dev/worker_queue.go"
click node_stack_queue "https://github.com/panjf2000/ants/blob/dev/worker_stack.go"
click node_loop_queue "https://github.com/panjf2000/ants/blob/dev/worker_loop_queue.go"
click node_multi_pool "https://github.com/panjf2000/ants/blob/dev/multipool.go"
click node_multi_func "https://github.com/panjf2000/ants/blob/dev/multipool_func.go"
click node_multi_generic "https://github.com/panjf2000/ants/blob/dev/multipool_func_generic.go"
click node_spinlock "https://github.com/panjf2000/ants/blob/dev/pkg/sync/spinlock.go"
click node_sync_package "https://github.com/panjf2000/ants/blob/dev/pkg/sync/sync.go"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_pool_api,node_func_api,node_generic_api,node_options toneBlue
class node_pool_core toneAmber
class node_task_worker,node_func_worker,node_generic_worker,node_queue_iface,node_stack_queue,node_loop_queue toneMint
class node_multi_pool,node_multi_func,node_multi_generic,node_spinlock,node_sync_package toneRose
class node_caller toneIndigo
```

## Components

### Pool APIs

- [Pool API](https://github.com/panjf2000/ants/blob/dev/pool.go): `pool.go`
- [Function API](https://github.com/panjf2000/ants/blob/dev/pool_func.go): `pool_func.go`
- [Generic API](https://github.com/panjf2000/ants/blob/dev/pool_func_generic.go): `pool_func_generic.go`
- [Pool options](https://github.com/panjf2000/ants/blob/dev/options.go): `options.go`

### Pool Runtime

- [Pool lifecycle](https://github.com/panjf2000/ants/blob/dev/ants.go): `ants.go`

### Workers And Queues

- [Task worker](https://github.com/panjf2000/ants/blob/dev/worker.go): `worker.go`
- [Function worker](https://github.com/panjf2000/ants/blob/dev/worker_func.go): `worker_func.go`
- [Generic worker](https://github.com/panjf2000/ants/blob/dev/worker_func_generic.go): `worker_func_generic.go`
- [Worker queue](https://github.com/panjf2000/ants/blob/dev/worker_queue.go): `worker_queue.go`
- [Stack queue](https://github.com/panjf2000/ants/blob/dev/worker_stack.go): `worker_stack.go`
- [Ring queue](https://github.com/panjf2000/ants/blob/dev/worker_loop_queue.go): `worker_loop_queue.go`

### Multi-Pool And Sync

- [Task multipool](https://github.com/panjf2000/ants/blob/dev/multipool.go): `multipool.go`
- [Function multipool](https://github.com/panjf2000/ants/blob/dev/multipool_func.go): `multipool_func.go`
- [Generic multipool](https://github.com/panjf2000/ants/blob/dev/multipool_func_generic.go): `multipool_func_generic.go`
- [Spin lock](https://github.com/panjf2000/ants/blob/dev/pkg/sync/spinlock.go): `pkg/sync/spinlock.go`
- [Sync package](https://github.com/panjf2000/ants/blob/dev/pkg/sync/sync.go): `pkg/sync/sync.go`

### Other components

- **Go caller**

## Connections

- Go caller → Pool API: creates pool (evidence: `README.md`)
- Go caller → Pool API: submits tasks (evidence: `README.md`)
- Go caller → Function API: submits arguments (evidence: `README.md`)
- Go caller → Pool options: configures pool (evidence: `README.md`)
- Go caller → Pool API: tunes capacity (evidence: `README.md`)
- Go caller → Pool API: releases or reboots (evidence: `README.md`)
- Pool API → Pool lifecycle: uses lifecycle
- Function API → Pool lifecycle: uses lifecycle
- Generic API → Pool lifecycle: uses lifecycle
- Pool lifecycle → Worker queue: manages workers
- Worker queue → Stack queue: selects stack (evidence: `worker_queue.go`)
- Worker queue → Ring queue: selects ring (evidence: `worker_queue.go`)
- Stack queue → Task worker: finishes workers (evidence: `worker_stack.go`)
- Ring queue → Task worker: finishes workers (evidence: `worker_loop_queue.go`)
- Task worker → Pool lifecycle: updates runtime (evidence: `worker.go`)
- Function worker → Pool lifecycle: updates runtime (evidence: `worker_func.go`)
- Generic worker → Pool lifecycle: updates runtime (evidence: `worker_func_generic.go`)
- Task multipool → Pool API: dispatches tasks (evidence: `multipool.go`)
- Function multipool → Function API: dispatches arguments (evidence: `multipool_func.go`)
- Generic multipool → Generic API: dispatches arguments
- Pool lifecycle → Spin lock: uses synchronization
