Deadly simple worker pool
package main
import (
"context"
"errors"
"fmt"
workerpool "github.com/zelenin/go-worker-pool"
"log"
)
func main() {
pool := workerpool.NewPool(2, 10)
go func() {
errorChan := pool.Errors()
for {
err, ok := <-errorChan
if !ok {
break
}
log.Printf("%s", err)
}
}()
for i := int64(1); i < 100; i++ {
if pool.Stopped() {
break
}
id := i
pool.AddTask(func(ctx context.Context) error {
if id%2 == 0 {
return fmt.Errorf("Task #%d: %w", id, errors.New("task error"))
}
return nil
})
}
pool.Stop()
pool.Wait()
}
Aleksandr Zelenin, e-mail: aleksandr@zelenin.me