/
backoff.go
201 lines (163 loc) · 4.11 KB
/
backoff.go
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package remilia
import (
"context"
"math"
"math/rand"
"time"
)
type randomWrapper interface {
Int63n(n int64) int64
}
type defaultRandom struct{}
func (r *defaultRandom) Int63n(n int64) int64 {
return rand.Int63n(n)
}
type backoff interface {
Reset()
Next() time.Duration
GetMaxAttempt() uint8
GetCurrentAttempt() uint8
}
type exponentialBackoff struct {
minDelay time.Duration
maxDelay time.Duration
multiplier float64
attempt uint8
maxAttempt uint8
linearAttempt uint8
random randomWrapper
backoff jitterBackoff
}
var (
defaultMinDelay = 1000 * time.Millisecond
defaultMaxDelay = 50 * time.Second
defaultMultiplier = 2.0
defaultMaxAttempt = uint8(10)
defaultLinearAttempt = uint8(5)
defaultRandomStruct = &defaultRandom{}
)
func newExponentialBackoff(optFns ...exponentialBackoffOptionFunc) *exponentialBackoff {
eb := &exponentialBackoff{
minDelay: defaultMinDelay,
maxDelay: defaultMaxDelay,
multiplier: defaultMultiplier,
attempt: 0,
maxAttempt: defaultMaxAttempt,
linearAttempt: defaultLinearAttempt,
random: defaultRandomStruct,
}
// TODO: return the error from option func
for _, optFn := range optFns {
optFn(eb)
}
eb.backoff = fullJitterBuilder(eb.minDelay, eb.maxDelay, eb.multiplier, eb.random)
eb.Reset()
return eb
}
func (eb *exponentialBackoff) Reset() {
eb.attempt = 0
}
func (eb *exponentialBackoff) Next() time.Duration {
eb.attempt++
delay := eb.backoff(eb.attempt)
return delay
}
func (eb *exponentialBackoff) GetMaxAttempt() uint8 {
return eb.maxAttempt
}
func (eb *exponentialBackoff) GetCurrentAttempt() uint8 {
return eb.attempt
}
type exponentialBackoffOptionFunc optionFunc[*exponentialBackoff]
func withRandomImp(r randomWrapper) exponentialBackoffOptionFunc {
return func(eb *exponentialBackoff) error {
eb.random = r
return nil
}
}
func WithWorkMinDelay(d time.Duration) exponentialBackoffOptionFunc {
return func(eb *exponentialBackoff) error {
eb.minDelay = d
return nil
}
}
func WithWorkMaxDelay(d time.Duration) exponentialBackoffOptionFunc {
return func(eb *exponentialBackoff) error {
eb.maxDelay = d
return nil
}
}
func WithWorkMultiplier(m float64) exponentialBackoffOptionFunc {
return func(eb *exponentialBackoff) error {
eb.multiplier = m
return nil
}
}
func WithWorkMaxAttempt(a uint8) exponentialBackoffOptionFunc {
return func(eb *exponentialBackoff) error {
eb.maxAttempt = a
return nil
}
}
func WithWorkLinearAttempt(a uint8) exponentialBackoffOptionFunc {
return func(eb *exponentialBackoff) error {
eb.linearAttempt = a
return nil
}
}
type jitterBackoff func(attempt uint8) time.Duration
func fullJitterBuilder(minDelay time.Duration, capacity time.Duration, multiplier float64, random randomWrapper) jitterBackoff {
return func(attempt uint8) time.Duration {
cap := float64(capacity)
att := float64(attempt)
base := float64(minDelay)
// TODO: switch to linear backoff after a certain number of attempts
temp := math.Min(cap, base*math.Pow(att, multiplier))
diff := int64(temp) - int64(base)
if diff <= 0 {
diff = 1
}
sleep := random.Int63n(diff) + int64(base)
return time.Duration(sleep)
}
}
// type exponentialBackoffFactory struct {
// opts []exponentialBackoffOptionFunc
// }
// func newExponentialBackoffFactory(opts ...exponentialBackoffOptionFunc) *exponentialBackoffFactory {
// return &exponentialBackoffFactory{
// opts: opts,
// }
// }
// func (f *exponentialBackoffFactory) New() *exponentialBackoff {
// return newExponentialBackoff(f.opts...)
// }
// func (f *exponentialBackoffFactory) Reset(eb *exponentialBackoff) {
// eb.Reset()
// }
type retryableFunc func() error
func retry(ctx context.Context, op retryableFunc, eb backoff) error {
var lastErr error
maxAttempts := eb.GetMaxAttempt()
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
lastErr = op()
if lastErr == nil {
return nil
}
delay := eb.Next()
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(delay):
}
}
if eb.GetCurrentAttempt() >= maxAttempts {
break
}
}
return lastErr
}