It's a tiny DRY lib for goroutine pooling and throttling.
- Provide a worker function that takes
interface{}
as input and returnsinterface{}
as output. - Set your limits on number of running tasks per second
maxWorkersPerSecond
and task in generalmaxWorkers
. - Shoot your tasks int
Input
channel and listen toOutput
.
pool := gaziri.NewPool(
func(value interface{}) interface{} {
// works goes here
time.Sleep(2 * time.Second)
return value.(int) * 2
},
100, // max workers
30, // max rps
)
go func() {
for i := 0; i < jobs; i++ {
pool.Input <- i // enqueue tasks
}
}()
for i := 0; i < jobs; i++ {
<-pool.Output // get results
}