/
circuit_breaker.go
195 lines (154 loc) · 3.99 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
package topolib
import (
"context"
"errors"
"net/http"
"sync/atomic"
"time"
)
type circuitBreakerCallback func(context.Context) (*http.Response, error)
const (
circuitBreakerStateClosed uint32 = iota
circuitBreakerStateHalfOpened
circuitBreakerStateOpened
)
type circuitBreaker struct {
state uint32
stateMutexChan chan bool
halfOpenTimer *time.Timer
failuresCleanupTimer *time.Timer
halfOpenAttempts uint32
failuresCount uint32
openThreshold uint32
halfOpenTimeout time.Duration
resetFailuresTimeout time.Duration
}
func (c *circuitBreaker) Do(ctx context.Context, callback circuitBreakerCallback) (*http.Response, error) {
switch atomic.LoadUint32(&c.state) {
case circuitBreakerStateClosed:
return c.doClosed(ctx, callback)
case circuitBreakerStateHalfOpened:
return c.doHalfOpened(ctx, callback)
default:
return nil, ErrCircuitBreakerOpened
}
}
func (c *circuitBreaker) doClosed(ctx context.Context, callback circuitBreakerCallback) (*http.Response, error) {
resp, err := callback(ctx)
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
select {
case <-ctx.Done():
return nil, ctx.Err()
case c.stateMutexChan <- true:
defer func() {
<-c.stateMutexChan
}()
}
}
if c.isErrorOk(err) {
c.switchState(circuitBreakerStateClosed)
return resp, err
}
c.failuresCount++
if c.state == circuitBreakerStateClosed && c.failuresCount > c.openThreshold {
c.switchState(circuitBreakerStateOpened)
}
return resp, err
}
func (c *circuitBreaker) doHalfOpened(ctx context.Context, callback circuitBreakerCallback) (*http.Response, error) {
if !atomic.CompareAndSwapUint32(&c.halfOpenAttempts, 0, 1) {
return nil, ErrCircuitBreakerOpened
}
resp, err := callback(ctx)
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
select {
case <-ctx.Done():
return nil, ctx.Err()
case c.stateMutexChan <- true:
defer func() {
<-c.stateMutexChan
}()
}
}
if c.state != circuitBreakerStateHalfOpened {
return resp, err
}
if c.isErrorOk(err) {
c.switchState(circuitBreakerStateClosed)
} else {
c.switchState(circuitBreakerStateOpened)
}
return resp, err
}
func (c *circuitBreaker) switchState(state uint32) {
switch state {
case circuitBreakerStateClosed:
c.stopTimer(&c.halfOpenTimer)
c.ensureTimer(&c.failuresCleanupTimer, c.resetFailuresTimeout, c.resetFailures)
case circuitBreakerStateHalfOpened:
c.stopTimer(&c.failuresCleanupTimer)
c.stopTimer(&c.halfOpenTimer)
case circuitBreakerStateOpened:
c.stopTimer(&c.failuresCleanupTimer)
c.ensureTimer(&c.halfOpenTimer, c.halfOpenTimeout, c.tryHalfOpen)
}
c.failuresCount = 0
atomic.StoreUint32(&c.halfOpenAttempts, 0)
atomic.StoreUint32(&c.state, state)
}
func (c *circuitBreaker) resetFailures() {
c.stateMutexChan <- true
defer func() {
<-c.stateMutexChan
}()
c.stopTimer(&c.failuresCleanupTimer)
if c.state == circuitBreakerStateClosed {
c.switchState(circuitBreakerStateClosed)
}
}
func (c *circuitBreaker) tryHalfOpen() {
c.stateMutexChan <- true
defer func() {
<-c.stateMutexChan
}()
if c.state == circuitBreakerStateOpened {
c.switchState(circuitBreakerStateHalfOpened)
}
}
func (c *circuitBreaker) stopTimer(timerRef **time.Timer) {
timer := *timerRef
if timer == nil {
return
}
timer.Stop()
select {
case <-timer.C:
default:
}
*timerRef = nil
}
func (c *circuitBreaker) ensureTimer(timerRef **time.Timer, timeout time.Duration, callback func()) {
if *timerRef == nil {
*timerRef = time.AfterFunc(timeout, callback)
}
}
func (c *circuitBreaker) isErrorOk(err error) bool {
return err == nil || errors.Is(err, ErrCircuitBreakerIgnore)
}
func newCircuitBreaker(openThreshold uint32,
halfOpenTimeout, resetFailuresTimeout time.Duration) *circuitBreaker {
cb := &circuitBreaker{
stateMutexChan: make(chan bool, 1),
openThreshold: openThreshold,
halfOpenTimeout: halfOpenTimeout,
resetFailuresTimeout: resetFailuresTimeout,
}
cb.switchState(circuitBreakerStateClosed)
return cb
}