/
client.go
292 lines (246 loc) · 8.3 KB
/
client.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package fetcher
import (
"bytes"
"context"
"io"
"net"
"net/http"
"net/http/httptrace"
"strings"
"time"
"go.opencensus.io/plugin/ochttp"
)
var _ Fetcher = (*Client)(nil)
// Client implements Fetcher interface and is required to execute a Request
type Client struct {
client *http.Client
// parentRequestOptions will be added to every NewRequest created with this Client
parentRequestOptions []RequestOption
keepAlive time.Duration
handshakeTimeout time.Duration
maxIdleConnsPerHost int
// Rate Limiting
rateLimit rateLimit
errorLogFunc LogFunc
debugLogFunc LogFunc
}
// NewClient returns a new Client with the given options executed
func NewClient(c context.Context, opts ...ClientOption) (*Client, error) {
cl := &Client{
keepAlive: 60 * time.Second,
handshakeTimeout: 10 * time.Second,
}
var err error
// execute all options
for _, opt := range opts {
if err = opt(c, cl); err != nil {
return nil, err
}
}
cl.setClient()
return cl, nil
}
// Do uses the client receiver to execute the provided request
func (cl *Client) Do(c context.Context, req *Request) (*Response, error) {
// if the context has been canceled or the deadline exceeded, don't start the request
if c.Err() != nil {
return nil, c.Err()
}
// if per request loggers haven't been set, inherit from the client
if cl.debugLogFunc != nil && req.debugLogFunc == nil {
req.debugLogFunc = cl.debugLogFunc
req.debugf("request using client debugLogFunc")
}
if cl.errorLogFunc != nil && req.errorLogFunc == nil {
req.errorLogFunc = cl.errorLogFunc
req.debugf("request using client errorLogFunc")
}
// inject user provided ClientTrace into the context
if req.clientTrace != nil {
req.debugf("injecting ClientTrace into context")
c = httptrace.WithClientTrace(c, req.clientTrace)
}
// set the context deadline if one was provided in the request options
if !req.deadline.IsZero() {
req.debugf("setting context deadline to %s", req.deadline)
var cancelFunc context.CancelFunc
c, cancelFunc = context.WithDeadline(c, req.deadline)
defer cancelFunc()
}
req.client = cl
httpResp, err := doWithRetries(c, req)
if err != nil {
return nil, err
}
resp := NewResponse(c, req, httpResp)
// execute all afterDoFuncs
for _, afterDo := range req.afterDoFuncs {
if err = afterDo(req, resp); err != nil {
return nil, err
}
}
return resp, nil
}
func doWithRetries(c context.Context, req *Request) (*http.Response, error) {
reqc := req.request.WithContext(c)
if buf, ok := req.payload.(*bytes.Buffer); ok {
defer putBuffer(buf)
}
var httpResp *http.Response
var err error
for i := 1; ; i++ {
// run rate-limiting
req.client.rateLimit.limit(c)
req.debugf("request attempt #%d", i)
httpResp, err = req.client.client.Do(reqc)
if err != nil && req.isErrBreaking(err) {
req.errorf("http.Client.Do err: %s | req: %s", err.Error(), req.String())
return nil, err
}
switch {
// returned when there is an underlying bad connection, so we want to retry as if it's a 500+ StatusCode
// NOTE: the io.EOF error will only be handled here if the WithRetryOnEOFError has been included with the Request
case err == io.EOF:
req.debugf("http.Client.Do returned io.EOF - request will retry | req: %s", req.String())
case err != nil && strings.Contains(err.Error(), "read: connection reset by peer"):
req.debugf("http.Client.Do returned 'read: connection reset by peer' - request will retry | req: %s", req.String())
// if we used a multipart form, we need to check for an error from the goroutine
case i == 1 && req.optMultiPartForm && req.multiPartFormErr != nil:
return nil, req.multiPartFormErr
// further attempts will be made only on 500+ status codes
// NOTE: the error returned from cl.client.Do(reqc) only contains scenarios regarding
// a bad request given, or a response with Location header missing or bad
case httpResp.StatusCode < 500:
req.debugf("status code %d < 500, exiting retry loop", httpResp.StatusCode)
return httpResp, nil
}
// return resp and err if this is the last attempt, so we don't close the response body
// or sleep unnecessarily
if i == req.maxAttempts {
req.debugf("max attempts (%d) reached, exiting retry loop", req.maxAttempts)
return httpResp, err
}
if httpResp != nil {
// close the response body before we lose our reference to it
if err = httpResp.Body.Close(); err != nil {
req.errorf(err.Error())
return nil, err
}
}
// wait before retrying, returning early if the context is cancelled
if err = req.waitForRetry(c, i); err != nil {
return nil, err
}
}
}
func (req *Request) waitForRetry(c context.Context, i int) error {
delay := req.backoffStrategy.waitDuration(i)
req.debugf("waiting %s before next retry", delay)
select {
case <-time.After(delay):
return nil
case <-c.Done():
req.debugf("context cancelled during backoff delay")
return c.Err()
}
}
// WithRequestOptions sets RequestOptions to be inherited by each NewRequest
func WithRequestOptions(opts []RequestOption) ClientOption {
return func(c context.Context, cl *Client) error {
cl.parentRequestOptions = opts
return nil
}
}
// Get is a helper func for Do, setting the Method internally
func (cl *Client) Get(c context.Context, url string, opts ...RequestOption) (*Response, error) {
req, err := cl.NewRequest(c, http.MethodGet, url, opts...)
if err != nil {
return nil, err
}
return cl.Do(c, req)
}
// Head is a helper func for Do, setting the Method internally
func (cl *Client) Head(c context.Context, url string, opts ...RequestOption) (*Response, error) {
req, err := cl.NewRequest(c, http.MethodHead, url, opts...)
if err != nil {
return nil, err
}
return cl.Do(c, req)
}
// Post is a helper func for Do, setting the Method internally
func (cl *Client) Post(c context.Context, url string, opts ...RequestOption) (*Response, error) {
req, err := cl.NewRequest(c, http.MethodPost, url, opts...)
if err != nil {
return nil, err
}
return cl.Do(c, req)
}
// Put is a helper func for Do, setting the Method internally
func (cl *Client) Put(c context.Context, url string, opts ...RequestOption) (*Response, error) {
req, err := cl.NewRequest(c, http.MethodPut, url, opts...)
if err != nil {
return nil, err
}
return cl.Do(c, req)
}
// Patch is a helper func for Do, setting the Method internally
func (cl *Client) Patch(c context.Context, url string, opts ...RequestOption) (*Response, error) {
req, err := cl.NewRequest(c, http.MethodPatch, url, opts...)
if err != nil {
return nil, err
}
return cl.Do(c, req)
}
// Delete is a helper func for Do, setting the Method internally
func (cl *Client) Delete(c context.Context, url string, opts ...RequestOption) (*Response, error) {
req, err := cl.NewRequest(c, http.MethodDelete, url, opts...)
if err != nil {
return nil, err
}
return cl.Do(c, req)
}
// ClientOption is a func to configure optional Client settings
type ClientOption func(c context.Context, cl *Client) error
// WithKeepAlive is a ClientOption that sets the cl.keepAlive field to the given duration
func WithKeepAlive(dur time.Duration) ClientOption {
return func(c context.Context, cl *Client) error {
cl.keepAlive = dur
return nil
}
}
// WithHandshakeTimeout is a ClientOption that sets the cl.handshakeTimeout field to the given duration
func WithHandshakeTimeout(dur time.Duration) ClientOption {
return func(c context.Context, cl *Client) error {
cl.handshakeTimeout = dur
return nil
}
}
// WithMaxIdleConnsPerHost is a ClientOption that sets the cl.maxIdleConnsPerHost field to the given int
func WithMaxIdleConnsPerHost(maxConns int) ClientOption {
return func(c context.Context, cl *Client) error {
cl.maxIdleConnsPerHost = maxConns
return nil
}
}
// WithRateLimit is a ClientOption that sets the cl.rateLimitting up for this client
func WithRateLimit(rate int, dur time.Duration) ClientOption {
return func(c context.Context, cl *Client) error {
cl.rateLimit = newRateLimit(rate, dur)
return nil
}
}
// setClient creates the standard http.Client using the settings in the given Client
func (cl *Client) setClient() {
cl.client = &http.Client{
Transport: &ochttp.Transport{
Base: &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
KeepAlive: cl.keepAlive,
}).Dial,
TLSHandshakeTimeout: cl.handshakeTimeout,
MaxIdleConnsPerHost: cl.maxIdleConnsPerHost,
},
},
}
}