Repository navigation
Expand file tree
/
Copy pathbackoff.go
More file actions
134 lines (109 loc) · 3.14 KB
/
Copy pathbackoff.go
File metadata and controls
134 lines (109 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package backoff
import (
"errors"
"math"
"math/rand"
"time"
)
type Backoff struct {
min *time.Duration
max *time.Duration
jitter bool
slotTime time.Duration
attempts uint
maxAttempts uint
performedAttempts uint
}
var ErrMaxDurationMustBeGreater = errors.New("max duration must be greater than min duration")
var ErrInvalidSlotTime = errors.New("slot time cannot be negative")
var ErrInvalidMaxAttempts = errors.New("max attempt cannot be 0")
// New creates a fully parameterized Backoff
// min, max minimum and maximum waiting time, nextDuration will always return values between min and max
// slotTime theoretical duration to perform the action that we are retrying, more info: https://en.wikipedia.org/wiki/Slot_time
// jitter if true, the next duration will be a random number between minimum and next computed duration
// about jitter: https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/
// maxAttempts maximum number of attempts at which the next duration will stop to increase (when jitter is activated
// the range will stop to increase)
func New(min, max, slotTime time.Duration, jitter bool, maxAttempts uint) (*Backoff, error) {
if max < min {
return nil, ErrMaxDurationMustBeGreater
}
if slotTime < 0 {
return nil, ErrInvalidSlotTime
}
if maxAttempts == 0 {
return nil, ErrInvalidMaxAttempts
}
return &Backoff{
min: &min,
max: &max,
jitter: jitter,
slotTime: slotTime,
maxAttempts: maxAttempts,
}, nil
}
const (
// default backoff configuration
defaultUnit = time.Millisecond
defaultMin = 0 * defaultUnit
defaultJitter = true
defaultSlotTime = 100 * defaultUnit
defaultMaxAttempts uint = 10
)
// NewDefault creates a Backoff with default configuration
func NewDefault() Backoff {
dMin := defaultMin
return Backoff{
min: &dMin,
jitter: defaultJitter,
slotTime: defaultSlotTime,
maxAttempts: defaultMaxAttempts,
}
}
// NextDuration returns the next waiting time
func (b *Backoff) NextDuration() time.Duration {
if b.jitter {
return b.expJitter()
}
return b.exp()
}
func (b *Backoff) expJitter() time.Duration {
nd := b.calcNextDuration()
if nd == 0 {
return nd
}
rand.Seed(time.Now().UnixNano())
x := nd - *b.min
if x > 0 {
x = time.Duration(rand.Intn(int(x)))
}
return x + *b.min
}
func (b *Backoff) exp() time.Duration {
return b.calcNextDuration()
}
func (b *Backoff) calcNextDuration() time.Duration {
b.incAttempts()
d := time.Duration(math.Pow(2, float64(b.attempts))-1) * b.slotTime
if b.min != nil && d < *b.min {
return *b.min
}
if b.max != nil && d > *b.max {
return *b.max
}
return d
}
func (b *Backoff) incAttempts() {
if b.attempts < b.maxAttempts {
b.attempts++
}
b.performedAttempts++
}
// HasReachedMaxAttempts returns whether the backoff has reached the max number of attempts
func (b *Backoff) HasReachedMaxAttempts() bool {
return b.attempts == b.maxAttempts
}
// PerformedAttempts returns the current number of performed attempts
func (b *Backoff) PerformedAttempts() uint {
return b.performedAttempts
}