Documentation ¶
Overview ¶
Package retry provides exponential backoff.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Retry ¶
func Retry[T any]( ctx context.Context, f func(ctx context.Context) (T, error), opts ...Option, ) (T, error)
Retry calls f, retrying with exponential backoff on errors. After the first error, will wait MinDelay before retrying. Each successive retry waits for twice as long, up to MaxDelay. f might be failing because an upstream system is overloaded and retries may exacerbate that problem, doubling the delay means that even uncoordinated clients will eventually estimate the available capacity in the upstream.
Delays are jittered by +/-50% to avoid thundering herds. That is, the configured MinDelay is the _average_ first delay, and the actual minimum delay is half of the configured. Likewise for MaxDelay, the configured is the average max delay, but the actual maximum delay is 1.5x this value.
Attempts that are very old are forgotten with regard to picking the next delay because they are not likely to still be relevant to the health of the upstream system. This is to make Retry usable for retrying very long-running operations, for example keeping a long-lived stream alive. The age is set so that if f is consistently failing once per MaxDelay, the next delay will be MaxDelay, but any attempts older than the minimum needed to achieve that are forgotten.
Types ¶
type Option ¶
type Option struct {
// contains filtered or unexported fields
}
Option affects the behavior of Retry.
func Attempts ¶
The maximum number of times to call f before returning the error. Attempts(1) means try just once and do not retry.