worker

package
v1.3.5 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

worker/manager_metrics.go

Index

Constants

This section is empty.

Variables

View Source
var ErrQueueFull = errors.New("queue is full")

ErrQueueFull is returned when the pool queue is full.

Functions

This section is empty.

Types

type Manager

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

Manager orchestrates multiple workers, providing restart and panic recovery.

func NewManager

func NewManager(logger *slog.Logger, opts ...ManagerOption) *Manager

func (*Manager) Add

func (m *Manager) Add(workers ...Worker)

Add appends one or more workers to the manager.

func (*Manager) Start

func (m *Manager) Start(ctx context.Context)

Start launches all added workers in separate goroutines.

func (*Manager) Wait

func (m *Manager) Wait()

Wait blocks until all workers have finished.

type ManagerMetrics

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

func NewManagerMetrics

func NewManagerMetrics(reg prometheus.Registerer) *ManagerMetrics

type ManagerOption

type ManagerOption func(*Manager)

func WithInitialDelay

func WithInitialDelay(d time.Duration) ManagerOption

WithInitialDelay overrides the base backoff delay (default 1s).

func WithManagerMetrics

func WithManagerMetrics(metrics *ManagerMetrics) ManagerOption

func WithMaxRestarts

func WithMaxRestarts(n int) ManagerOption

WithMaxRestarts sets the maximum number of consecutive failures before the worker is permanently stopped. 0 means unlimited (default).

type Metrics

type Metrics interface {
	TasksSubmitted(ctx context.Context)
	TasksRejected(ctx context.Context)
	TasksPanicked(ctx context.Context)
}

Metrics defines counters for the worker pool.

func NewNoopMetrics

func NewNoopMetrics() Metrics

NewNoopMetrics returns a no-op implementation.

type Option

type Option func(*Options)

Option applies a configuration to Options.

func WithLogger

func WithLogger(l *slog.Logger) Option

WithLogger sets the logger.

func WithMetrics

func WithMetrics(m Metrics) Option

WithMetrics sets the metrics implementation.

func WithQueueSize

func WithQueueSize(n int) Option

WithQueueSize sets the queue size.

func WithWorkers

func WithWorkers(n int) Option

WithWorkers sets the number of workers.

type Options

type Options struct {
	Workers   int
	QueueSize int
	Logger    *slog.Logger
	Metrics   Metrics
}

Options holds configuration for the pool constructed via functional options.

type Pool

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

Pool is a bounded worker pool. It runs a fixed number of worker goroutines and accepts jobs up to a fixed queue size. Submitting a job never spawns a new goroutine; if the queue is full Submit returns ErrQueueFull.

func New

func New(opts ...Option) *Pool

New creates a new Pool using functional options. Defaults: workers=1, queueSize=workers, logger=slog.Default().

func (*Pool) Close

func (p *Pool) Close()

Close closes the job queue, signalling workers to drain any remaining jobs and then exit. It is safe to call Close concurrently and it is idempotent. After Close, Submit will return ErrQueueFull for new jobs. Call Wait after Close to block until all workers have stopped.

func (*Pool) Start

func (p *Pool) Start(ctx context.Context)

Start launches the worker goroutines. Start is idempotent.

func (*Pool) Submit

func (p *Pool) Submit(ctx context.Context, job func(context.Context) error) error

Submit enqueues a job for execution. If the queue is full, ErrQueueFull is returned. Submit never spawns goroutines; it either enqueues the job or returns an error. After Close is called, Submit returns ErrQueueFull immediately.

func (*Pool) Wait

func (p *Pool) Wait()

Wait waits until all worker goroutines have exited (after the start context has been cancelled or Close has been called).

type Worker

type Worker interface {
	Name() string
	// Run starts the worker loop. It should block until the context is cancelled.
	Run(ctx context.Context) error
}

Worker represents a background process that runs alongside the HTTP server.

Jump to

Keyboard shortcuts

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