workq

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: May 3, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package workq is a minimal bounded worker pool for fire-and-forget background work (e.g. post-upload indexing). Jobs carry a context derived from the pool's root context; Close() cancels that context and waits for workers to drain, honouring the caller's deadline.

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("workq: closed")

ErrClosed is returned by Submit after Close has been called.

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

ErrQueueFull is returned by Submit when the job queue is saturated. Callers should surface this as 503 Service Unavailable with Retry-After.

Functions

This section is empty.

Types

type Config

type Config struct {
	Workers    int
	QueueDepth int
}

Config sizes the pool. Zero values use safe defaults (1 worker, 16-deep queue). Total in-flight + queued capacity is Workers + QueueDepth.

type Job

type Job func(ctx context.Context)

Job is a unit of work. It receives the pool's context so it can abort on shutdown.

type Pool

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

Pool is a fixed-size worker pool with a bounded submission queue.

func New

func New(cfg Config) *Pool

New constructs and starts a Pool.

func (*Pool) Close

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

Close stops accepting new work and waits for workers to drain. If the caller's ctx fires before drain completes, the pool context is cancelled so in-flight jobs honouring cancellation can abort, and ctx.Err() is returned.

func (*Pool) Stats

func (p *Pool) Stats() Stats

Stats returns a snapshot of pool utilisation.

func (*Pool) Submit

func (p *Pool) Submit(job Job) error

Submit enqueues job. Non-blocking: returns ErrQueueFull immediately if no queue slot is available, ErrClosed if the pool is shutting down.

type Stats

type Stats struct {
	Depth    int64
	Rejected int64
}

Stats is a point-in-time snapshot of pool utilisation. Depth is the count of jobs currently queued but not yet picked up by a worker; Rejected is the monotonic count of Submit calls that returned ErrQueueFull since process start. Safe to call concurrently.

Jump to

Keyboard shortcuts

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