Skip to content

ants

A Go library that caps and reuses goroutines through a fixed-size worker pool.

Stars
14.5k
Forks
1.4k
Licence
MIT
Language
Go
Last update
2 months ago
Type
Library

What it is

ants is a Go library that implements a goroutine pool with fixed capacity. It manages and recycles large numbers of goroutines so a program can limit how many run at once. Developers use it to control concurrency in programs that would otherwise spawn goroutines without bound.

  • Cap the number of concurrent goroutines handling incoming work with ants.NewPool(10000)
  • Submit tasks with ants.Submit and let the pool run them on reused workers
  • Resize a running pool with pool.Tune to change its capacity
  • Release or reboot a pool with pool.Release, pool.ReleaseTimeout, or pool.Reboot

Great for

  • Go developers who need to limit concurrent goroutines in a service
  • Programs that submit very many short tasks and want reuse instead of constant goroutine creation
  • Teams that want panic handling and runtime resizing of the worker pool
  • Developers who want to pre-allocate pool memory for large, long-running workloads

Not ideal for

  • Non-Go projects, since the library is written for Go only
  • Workloads that need tasks to run strictly in submission order, because the README says ordering is not guaranteed
  • Developers new to Go concurrency who would benefit more from learning the basics first
Step-by-step guide: how to use antsInstall, first run, examples, tips and FAQs

How to get it

  1. 1

    Install with a package manager

    You'll need: Go (the README badge indicates Go 1.19 or newer)

    Install ants v2 (with GO111MODULE=on)

    go get -u github.com/panjf2000/ants/v2
  2. 2

    Run it

    • Install the library with go get as shown in the README
    • Create a pool with p, _ := ants.NewPool(10000)
    • Submit a task with ants.Submit(func(){})
    • Check the pkg.go.dev examples for fuller usage
  3. 3

    Get the most out of it

    • Use ants.Options and option functions to customize pool behavior in NewPool, NewPoolWithFunc, or NewPoolWithFuncGeneric
    • Enable ants.WithPreAlloc(true) only for very large pools with long-running tasks, as the README describes
    • Use pool.Tune at runtime when workload capacity needs change
    • Use pool.ReleaseTimeout to shut down with a time limit

Downloads

Strengths

  • Automatically recycles goroutines and purges overdue ones periodically, per the README
  • Handles panics so a single failing task does not crash the program, per the README
  • Offers many APIs, including dynamic capacity tuning, pool release and reboot, and a non-blocking mechanism

Trade-offs

  • The README does not state a concrete benchmark; its claim of higher performance than unlimited goroutines is unquantified
  • Tasks are not guaranteed to run in order, which may matter for some designs
  • Some optional features, such as preallocated memory, are only helpful in specific large-capacity scenarios according to the README

How it compares

ProjectStarsTypeLevelUpdatedBest for
ants (this)14.5kLibraryintermediate2 months agoGo developers who need to limit concurrent goroutines in a service
asynq13.8kLibraryintermediate17 days agoGo developers who need background jobs such as sending emails or resizing images

Stats synced from GitHub. The assessment above was drafted with AI from the project's README on 2026-10-29 and may miss recent changes — check the README before relying on a command. MIT licence: free to use, modify and distribute, including commercially, as long as you keep the copyright notice.