-
Notifications
You must be signed in to change notification settings - Fork 2
/
retry.go
98 lines (77 loc) · 2.52 KB
/
retry.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
package retry
import (
"context"
"crypto/x509"
"errors"
"net/http"
"net/url"
"regexp"
retry "github.com/avast/retry-go/v3"
)
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`)
// ErrInvalidStatusCode for http retry.
ErrInvalidStatusCode = errors.New("invalid status code")
)
// NewRoundTripper for retry.
func NewRoundTripper(cfg *Config, hrt http.RoundTripper) *RoundTripper {
return &RoundTripper{cfg: cfg, RoundTripper: hrt}
}
// RoundTripper for retry.
type RoundTripper struct {
cfg *Config
http.RoundTripper
}
func (r *RoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
var (
res *http.Response
err error
)
ctx := req.Context()
operation := func() error {
tctx, cancel := context.WithTimeout(ctx, r.cfg.Timeout)
defer cancel()
res, err = r.RoundTripper.RoundTrip(req.WithContext(tctx))
if err != nil {
return r.recover(err)
}
// 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 res.StatusCode == 0 || (res.StatusCode >= 500 && res.StatusCode != 501) {
return ErrInvalidStatusCode
}
return nil
}
// We don't need to check the error as it's only used to retry. We save the last error in err.
retry.Do(operation, retry.Attempts(r.cfg.Attempts)) // nolint:errcheck
return res, err
}
func (r *RoundTripper) recover(err error) error {
var uerr *url.Error
if errors.As(err, &uerr) {
// Don't retry if the error was due to too many redirects.
if redirectsErrorRe.MatchString(uerr.Error()) {
return nil
}
// Don't retry if the error was due to an invalid protocol scheme.
if schemeErrorRe.MatchString(uerr.Error()) {
return nil
}
var uaerr x509.UnknownAuthorityError
// Don't retry if the error was due to TLS cert verification failure.
if errors.As(uerr.Err, &uaerr) {
return nil
}
}
// The error is likely recoverable so retry.
return err
}