-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_client.go
225 lines (186 loc) · 5.4 KB
/
http_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
package boomerang
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"
)
// LenReader is an interface implemented by many in-memory io.Reader's. Used
// for automatically sending the right Content-Length header when possible.
type LenReader interface {
Len() int
}
func NewRequest(method, url string, body io.ReadSeeker) (*http.Request, error) {
// Make the request with the noopcloser for the body.
return http.NewRequest(method, url, body)
}
// DefaultRetryPolicy provides a default callback for Client.CheckRetry, which
// will retry on connection errors and server errors.
func DefaultRetryPolicy(resp *http.Response, err error) (bool, error) {
if err != nil {
return true, err
}
// Check the response code. We retry on 500-range responses to allow
// the server time to recover, as 500's are typically not permanent
// errors and may relate to outages on the server side. This will catch
// invalid response codes as well, like 0 and 999.
if resp.StatusCode == 0 || resp.StatusCode >= 500 {
return true, nil
}
return false, nil
}
type ClientConfig struct {
RecordMetrics bool
MetricNamespace string
Timeout time.Duration
Transport *http.Transport
Backoff Backoff
RetryFunc CheckRetry
MaxRetries int
}
func NewHttpClient(config *ClientConfig) *HttpClient {
nc := new(HttpClient)
nc.client = &http.Client{
Timeout: config.Timeout,
Transport: config.Transport,
}
nc.Logger = log.New(os.Stderr, "", log.LstdFlags)
if config.RetryFunc != nil {
nc.CheckRetry = config.RetryFunc
} else {
nc.CheckRetry = DefaultRetryPolicy
}
if config.MaxRetries > 0 {
nc.MaxRetries = config.MaxRetries
}
if nc.Backoff != nil {
nc.Backoff = config.Backoff
} else {
nc.Backoff = NewConstantBackoff(
defaultMinTimeout,
)
}
nc.RecordMetrics = config.RecordMetrics
if nc.RecordMetrics {
nc.MetricsCtx = NewPrometheusMetrics(config.MetricNamespace, config.MetricNamespace)
}
return nc
}
func DefaultHttpClient(config *ClientConfig) Client {
nc := new(HttpClient)
nc.client = &http.Client{
Timeout: config.Timeout,
Transport: config.Transport,
}
nc.Logger = log.New(os.Stderr, "", log.LstdFlags)
nc.Backoff = NewConstantBackoff(
defaultMinTimeout,
)
nc.CheckRetry = DefaultRetryPolicy
nc.MaxRetries = DefaultMaxHttpRetries
nc.RecordMetrics = config.RecordMetrics
if nc.RecordMetrics {
nc.MetricsCtx = NewPrometheusMetrics(config.MetricNamespace, config.MetricNamespace)
}
return nc
}
type HttpClient struct {
// client *http.Client
client *http.Client
Logger *log.Logger // Customer logger instance.
Backoff Backoff
// CheckRetry specifies the policy for handling retries, and is called
// after each request. The default policy is DefaultRetryPolicy.
CheckRetry CheckRetry
MaxRetries int
// To explicitly state if no metrics are to be recorded for this client
RecordMetrics bool
MetricsCtx Metrics
}
func (c *HttpClient) SetRetries(retry int) {
c.MaxRetries = retry
}
func (c *HttpClient) SetBackoff(bc Backoff) {
c.Backoff = bc
}
func (c *HttpClient) TurnOffMetrics() {
c.RecordMetrics = false
}
func (c *HttpClient) QuietMode() {
c.Logger.SetFlags(0)
c.Logger.SetOutput(ioutil.Discard)
}
func (c *HttpClient) Head(url string) (*http.Response, error) {
req, err := NewRequest("HEAD", url, nil)
if err != nil {
return nil, err
}
return c.Do(req)
}
func (c *HttpClient) Get(url string) (*http.Response, error) {
req, err := NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
return c.Do(req)
}
func (c *HttpClient) Post(url string, contentType string, body io.ReadSeeker) (*http.Response, error) {
req, err := NewRequest("POST", url, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", contentType)
return c.Do(req)
}
func (c *HttpClient) PostForm(url string, data url.Values) (*http.Response, error) {
return c.Post(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
}
func (c *HttpClient) Do(req *http.Request) (*http.Response, error) {
req.Close = true
for i := c.MaxRetries; i > 0; i-- {
// Recording time just before attempt
begin := time.Now()
// Attempt the request
resp, err := c.client.Do(req)
// record related metrics unless explicitly denied
if resp != nil && c.RecordMetrics {
c.MetricsCtx.Record(begin, resp.StatusCode, err)
}
// Check if we should continue with retries.
checkOK, checkErr := c.CheckRetry(resp, err)
if err != nil {
c.Logger.Printf("[ERR] %s %s request failed: %v", req.Method, req.URL, err)
}
if !checkOK {
if checkErr != nil {
err = checkErr
}
return resp, err
}
// We're going to retry, consume any response to reuse the connection.
if err == nil {
c.drainBody(resp.Body)
}
waitTime := c.Backoff.NextInterval(i)
desc := fmt.Sprintf("%s %s", req.Method, req.URL)
// desc = fmt.Sprintf("%s (status: %d)", desc, code)
c.Logger.Printf("[DEBUG] %s: retrying in %s (%d left)", desc, waitTime, i)
time.Sleep(waitTime)
}
// Return an error if we fall out of the retry loop
return nil, fmt.Errorf("%s %s giving up after %d attempts",
req.Method, req.URL, c.MaxRetries)
}
// Try to read the response body so we can reuse this connection.
func (c *HttpClient) drainBody(body io.ReadCloser) {
defer body.Close()
_, err := io.Copy(ioutil.Discard, io.LimitReader(body, respReadLimit))
if err != nil {
c.Logger.Printf("[ERR] error reading response body: %v", err)
}
}