A resilient worker pool for Go that stops execution after a configurable number of errors. Supports panic recovery and concurrent task processing.
- ✅ Configurable number of workers (
n) - ✅ Stop execution after
merrors (or ignore errors ifm <= 0) - ✅ Panic recovery: panics are treated as errors, workers continue
- ✅ Thread-safe
import "github.com/Averlex/workerpool"
tasks := []workerpool.Task{
func() error { /* task 1 */ return nil },
func() error { /* task 2 */ return errors.New("failed") },
func() error { /* task 3 */ panic("oops") },
// ...
}
// Run with 4 workers, stop after 2 errors.
err := workerpool.Run(tasks, 4, 2)
if err == workerpool.ErrErrorsLimitExceeded {
log.Println("Stopped: too many errors")
}type Task func() error
Run(tasks []Task, n, m int) errorRun(tasks []Task, n, m int) errorn: number of concurrent workers.m: maximum number of errors before stopping. Ifm <= 0, all errors are ignored.
- Returns:
nil— all tasks completed successfully.ErrNoTasks— no tasks provided.ErrNoWorkers—n <= 0.ErrErrorsLimitExceeded— more thanmerrors occurred.
- Each error returned by a task counts as one error.
- Each panic is recovered and also counts as one error.
- As soon as the error count exceeds
m, no new tasks are started.
go get github.com/Averlex/workerpool