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 Option ¶
type Option func(*WorkerPool)
func WithExecutionTimeout ¶
func WithTickInterval ¶
func WithWorkerCount ¶
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) Stop ¶
func (pool *WorkerPool) Stop() error
Click to show internal directories.
Click to hide internal directories.