worker

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package worker provides registered, bounded workers with explicit retry, metrics, cancellation and draining.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Concurrency, Queue int
	Report             func(error)
	Retry              *retry.Policy
	Observer           instrument.Hook
}

Config controls a pool. Report is mandatory and must not panic. Retry repeats only explicitly idempotent, classified failures.

type Job

type Job func(context.Context) error

Job executes one cancellable operation. It must honor ctx and must not retain caller-owned mutable inputs without synchronization.

type Pool

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

Pool owns worker goroutines and bounded accepted work. Submit contexts control both admission and the individual job lifetime.

func New

func New(ctx context.Context, concurrency, queue int, report func(error)) (*Pool, error)

New creates a pool without automatic retries. All failures are delivered to report.

func NewWithConfig

func NewWithConfig(ctx context.Context, c Config) (*Pool, error)

NewWithConfig validates all retry/admission settings before starting any owned goroutine.

func (*Pool) Cancel

func (p *Pool) Cancel()

Cancel requests immediate cancellation. Shutdown can subsequently await actual worker termination.

func (*Pool) Register

func (p *Pool) Register(name string, job Job) error

Register adds an immutable named job definition to this pool, never to a global registry.

func (*Pool) Shutdown

func (p *Pool) Shutdown(ctx context.Context) error

Shutdown stops admission and drains accepted work; on deadline it cancels running work and returns immediately. Unstarted work canceled by the pool is discarded and reported with its context error. Handlers and Report must honor their documented contracts. Repeated calls wait on the same cleanup completion without creating extra wait goroutines.

func (*Pool) Stats

func (p *Pool) Stats() Stats

Stats returns per-pool counters with no application labels or mandatory telemetry globals.

func (*Pool) Submit

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

Submit blocks only until bounded admission, cancellation, or shutdown. Canceling ctx after admission also cancels this job. To intentionally detach a background job, pass an explicitly owned context such as context.WithoutCancel(requestContext).

func (*Pool) SubmitNamed

func (p *Pool) SubmitNamed(ctx context.Context, name string) error

SubmitNamed enqueues a previously registered job using the same cancellation/backpressure policy as Submit.

type Stats

type Stats struct {
	Submitted, Started, Completed, Succeeded, Failed, Panics, Active int64
	Queued                                                           int
}

Stats includes finished jobs (Completed), including accepted jobs canceled before execution, outcome counts and current active/queued work. Panics counts recovered attempts.

Jump to

Keyboard shortcuts

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