As an open-source project, go-playground/pool has picked up 721 stars on GitHub (Go). :speedboat: a limited consumer goroutine or unlimited goroutine pool for easier goroutine handling and cancellation
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
Package pool implements a limited consumer goroutine or unlimited goroutine pool for easier goroutine handling and cancellation.
Features:
Dead simple to use and makes no assumptions about how you will use it.
Automatic recovery from consumer goroutines which returns an error to the results
Pool v2 advantages over Pool v1:
Up to 300% faster due to lower contention ( BenchmarkSmallRun used to take 3 seconds, now 1 second )
Cancels are much faster
Easier to use, no longer need to know the # of Work Units to be processed.
Pool can now be used as a long running/globally defined pool if desired ( v1 Pool was only good for one run )
Supports single units of work as well as batching
Pool can easily be reset after a Close() or Cancel() for reuse.
Multiple Batches can be run and even cancelled on the same Pool.
Supports individual Work Unit cancellation.
Pool v3 advantages over Pool v2:
Objects are not interfaces allowing for less breaking changes going forward.
Now there are 2 Pool types, both completely interchangeable, a limited worker pool and unlimited pool.
Simpler usage of Work Units, instead of <-work.Done now can do work.Wait()
Installation
Use go get.
go get gopkg.in/go-playground/pool.v3
Then import the pool package into your own code.
import "gopkg.in/go-playground/pool.v3"
Important Information READ THIS!
It is recommended that you cancel a pool or batch from the calling function and not inside of the Unit of Work, it will work fine, however because of the goroutine scheduler and context switching it may not cancel as soon as if called from outside.
When Batching DO NOT FORGET TO CALL batch.QueueComplete(), if you do the Batch WILL deadlock
It is your responsibility to call WorkUnit.IsCancelled() to check if it's cancelled after a blocking operation like waiting for a connection from a pool. (optional)
Usage and documentation
Please see http://godoc.org/gopkg.in/go-playground/pool.v3 for detailed usage docs.
Examples:
both Limited Pool and Unlimited Pool have the same signatures and are completely interchangeable.
Per Unit Work
package main
import (
"fmt"
"time"
"gopkg.in/go-playground/pool.v3"
)
func main() {
p := pool.NewLimited(10)
defer p.Close()
user := p.Queue(getUser(13))
other := p.Queue(getOtherInfo(13))
user.Wait()
if err := user.Error(); err != nil {
// handle error
}
// do stuff with user
username := user.Value().(string)
fmt.Println(username)
other.Wait()
if err := other.Error(); err != nil {
// handle error
}
// do stuff with other
otherInfo := other.Value().(string)
fmt.Println(otherInfo)
}
func getUser(id int) pool.WorkFunc {
return func(wu pool.WorkUnit) (interface{}, error) {
// simulate waiting for something, like TCP connection to be established
// or connection from pool grabbed
time.Sleep(time.Second * 1)
if wu.IsCancelled() {
// return values not used
return nil, nil
}
// ready for processing...
return "Joeybloggs", nil
}
}
func getOtherInfo(id int) pool.WorkFunc {
return func(wu pool.WorkUnit) (interface{}, error) {
// simulate waiting for something, like TCP connection to be established
// or connection from pool grabbed
time.Sleep(time.Second * 1)
if wu.IsCancelled() {
// return values not used
return nil, nil
}
// ready for processing...
return "Other Info", nil
}
}
Batch Work
package main
import (
"fmt"
"time"
"gopkg.in/go-playground/pool.v3"
)
func main() {
p := pool.NewLimited(10)
defer p.Close()
batch := p.Batch()
// for max speed Queue in another goroutine
// but it is not required, just can't start reading results
// until all items are Queued.
go func() {
for i := 0; i < 10; i++ {
batch.Queue(sendEmail("email content"))
}
// DO NOT FORGET THIS OR GOROUTINES WILL DEADLOCK
// if calling Cancel() it calles QueueComplete() internally
batch.QueueComplete()
}()
for email := range batch.Results() {
if err := email.Error(); err != nil {
// handle error
// maybe call batch.Cancel()
}
// use return value
fmt.Println(email.Value().(bool))
}
}
func sendEmail(email string) pool.WorkFunc {
return func(wu pool.WorkUnit) (interface{}, error) {
// simulate waiting for something, like TCP connection to be established
// or connection from pool grabbed
time.Sleep(time.Second * 1)
if wu.IsCancelled() {
// return values not used
return nil, nil
}
// ready for processing...
return true, nil // everything ok, send nil, error if not
}
}
Benchmarks
Run on MacBook Pro (Retina, 15-inch, Late 2013) 2.6 GHz Intel Core i7 16 GB 1600 MHz DDR3 using Go 1.6.2
run with 1, 2, 4,8 and 16 cpu to show it scales well...16 is double the # of logical cores on this machine.
NOTE: Cancellation times CAN vary depending how busy your system is and how the goroutine scheduler is but
worse case I've seen is 1s to cancel instead of 0ns
No homepage URL was recorded for go-playground/pool in TopGit's last sync. The README tab above frequently contains screenshots and demo links, or check the repository description on GitHub.
Does go-playground/pool have any tags?
TopGit's last sync did not record any GitHub topics for go-playground/pool. GitHub topics appear in the right sidebar of a repository page; that's the authoritative place to check.
How active is development on go-playground/pool?
The most recent commit recorded on go-playground/pool was 5.4 years ago, based on the GitHub push timestamp. The repository has 63 forks — one of the better signals of community interest.
Is go-playground/pool open source?
Yes — go-playground/pool ships under the MIT license, which makes its source code freely readable (and, depending on license terms, forkable and reusable). Source: github.com/go-playground/pool.
What license does go-playground/pool use?
go-playground/pool is released under the MIT license. Always verify the LICENSE file directly on GitHub for the authoritative terms — license strings can be edited out of sync with a project's actual stance.
Where do I read more about go-playground/pool?
This TopGit page is a snapshot — the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/go-playground/pool is the definitive source.
Read full README in the tab above.
Still deciding about pool?
One click hands the question to an AI along with this page — see what it says about pool.