simple worker pool
The workers package implements a simple fixed-size pool of goroutines for executing functions concurrently. The purpose is to limit concurrency to the number of goroutines in the pool.
When all workers are busy, the channel for submitting tasks blocks. If you are looking for a worker pool that never blocks when submitting tasks, see workerpool.
$ go get github.com/gammazero/workers
package main
import (
"fmt"
"github.com/gammazero/workers"
)
func main() {
do, done := workers.New(5)
requests := []string{"alpha", "beta", "gamma", "delta", "epsilon"}
for _, r := range requests {
do <- func() {
fmt.Println("Handling request:", r)
}
}
close(do) // stop workers
<-done // wait for all workers to exit
fmt.Println("All done")
}