-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
283 lines (245 loc) · 8.56 KB
/
http.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
package utility
import (
"crypto/tls"
"net"
"net/http"
"sync"
"time"
"github.com/PuerkitoBio/rehttp"
"golang.org/x/oauth2"
)
const httpClientTimeout = 5 * time.Minute
var httpClientPool *sync.Pool
func init() {
initHTTPPool()
}
func initHTTPPool() {
httpClientPool = &sync.Pool{
New: func() interface{} { return newBaseConfiguredHttpClient() },
}
}
func newBaseConfiguredHttpClient() *http.Client {
return &http.Client{
Timeout: httpClientTimeout,
Transport: newConfiguredBaseTransport(),
}
}
func newConfiguredBaseTransport() *http.Transport {
return &http.Transport{
TLSClientConfig: &tls.Config{},
Proxy: http.ProxyFromEnvironment,
DisableCompression: false,
DisableKeepAlives: true,
IdleConnTimeout: 20 * time.Second,
MaxIdleConnsPerHost: 10,
MaxIdleConns: 50,
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 0,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
}
}
func setupOauth2HTTPClient(token string, client *http.Client) *http.Client {
client.Transport = &oauth2.Transport{
Base: client.Transport,
Source: oauth2.ReuseTokenSource(nil, oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)),
}
return client
}
// GetHTTPClient produces default HTTP client from the pool,
// constructing a new client if needed. Always pair calls to
// GetHTTPClient with defered calls to PutHTTPClient.
func GetHTTPClient() *http.Client { return httpClientPool.Get().(*http.Client) }
// PutHTTPClient returns the client to the pool, automatically
// reconfiguring the transport.
func PutHTTPClient(c *http.Client) {
c.Timeout = httpClientTimeout
switch transport := c.Transport.(type) {
case *http.Transport:
transport.TLSClientConfig.InsecureSkipVerify = false
c.Transport = transport
case *rehttp.Transport:
c.Transport = transport.RoundTripper
PutHTTPClient(c)
return
case *oauth2.Transport:
c.Transport = transport.Base
PutHTTPClient(c)
return
default:
c.Transport = newConfiguredBaseTransport()
}
httpClientPool.Put(c)
}
// HTTPRetryConfiguration makes it possible to configure the retry
// semantics for retryable clients. In most cases, construct this
// object using the NewDefaultHttpRetryConf, which provides reasonable
// defaults.
type HTTPRetryConfiguration struct {
MaxDelay time.Duration
BaseDelay time.Duration
MaxRetries int
TemporaryErrors bool
Methods []string
Statuses []int
Errors []error
ErrorStrings []string
}
// NewDefaultHTTPRetryConf constructs a HTTPRetryConfiguration object
// with reasonable defaults.
func NewDefaultHTTPRetryConf() HTTPRetryConfiguration {
return HTTPRetryConfiguration{
MaxRetries: 50,
TemporaryErrors: true,
MaxDelay: 5 * time.Second,
BaseDelay: 50 * time.Millisecond,
Methods: []string{
http.MethodGet,
http.MethodPost,
http.MethodPut,
http.MethodDelete,
http.MethodPatch,
},
Statuses: []int{
http.StatusInternalServerError,
http.StatusBadGateway,
http.StatusServiceUnavailable,
http.StatusGatewayTimeout,
http.StatusInsufficientStorage,
http.StatusConflict,
http.StatusRequestTimeout,
http.StatusPreconditionFailed,
http.StatusExpectationFailed,
},
}
}
// GetHTTPRetryableClient produces an HTTP client that automatically
// retries failed requests according to the configured
// parameters. Couple calls to GetHTTPRetryableClient, with defered
// calls to PutHTTPClient.
func GetHTTPRetryableClient(conf HTTPRetryConfiguration) *http.Client {
client := GetHTTPClient()
statusRetries := []rehttp.RetryFn{}
if len(conf.Statuses) > 0 {
statusRetries = append(statusRetries, rehttp.RetryStatuses(conf.Statuses...))
} else {
conf.TemporaryErrors = true
}
if conf.TemporaryErrors {
statusRetries = append(statusRetries, rehttp.RetryTemporaryErr())
}
if len(conf.Errors) > 0 {
statusRetries = append(statusRetries, rehttp.RetryIsErr(func(err error) bool {
for _, errToCheck := range conf.Errors {
if err == errToCheck {
return true
}
}
return false
}))
}
if len(conf.ErrorStrings) > 0 {
statusRetries = append(statusRetries, rehttp.RetryIsErr(func(err error) bool {
for _, errToCheck := range conf.ErrorStrings {
if err.Error() == errToCheck {
return true
}
}
return false
}))
}
retryFns := []rehttp.RetryFn{rehttp.RetryAny(statusRetries...)}
if len(conf.Methods) > 0 {
retryFns = append(retryFns, rehttp.RetryHTTPMethods(conf.Methods...))
}
if conf.MaxRetries > 0 {
retryFns = append(retryFns, rehttp.RetryMaxRetries(conf.MaxRetries))
}
client.Transport = rehttp.NewTransport(client.Transport,
rehttp.RetryAll(retryFns...),
rehttp.ExpJitterDelay(conf.BaseDelay, conf.MaxDelay))
return client
}
// GetDefaultHTTPRetryableClient provides a retryable client with
// the default settings. Couple calls to GetHTTPRetryableClient, with defered
// calls to PutHTTPClient.
func GetDefaultHTTPRetryableClient() *http.Client {
return GetHTTPRetryableClient(NewDefaultHTTPRetryConf())
}
// HTTPRetryFunction makes it possible to write customizable retry
// logic. Returning true if the request should be retried again and
// false otherwise.
type HTTPRetryFunction func(index int, req *http.Request, resp *http.Response, err error) bool
// HTTPDelayFunction makes it possible to write customizable retry
// backoff logic, by allowing you to evaluate the previous request and
// response and return the duration to wait before the next request.
type HTTPDelayFunction func(index int, req *http.Request, resp *http.Response, err error) time.Duration
func makeRetryFn(in HTTPRetryFunction) rehttp.RetryFn {
return func(attempt rehttp.Attempt) bool {
return in(attempt.Index, attempt.Request, attempt.Response, attempt.Error)
}
}
func makeDelayFn(in HTTPDelayFunction) rehttp.DelayFn {
return func(attempt rehttp.Attempt) time.Duration {
return in(attempt.Index, attempt.Request, attempt.Response, attempt.Error)
}
}
// GetCustomHTTPRetryableClient allows you to generate an HTTP client
// that automatically retries failed request based on the provided
// custom logic.
func GetCustomHTTPRetryableClient(retry HTTPRetryFunction, delay HTTPDelayFunction) *http.Client {
client := GetHTTPClient()
client.Transport = rehttp.NewTransport(client.Transport, makeRetryFn(retry), makeDelayFn(delay))
return client
}
// GetOAuth2HTTPClient produces an HTTP client that will supply OAuth2
// credentials with all requests. There is no validation of the
// token, and you should always call PutHTTPClient to return the
// client to the pool when you're done with it.
func GetOAuth2HTTPClient(oauthToken string) *http.Client {
return setupOauth2HTTPClient(oauthToken, GetHTTPClient())
}
// GetOauth2DefaultHTTPRetryableClient constructs an HTTP client that
// supplies OAuth2 credentials with all requests, retrying failed
// requests automatically according to the default retryable
// options. There is no validation of the token, and you should always
// call PutHTTPClient to return the client to the pool when you're
// done with it.
func GetOauth2DefaultHTTPRetryableClient(oauthToken string) *http.Client {
return setupOauth2HTTPClient(oauthToken, GetDefaultHTTPRetryableClient())
}
// GetOauth2HTTPRetryableClient constructs an HTTP client that
// supplies OAuth2 credentials with all requests, retrying failed
// requests automatically according to the configuration
// provided. There is no validation of the token, and you should
// always call PutHTTPClient to return the client to the pool when
// you're done with it.
func GetOauth2HTTPRetryableClient(oauthToken string, conf HTTPRetryConfiguration) *http.Client {
return setupOauth2HTTPClient(oauthToken, GetHTTPRetryableClient(conf))
}
// GetOauth2HTTPRetryableClient constructs an HTTP client that
// supplies OAuth2 credentials with all requests, retrying failed
// requests automatically according to definitions of the provided
// functions. There is no validation of the token, and you should
// always call PutHTTPClient to return the client to the pool when
// you're done with it.
func GetOauth2CustomHTTPRetryableClient(token string, retry HTTPRetryFunction, delay HTTPDelayFunction) *http.Client {
return setupOauth2HTTPClient(token, GetCustomHTTPRetryableClient(retry, delay))
}
// TemporayError defines an interface for use in retryable HTTP
// clients to identify certain errors as Temporary.
type TemporaryError interface {
error
Temporary() bool
}
// IsTemporaryError returns true if the error object is also a
// temporary error.
func IsTemporaryError(err error) bool {
if terr, ok := err.(TemporaryError); ok {
return terr.Temporary()
}
return false
}