Repository navigation
Expand file tree
/
Copy pathretry.go
More file actions
131 lines (122 loc) · 3.88 KB
/
Copy pathretry.go
File metadata and controls
131 lines (122 loc) · 3.88 KB
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
package sdk
import (
"io"
"math/rand/v2"
"net/http"
"strconv"
"strings"
"time"
)
// Retries (off unless WithRetries asks for them): idempotent methods only,
// on these statuses, honouring Retry-After up to maxRetryAfter, else backing
// off exponentially from retryBase.
var (
retryMethods = map[string]bool{http.MethodGet: true, http.MethodHead: true, http.MethodOptions: true, http.MethodPut: true, http.MethodDelete: true}
retryStatuses = map[int]bool{http.StatusTooManyRequests: true, http.StatusServiceUnavailable: true}
)
const (
retryBase = 500 * time.Millisecond
maxRetryAfter = 60 * time.Second
)
// WithRetries sends a GET, PUT or DELETE refused with 429 or 503 again, up
// to n more times, after the server's Retry-After (up to 60 s; a longer one
// is not waited for) or an exponential backoff. Off (0) by default. The
// client's timeout covers all the attempts.
func WithRetries(n int) Option { return func(o *options) { o.retries = n } }
// resendTransport sends a request again: once with a fresh token after a
// 401, when src can refresh it, and after a 429 or 503 up to retries times
// (retryDelay). One loop holds both, so a refresh neither resets the retry
// budget nor resends a refused token. A request whose body cannot be
// replayed is never resent.
type resendTransport struct {
base http.RoundTripper
src RefreshingTokenSource // nil: no refresh
retries int
}
func (t resendTransport) RoundTrip(req *http.Request) (*http.Response, error) {
base := t.base
if base == nil {
base = http.DefaultTransport
}
replayable := req.Body == nil || req.Body == http.NoBody || req.GetBody != nil
refreshed := false
for tries := 0; ; {
resp, err := base.RoundTrip(req)
if err != nil || !replayable {
return resp, err
}
var next *http.Request
switch {
case resp.StatusCode == http.StatusUnauthorized && t.src != nil && !refreshed && req.Header.Get("Authorization") != "":
discard(resp)
t.src.Invalidate()
tok, err := t.src.Token(req.Context())
if err != nil {
return nil, err
}
if next, err = clone(req); err != nil {
return nil, err
}
next.Header.Set("Authorization", "Bearer "+tok)
refreshed = true
default:
delay, ok := retryDelay(resp, req.Method, tries, t.retries)
if !ok {
return resp, nil
}
discard(resp)
timer := time.NewTimer(delay)
select {
case <-req.Context().Done():
timer.Stop()
return nil, req.Context().Err()
case <-timer.C:
}
if next, err = clone(req); err != nil {
return nil, err
}
tries++
}
req = next
}
}
// clone is req ready to send again, its body replayed.
func clone(req *http.Request) (*http.Request, error) {
next := req.Clone(req.Context())
if req.GetBody != nil {
body, err := req.GetBody()
if err != nil {
return nil, err
}
next.Body = body
}
return next, nil
}
func discard(resp *http.Response) {
_, _ = io.Copy(io.Discard, resp.Body)
resp.Body.Close()
}
// retryDelay is how long to wait before sending a refused request again,
// and false not to: when retries are used up, the method is not
// idempotent, the status is not one to retry, or the server asks for a
// longer wait than maxRetryAfter.
func retryDelay(resp *http.Response, method string, tries, retries int) (time.Duration, bool) {
if tries >= retries || !retryMethods[strings.ToUpper(method)] || !retryStatuses[resp.StatusCode] {
return 0, false
}
after := strings.TrimSpace(resp.Header.Get("Retry-After"))
if after == "" {
backoff := float64(retryBase) * float64(int64(1)<<tries) * (0.5 + rand.Float64()/2) //nolint:gosec // jitter, not security
return time.Duration(backoff), true
}
var d time.Duration
if secs, err := strconv.ParseFloat(after, 64); err == nil {
d = time.Duration(secs * float64(time.Second))
} else if when, err := http.ParseTime(after); err == nil {
d = time.Until(when)
} else {
return 0, false
}
d = max(d, 0)
return d, d <= maxRetryAfter
}