1// Package backoff implements backoff algorithms for retrying operations.
2//
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.
6//
7// There is also Ticker type similar to time.Ticker.
8// You can use it if you need to work with channels.
9//
10// See Examples section below for usage examples.
11package backoff
12
13import "time"
14
15// BackOff is a backoff policy for retrying an operation.
16type 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.
19	//
20	// Example usage:
21	//
22	// 	duration := backoff.NextBackOff();
23	// 	if (duration == backoff.Stop) {
24	// 		// Do not retry operation.
25	// 	} else {
26	// 		// Sleep for duration and retry operation.
27	// 	}
28	//
29	NextBackOff() time.Duration
30
31	// Reset to initial state.
32	Reset()
33}
34
35// Stop indicates that no more retries should be made for use in NextBackOff().
36const Stop time.Duration = -1
37
38// ZeroBackOff is a fixed backoff policy whose backoff time is always zero,
39// meaning that the operation is retried immediately without waiting, indefinitely.
40type ZeroBackOff struct{}
41
42func (b *ZeroBackOff) Reset() {}
43
44func (b *ZeroBackOff) NextBackOff() time.Duration { return 0 }
45
46// StopBackOff is a fixed backoff policy that always returns backoff.Stop for
47// NextBackOff(), meaning that the operation should never be retried.
48type StopBackOff struct{}
49
50func (b *StopBackOff) Reset() {}
51
52func (b *StopBackOff) NextBackOff() time.Duration { return Stop }
53
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.
57type ConstantBackOff struct {
58	Interval time.Duration
59}
60
61func (b *ConstantBackOff) Reset()                     {}
62func (b *ConstantBackOff) NextBackOff() time.Duration { return b.Interval }
63
64func NewConstantBackOff(d time.Duration) *ConstantBackOff {
65	return &ConstantBackOff{Interval: d}
66}
67