workerpool

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package workerpool provides bounded, lifecycle-aware execution for work submitted by message adapters and other optional capabilities.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrClosed indicates that the processor is no longer accepting work.
	ErrClosed = errors.New("worker pool is closed")
	// ErrPanic indicates that a task panicked. The original panic value is
	// retained in the wrapped error.
	ErrPanic = errors.New("worker pool task panicked")
)

Functions

This section is empty.

Types

type Handle

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

Handle represents accepted work. A handle must be awaited by callers that need to preserve message acknowledgement semantics.

func (*Handle) Wait

func (h *Handle) Wait(ctx context.Context) error

Wait blocks until the task finishes or ctx is cancelled. Cancelling ctx does not cancel the task itself; task cancellation is controlled by the context passed to Submit.

type Options

type Options struct {
	Workers       int
	QueueCapacity int
}

Options configures a Processor. Workers must be positive. QueueCapacity is the number of tasks allowed to wait behind active workers.

type Processor

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

Processor executes accepted tasks with a fixed number of workers and a bounded waiting queue. Close drains accepted tasks before returning.

func New

func New(options Options) (*Processor, error)

New creates a bounded processor and starts its workers.

func (*Processor) Close

func (p *Processor) Close(ctx context.Context) error

Close stops accepting work, drains accepted tasks, and waits for workers to exit. If ctx expires first, Close returns its error while workers continue draining in the background; a later Close call can be used to wait again.

func (*Processor) Stats

func (p *Processor) Stats() Stats

Stats returns a snapshot suitable for diagnostics and metrics export.

func (*Processor) Submit

func (p *Processor) Submit(ctx context.Context, fn Task) (*Handle, error)

Submit waits for queue capacity, then accepts fn for execution. If ctx is cancelled while waiting for capacity, no work is accepted and ctx.Err() is returned. A successful submission returns a handle whose completion should be awaited before acknowledging a broker message.

type Stats

type Stats struct {
	Workers   int
	Queued    int
	Running   int
	Accepted  uint64
	Completed uint64
	Failed    uint64
	Rejected  uint64
}

Stats is a point-in-time processor snapshot.

type Task

type Task func(context.Context) error

Task is one unit of work. The task context is the context supplied to Submit; callers should observe its cancellation where appropriate.

Jump to

Keyboard shortcuts

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