worker

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package worker provides a bounded, lease-aware consumer runtime for BlockQueue. It owns delivery execution, not the underlying queue lifecycle.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAlreadyStarted          = errors.New("worker has already been started")
	ErrInvalidConfiguration    = errors.New("invalid worker configuration")
	ErrDrainTimeout            = errors.New("worker drain deadline exceeded")
	ErrJobCompleted            = errors.New("job attempt is already completed")
	ErrTransactionsUnsupported = errors.New("worker client does not support transactions")
	ErrRetryRequested          = errors.New("worker retry requested")
	ErrCancelRequested         = errors.New("worker cancellation requested")
	ErrClientProtocol          = errors.New("worker client violated claim contract")
)

Functions

func CancelJob

func CancelJob(err error) error

CancelJob marks a handler failure as permanent. The worker receipt-fences a terminal cancellation instead of consuming retries or moving the job to DLQ.

func RetryAfter

func RetryAfter(delay time.Duration, err error) error

RetryAfter returns a handler error that overrides the subscriber retry delay for this attempt. Non-positive delay falls back to the subscriber policy.

Types

type BatchClient

BatchClient enables set-based automatic ACK/NACK. *blockqueue.Queue implements BatchClient. Failed batch items are retried through Client using their original receipt tokens.

type Client

Client is the queue surface required by a Worker. ClaimWait implementations must never return more deliveries than limit. *blockqueue.Queue implements Client.

type Group

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

Group supervises multiple independent topic/subscriber workers under one lifecycle. It does not impose global concurrency; every Worker retains its own bounded concurrency and drain policy.

func NewGroup

func NewGroup(workers ...*Worker) (*Group, error)

NewGroup validates and registers workers. A Worker may belong to only one position in the group, and both Group and Worker are single-use.

func (*Group) Run

func (group *Group) Run(ctx context.Context) error

Run starts every registered Worker. A terminal error from one worker cancels and drains the others, then all worker errors are joined. Canceling ctx is a normal shutdown and returns nil when every worker drains cleanly.

type Handler

type Handler interface {
	Handle(context.Context, *Job) error
}

Handler processes one leased delivery. Returning nil acknowledges it; returning an error records a NACK and applies the subscriber retry policy. Return CancelJob for a permanent, receipt-fenced cancellation.

type HandlerFunc

type HandlerFunc func(context.Context, *Job) error

HandlerFunc adapts a function to Handler.

func (HandlerFunc) Handle

func (handler HandlerFunc) Handle(ctx context.Context, job *Job) error

type Job

type Job struct {
	blockqueue.Delivery
	// contains filtered or unexported fields
}

Job is one receipt-fenced delivery attempt. Delivery is embedded so handler code can access Message, Headers, ID, and ReceiptToken directly.

func (*Job) Ack

func (job *Job) Ack(ctx context.Context) error

Ack manually acknowledges the job. A handler that calls Ack should return immediately; the worker will observe Completed and will not ACK twice.

func (*Job) Cancel

func (job *Job) Cancel(ctx context.Context, reason string) error

Cancel terminally cancels this exact receipt-fenced attempt. Unlike Nack, cancellation does not consume retry budget or enter the DLQ.

func (*Job) CancelTx

func (job *Job) CancelTx(
	ctx context.Context,
	options *sql.TxOptions,
	reason string,
	fn func(*sql.Tx) error,
) error

CancelTx executes application writes and receipt-fenced cancellation in one database transaction. The completion outcome is set only after the transaction commits.

func (*Job) CompleteTx

func (job *Job) CompleteTx(
	ctx context.Context,
	options *sql.TxOptions,
	fn func(*sql.Tx) error,
) error

CompleteTx executes application writes and this job's ACK in one database transaction. The completion outcome is set only after the transaction commits.

func (*Job) Completed

func (job *Job) Completed() bool

Completed reports whether this attempt has already been ACKed, NACKed, cancelled, or transactionally completed through Job.

func (*Job) Nack

func (job *Job) Nack(ctx context.Context, retryDelay time.Duration, failure error) error

Nack manually records a failure. A zero delay selects the subscriber retry policy. A handler that calls Nack should return immediately.

type Options

