-
Notifications
You must be signed in to change notification settings - Fork 12
/
backoff.go
423 lines (382 loc) · 13.3 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// Copyright 2022 The searKing Author. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package http
import (
"bytes"
"context"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"time"
time_ "github.com/searKing/golang/go/time"
)
var (
// A regular expression to match the error returned by net/http when the
// configured number of redirects is exhausted. This error isn't typed
// specifically so we resort to matching on the error string.
redirectsErrorRe = regexp.MustCompile(`stopped after \d+ redirects\z`)
// A regular expression to match the error returned by net/http when the
// scheme specified in the URL is invalid. This error isn't typed
// specifically so we resort to matching on the error string.
schemeErrorRe = regexp.MustCompile(`unsupported protocol scheme`)
)
// RetryAfter tries to parse Retry-After response header when a http.StatusTooManyRequests
// (HTTP Code 429) is found in the resp parameter. Hence, it will return the number of
// seconds the server states it may be ready to process more requests from this client.
// Don't retry if the error was due to too many redirects.
// Don't retry if the error was due to an invalid protocol scheme.
// Don't retry if the error was due to TLS cert verification failure.
// Don't retry if the http's StatusCode is http.StatusNotImplemented.
func RetryAfter(resp *http.Response, err error, defaultBackoff time.Duration) (backoff time.Duration, retry bool) {
backoff = defaultBackoff
if resp != nil {
if resp.StatusCode == http.StatusTooManyRequests || resp.StatusCode == http.StatusServiceUnavailable {
if sleepInSeconds, err := strconv.ParseInt(resp.Header.Get("Retry-After"), 10, 64); err == nil {
backoff = time.Second * time.Duration(sleepInSeconds)
}
}
}
if err != nil {
if v, ok := err.(*url.Error); ok {
// Don't retry if the error was due to too many redirects.
if redirectsErrorRe.MatchString(v.Error()) {
return backoff, false
}
// Don't retry if the error was due to an invalid protocol scheme.
if schemeErrorRe.MatchString(v.Error()) {
return backoff, false
}
// Don't retry if the error was due to TLS cert verification failure.
if _, ok := v.Err.(x509.UnknownAuthorityError); ok {
return backoff, false
}
}
// The error is likely recoverable so retry.
return backoff, true
}
if resp != nil {
// 429 Too Many Requests is recoverable. Sometimes the server puts
// a Retry-After response header to indicate when the server is
// available to start processing request from client.
if resp.StatusCode == http.StatusTooManyRequests {
return backoff, true
}
// Check the response code. We retry on 500-range responses to allow
// the server time to recover, as 500's are typically not permanent
// errors and may relate to outages on the server side. This will catch
// invalid response codes as well, like 0 and 999.
if resp.StatusCode == 0 || (resp.StatusCode >= http.StatusInternalServerError && resp.StatusCode != http.StatusNotImplemented) {
return backoff, true
}
}
return backoff, false
}
// ReplaceHttpRequestBody replace Body and recalculate ContentLength
// If ContentLength should not be recalculated, save and restore it after ReplaceHttpRequestBody
func ReplaceHttpRequestBody(req *http.Request, body io.Reader) {
if req.Body != nil {
req.Body.Close()
}
rc, ok := body.(io.ReadCloser)
if !ok && body != nil {
rc = io.NopCloser(body)
}
req.Body = rc
req.ContentLength = 0
if body != nil {
switch v := body.(type) {
case *bytes.Buffer:
req.ContentLength = int64(v.Len())
buf := v.Bytes()
req.GetBody = func() (io.ReadCloser, error) {
r := bytes.NewReader(buf)
return io.NopCloser(r), nil
}
case *bytes.Reader:
req.ContentLength = int64(v.Len())
snapshot := *v
req.GetBody = func() (io.ReadCloser, error) {
r := snapshot
return io.NopCloser(&r), nil
}
case *strings.Reader:
req.ContentLength = int64(v.Len())
snapshot := *v
req.GetBody = func() (io.ReadCloser, error) {
r := snapshot
return io.NopCloser(&r), nil
}
default:
// This is where we'd set it to -1 (at least
// if body != NoBody) to mean unknown, but
// that broke people during the Go 1.8 testing
// period. People depend on it being 0 I
// guess. Maybe retry later. See Issue 18117.
}
// For client requests, Request.ContentLength of 0
// means either actually 0, or unknown. The only way
// to explicitly say that the ContentLength is zero is
// to set the Body to nil. But turns out too much code
// depends on NewRequest returning a non-nil Body,
// so we use a well-known ReadCloser variable instead
// and have the http package also treat that sentinel
// variable to mean explicitly zero.
if req.GetBody != nil && req.ContentLength == 0 {
req.Body = http.NoBody
req.GetBody = func() (io.ReadCloser, error) { return http.NoBody, nil }
}
}
}
// ClientInvoker is called by ClientInterceptor to complete RPCs.
type ClientInvoker func(req *http.Request, retry int) (*http.Response, error)
// ClientInterceptor intercepts the execution of a HTTP on the client.
// interceptors can be specified as a DoWithBackoffOption, using
// WithClientInterceptor() or WithChainClientInterceptor(), when DoWithBackoffOption.
// When a interceptor(s) is set, gRPC delegates all http invocations to the interceptor,
// and it is the responsibility of the interceptor to call invoker to complete the processing
// of the HTTP.
type ClientInterceptor func(req *http.Request, retry int, invoker ClientInvoker, opts ...DoWithBackoffOption) (resp *http.Response, err error)
type RetryAfterHandler func(resp *http.Response, err error, defaultBackoff time.Duration) (backoff time.Duration, retry bool)
// DoRetryHandler send an HTTP request with retry seq and returns an HTTP response, following
// policy (such as redirects, cookies, auth) as configured on the
// client.
type DoRetryHandler = ClientInvoker
var DefaultClientDoRetryHandler = func(req *http.Request, retry int) (*http.Response, error) {
return http.DefaultClient.Do(req)
}
var DefaultTransportDoRetryHandler = func(req *http.Request, retry int) (*http.Response, error) {
return http.DefaultTransport.RoundTrip(req)
}
//go:generate go-option -type "doWithBackoff"
type doWithBackoff struct {
DoRetryHandler DoRetryHandler
clientInterceptor ClientInterceptor
ChainClientInterceptors []ClientInterceptor
RetryAfter RetryAfterHandler
ExponentialBackOffOption []time_.ExponentialBackOffOption
}
func (o *doWithBackoff) SetDefault() {
o.DoRetryHandler = DefaultClientDoRetryHandler
o.RetryAfter = RetryAfter
}
// getClientInvoker recursively generate the chained client invoker.
func getClientInvoker(interceptors []ClientInterceptor, curr int, finalInvoker ClientInvoker, opts ...DoWithBackoffOption) ClientInvoker {
if curr == len(interceptors)-1 {
return finalInvoker
}
return func(req *http.Request, retry int) (*http.Response, error) {
return interceptors[curr+1](req, retry, getClientInvoker(interceptors, curr+1, finalInvoker), opts...)
}
}
func (o *doWithBackoff) Complete() {
if o.DoRetryHandler == nil {
o.DoRetryHandler = DefaultClientDoRetryHandler
}
interceptors := o.ChainClientInterceptors
o.ChainClientInterceptors = nil
// Prepend o.ClientInterceptor to the chaining interceptors if it exists, since ClientInterceptor will
// be executed before any other chained interceptors.
if o.clientInterceptor != nil {
interceptors = append([]ClientInterceptor{o.clientInterceptor}, interceptors...)
}
var chainedInt ClientInterceptor
if len(interceptors) == 0 {
chainedInt = nil
} else if len(interceptors) == 1 {
chainedInt = interceptors[0]
} else {
chainedInt = func(req *http.Request, retry int, invoker ClientInvoker, opts ...DoWithBackoffOption) (resp *http.Response, err error) {
return interceptors[0](req, retry, getClientInvoker(interceptors, 0, invoker), opts...)
}
}
o.clientInterceptor = chainedInt
}
// DoWithBackoff will retry by exponential backoff if failed.
// If request is not rewindable, retry wil be skipped.
func DoWithBackoff(httpReq *http.Request, opts ...DoWithBackoffOption) (resp *http.Response, err error) {
var opt doWithBackoff
opt.SetDefault()
opt.ApplyOptions(opts...)
if opt.RetryAfter == nil {
opt.RetryAfter = RetryAfter
}
opt.Complete()
var option []time_.ExponentialBackOffOption
option = append(option, time_.WithExponentialBackOffOptionMaxElapsedCount(3))
option = append(option, opt.ExponentialBackOffOption...)
backoff := time_.NewDefaultExponentialBackOff(option...)
rewindableErr := RequestWithBodyRewindable(httpReq)
var retries int
var errs []error
defer func() {
if resp != nil {
if err := errors.Join(errs...); err != nil {
if resp.Header == nil {
resp.Header = make(http.Header)
}
resp.Header.Add("Warning", Warn{
Warn: err.Error(),
WarnCode: WarnMiscellaneousWarning,
}.String())
}
}
}()
for {
if retries > 0 && httpReq.GetBody != nil {
newBody, err := httpReq.GetBody()
if err != nil {
errs = append(errs, err)
return nil, errors.Join(errs...)
}
httpReq.Body = newBody
}
var do = opt.DoRetryHandler
httpDo := do
if opt.clientInterceptor != nil {
httpDo = func(req *http.Request, retry int) (*http.Response, error) {
return opt.clientInterceptor(req, retry, do, opts...)
}
}
resp, err = httpDo(httpReq, retries)
errs = append(errs, err)
wait, ok := backoff.NextBackOff()
if !ok {
if err != nil {
return nil, fmt.Errorf("http do reach backoff limit after retries %d: %w", retries, errors.Join(errs...))
} else {
return resp, nil
}
}
wait, retry := opt.RetryAfter(resp, err, wait)
if !retry {
if err != nil {
return nil, fmt.Errorf("http do reach server limit after retries %d: %w", retries, errors.Join(errs...))
} else {
return resp, nil
}
}
if rewindableErr != nil {
if err != nil {
return nil, fmt.Errorf("http do cannot rewindbody after retries %d: %w", retries, errors.Join(errs...))
} else {
resp.Header.Add("Warning", Warn{
Warn: errors.Join(errs...).Error(),
WarnCode: WarnMiscellaneousWarning,
}.String())
return resp, nil
}
}
timer := time.NewTimer(wait)
select {
case <-timer.C:
retries++
continue
case <-httpReq.Context().Done():
timer.Stop()
if err != nil {
return nil, fmt.Errorf("http do canceled after retries %d: %w", retries, errors.Join(errs...))
} else {
return resp, nil
}
}
}
}
func HeadWithBackoff(ctx context.Context, url string, opts ...DoWithBackoffOption) (*http.Response, error) {
req, err := http.NewRequest(http.MethodHead, url, nil)
req = req.WithContext(ctx)
if err != nil {
return nil, err
}
return DoWithBackoff(req, opts...)
}
func GetWithBackoff(ctx context.Context, url string, opts ...DoWithBackoffOption) (*http.Response, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
req = req.WithContext(ctx)
if err != nil {
return nil, err
}
return DoWithBackoff(req, opts...)
}
func PostWithBackoff(ctx context.Context, url, contentType string, body io.Reader, opts ...DoWithBackoffOption) (resp *http.Response, err error) {
req, err := http.NewRequest(http.MethodPost, url, body)
req = req.WithContext(ctx)
if err != nil {
return nil, err
}
if contentType != "" {
req.Header.Set("Content-Type", contentType)
}
return DoWithBackoff(req, opts...)
}
func PostFormWithBackoff(ctx context.Context, url string, data url.Values, opts ...DoWithBackoffOption) (resp *http.Response, err error) {
return PostWithBackoff(ctx, url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()), opts...)
}
func PutWithBackoff(ctx context.Context, url, contentType string, body io.Reader, opts ...DoWithBackoffOption) (resp *http.Response, err error) {
req, err := http.NewRequest(http.MethodPut, url, body)
req = req.WithContext(ctx)
if err != nil {
return nil, err
}
if contentType != "" {
req.Header.Set("Content-Type", contentType)
}
return DoWithBackoff(req, opts...)
}
// DoJson the same as HttpDo, but bind with json
func DoJson(httpReq *http.Request, req, resp interface{}) error {
if req != nil {
data, err := json.Marshal(req)
if err != nil {
return err
}
reqBody := bytes.NewReader(data)
httpReq.Header.Set("Content-Type", "application/json")
ReplaceHttpRequestBody(httpReq, reqBody)
}
httpResp, err := DefaultClientDoRetryHandler(httpReq, 0)
if err != nil {
return err
}
defer httpResp.Body.Close()
if resp == nil {
return nil
}
body, err := io.ReadAll(httpResp.Body)
if err != nil {
return err
}
return json.Unmarshal(body, resp)
}
// DoJsonWithBackoff the same as DoWithBackoff, but bind with json
func DoJsonWithBackoff(httpReq *http.Request, req, resp interface{}, opts ...DoWithBackoffOption) error {
if req != nil {
data, err := json.Marshal(req)
if err != nil {
return err
}
reqBody := bytes.NewReader(data)
httpReq.Header.Set("Content-Type", "application/json")
ReplaceHttpRequestBody(httpReq, reqBody)
}
httpResp, err := DoWithBackoff(httpReq, opts...)
if err != nil {
return err
}
defer httpResp.Body.Close()
if resp == nil {
return nil
}
body, err := io.ReadAll(httpResp.Body)
if err != nil {
return err
}
return json.Unmarshal(body, resp)
}