-
Notifications
You must be signed in to change notification settings - Fork 0
/
breaker.go
280 lines (237 loc) · 6.32 KB
/
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
package gcb
import (
"errors"
"net/http"
"sync"
"time"
)
var (
// ErrTooManyRequests is returned when the CB state is half open and the requests count is over the cb maxRequests
ErrTooManyRequests = errors.New("too many requests")
// ErrOpenState is returned when the CB state is open
ErrOpenState = errors.New("circuit breaker is open")
)
type (
State int8
// Counts holds the numbers of requests and their successes/failures.
// Breaker clears the internal Counts either
// on the change of the state or at the closed-state intervals.
// Counts ignores the results of the requests sent before clearing.
Counts struct {
Requests uint32
TotalSuccesses uint32
TotalFailures uint32
ConsecutiveSuccesses uint32
ConsecutiveFailures uint32
}
ReadyToTrip func(counts Counts) bool
OnStateChange func(name string, from State, to State)
// Breaker is a state machine to prevent sending requests that are likely to fail.
Breaker struct {
// Name is the name of the CircuitBreaker.
name string
// MaxRequests is the maximum number of requests allowed to pass through
// when the CircuitBreaker is half-open.
// If MaxRequests is 0, the CircuitBreaker allows only 1 request.
maxRequests uint32
// Interval is the cyclic period of the closed state
// for the CircuitBreaker to clear the internal Counts.
// If Interval is 0, the CircuitBreaker doesn't clear internal Counts during the closed state.
interval time.Duration
// Timeout is the period of the open state,
// after which the state of the CircuitBreaker becomes half-open.
// If Timeout is 0, the timeout value of the CircuitBreaker is set to 60 seconds.
timeout time.Duration
// ReadyToTrip is called with a copy of Counts whenever a request fails in the closed state.
// If ReadyToTrip returns true, the CircuitBreaker will be placed into the open state.
// If ReadyToTrip is nil, default ReadyToTrip is used.
// Default ReadyToTrip returns true when the number of consecutive failures is more than 5.
readyToTrip func(counts Counts) bool
// OnStateChange is called whenever the state of the CircuitBreaker changes.
onStateChange func(name string, from State, to State)
mutex sync.Mutex
state State
generation uint64
counts Counts
expiry time.Time
}
)
const (
defaultTimeout = time.Duration(60) * time.Second
defaultInterval = time.Duration(30) * time.Second
defaultMaxRequests = 1
Close State = iota
HalfOpen
Open
)
func NewBreaker(opts ...Option) *Breaker {
// defaults
config := &Config{
timeout: defaultTimeout,
interval: defaultInterval,
maxRequests: defaultMaxRequests,
readyToTrip: defaultReadyToTrip,
onStateChange: defaultOnStateChange,
}
// apply opts
for _, opt := range opts {
opt(config)
}
cb := &Breaker{
timeout: config.timeout,
maxRequests: config.maxRequests,
readyToTrip: config.readyToTrip,
onStateChange: config.onStateChange,
state: Close,
}
cb.toNewGeneration(time.Now())
return cb
}
// TODO: why 3?
func defaultReadyToTrip(counts Counts) bool {
return counts.ConsecutiveFailures > 3
}
func defaultOnStateChange(name string, from State, to State) {
// noop
}
func (s State) String() string {
switch s {
case Open:
return "Open"
case HalfOpen:
return "HalfOpen"
case Close:
return "Close"
}
return ""
}
func (c *Counts) onRequest() {
c.Requests++
}
func (c *Counts) onSuccess() {
c.TotalSuccesses++
c.ConsecutiveSuccesses++
c.ConsecutiveFailures = 0
}
func (c *Counts) onFailure() {
c.TotalFailures++
c.ConsecutiveFailures++
c.ConsecutiveSuccesses = 0
}
func (c *Counts) clear() {
c.Requests = 0
c.TotalSuccesses = 0
c.TotalFailures = 0
c.ConsecutiveSuccesses = 0
c.ConsecutiveFailures = 0
}
// Execute runs the given request if the CircuitBreaker accepts it.
// Execute returns an error instantly if the CircuitBreaker rejects the request.
// Otherwise, Execute returns the result of the request.
// If a panic occurs in the request, the CircuitBreaker handles it as an error
// and causes the same panic again.
func (cb *Breaker) Execute(req func() (*http.Response, error)) (*http.Response, error) {
generation, err := cb.beforeRequest()
if err != nil {
return nil, err
}
defer func() {
e := recover()
if e != nil {
cb.afterRequest(generation, false)
panic(e)
}
}()
result, err := req()
cb.afterRequest(generation, err == nil)
return result, err
}
func (cb *Breaker) beforeRequest() (uint64, error) {
cb.mutex.Lock()
defer cb.mutex.Unlock()
state, generation := cb.currentState(time.Now())
if state == Open {
return generation, ErrOpenState
} else if state == HalfOpen && cb.counts.Requests >= cb.maxRequests {
return generation, ErrTooManyRequests
}
cb.counts.onRequest()
return generation, nil
}
func (cb *Breaker) afterRequest(before uint64, success bool) {
cb.mutex.Lock()
defer cb.mutex.Unlock()
now := time.Now()
state, generation := cb.currentState(now)
if generation != before {
return
}
if success {
cb.onSuccess(state, now)
} else {
cb.onFailure(state, now)
}
}
func (cb *Breaker) toNewGeneration(now time.Time) {
cb.generation++
cb.counts.clear()
var zero time.Time
switch cb.state {
case Close:
if cb.interval == 0 {
cb.expiry = zero
} else {
cb.expiry = now.Add(cb.interval)
}
case Open:
cb.expiry = now.Add(cb.timeout)
default: // StateHalfOpen
cb.expiry = zero
}
}
func (cb *Breaker) currentState(now time.Time) (State, uint64) {
switch cb.state {
case Close:
if !cb.expiry.IsZero() && cb.expiry.Before(now) {
cb.toNewGeneration(now)
}
case Open:
if cb.expiry.Before(now) {
cb.setState(HalfOpen, now)
}
}
return cb.state, cb.generation
}
func (cb *Breaker) setState(state State, now time.Time) {
if cb.state == state {
return
}
prev := cb.state
cb.state = state
cb.toNewGeneration(now)
if cb.onStateChange != nil {
cb.onStateChange(cb.name, prev, state)
}
}
func (cb *Breaker) onSuccess(state State, now time.Time) {
switch state {
case Close:
cb.counts.onSuccess()
case HalfOpen:
cb.counts.onSuccess()
if cb.counts.ConsecutiveSuccesses >= cb.maxRequests {
cb.setState(Close, now)
}
}
}
func (cb *Breaker) onFailure(state State, now time.Time) {
switch state {
case Close:
cb.counts.onFailure()
if cb.readyToTrip(cb.counts) {
cb.setState(Open, now)
}
case HalfOpen:
cb.setState(Open, now)
}
}