Documentation
¶
Index ¶
Constants ¶
const ( // InfiniteTries represents infinite `tries`. Use this in the `Try` method to // keep trying until Completable returns true InfiniteTries = math.MaxInt8 // AllTriesFailed indicates that all requested tries failed AllTriesFailed = Error("all tries failed") // BackoffContextTimeoutExceeded indicates that the backoff context Done // channel was closed BackoffContextTimeoutExceeded = Error("backoff context timeout exceeded") )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Backoff ¶
type Backoff struct {
// contains filtered or unexported fields
}
Backoff is a simple backoff implementation. You will want to use NewBackoff or NewBackoffWithTimeout to create an instance.
func NewBackoff ¶
NewBackoff creates a new Backoff struct. Intervals represents the interval calculation algorithm (ex: Exponential). Tries represents the number of times to call the function. Context may be a cancellable context.
If you want a timeout Context, consider using NewBackoffWithTimeout instead.
func (*Backoff) Try ¶
Try will try to call the provided Completable the number of times specifed in NewBackoff until an execution of Completable returns true.
If the Completable returns false more times than specified in tries in NewBackoff, then Try will return a AllTriesFailed error.
If the provided context cancel function is called before a Completable call returns true, then Try will return a BackoffContextTimeoutExceeded error.
type Completable ¶
Completable is a function that should complete and terminate early if the context.Done() channel is closed.
type Exponential ¶
type Exponential struct {
Base time.Duration
Unit time.Duration
Initial time.Duration
Max time.Duration
}
Exponential implements an exponential interval function.
func DefaultBinaryExponential ¶
func DefaultBinaryExponential() Exponential
DefaultBinaryExponential creates a binary exponential interval function with the following series: 0.5s, 1s, 2s, 4s, 8s, 16s, 20s, 20s, ...
type ExponentialJitter ¶
type ExponentialJitter struct {
Exponential
JitterMax time.Duration
Rand *rand.Rand
}
ExponentialJitter implements an exponential interval function with a random jitter factor added to each fixed interval.
func DefaultBinaryExponentialJitter ¶
func DefaultBinaryExponentialJitter() (ExponentialJitter, error)
DefaultBinaryExponentialJitter creates a DefaultBinaryExponential interval function with each interval adjusted by a random value between +/- 500ms. The current underlying implemntation uses crypto/rand to seed the psuedo-random generator.
Since the crypt/rand generator can fail due to io errors, the method returns an error if any.
type Intervals ¶
Intervals represents the interface backoff interval function should implement. `i` represents the current iteration. `last` represents the last backoff duration for the previous iteration, zero if this is the first iteration. The number of iterations is expected to be fairly small, but if the number of iterations is InfiniteTries (math.MaxInt8), `i` will always be InfiniteTries.