Core Concepts
Backpressure Handling
Section titled “Backpressure Handling”The pool enforces backpressure when the queue is full. You choose how long a caller may wait for space:
// Block until capacity is availableerr := pool.Submit(context.Background(), job)
// Wait at most 5 seconds for capacitywaitCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)defer cancel()err := pool.SubmitTask(waitCtx, adaptivepool.Task{ Context: context.Background(), // the job's own context, unaffected by waitCtx Run: job,})if errors.Is(err, adaptivepool.ErrTimeout) { // Handle overload}
// Don't wait at all if the queue is fullerr := pool.TrySubmit(context.Background(), adaptivepool.Task{Run: job})if errors.Is(err, adaptivepool.ErrQueueFull) { // Drop or handle overload}The context passed to Submit is also the context the job runs with, so a wait deadline on it would also cancel the job. Set Task.Context to keep the two apart.
A nil error means the task was queued. Otherwise the submit methods return:
| Error | When |
|---|---|
ErrNilJob | Run is nil |
ErrPoolShutdown | Shutdown has started |
ErrTimeout | The submit context’s deadline passed. It wraps context.DeadlineExceeded |
context.Canceled | The submit context was cancelled |
ErrQueueFull | TrySubmit only: the queue was full |
Metrics
Section titled “Metrics”In-process metrics are always available, with no exporter configured:
metrics := pool.Metrics()
fmt.Printf("Queue Depth: %d\n", metrics.QueueDepth())fmt.Printf("Active / Busy Workers: %d / %d\n", metrics.ActiveWorkers(), metrics.BusyWorkers())fmt.Printf("Jobs Processed / Failed: %d / %d\n", metrics.JobsProcessed(), metrics.JobsFailed())fmt.Printf("Jobs Rejected / Dropped: %d / %d\n", metrics.JobsRejected(), metrics.JobsDropped())fmt.Printf("Retries: %d\n", metrics.RetryAttempts())fmt.Printf("Avg Latency / Queue Wait: %v / %v\n", metrics.AvgJobLatency(), metrics.AvgQueueWait())To export metrics to Prometheus or OpenTelemetry, see Observability.
Graceful Shutdown
Section titled “Graceful Shutdown”Shutting down the pool safely ensures that all in-flight jobs are completed (drained) before the program exits.
// Shutdown with timeoutctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)defer cancel()
if err := pool.Shutdown(ctx); err != nil { var se *adaptivepool.ShutdownError if errors.As(err, &se) { log.Printf("shutdown timed out, %d queued jobs dropped", se.Dropped) }}Shutdown behavior:
- Stops accepting new jobs (returns
ErrPoolShutdown) - Drains queued and in-flight jobs within the timeout
- Terminates all workers deterministically
- If the timeout expires, aborts retry waits, lets running jobs finish, and drops jobs still queued. Each dropped job is reported to
OnTaskDonewith statusshutdown, andShutdownreturns a*ShutdownErrorwith the count. It wraps the context’s error, soerrors.Is(err, context.DeadlineExceeded)still matches.
Shutdown can be called more than once: later calls return the first call’s result.