ants README

The project's README from github.com/panjf2000/ants. As one markdown file: README.md.

A goroutine pool for Go


English | 中文

📖 Introduction

Library ants implements a goroutine pool with fixed capacity, managing and recycling a massive number of goroutines, allowing developers to limit the number of goroutines in your concurrent programs.

🚀 Features:

💡 How ants works

Flow Diagram

ants-flowchart-en

Activity Diagrams

🧰 How to install

For ants v1

go get -u github.com/panjf2000/ants

For ants v2 (with GO111MODULE=on)

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

🛠 How to use

Check out the examples for basic usage.

Functional options for pool

ants.Optionscontains all optional configurations of the ants pool, which allows you to customize the goroutine pool by invoking option functions to set up each configuration in NewPool/NewPoolWithFunc/NewPoolWithFuncGeneric method.

Check out ants.Options and ants.Option for more details.

Customize pool capacity

ants supports customizing the capacity of the pool. You can call the NewPool method to instantiate a Pool with a given capacity, as follows:

p, _ := ants.NewPool(10000)

Submit tasks

Tasks can be submitted by calling ants.Submit

ants.Submit(func(){})

Tune pool capacity at runtime

You can tune the capacity of ants pool at runtime with ants.Tune:

pool.Tune(1000) // Tune its capacity to 1000
pool.Tune(100000) // Tune its capacity to 100000

Don't worry about the contention problems in this case, the method here is thread-safe (or should be called goroutine-safe).

Pre-malloc goroutine queue in pool

ants allows you to pre-allocate the memory of the goroutine queue in the pool, which may get a performance enhancement under some special certain circumstances such as the scenario that requires a pool with ultra-large capacity, meanwhile, each task in goroutine lasts for a long time, in this case, pre-mallocing will reduce a lot of memory allocation in goroutine queue.

// ants will pre-malloc the whole capacity of pool when calling ants.NewPool.
p, _ := ants.NewPool(100000, ants.WithPreAlloc(true))

Release pool

pool.Release()

or

pool.ReleaseTimeout(time.Second * 3)

Reboot pool

// A pool that has been released can be still used after calling the Reboot().
pool.Reboot()

⚙️ About sequence

All tasks submitted to ants pool will not be guaranteed to be addressed in order, because those tasks scatter among a series of concurrent workers, thus those tasks would be executed concurrently.

👏 Contributors

Please read our Contributing Guidelines before opening a PR and thank you to all the developers who already made contributions to ants!

📄 License

The source code in ants is available under the MIT License.

📚 Relevant Articles

🖥 Use cases

business corporations & open-source organizations

Trusted by the following corporations/organizations.

If you're also using ants in production, please help us enrich this list by opening a pull request.

open-source software

The open-source projects below do concurrent programming with the help of ants.

All use cases:

If you have ants integrated into projects, feel free to open a pull request refreshing this list of use cases.

🔋 JetBrains OS licenses

ants has been being developed with GoLand under the free JetBrains Open Source license(s) granted by JetBrains s.r.o., hence I would like to express my thanks here.

JetBrains logo.

☕️ Buy me a coffee

Please be sure to leave your name, GitHub account, or other social media accounts when you donate by the following means so that I can add it to the list of donors as a token of my appreciation.

Buy me a coffee Patreon OpenCollective

🔋 Sponsorship

DigitalOcean Referral Badge

ants (panjf2000/ants)

Searching Database…

14.5K ·GitHub
这是什么?ants 是一个 Go 语言的协程池库:它能以固定容量管理并复用大量协程(goroutine),让你可以限制程序中同时运行的协程数量。
这是我导入到 Go 代码里用的库,还是一个可以单独运行的工具?它是一个库,不是独立工具——你需要把它导入自己的 Go 程序中,在代码里创建和管理协程池。
我怎么把它加到我的项目里?先在 GO111MODULE=on 下安装 v2 模块,然后用 ants.NewPool(size) 创建协程池,再通过 ants.Submit 提交任务。
除了 Go 之外,使用它还需要别的东西吗?只需要 Go 工具链——它是单一模块,没有任何第三方依赖,也不用配置什么服务。
我的任务会按我提交的顺序执行吗?不会——任务会分散到多个并发的工作协程上执行,所以 ants 不保证按提交顺序执行。
为什么要用协程池,而不是直接无限地开协程?每个任务都开一个协程会让内存占用暴涨;ants 改为复用工作协程,既能控制内存,在 Go 里性能甚至可能超过无限开协程的方式。
如果我的某个任务发生 panic 会怎样?ants 会妥善处理 panic,某个任务崩溃不会拖垮你的整个程序。
程序运行时我可以调整协程池的大小吗?可以——调用 ants.Tune 就能在运行时修改容量;这个方法是协程安全的,你也可以释放协程池,之后再重新启动它。
这个协程池还自带哪些功能?它提供了查询运行中协程数量的 API、定期清理过期协程、非阻塞提交,以及可选的预分配环形缓冲区,用于超大协程池。
有哪些人在生产环境中使用 ants?使用它的开源项目包括 gnet、Milvus、TDengine、SiYuan、AdGuard DNS 和 tRPC-Go,README 中还列出了在生产环境中使用它的企业和组织。
What are similar projects?conc, fasthttp, asynq, coost, jemalloc, tokio, echo, codon
Creator
panjf2000
Size
1.9 MB
Stars
14.5K
Age
8y
Commits · 90 Days