workerpool

package
v1.3.12 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package workerpool provides a generic, tick-driven worker pool that repeatedly invokes an Executor on a fixed interval.

The pool spawns N concurrent workers that wake up on each tick interval and execute the handler. This is useful for periodic background jobs (e.g., cache refreshes, cleanup tasks) that should run concurrently without blocking each other. Execution timeouts are enforced per worker.

Basic usage:

executor := &MyExecutor{}
pool := workerpool.New(executor,
    workerpool.WithWorkerCount(3),
    workerpool.WithTickInterval(1*time.Minute),
    workerpool.WithExecutionTimeout(30*time.Second),
)
if err := pool.Start(ctx); err != nil {
    return err
}
defer pool.Stop()

Each worker runs independently; if one times out or fails, others continue executing. The pool prevents concurrent overlapping executions of the same worker (each waits for the previous to finish).

Index

Constants

This section is empty.

Variables

View Source
var ErrAlreadyRunning = errors.New("worker pool is already running")

Functions

This section is empty.

Types

type Executor

type Executor interface {
	Execute(ctx context.Context) error
}

type Option

type Option func(*WorkerPool)

func WithExecutionTimeout

func WithExecutionTimeout(timeout time.Duration) Option

func WithName

func WithName(name string) Option

func WithTickInterval

func WithTickInterval(duration time.Duration) Option

func WithWorkerCount

func WithWorkerCount(count int) Option

type WorkerPool

type WorkerPool struct {
	// contains filtered or unexported fields
}

func New

func New(executor Executor, opts ...Option) *WorkerPool

func (*WorkerPool) Name

func (pool *WorkerPool) Name() string

func (*WorkerPool) Start

func (pool *WorkerPool) Start(ctx context.Context) error

func (*WorkerPool) Stop

func (pool *WorkerPool) Stop() error

Jump to

Keyboard shortcuts

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