-
Notifications
You must be signed in to change notification settings - Fork 19
/
do.go
251 lines (226 loc) · 6.41 KB
/
do.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
package retryablehttp
import (
"context"
"crypto/tls"
"fmt"
"io"
"net/http"
"net/http/httptrace"
"strings"
"time"
dac "github.com/Mzack9999/go-http-digest-auth-client"
)
// PassthroughErrorHandler is an ErrorHandler that directly passes through the
// values from the net/http library for the final request. The body is not
// closed.
func PassthroughErrorHandler(resp *http.Response, err error, _ int) (*http.Response, error) {
return resp, err
}
// Do wraps calling an HTTP method with retries.
func (c *Client) Do(req *Request) (*http.Response, error) {
var resp *http.Response
var err error
// Create a main context that will be used as the main timeout
mainCtx, cancel := context.WithTimeout(context.Background(), c.options.Timeout)
defer cancel()
retryMax := c.options.RetryMax
if ctxRetryMax := req.Context().Value(RETRY_MAX); ctxRetryMax != nil {
if maxRetriesParsed, ok := ctxRetryMax.(int); ok {
retryMax = maxRetriesParsed
}
}
for i := 0; ; i++ {
// request body can be read multiple times
// hence no need to rewind it
if c.RequestLogHook != nil {
c.RequestLogHook(req.Request, i)
}
if c.options.Trace {
c.wrapContextWithTrace(req)
}
if req.hasAuth() && req.Auth.Type == DigestAuth {
digestTransport := dac.NewTransport(req.Auth.Username, req.Auth.Password)
digestTransport.HTTPClient = c.HTTPClient
resp, err = digestTransport.RoundTrip(req.Request)
} else {
// Attempt the request with standard behavior
resp, err = c.HTTPClient.Do(req.Request)
}
// Check if we should continue with retries.
checkOK, checkErr := c.CheckRetry(req.Context(), resp, err)
// if err is equal to missing minor protocol version retry with http/2
if err != nil && strings.Contains(err.Error(), "net/http: HTTP/1.x transport connection broken: malformed HTTP version \"HTTP/2\"") {
resp, err = c.HTTPClient2.Do(req.Request)
checkOK, checkErr = c.CheckRetry(req.Context(), resp, err)
}
if err != nil {
// Increment the failure counter as the request failed
req.Metrics.Failures++
} else {
// Call this here to maintain the behavior of logging all requests,
// even if CheckRetry signals to stop.
if c.ResponseLogHook != nil {
// Call the response logger function if provided.
c.ResponseLogHook(resp)
}
}
// Now decide if we should continue.
if !checkOK {
if checkErr != nil {
err = checkErr
}
c.closeIdleConnections()
return resp, err
}
// We do this before drainBody beause there's no need for the I/O if
// we're breaking out
remain := retryMax - i
if remain <= 0 {
break
}
// Increment the retries counter as we are going to do one more retry
req.Metrics.Retries++
// We're going to retry, consume any response to reuse the connection.
if err == nil && resp != nil {
c.drainBody(req, resp)
}
// Wait for the time specified by backoff then retry.
// If the context is cancelled however, return.
wait := c.Backoff(c.options.RetryWaitMin, c.options.RetryWaitMax, i, resp)
// Exit if the main context or the request context is done
// Otherwise, wait for the duration and try again.
// use label to explicitly specify what to break
selectstatement:
select {
case <-mainCtx.Done():
break selectstatement
case <-req.Context().Done():
c.closeIdleConnections()
return nil, req.Context().Err()
case <-time.After(wait):
}
}
if c.ErrorHandler != nil {
c.closeIdleConnections()
return c.ErrorHandler(resp, err, retryMax+1)
}
// By default, we close the response body and return an error without
// returning the response
if resp != nil {
resp.Body.Close()
}
c.closeIdleConnections()
return nil, fmt.Errorf("%s %s giving up after %d attempts: %w", req.Method, req.URL, retryMax+1, err)
}
// Try to read the response body so we can reuse this connection.
func (c *Client) drainBody(req *Request, resp *http.Response) {
_, err := io.Copy(io.Discard, io.LimitReader(resp.Body, c.options.RespReadLimit))
if err != nil {
req.Metrics.DrainErrors++
}
resp.Body.Close()
}
const closeConnectionsCounter = 100
func (c *Client) closeIdleConnections() {
if c.options.KillIdleConn {
if c.requestCounter.Load() < closeConnectionsCounter {
c.requestCounter.Add(1)
} else {
c.requestCounter.Store(0)
c.HTTPClient.CloseIdleConnections()
}
}
}
func (c *Client) wrapContextWithTrace(req *Request) {
traceInfo := &TraceInfo{}
trace := &httptrace.ClientTrace{
GotConn: func(connInfo httptrace.GotConnInfo) {
traceInfo.GotConn = TraceEventInfo{
Time: time.Now(),
Info: connInfo,
}
},
DNSDone: func(dnsInfo httptrace.DNSDoneInfo) {
traceInfo.DNSDone = TraceEventInfo{
Time: time.Now(),
Info: dnsInfo,
}
},
GetConn: func(hostPort string) {
traceInfo.GetConn = TraceEventInfo{
Time: time.Now(),
Info: hostPort,
}
},
PutIdleConn: func(err error) {
traceInfo.PutIdleConn = TraceEventInfo{
Time: time.Now(),
Info: err,
}
},
GotFirstResponseByte: func() {
traceInfo.GotFirstResponseByte = TraceEventInfo{
Time: time.Now(),
}
},
Got100Continue: func() {
traceInfo.Got100Continue = TraceEventInfo{
Time: time.Now(),
}
},
DNSStart: func(di httptrace.DNSStartInfo) {
traceInfo.DNSStart = TraceEventInfo{
Time: time.Now(),
Info: di,
}
},
ConnectStart: func(network, addr string) {
traceInfo.ConnectStart = TraceEventInfo{
Time: time.Now(),
Info: struct {
Network, Addr string
}{network, addr},
}
},
ConnectDone: func(network, addr string, err error) {
if err == nil {
traceInfo.ConnectDone = TraceEventInfo{
Time: time.Now(),
Info: struct {
Network, Addr string
Error error
}{network, addr, err},
}
}
},
TLSHandshakeStart: func() {
traceInfo.TLSHandshakeStart = TraceEventInfo{
Time: time.Now(),
}
},
TLSHandshakeDone: func(cs tls.ConnectionState, err error) {
if err == nil {
traceInfo.TLSHandshakeDone = TraceEventInfo{
Time: time.Now(),
Info: struct {
ConnectionState tls.ConnectionState
Error error
}{cs, err},
}
}
},
WroteHeaders: func() {
traceInfo.WroteHeaders = TraceEventInfo{
Time: time.Now(),
}
},
WroteRequest: func(wri httptrace.WroteRequestInfo) {
traceInfo.WroteRequest = TraceEventInfo{
Time: time.Now(),
Info: wri,
}
},
}
req.TraceInfo = traceInfo
req.Request = req.Request.WithContext(httptrace.WithClientTrace(req.Request.Context(), trace))
}