workerpool

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 16, 2025 License: MIT Imports: 3 Imported by: 0

README

workerpool

A resilient worker pool for Go that stops execution after a configurable number of errors. Supports panic recovery and concurrent task processing.

Features

  • ✅ Configurable number of workers (n)
  • ✅ Stop execution after m errors (or ignore errors if m <= 0)
  • ✅ Panic recovery: panics are treated as errors, workers continue
  • ✅ Thread-safe

Usage

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")
}

Interface

type Task func() error

Run(tasks []Task, n, m int) error
  • Run(tasks []Task, n, m int) error
    • n: number of concurrent workers.
    • m: maximum number of errors before stopping. If m <= 0, all errors are ignored.
  • Returns:
  • nil — all tasks completed successfully.
  • ErrNoTasks — no tasks provided.
  • ErrNoWorkersn <= 0.
  • ErrErrorsLimitExceeded — more than m errors occurred.

Error Handling

  • 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.

Installation

go get github.com/Averlex/workerpool

Documentation

Overview

Package workerpool implements Run function - a worker pool implementation, which supports:

- up to `n` adjustable workers;

- limit error up to `m` - pool stops after reaching the error limit;

- panic protection for each worker - each panic counts as an error, worker continues processing.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrErrorsLimitExceeded is an error indicating that the limit of errors is exceeded.
	ErrErrorsLimitExceeded = errors.New("errors limit exceeded")
	// ErrNoWorkers is an error indicating that no workers were set.
	ErrNoWorkers = errors.New("no workers set")
	// ErrNoTasks is an error indicating that no tasks were set.
	ErrNoTasks = errors.New("no tasks set")
)

Functions

func Run

func Run(tasks []Task, n, m int) error

Run starts tasks in n goroutines and stops its work when receiving m errors from tasks. Values m <= 0 are treated as a scenario where all errors are ignored.

Panics are gracefully handled, each of them is treated as a simple error.

If no tasks are set or n <= 0 it returns ErrNoTasks or ErrNoWorkers respectively.

It returns ErrErrorsLimitExceeded if the error limit is reached.

Types

type Task

type Task func() error

Task is a function that is executed in a separate worker.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL