Documentation
¶
Overview ¶
Package worker provides a bounded, lease-aware consumer runtime for BlockQueue. It owns delivery execution, not the underlying queue lifecycle.
Index ¶
- Variables
- func CancelJob(err error) error
- func RetryAfter(delay time.Duration, err error) error
- type BatchClient
- type Client
- type Group
- type Handler
- type HandlerFunc
- type Job
- func (job *Job) Ack(ctx context.Context) error
- func (job *Job) Cancel(ctx context.Context, reason string) error
- func (job *Job) CancelTx(ctx context.Context, options *sql.TxOptions, reason string, ...) error
- func (job *Job) CompleteTx(ctx context.Context, options *sql.TxOptions, fn func(*sql.Tx) error) error
- func (job *Job) Completed() bool
- func (job *Job) Nack(ctx context.Context, retryDelay time.Duration, failure error) error
- type Options
- type TransactionalClient
- type TypedHandler
- type TypedHandlerFunc
- type TypedJob
- type Worker
Constants ¶
This section is empty.
Variables ¶
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 ¶
Types ¶
type BatchClient ¶
type BatchClient interface {
Client
BatchAckDeliveries(context.Context, blockqueue.Topic, string, []blockqueue.BatchAckItem) []blockqueue.DeliveryResult
BatchNackDeliveries(context.Context, blockqueue.Topic, string, []blockqueue.BatchNackItem) []blockqueue.DeliveryResult
}
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 ¶
type Client interface {
ClaimWait(context.Context, blockqueue.Topic, string, int, time.Duration) (blockqueue.Deliveries, error)
AckDelivery(context.Context, blockqueue.Topic, string, string, string) error
NackDelivery(context.Context, blockqueue.Topic, string, string, string, time.Duration, string) error
ExtendLease(context.Context, blockqueue.Topic, string, string, string, time.Duration) (time.Time, error)
CancelClaimedDelivery(context.Context, blockqueue.Topic, string, string, string, string) error
}
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.
type Handler ¶
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 ¶
HandlerFunc adapts a function to Handler.
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 ¶
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 ¶
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.
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 ¶
TypedHandler processes a JSON-decoded delivery.
type TypedHandlerFunc ¶
TypedHandlerFunc adapts a function to TypedHandler.
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 ¶
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.