Skip to content

Retries

Retries are enabled per pool with a reusable policy:

pool, err := adaptivepool.New(
adaptivepool.WithRetry(adaptivepool.RetryPolicy{
MaxAttempts: 5,
Backoff: adaptivepool.ExponentialBackoff{Base: 100 * time.Millisecond},
MaxDelay: 2 * time.Second,
MaxElapsedTime: 10 * time.Second,
Jitter: true,
}),
)
  • A failed job is retried on the same worker: it waits, then runs again. Nothing is re-queued and no goroutines or timers are left behind.
  • A retrying job keeps its worker busy, so retries consume pool capacity. Bound them with MaxAttempts, MaxDelay and MaxElapsedTime.
  • Every error is retried by default. The exceptions are errors wrapped with Permanent, a done job context, and panics, which are never retried.
  • Only retry jobs that are idempotent: an attempt may have partly succeeded before it failed.
OptionMeaning
MaxAttemptsRequired. Total runs, including the first
BackoffWait before each retry: ConstantBackoff, LinearBackoff or ExponentialBackoff (the default: 100ms base, ×1.5), or your own Next(retry int) time.Duration
MaxDelayCaps any single wait, including RetryAfter hints
JitterFull jitter: each wait is drawn from [0, delay), so jobs that failed together don’t retry in lockstep
MaxElapsedTimeTotal time budget for one job. Retrying stops as soon as the next wait would exceed it
ErrorLimitsFewer attempts for specific errors, matched with errors.Is
func chargeCard(ctx context.Context) error {
if err := validate(req); err != nil {
return adaptivepool.Permanent(err) // invalid input: never retried
}
resp, err := gateway.Charge(ctx, req)
if err != nil {
return err // network errors: retried with backoff
}
if resp.RateLimited {
// Wait exactly this long before the next attempt (still capped by MaxDelay)
return adaptivepool.RetryAfter(errRateLimited, resp.RetryAfter)
}
return nil
}

If the job’s context is cancelled, retrying stops immediately and a backoff wait ends at once.

When a job gives up, its error is a *RetryError. errors.Is matches both the stop reason and the job’s last error:

ReasonStatus
ErrRetriesExhaustedretries_exhausted
ErrRetryBudgetExceededbudget_exceeded
ErrPermanentFailurepermanent
context.Canceled / context.DeadlineExceededcanceled
ErrPoolShutdownshutdown (a forced shutdown aborted the wait)

The status appears in logs, metrics and the OnTaskDone hook. See Observability.

For every option with its use case, see the Retries and Observability guide.