Skip to content
forked from panjf2000/ants

🐜🐜🐜 ants is the most powerful and reliable pooling solution for Go.

License

Notifications You must be signed in to change notification settings

phamnam2003/ants

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

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.

Attribution

This project is derived from: https://github.com/panjf2000/ants

Original Author: Andy Pan License: MIT

This fork includes significant modifications and refactoring.

πŸš€ Features

  • Managing and recycling a massive number of goroutines automatically
  • Purging overdue goroutines periodically
  • Abundant APIs: submitting tasks, getting the number of running goroutines, tuning the capacity of the pool dynamically, releasing the pool, rebooting the pool, etc.
  • Handle panic gracefully to prevent programs from crash
  • EfficientΒ inΒ memoryΒ usage and it may even achieveΒ higher performanceΒ than unlimited goroutines in Go
  • Nonblocking mechanism
  • Preallocated memory (ring buffer, optional)

πŸ’‘ How ants works

Flow Diagram

ants-flowchart-en

Activity Diagrams

🧰 How to install

go get -u github.com/phamnam2003/ants

πŸ›  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.

About

🐜🐜🐜 ants is the most powerful and reliable pooling solution for Go.

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%