-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolicies.go
210 lines (184 loc) · 6.12 KB
/
policies.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
202
203
204
205
206
207
208
209
210
// SPDX-License-Identifier: MIT
//
// Copyright 2022 Andrew Bursavich. All rights reserved.
// Use of this source code is governed by The MIT License
// which can be found in the LICENSE file.
package retry
import (
"math"
"math/rand/v2"
"time"
)
// Default policy values.
const (
DefaultMinBackoff = 150 * time.Millisecond
DefaultMaxBackoff = 15 * time.Second
DefaultGrowthFactor = 1.5
DefaultJitterFactor = 0.5
)
var defaultPolicy = WithDefaultRandomJitter(DefaultExponentialBackoff())
// DefaultPolicy returns a Policy using the default exponential backoff
// with the default random jitter.
//
// This results in the following behavior:
//
// Attempt Backoff Total
// 1 [0.075s, 0.225s] [ 0.075s, 0.225s]
// 2 [0.113s, 0.338s] [ 0.188s, 0.562s]
// 3 [0.169s, 0.506s] [ 0.356s, 1.069s]
// 4 [0.253s, 0.759s] [ 0.609s, 1.828s]
// 5 [0.380s, 1.139s] [ 0.989s, 2.967s]
// 6 [0.570s, 1.709s] [ 1.559s, 4.676s]
// 7 [0.854s, 2.563s] [ 2.413s, 7.239s]
// 8 [1.281s, 3.844s] [ 3.694s, 11.083s]
// 9 [1.922s, 5.767s] [ 5.617s, 16.850s]
// 10 [2.883s, 8.650s] [ 8.500s, 25.499s]
// 11 [4.325s, 12.975s] [12.825s, 38.474s]
// 12 [6.487s, 19.462s] [19.312s, 57.936s]
// 13 [7.500s, 22.500s] [26.812s, 80.436s]
// 14 [7.500s, 22.500s] [34.312s, 102.936s]
// 15 [7.500s, 22.500s] [41.812s, 125.436s]
// ... ... ...
func DefaultPolicy() Policy {
return defaultPolicy
}
var never = WithMaxRetries(nil, 0)
// Never returns a Policy that doesn't allow any retry attempts.
func Never() Policy {
return never
}
var immediately = ConstantBackoff(0)
// Immediately returns a Policy that retries with no backoff.
func Immediately() Policy {
return immediately
}
// ConstantBackoff returns a Policy that uses a constant backoff duration.
func ConstantBackoff(backoff time.Duration) Policy {
return constantBackoff{backoff}
}
type constantBackoff struct {
backoff time.Duration
}
func (p constantBackoff) Next(err error, start, now time.Time, attempt int) (time.Duration, bool) {
return time.Duration(p.backoff), true
}
// ExponentialBackoff returns a Policy in which the backoff grows exponentially.
// The backoff will start at the min and will be scaled by the growth factor
// for each successive attempt until it's capped at the max.
func ExponentialBackoff(min, max time.Duration, factor float64) Policy {
if min <= 0 {
min = DefaultMinBackoff
}
if max <= 0 {
max = DefaultMaxBackoff
}
if factor <= 1 {
factor = DefaultGrowthFactor
}
return &exponentialBackoff{
min: min,
max: max,
factor: factor,
}
}
// DefaultExponentialBackoff returns an ExponentialBackoff Policy with the default values
// of min 150ms, max 15s, and factor 150%.
//
// This results in the following behavior:
//
// Attempt Backoff Total
// 1 0.150s 0.150s
// 2 0.225s 0.375s
// 3 0.338s 0.713s
// 4 0.506s 1.219s
// 5 0.759s 1.978s
// 6 1.139s 3.117s
// 7 1.709s 4.826s
// 8 2.563s 7.389s
// 9 3.844s 11.233s
// 10 5.767s 17.000s
// 11 8.650s 25.649s
// 12 12.975s 38.624s
// 13 15.000s 53.624s
// 14 15.000s 68.624s
// 15 15.000s 83.624s
// ... ... ...
func DefaultExponentialBackoff() Policy {
return ExponentialBackoff(DefaultMinBackoff, DefaultMaxBackoff, DefaultGrowthFactor)
}
type exponentialBackoff struct {
min time.Duration
max time.Duration
factor float64
}
func (p *exponentialBackoff) Next(err error, start, now time.Time, attempt int) (time.Duration, bool) {
growthFactor := math.Pow(p.factor, float64(attempt-1))
backoff := growthFactor * float64(p.min)
if float64(p.max) < backoff {
return p.max, true
}
return time.Duration(backoff), true
}
// WithRandomJitter returns a Policy that wraps the parent Policy and adds or subtracts
// random jitter as a factor of its backoff. For example, with a factor of 0.5
// and a parent backoff of 10s, the randomized backoff would be in [5s, 15s].
func WithRandomJitter(parent Policy, factor float64) Policy {
if factor <= 0 || factor > 1 {
factor = DefaultJitterFactor
}
return &withRandomJitter{parent: parent, factor: factor}
}
// WithDefaultRandomJitter returns a Policy that wraps the parent Policy with random jitter
// using the default factor of 50%.
func WithDefaultRandomJitter(parent Policy) Policy {
return WithRandomJitter(parent, DefaultJitterFactor)
}
type withRandomJitter struct {
parent Policy
factor float64
}
func (p *withRandomJitter) Next(err error, start, now time.Time, attempt int) (time.Duration, bool) {
d, allow := p.parent.Next(err, start, now, attempt)
if !allow {
return 0, false
}
r := rand.Float64()
// r = [0, 1)
// 2*r = [0, 2)
// 2*r - 1 = [-1, 1)
// f*(2*r - 1) = [-f, f)
// 1 + f*(2*r - 1) = [1 - f, 1 + f)
// d*(1 + f*(2*r - 1)) = [d - f*d, d + f*d)
return time.Duration(float64(d) * (1 + (p.factor * (2*r - 1)))), true
}
// WithMaxRetries returns a Policy that wraps the parent Policy and sets a limit
// for the total number of retry attempts.
func WithMaxRetries(parent Policy, limit int) Policy {
return &maxRetries{parent, limit}
}
type maxRetries struct {
parent Policy
limit int
}
func (p *maxRetries) Next(err error, start, now time.Time, attempt int) (time.Duration, bool) {
if attempt > p.limit {
return 0, false
}
return p.parent.Next(err, start, now, attempt)
}
// WithMaxElapsedDuration returns a Policy that wraps the parent Policy and sets a limit
// for the total elapsed duration in which retries are allowed.
func WithMaxElapsedDuration(parent Policy, limit time.Duration) Policy {
return &maxElapsed{parent, limit}
}
type maxElapsed struct {
parent Policy
limit time.Duration
}
func (p *maxElapsed) Next(err error, start, now time.Time, attempt int) (time.Duration, bool) {
d, ok := p.parent.Next(err, start, now, attempt)
if start.Add(p.limit).Before(now.Add(d)) {
return 0, false
}
return d, ok
}