Documentation
¶
Overview ¶
Package backoff provides a unified retry strategy with configurable backoff, jitter, adaptive behavior, and error filtering.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Do ¶
func Do(ctx context.Context, cfg Config, fn func(ctx context.Context) error) (attempt int, err error)
Do executes fn, retrying on error according to cfg. Returns the total number of attempts taken and the last error (nil on success).
func Middleware ¶
func Middleware(cfg Config, logger watermill.LoggerAdapter) message.HandlerMiddleware
Middleware returns a Watermill handler middleware that wraps h with retry behavior governed by cfg. Each retry is logged via logger. The cfg is copied per message to avoid data races on concurrent invocations.
Types ¶
type Config ¶
type Config struct {
// MaxAttempts is the total number of execution attempts. 0 or negative means no retry (runs once).
MaxAttempts int
// InitialInterval is the delay before the first retry. Zero defaults to 1s.
InitialInterval time.Duration
// MaxInterval caps the delay between retries. Zero defaults to 30s.
MaxInterval time.Duration
// Multiplier controls delay growth per retry. 1.0 = linear, 2.0 = exponential. Zero defaults to 2.0.
Multiplier float64
// Jitter enables full jitter: actual sleep is randomly chosen from [0, delay).
Jitter bool
// Adaptive enables success-based adaptive backoff: delay halves after success, doubles after failure.
Adaptive bool
// RetryOn lists error codes that trigger retry. Empty means retry all errors.
RetryOn []string
// IsRetryable is an optional custom predicate. When set, it overrides RetryOn.
IsRetryable func(err error) bool
// MaxElapsedTime caps total retry wall-clock time. 0 means no limit.
MaxElapsedTime time.Duration
// OnRetry is called before each retry attempt (not on the first attempt). May be nil.
OnRetry func(attempt int, delay time.Duration, err error)
}
Config defines the retry strategy for all consumers (pipeline, workflow, event).
Click to show internal directories.
Click to hide internal directories.