Just a worker pool with regulation of the number of simultaneously running tasks and the number of maximum tasks in the queue.
You don't have to use closures and manage synchronization by yourself – pool will take care of it.
- Declare task, which satisfies interface
pool.Task
:
var _ pool.Task[int] = (*DummyTask[int])(nil)
type DummyTask[T any] func() (T, error)
func (t *DummyTask[T]) Execute() (T, error) {
return (*t)()
}
func (*DummyTask[T]) RetryAmount() int {
return 0
}
- Initialize worker pool:
func PoolWithDummyTask() (int, error) {
p := pool.New[int](1, 1)
defer p.Close()
p.Init()
// …
}
- Enqueue you task and wait for result or error:
func PoolWithDummyTask() (int, error) {
// …
tw, err := p.Enqueue(&t)
if err != nil {
return 0, err
}
return tw.Result(), tw.Error()
}
Checkout docs.