ants is a Go library that implements a goroutine pool with a set capacity. It recycles workers so concurrent programs can cap how many goroutines run at once.
Overview
The ants package provides a pool that manages a large number of goroutines under a fixed limit, aimed at programs that would otherwise spawn unbounded goroutines. It automatically recycles workers and can purge overdue goroutines on a schedule.
You submit work with pool APIs, inspect how many goroutines are active, change capacity at runtime, and release or reboot the pool when needed. Panics inside workers are handled so they do not take down the whole program. An optional ring-buffer preallocation can cut allocation overhead for very large pools with long-running tasks.
Task order is not guaranteed because work is spread across concurrent workers. The library offers a nonblocking submission mode and is used as a dependency in many production Go codebases for HTTP services, databases, and networking stacks.
Install it as a Go module dependency; configuration is done through constructor options and functional option helpers rather than a standalone server.
Key features
- Fixed-capacity goroutine pool
- Automatic worker recycling
- Periodic purge of stale workers
- Runtime pool capacity tuning
- Pool release and reboot
- Graceful panic handling
- Optional preallocated task queue
- Nonblocking task submission
Use cases
- Limit goroutines in web services
- Bound concurrency in data pipelines
- Worker pools for RPC frameworks
- Throttled background job processing
- High-volume network event handling