-
Notifications
You must be signed in to change notification settings - Fork 0
/
retryer.go
220 lines (190 loc) · 6.62 KB
/
retryer.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
211
212
213
214
215
216
217
218
219
220
package https
import (
"math"
"net"
"net/http"
"strconv"
"time"
)
// RequestRetryOption represents the type for request retry options.
type RequestRetryOption int
// Constants for different request retry options.
const (
Default RequestRetryOption = iota
Enable
Disable
)
// String returns the string representation of the RequestRetryOption.
func (r RequestRetryOption) String() string {
switch r {
case Enable:
return "enable"
case Disable:
return "disable"
default:
return "default"
}
}
// RetryConfigurationOptions represents a function that modifies RetryConfiguration settings.
type RetryConfigurationOptions func(*RetryConfiguration)
// RetryConfiguration contains the settings for request retry behavior.
type RetryConfiguration struct {
maxRetryAttempts int64
retryOnTimeout bool
retryInterval time.Duration
maximumRetryWaitTime time.Duration
backoffFactor int64
httpStatusCodesToRetry []int64
httpMethodsToRetry []string
}
// NewRetryConfiguration creates a new RetryConfiguration instance with the provided options.
func NewRetryConfiguration(options ...RetryConfigurationOptions) RetryConfiguration {
retryConfig := RetryConfiguration{}
for _, option := range options {
option(&retryConfig)
}
return retryConfig
}
// WithMaxRetryAttempts sets the maximum number of retry attempts allowed.
func WithMaxRetryAttempts(maxRetryAttempts int64) RetryConfigurationOptions {
return func(r *RetryConfiguration) {
r.maxRetryAttempts = maxRetryAttempts
}
}
// WithRetryOnTimeout sets whether to retry on timeouts.
func WithRetryOnTimeout(retryOnTimeout bool) RetryConfigurationOptions {
return func(r *RetryConfiguration) {
r.retryOnTimeout = retryOnTimeout
}
}
// WithRetryInterval sets the interval between retries.
func WithRetryInterval(retryInterval time.Duration) RetryConfigurationOptions {
return func(r *RetryConfiguration) {
r.retryInterval = retryInterval
}
}
// WithMaximumRetryWaitTime sets the maximum wait time before giving up retrying.
func WithMaximumRetryWaitTime(maximumRetryWaitTime time.Duration) RetryConfigurationOptions {
return func(r *RetryConfiguration) {
r.maximumRetryWaitTime = maximumRetryWaitTime
}
}
// WithBackoffFactor sets the backoff factor for exponential backoff.
func WithBackoffFactor(backoffFactor int64) RetryConfigurationOptions {
return func(r *RetryConfiguration) {
r.backoffFactor = backoffFactor
}
}
// WithHttpStatusCodesToRetry sets the list of HTTP status codes to retry on.
func WithHttpStatusCodesToRetry(httpStatusCodesToRetry []int64) RetryConfigurationOptions {
return func(r *RetryConfiguration) {
r.httpStatusCodesToRetry = httpStatusCodesToRetry
}
}
// WithHttpMethodsToRetry sets the list of HTTP methods to retry on.
func WithHttpMethodsToRetry(httpMethodsToRetry []string) RetryConfigurationOptions {
return func(r *RetryConfiguration) {
r.httpMethodsToRetry = httpMethodsToRetry
}
}
// MaxRetryAttempts returns the maximum number of retry attempts allowed.
func (r *RetryConfiguration) MaxRetryAttempts() int64 {
return r.maxRetryAttempts
}
// MaximumRetryWaitTime returns the maximum wait time before giving up retrying.
func (r *RetryConfiguration) MaximumRetryWaitTime() time.Duration {
return r.maximumRetryWaitTime
}
// BackoffFactor returns the backoff factor for exponential backoff.
func (r *RetryConfiguration) BackoffFactor() int64 {
return r.backoffFactor
}
// RetryInterval returns the interval between retries.
func (r *RetryConfiguration) RetryInterval() time.Duration {
return r.retryInterval
}
// RetryOnTimeout returns whether to retry on timeouts.
func (r *RetryConfiguration) RetryOnTimeout() bool {
return r.retryOnTimeout
}
// HttpMethodsToRetry returns the list of HTTP methods to retry on.
func (r *RetryConfiguration) HttpMethodsToRetry() []string {
return r.httpMethodsToRetry
}
// HttpStatusCodesToRetry returns the list of HTTP status codes to retry on.
func (r *RetryConfiguration) HttpStatusCodesToRetry() []int64 {
return r.httpStatusCodesToRetry
}
// containsHttpStatusCodesToRetry checks if the given HTTP status code exists in the list of status codes to retry on.
func (rc *RetryConfiguration) containsHttpStatusCodesToRetry(httpStatusCode int64) bool {
for _, val := range rc.httpStatusCodesToRetry {
if val == httpStatusCode {
return true
}
}
return false
}
// containsHttpMethodsToRetry checks if the given HTTP method exists in the list of methods to retry on.
func (rc *RetryConfiguration) containsHttpMethodsToRetry(httpMethod string) bool {
for _, v := range rc.httpMethodsToRetry {
if v == httpMethod {
return true
}
}
return false
}
// GetRetryWaitTime calculates the wait time for the next retry attempt.
func (rc *RetryConfiguration) GetRetryWaitTime(
maxWaitTime time.Duration,
retryCount int64,
response *http.Response,
timeoutError error) time.Duration {
retry := false
var retryAfter, retryWaitTime time.Duration
if retryCount < rc.maxRetryAttempts {
if err, ok := timeoutError.(net.Error); ok && err.Timeout() {
retry = rc.retryOnTimeout
} else if response != nil && response.Header != nil {
retryAfter = getRetryAfterInSeconds(response.Header)
if retryAfter > 0 || rc.containsHttpStatusCodesToRetry(int64(response.StatusCode)) {
retry = true
}
}
if retry {
retryWaitTime = defaultBackoff(rc.retryInterval, maxWaitTime, retryAfter, rc.backoffFactor, retryCount)
}
}
return retryWaitTime
}
// ShouldRetry determines if the request should be retried based on the RetryConfiguration and request HTTP method.
func (rc *RetryConfiguration) ShouldRetry(retryRequestOption RequestRetryOption, httpMethod string) bool {
switch retryRequestOption.String() {
default:
if rc.maxRetryAttempts > 0 && httpMethod != "" && rc.containsHttpMethodsToRetry(httpMethod) {
return true
}
return false
case "enable":
return true
case "disable":
return false
}
}
// getRetryAfterInSeconds extracts and returns the Retry-After duration from the response headers in seconds.
func getRetryAfterInSeconds(headers http.Header) time.Duration {
if s, ok := headers[http.CanonicalHeaderKey("retry-after")]; ok {
if sleep, err := strconv.ParseInt(s[0], 10, 64); err == nil {
return time.Duration(sleep)
}
}
return 0
}
// defaultBackoff calculates the backoff time for exponential backoff based on the retry configuration.
func defaultBackoff(retryInterval, maxWaitTime, retryAfter time.Duration, backoffFactor, retryCount int64) time.Duration {
waitTime := (math.Pow(float64(backoffFactor), float64(retryCount)) * float64(retryInterval))
sleep := math.Max(waitTime, float64(retryAfter))
if time.Duration(sleep) <= maxWaitTime {
return time.Duration(sleep)
}
return 0
}