1 // Package backoff implements backoff algorithms for retrying operations.
3 // Use Retry function for retrying operations that may fail.
4 // If Retry does not meet your needs,
5 // copy/paste the function into your project and modify as you wish.
7 // There is also Ticker type similar to time.Ticker.
8 // You can use it if you need to work with channels.
10 // See Examples section below for usage examples.
15 // BackOff is a backoff policy for retrying an operation.
16 type BackOff interface {
17 // NextBackOff returns the duration to wait before retrying the operation,
18 // or backoff. Stop to indicate that no more retries should be made.
22 // duration := backoff.NextBackOff();
23 // if (duration == backoff.Stop) {
24 // // Do not retry operation.
26 // // Sleep for duration and retry operation.
29 NextBackOff() time.Duration
31 // Reset to initial state.
35 // Stop indicates that no more retries should be made for use in NextBackOff().
36 const Stop time.Duration = -1
38 // ZeroBackOff is a fixed backoff policy whose backoff time is always zero,
39 // meaning that the operation is retried immediately without waiting, indefinitely.
40 type ZeroBackOff struct{}
42 func (b *ZeroBackOff) Reset() {}
44 func (b *ZeroBackOff) NextBackOff() time.Duration { return 0 }
46 // StopBackOff is a fixed backoff policy that always returns backoff.Stop for
47 // NextBackOff(), meaning that the operation should never be retried.
48 type StopBackOff struct{}
50 func (b *StopBackOff) Reset() {}
52 func (b *StopBackOff) NextBackOff() time.Duration { return Stop }
54 // ConstantBackOff is a backoff policy that always returns the same backoff delay.
55 // This is in contrast to an exponential backoff policy,
56 // which returns a delay that grows longer as you call NextBackOff() over and over again.
57 type ConstantBackOff struct {
58 Interval time.Duration
61 func (b *ConstantBackOff) Reset() {}
62 func (b *ConstantBackOff) NextBackOff() time.Duration { return b.Interval }
64 func NewConstantBackOff(d time.Duration) *ConstantBackOff {
65 return &ConstantBackOff{Interval: d}