Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrMaxRetries = errors.New("reached max retries")
ErrMaxRetries indicates that a strategy's retry budget has been exhausted.
Functions ¶
This section is empty.
Types ¶
type BackoffStrategy ¶
type BackoffStrategy struct {
// contains filtered or unexported fields
}
BackoffStrategy configures delays and retry limits. Caller starts a fresh sequence for every Call. Direct calls to Delay advance one sequence; copies of a strategy share that direct sequence and its retry counter.
func NewBackoffStrategy ¶
func NewBackoffStrategy(generator Generator, durationUnit time.Duration) BackoffStrategy
NewBackoffStrategy multiplies generator's sequence by durationUnit. Built-in generators start from the beginning for each Call, regardless of prior use. Custom generators can be used directly with Delay; to use one with Caller, use NewBackoffStrategyWithFactory so each Call gets independent state. Negative duration units are reported as errors by Delay.
func NewBackoffStrategyWithFactory ¶ added in v1.1.1
func NewBackoffStrategyWithFactory(factory func() Generator, durationUnit time.Duration) BackoffStrategy
NewBackoffStrategyWithFactory constructs a strategy for a custom generator. factory must be safe to call concurrently and return a new, non-nil generator with independent state each time. It is called lazily on the first retry.
func (BackoffStrategy) Delay ¶
func (b BackoffStrategy) Delay() (time.Duration, error)
Delay returns the next delay, or ErrMaxRetries once the configured number of delays has been returned. It reports invalid configuration as an error. Concurrent calls to Delay on the same strategy are serialized.
func (BackoffStrategy) WithCappedDuration ¶
func (b BackoffStrategy) WithCappedDuration(cappedDuration time.Duration) BackoffStrategy
WithCappedDuration caps the final delay, including jitter. Negative caps are reported as errors by Delay.
func (BackoffStrategy) WithJitter ¶
func (b BackoffStrategy) WithJitter(jitter time.Duration) BackoffStrategy
WithJitter adds uniform jitter in [-jitter, jitter) to each delay. Zero disables jitter. Negative values are reported as errors by Delay.
func (BackoffStrategy) WithMaxRetries ¶
func (b BackoffStrategy) WithMaxRetries(maxRetries int64) BackoffStrategy
WithMaxRetries permits maxRetries retries after the initial attempt, per Call and per registered rule. Zero disables retries. Negative limits are reported as errors by Delay.
type Caller ¶
type Caller struct {
// contains filtered or unexported fields
}
Caller calls functions with the configured retry strategies. Configuration methods return independent callers. A configured Caller can be reused and its Call method can be used concurrently.
func NewCaller ¶
func NewCaller() Caller
NewCaller constructs a Caller. The zero value is also ready to use.
func (Caller) Call ¶
Call invokes f until it succeeds, returns an unregistered error, exhausts its retry budget, or ctx is canceled. Retry state is independent for every Call. On cancellation, the returned error matches ctx.Err() and, if present, the last error from f through errors.Is. A successful f returns nil.
Call does not run f in a separate goroutine. To cancel work inside f, have f observe its own context (including any deadline needed for that work).
func (Caller) WithMaxDuration ¶
WithMaxDuration limits the time spent calling f and waiting between attempts. It interrupts backoff waits, but cannot interrupt f while it is executing. A nonpositive duration prevents f from being called.
func (Caller) WithRetriableError ¶
func (c Caller) WithRetriableError(err error, strategy BackoffStrategy) Caller
WithRetriableError registers a strategy for errors matching err via errors.Is. Rules are matched in registration order; the first matching rule is used. Each rule has its own retry budget and generator for each Call.
func (Caller) WithRetryOnAnyError ¶
func (c Caller) WithRetryOnAnyError(strategy BackoffStrategy) Caller
WithRetryOnAnyError retries all errors using strategy. When configured, it takes precedence over the rules registered with WithRetriableError.
type Generator ¶
type Generator interface {
// Next method returns a next number in the sequence.
Next() int64
}
Generator produces a backoff sequence. Stateful generators should not be shared between goroutines. Caller creates its own generators for each Call.
func NewConstant ¶
NewConstant creates a Generator that always return the same number (specified as `c` parameter).
func NewExponential ¶
NewExponential starts at 1 and multiplies by factor each time. Integer arithmetic preserves exact values and saturates at the int64 bounds.
func NewFibonacci ¶
func NewFibonacci() Generator
NewFibonacci creates a Generator where every next number is a sum of two previous numbers (Fibonacci sequence). Starts with 1 and saturates at math.MaxInt64 on overflow.
