Pooly is a golang package to take a pool of workers under control. The API is pretty straightforward:
You can read a full working example here
func waitSignal(done func()) {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigs
done()
}()
}
func main() {
ctx, done := context.WithCancel(context.Background())
waitSignal(done)
p := pooly.New(ctx, 4)
p.RunFunc(func() {
time.Sleep(time.Second * 2)
fmt.Print("Job 1 Done\n")
})
p.RunFunc(func() {
time.Sleep(time.Second * 10)
fmt.Print("Job 2 Done\n")
})
p.Wait()
}
p := pooly.New(
ctx, // The context
4, // the number of workers
)
p.RunFunc(func() {
// Submit a func to the pool
})
p.Wait() // Wait for the shutdown sequence after the Context closing.