-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathretryafter.go
73 lines (68 loc) · 1.95 KB
/
retryafter.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
package transport
import (
"context"
"net/http"
"strconv"
"time"
)
// RetryAfter determines whether or not the transport will automatically retry
// a request based on configured behaviors for 429 responses with Retry-After header.
type RetryAfter struct {
wrapped http.RoundTripper
backoffPolicy BackoffPolicy
}
// RoundTrip executes a request and applies one or more retry policies.
func (c *RetryAfter) RoundTrip(r *http.Request) (*http.Response, error) {
var copier, e = newRequestCopier(r)
var parentCtx = r.Context()
if e != nil {
return nil, e
}
var response *http.Response
var requestCtx, cancel = context.WithCancel(parentCtx)
var req = copier.Copy().WithContext(requestCtx)
var backoffer = c.backoffPolicy()
var retryAfter time.Duration
for {
if retryAfter > 0 {
select {
case <-parentCtx.Done():
cancel()
return nil, parentCtx.Err()
case <-time.After(retryAfter):
}
requestCtx, cancel = context.WithCancel(parentCtx) // nolint
req = copier.Copy().WithContext(requestCtx)
}
response, e = c.wrapped.RoundTrip(req)
if e != nil {
break
}
if response.StatusCode != 429 {
break
} else {
retryAfterString := response.Header.Get("Retry-After")
if retryAfterString == "" {
retryAfter = backoffer.Backoff(r, response, e)
} else {
var retryAfterInt int
var err error
if retryAfterInt, err = strconv.Atoi(retryAfterString); err != nil {
break
}
retryAfter = time.Duration(retryAfterInt) * time.Second
}
}
}
if e != nil {
cancel()
}
return response, e // nolint
}
// NewRetryAfter configures a RoundTripper decorator to honor a status code 429 response,
// using the Retry-After header directive when present, or the backoffPolicy if not present.
func NewRetryAfter() func(http.RoundTripper) http.RoundTripper {
return func(wrapped http.RoundTripper) http.RoundTripper {
return &RetryAfter{wrapped: wrapped, backoffPolicy: NewExponentialBackoffPolicy(1 * time.Second)}
}
}