Skip to content

Repository files navigation

👨‍🔧 worker-pool

Build StatusGo Report CardcodecovFOSSA Statuslicense

logo

Go simple async worker pool.

📖 ABOUT

Worker pool is a software design pattern for achieving concurrency of task execution. Maintains multiple workers waiting for tasks to be allocated for concurrent execution. By maintaining a pool of workers, the model increases performance and avoids latency in execution. The number of available workers might be tuned to the computing resources available.

You can read more about worker pools in Go here.

Contributors:

Want to contribute ? Feel free to send pull requests!

Have problems, bugs, feature ideas? We are using the github issue tracker to manage them.

📚 Documentation

For examplesvisit godoc#pkg-examples

For GoDoc reference, visit pkg.go.dev

🚏 HOW TO USE

🚅 Benchmark

CPU: 3,3 GHz Intel Core i7

RAM: 16 GB 2133 MHz LPDDR3

➜ worker-pool git:(master) ✗ go test -bench=. -cpu=4 -benchmem
goos: darwin
goarch: amd64
pkg: github.com/vardius/worker-pool/v2
BenchmarkWorker1-4 3944299 284 ns/op 56 B/op 3 allocs/op
BenchmarkWorker1Parallel-4 7394715 138 ns/op 48 B/op 2 allocs/op
BenchmarkWorker100-4 1657569 693 ns/op 56 B/op 3 allocs/op
BenchmarkWorker100Parallel-4 3673483 368 ns/op 48 B/op 2 allocs/op
BenchmarkWorkerNumCPU-4 2590293 445 ns/op 56 B/op 3 allocs/op
BenchmarkWorkerNumCPUParallel-4 3591553 298 ns/op 48 B/op 2 allocs/op
PASS
ok github.com/vardius/worker-pool/v2	9.511s

🏫 Basic example

package main
import (
"fmt""sync""github.com/vardius/worker-pool/v2"
)
funcmain() {
varwg sync.WaitGrouppoolSize:=1jobsAmount:=3workersAmount:=2// create new poolpool:=workerpool.New(poolSize)
out:=make(chanint, jobsAmount)
worker:=func(iint) {
deferwg.Done()
out<-i
}
fori:=1; i<=workersAmount; i++ {
iferr:=pool.AddWorker(worker); err!=nil {
panic(err)
}
}
wg.Add(jobsAmount)
fori:=0; i<jobsAmount; i++ {
iferr:=pool.Delegate(i); err!=nil {
panic(err)
}
}
gofunc() {
// stop all workers after jobs are donewg.Wait()
close(out)
pool.Stop() // stop removes all workers from pool, to resume work add them again
}()
sum:=0forn:=rangeout {
sum+=n
}
fmt.Println(sum)
// Output:// 3
}

📜 License

This package is released under the MIT license. See the complete license in the package

FOSSA Status

About

Go simple async worker pool

Topics

Resources

Code of conduct

Contributing

Stars

93 stars

Watchers

3 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages