Configure backoff behavior with Modifiers (Middleware)
mainSince base backoff algorithms are infinite, you must use middleware (modifiers) to add limits, caps, or randomness. Modifiers wrap an existing backoff.
Jitter (Randomness)
Reduces 'thundering herd' issues by adding randomness.
WithJitter(duration, b): Returns the next value +/- the specified duration.WithJitterPercent(percent, b): Returns the next value +/- a percentage of the result.WithFullJitter(b): Returns a random value in the range[0, next value).
Limits and Caps
WithMaxRetries(count, b): Stops after a specific number of retries. Note: total attempts will becount + 1.WithCappedDuration(duration, b): Ensures no single calculated sleep duration exceeds the specified value.WithMaxDuration(duration, b): Sets a best-effort limit on the total elapsed time for all retry attempts combined.
b := retry.NewFibonacci(1 * time.Second)
// Add jitter (+/- 500ms)
b = retry.WithJitter(500*time.Millisecond, b)
// Stop after 4 retries
b = retry.WithMaxRetries(4, b)
// Ensure no single sleep exceeds 2s
b = retry.WithCappedDuration(2 * time.Second, b)
// Ensure total execution doesn't exceed 5s
b = retry.WithMaxDuration(5 * time.Second, b)