type Options struct {
	// Concurrency is the maximum number of active handlers. Default: 1.
	Concurrency int
	// BatchSize caps one claim and is always limited by free concurrency slots.
	// Default: Concurrency.
	BatchSize int
	// LeaseDuration is renewed while a handler is active. Default: 1m.
	LeaseDuration time.Duration
	// HeartbeatInterval is deterministically jittered to avoid a batch
	// stampede. Default: one third of LeaseDuration.
	HeartbeatInterval time.Duration
	// DisableHeartbeat is intended for short tests and specialized consumers.
	DisableHeartbeat bool
	// OperationTimeout bounds automatic ACK/NACK and heartbeat calls.
	// Default: 10s.
	OperationTimeout time.Duration
	// DrainTimeout bounds graceful handler drain after Run context cancellation.
	// Default: 30s.
	DrainTimeout time.Duration
	// HardStopTimeout is a second, bounded wait after DrainTimeout cancels
	// handler contexts. Default: 1s.
	HardStopTimeout time.Duration
	// PausePollInterval controls how often an intentionally paused resource is
	// checked for resume. Pause is logged once, not on every poll. Default: 5s.
	PausePollInterval time.Duration
	// CompletionBatchSize caps set-based ACK/NACK transactions when the client
	// implements BatchClient. Default: 100; also capped by Concurrency.
	CompletionBatchSize int
	// CompletionFlushInterval bounds completion batching latency. Default: 1ms.
	CompletionFlushInterval time.Duration
	// Logger receives operational retry, panic, and completion failures.
	// Default: slog.Default().
	Logger *slog.Logger
	// DisableMetrics makes every worker collector a no-op.
	DisableMetrics bool
	// MetricRegisterer receives worker collectors. Default: Prometheus global
	// registerer.
	MetricRegisterer prometheus.Registerer
}

Options controls execution and lease ownership. Zero values use safe defaults.

type TransactionalClient

type TransactionalClient interface {
	Client
	WithTx(context.Context, *sql.TxOptions, func(*sql.Tx) error) error
	AckDeliveryTx(context.Context, *sql.Tx, blockqueue.Topic, string, string, string) error
	CancelClaimedDeliveryTx(context.Context, *sql.Tx, blockqueue.Topic, string, string, string, string) error
}

TransactionalClient is implemented by clients that can atomically combine application writes and delivery acknowledgement. *blockqueue.Queue implements TransactionalClient.

type TypedHandler

type TypedHandler[T any] interface {
	Handle(context.Context, *TypedJob[T]) error
}

TypedHandler processes a JSON-decoded delivery.

type TypedHandlerFunc

type TypedHandlerFunc[T any] func(context.Context, *TypedJob[T]) error

TypedHandlerFunc adapts a function to TypedHandler.

func (TypedHandlerFunc[T]) Handle

func (handler TypedHandlerFunc[T]) Handle(ctx context.Context, job *TypedJob[T]) error

type TypedJob

type TypedJob[T any] struct {
	*Job
	Args T
}

TypedJob contains JSON-decoded arguments and the underlying delivery job.

type Worker

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

Worker runs one bounded consumer for a topic/subscriber pair.

func New

func New(client Client, topic blockqueue.Topic, subscriber string, handler Handler, options Options) (*Worker, error)

New creates a worker for one topic/subscriber pair. The returned worker is single-use; create a new Worker after Run returns.

func NewJSON

func NewJSON[T any](
	client Client,
	topic blockqueue.Topic,
	subscriber string,
	handler TypedHandler[T],
	options Options,
) (*Worker, error)

NewJSON creates a worker that decodes Delivery.Message as JSON before invoking handler. Decode failures follow the subscriber NACK/DLQ policy so they remain observable and replayable; handlers explicitly return CancelJob only when cancellation is the intended business outcome.

func (*Worker) Run

func (worker *Worker) Run(ctx context.Context) error

Run claims and processes deliveries until ctx is canceled or a terminal topology/lifecycle error occurs. Context cancellation is a normal shutdown: claiming stops immediately and active handlers are given DrainTimeout to finish while their leases continue to heartbeat. After DrainTimeout, handler contexts are canceled and Run waits HardStopTimeout once more. A handler that ignores context cancellation can still outlive Run; such handlers violate the worker contract and must not access Queue after Run returns.

Jump to

Keyboard shortcuts

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