-
Notifications
You must be signed in to change notification settings - Fork 0
/
circuit_breaker.go
285 lines (244 loc) · 7.38 KB
/
circuit_breaker.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
package gcb
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"time"
)
var (
// makes sure circuit implements the round tripper interface
_ http.RoundTripper = (*circuit)(nil)
// We need to consume response bodies to maintain http connections, but
// limit the size we consume to respReadLimit.
respReadLimit = int64(4096)
rateLimitExceeded = errors.New("exceeded rate limit")
)
type (
// TODO: can be removed?
// ErrorHandler is called if shouldRetry are expired, containing the last status
// from the http library. If not specified, default behavior for the library is
// to close the body and return an error indicating how many tries were
// attempted. If overriding this, be sure to close the body if needed.
ErrorHandler func(resp *http.Response, err error, numTries int) (*http.Response, error)
// ReaderFunc is the type of function that can be given natively to newRequest
ReaderFunc func() (io.ReadCloser, error)
// LenReader is an interface implemented by many in-memory io.Reader's. Used
// for automatically sending the right Content-Length header when possible.
LenReader interface {
Len() int
}
// Request wraps the metadata needed to create HTTP requests.
Request struct {
// body is a seekable reader over the request body payload. This is
// used to rewind the request data in between shouldRetry.
Body ReaderFunc
// Embed an HTTP request directly. This makes a *Request act exactly
// like an *http.Request so that all meta methods are supported.
*http.Request
}
circuit struct {
retrier *Retrier
breaker *Breaker
RoundTripper http.RoundTripper
// ErrorHandler specifies the custom error handler to use, if any
ErrorHandler ErrorHandler
}
)
func newCircuitBreaker(opts ...Option) *circuit {
retrier := NewRetrier(opts...)
breaker := NewBreaker(opts...)
return &circuit{
retrier: retrier,
breaker: breaker,
RoundTripper: http.DefaultTransport,
}
}
// RoundTrip intercepts the request and takes action from here:
// The return
// - retry
// - rate limiting
// - circuit breaking
func (c *circuit) RoundTrip(req *http.Request) (*http.Response, error) {
// wraps the original request
//request, err := newRequest(req)
//if err != nil {
// return nil, err
//}
// the circuit breaker
res, err := c.breaker.Execute(func() (*http.Response, error) {
var code int // HTTP response code
var resp *http.Response // HTTP response
var err error
// run X times
var i uint32
for i = 0; ; i++ {
resp, err = c.RoundTripper.RoundTrip(req)
// Check if we should continue with shouldRetry.
shouldRetry, checkErr := c.retrier.retryPolicy(req.Context(), resp, err)
// Now decide if we should continue.
if !shouldRetry {
if checkErr != nil {
err = checkErr
}
// Depending on the policy, if the request is valid
// we'll return here
return resp, err
}
// We do this before drainBody because there's no need for the I/O if
// we're breaking out
remain := c.retrier.RetryMax - i
if remain <= 0 {
err = fmt.Errorf("%s: %s %s giving up after %d attempts", errMaxRetriesReached,
req.Method, req.URL, c.retrier.RetryMax+1)
break
}
// We're going to retry, consume any response to reuse the connection.
if err == nil && resp != nil {
c.drainBody(resp.Body)
}
wait := c.retrier.Backoff(c.retrier.RetryWaitMin, c.retrier.RetryWaitMax, i, resp)
c.logRetry(req, code, wait, remain)
select {
case <-req.Context().Done():
return nil, req.Context().Err()
case <-time.After(wait):
}
}
return resp, err
})
if req.Body != nil {
_ = req.Body.Close()
}
// If there is a response we keep the response for the client and ignore our
// errors, otherwise we return an error.
// Returning a response and an error would be ignored by the client middleware anyway and just return the error.
if res != nil {
return res, nil
}
return nil, err
}
func (c *circuit) logRetry(req *http.Request, code int, wait time.Duration, remain uint32) {
desc := fmt.Sprintf("%s %s", req.Method, req.URL)
if code > 0 {
desc = fmt.Sprintf("%s (status: %d)", desc, code)
}
log.Printf("[DEBUG] %s: retrying in %s (%d left)\n", desc, wait, remain)
}
// newRequest creates a new wrapped request.
//func newRequest(method, url string, rawBody io.ReadCloser) (*Request, error) {
// bodyReader, contentLength, err := getBodyReaderAndContentLength(rawBody)
// if err != nil {
// return nil, err
// }
//
// httpReq, err := http.NewRequest(method, url, rawBody)
// if err != nil {
// return nil, err
// }
// httpReq.ContentLength = contentLength
// httpReq.GetBody = bodyReader
//
// return &Request{bodyReader, httpReq}, nil
//}
func getBodyReaderAndContentLength(rawBody interface{}) (ReaderFunc, int64, error) {
var bodyReader ReaderFunc
var contentLength int64
if rawBody != nil {
switch body := rawBody.(type) {
// If they gave us a function already, great! Use it.
case ReaderFunc:
bodyReader = body
tmp, err := body()
if err != nil {
return nil, 0, err
}
if lr, ok := tmp.(LenReader); ok {
contentLength = int64(lr.Len())
}
if c, ok := tmp.(io.Closer); ok {
_ = c.Close()
}
case func() (io.Reader, error):
tmp, err := body()
bodyReader = func() (io.ReadCloser, error) {
return ioutil.NopCloser(tmp), nil
}
if err != nil {
return nil, 0, err
}
if lr, ok := tmp.(LenReader); ok {
contentLength = int64(lr.Len())
}
if c, ok := tmp.(io.Closer); ok {
_ = c.Close()
}
// If a regular byte slice, we can read it over and over via new
// readers
case []byte:
buf := body
bodyReader = func() (io.ReadCloser, error) {
return ioutil.NopCloser(bytes.NewReader(buf)), nil
}
contentLength = int64(len(buf))
// If a bytes.Buffer we can read the underlying byte slice over and
// over
case *bytes.Buffer:
buf := body
bodyReader = func() (io.ReadCloser, error) {
return ioutil.NopCloser(bytes.NewReader(buf.Bytes())), nil
}
contentLength = int64(buf.Len())
// We prioritize *bytes.Reader here because we don't really want to
// deal with it seeking so want it to match here instead of the
// io.ReadSeeker case.
case *bytes.Reader:
buf, err := ioutil.ReadAll(body)
if err != nil {
return nil, 0, err
}
bodyReader = func() (io.ReadCloser, error) {
return ioutil.NopCloser(bytes.NewReader(buf)), nil
}
contentLength = int64(len(buf))
// Compat case
case io.ReadSeeker:
raw := body
bodyReader = func() (io.ReadCloser, error) {
_, err := raw.Seek(0, 0)
return ioutil.NopCloser(raw), err
}
if lr, ok := raw.(LenReader); ok {
contentLength = int64(lr.Len())
}
// Read all in so we can reset
case io.Reader:
buf, err := ioutil.ReadAll(body)
if err != nil {
return nil, 0, err
}
bodyReader = func() (io.ReadCloser, error) {
readCloser := ioutil.NopCloser(bytes.NewReader(buf))
return readCloser, nil
}
contentLength = int64(len(buf))
default:
return nil, 0, fmt.Errorf("cannot handle type %T", rawBody)
}
}
return bodyReader, contentLength, nil
}
// Try to read the response body so we can reuse this connection.
func (c *circuit) drainBody(body io.ReadCloser) {
defer body.Close()
_, err := io.Copy(ioutil.Discard, io.LimitReader(body, respReadLimit))
if err != nil {
log.Printf("[ERR] error reading response body: %v", err)
}
}
func (c *circuit) GetState() State {
return c.breaker.state
}