-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnicehttp.go
More file actions
219 lines (194 loc) · 6.18 KB
/
Copy pathnicehttp.go
File metadata and controls
219 lines (194 loc) · 6.18 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
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
// Package nicehttp provides a custom HTTP client with built-in support
// for rate limiting, automatic retries, and exponential backoff.
package nicehttp
import (
"bytes"
"context"
"fmt"
"io"
"log"
"net/http"
"runtime/debug"
"strconv"
"time"
)
func getVersion() string {
info, ok := debug.ReadBuildInfo()
if ok {
for _, mod := range info.Deps {
if mod.Path != "github.com/rohfle/nicehttp" {
continue
}
if mod.Replace != nil {
mod = mod.Replace
}
if mod.Version != "" && mod.Version != "(devel)" {
return mod.Version
}
}
}
return "unknown"
}
var NoTimeout time.Duration = 0
var DefaultUserAgent = fmt.Sprintf("nicehttp/%s", getVersion())
type readSeekCloser struct {
*bytes.Reader
}
func (rsc readSeekCloser) Close() error {
// No resources to free, so just return no error
return nil
}
func newReadSeekCloser(b []byte) io.ReadSeekCloser {
return readSeekCloser{bytes.NewReader(b)}
}
type NiceTransport struct {
// Headers added to every request
// Include headers like "User-Agent" and "Authorization" here
defaultHeaders http.Header
// Number of times to retry the request on failure
maxAttempts int
// Timeout for a single connection attempt, after which the request
// will be retried. This is part of the total request time, which is
// controlled by the http.Client timeout.
// A value of 0 means no timeout.
attemptTimeout time.Duration
// downstreamTransport allows override of downstream roundtripper
// Not specifying a value means http.DefaultTransport will be used
downstreamTransport http.RoundTripper
// RateLimter ensures a minimum rate limit between the start of requests
limiter *Limiter
}
func (s *NiceTransport) Clone() *NiceTransport {
var snew NiceTransport
snew.defaultHeaders = s.defaultHeaders.Clone()
snew.maxAttempts = s.maxAttempts
snew.downstreamTransport = s.downstreamTransport
if s.limiter != nil {
snew.limiter = s.limiter.Clone()
}
return &snew
}
func shouldRetryRequest(resp *http.Response, err error) bool {
if err != nil {
// Retry if a network error has occurred, such as
// - connection timeouts,
// - dns resolution errors,
// - connection reset
return true
}
switch resp.StatusCode {
case 429, 503, 504:
// The request can be retried with backoff if its status is one of:
// - 429 Too Many Requests
// - 503 Service Unavailable
// - 504 Gateway Timeout
return true
}
return false
}
// RoundTrip is a custom implementation that handles backoff, error retries and done context
func (rt *NiceTransport) RoundTrip(origReq *http.Request) (*http.Response, error) {
// in order to retry requests, we need io.Seeker to support rewinding the body
var bodySeeker io.ReadSeekCloser = nil
// the body might be nil for some methods (eg GET / HEAD)
if origReq.Body != nil {
var hasSeek bool
bodySeeker, hasSeek = origReq.Body.(io.ReadSeekCloser)
if hasSeek {
// The body is seekable so we can stream the original
// Defer the close of the original request body
defer origReq.Body.Close()
// Prevent downstream roundtripper from closing the body
origReq.Body = io.NopCloser(bodySeeker)
} else {
// The body is not seekable so we have to read it fully into memory
data, err := io.ReadAll(origReq.Body)
// Close body immediately after read, regardless if there was an error or not
origReq.Body.Close()
if err != nil {
return nil, fmt.Errorf("while reading request body: %w", err)
}
// With the read data, create a new bytes.Reader with noop Close()
// No need to defer closing the bytes.Reader
bodySeeker = newReadSeekCloser(data)
origReq.Body = bodySeeker
}
}
// Set default headers (used for User-Agent and Authorization)
if len(rt.defaultHeaders) > 0 {
for key, values := range rt.defaultHeaders {
if len(origReq.Header[key]) > 0 {
// Header already exists with at least one value, do not replace with defaults
continue
}
// Support header keys with multiple values
for _, val := range values {
origReq.Header.Add(key, val)
}
}
}
origCtx := origReq.Context()
ctx := origCtx
var cancelAttemptCtx context.CancelFunc = nil //func() {}
// maxAttempts and attemptTimeout can be overridden per request through context
maxAttempts := rt.maxAttempts
if val, ok := GetMaxAttemptsFromContext(ctx); ok {
maxAttempts = val
}
attemptTimeout := rt.attemptTimeout
if val, ok := GetAttemptTimeoutFromContext(ctx); ok {
attemptTimeout = val
}
attempt := 0
for {
// This loop will run up to maxAttempts times
attempt += 1
// Wait patiently for the limiter before sending the request up to the context deadline
if err := rt.limiter.Wait(origCtx); err != nil {
// The original context is cancelled or has exceeded deadline
return nil, err
}
if attemptTimeout != NoTimeout {
// If an attempt timeout is set, create a context with attempt timeout for each request
ctx, cancelAttemptCtx = context.WithTimeout(origCtx, rt.attemptTimeout)
}
// The request must be cloned each time it is sent
req := origReq.Clone(ctx)
// Send request using downstream roundtripper
// This call usually closes req.Body, but the io.NopCloser code above prevents this
resp, err := rt.downstreamTransport.RoundTrip(req)
// Post request cleanup
if cancelAttemptCtx != nil {
cancelAttemptCtx()
}
// Determine if the request should be retried
needsRetry := shouldRetryRequest(resp, err)
retryAfter := parseRetryAfterHeader(resp)
rt.limiter.Done(needsRetry, retryAfter)
if attempt >= maxAttempts || !needsRetry {
// The request either succeeded, failed with an http status that cannot be retried,
// or the retry limit was reached. Return the response and error to the caller.
return resp, err
}
if bodySeeker != nil {
// Rewind the body to the start on retry
bodySeeker.Seek(0, io.SeekStart)
}
}
}
func parseRetryAfterHeader(resp *http.Response) time.Duration {
if resp == nil {
return 0
}
val := resp.Header.Get("Retry-After")
if val != "" {
if secs, err := strconv.Atoi(val); err == nil {
return time.Duration(secs) * time.Second
} else if t, err := http.ParseTime(val); err == nil {
return time.Until(t)
} else {
log.Printf("nicehttp: unexpected formatting of Retry-After value %q", val)
}
}
return 0
}