-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimplementation.go
226 lines (193 loc) · 5.19 KB
/
implementation.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
package fastbreaker
import (
"container/ring"
"sync/atomic"
"time"
)
type counters struct {
executions atomic.Uint64
failures atomic.Uint64
}
func (c *counters) reset() {
c.executions.Store(0)
c.failures.Store(0)
}
type fastBreaker struct {
configuration Configuration
state atomic.Value
ring *ring.Ring
advanceTicker *time.Ticker
totalCounters *counters
rejected atomic.Uint64
breakTimer *time.Timer
halfOpenAllowed atomic.Bool
}
// New creates a new CircuitBreaker with the passed Configuration.
func New(configuration Configuration) FastBreaker {
// Read configuration and applies default values.
if configuration.NumBuckets <= 0 {
configuration.NumBuckets = DefaultNumBuckets
}
configuration.BucketDuration = configuration.BucketDuration.Truncate(time.Second)
if configuration.BucketDuration <= 0 {
configuration.BucketDuration = DefaultBucketDuration
}
configuration.DurationOfBreak = configuration.DurationOfBreak.Truncate(time.Second)
if configuration.DurationOfBreak <= 0 {
configuration.DurationOfBreak = DefaultDurationOfBreak
}
if configuration.ShouldTrip == nil {
configuration.ShouldTrip = DefaultShouldTrip
}
// Build the circuit breaker.
cb := &fastBreaker{
configuration: configuration,
ring: ring.New(int(configuration.NumBuckets)),
advanceTicker: time.NewTicker(configuration.BucketDuration),
totalCounters: &counters{},
}
cb.state.Store(StateStopped)
// Initialize ring counter.
for i := 0; i < cb.ring.Len(); i++ {
cb.ring.Value = &counters{}
cb.ring = cb.ring.Next()
}
// Reset the counters.
cb.totalCounters.reset()
cb.reset()
// Start the advance window goroutine.
go cb.advanceWindow()
return cb
}
func (cb *fastBreaker) Configuration() Configuration {
return cb.configuration
}
func (cb *fastBreaker) Stop() {
cb.state.Store(StateStopped)
if cb.breakTimer != nil {
cb.breakTimer.Stop()
}
cb.advanceTicker.Stop()
}
func (cb *fastBreaker) Allow() (func(bool), error) {
switch cb.state.Load() {
case StateStopped:
// Stopped states rejects all executions.
return nil, ErrCircuitStopped
case StateClosed:
// Closed state allows all executions.
return cb.buildFeedbackFunc(StateClosed), nil
case StateHalfOpen:
// Half-open state allows just one execution.
if cb.halfOpenAllowed.CompareAndSwap(true, false) {
return cb.buildFeedbackFunc(StateHalfOpen), nil
}
}
// Reject other executions.
cb.rejected.Add(1)
return nil, ErrCircuitOpen
}
func (cb *fastBreaker) State() State {
return cb.state.Load().(State)
}
func (cb *fastBreaker) Executions() uint64 {
return cb.totalCounters.executions.Load()
}
func (cb *fastBreaker) Failures() uint64 {
return cb.totalCounters.failures.Load()
}
func (cb *fastBreaker) Rejected() uint64 {
return cb.rejected.Load()
}
func (cb *fastBreaker) RollingCounters() (uint64, uint64) {
var executions uint64 = 0
var failures uint64 = 0
cb.ring.Do(func(value any) {
counter := value.(*counters)
executions += counter.executions.Load()
failures += counter.failures.Load()
})
return executions, failures
}
func (cb *fastBreaker) buildFeedbackFunc(state State) func(bool) {
return func(success bool) {
cb.handleFeedback(state, success)
}
}
func (cb *fastBreaker) handleFeedback(executionState State, success bool) {
state := cb.state.Load()
// Ignore feedback of executions allowed then the circuit was in a different state.
if executionState != state {
return
}
switch state {
case StateClosed:
cb.incExecutions()
if !success {
cb.incFailures()
executions, failures := cb.RollingCounters()
// check if the circuit breaker should trip
if cb.configuration.ShouldTrip(executions, failures) {
cb.tripFrom(StateClosed)
}
}
case StateHalfOpen:
if success {
cb.reset()
} else {
cb.tripFrom(StateHalfOpen)
}
}
}
func (cb *fastBreaker) tripFrom(state State) bool {
if cb.state.CompareAndSwap(state, StateOpen) {
if cb.breakTimer == nil {
// Create a timer that will transition the circuit from StateOpen to StateHalfOpen.
cb.breakTimer = time.AfterFunc(
cb.configuration.DurationOfBreak,
func() {
if cb.state.CompareAndSwap(StateOpen, StateHalfOpen) {
cb.halfOpenAllowed.Store(true)
}
cb.breakTimer = nil
},
)
}
return true
}
return false
}
// reset resets the circuit breaker.
func (cb *fastBreaker) reset() {
// stop the breakTimer.
if cb.breakTimer != nil {
cb.breakTimer.Stop()
}
// reset the rolling counters.
if cb.state.Swap(StateClosed) != StateClosed {
for i := 0; i < cb.ring.Len(); i++ {
cb.ring.Value.(*counters).reset()
cb.ring = cb.ring.Next()
}
}
}
// incExecutions increments the number of executions.
func (cb *fastBreaker) incExecutions() {
cb.totalCounters.executions.Add(1)
cb.ring.Value.(*counters).executions.Add(1)
}
// incFailures increments the number of failures.
func (cb *fastBreaker) incFailures() {
cb.totalCounters.failures.Add(1)
cb.ring.Value.(*counters).failures.Add(1)
}
// advanceWindow moves the ring to the next value.
func (cb *fastBreaker) advanceWindow() {
for range cb.advanceTicker.C {
next := cb.ring.Next()
// reset the next counters.
next.Value.(*counters).reset()
// advance the ring.
cb.ring = next
}
}