Repository navigation
Expand file tree
/
Copy pathcounter.go
More file actions
227 lines (190 loc) · 4.39 KB
/
Copy pathcounter.go
File metadata and controls
227 lines (190 loc) · 4.39 KB
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
package gobreaker
// Counts holds the numbers of requests and their successes/failures/exclusions.
// CircuitBreaker 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.
type Counts struct {
Requests uint32
TotalSuccesses uint32
TotalFailures uint32
TotalExclusions uint32
ConsecutiveSuccesses uint32
ConsecutiveFailures uint32
}
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) onExclusion() {
c.TotalExclusions++
}
func (c *Counts) validRequests() uint32 {
if c.Requests < c.TotalExclusions {
return 0
}
return c.Requests - c.TotalExclusions
}
func (c *Counts) clear() {
c.Requests = 0
c.TotalSuccesses = 0
c.TotalFailures = 0
c.TotalExclusions = 0
c.ConsecutiveSuccesses = 0
c.ConsecutiveFailures = 0
}
type rollingCounts struct {
Counts
age uint64
buckets []Counts
}
func newRollingCounts(numBuckets int64) *rollingCounts {
if numBuckets < 0 {
numBuckets = 0
}
return &rollingCounts{
buckets: make([]Counts, numBuckets),
}
}
func (rc *rollingCounts) index(age uint64) uint64 {
if len(rc.buckets) == 0 {
return 0
}
return age % uint64(len(rc.buckets))
}
func (rc *rollingCounts) current() uint64 {
return rc.index(rc.age)
}
func (rc *rollingCounts) onRequest() {
rc.Counts.onRequest()
rc.buckets[rc.current()].onRequest()
}
func (rc *rollingCounts) onSuccess(age uint64) {
if age > rc.age {
return
}
if rc.age-age < uint64(len(rc.buckets)) {
rc.Counts.onSuccess()
rc.buckets[rc.index(age)].onSuccess()
}
}
func (rc *rollingCounts) onFailure(age uint64) {
if age > rc.age {
return
}
if rc.age-age < uint64(len(rc.buckets)) {
rc.Counts.onFailure()
rc.buckets[rc.index(age)].onFailure()
}
}
func (rc *rollingCounts) onExclusion(age uint64) {
if age > rc.age {
return
}
if rc.age-age < uint64(len(rc.buckets)) {
rc.Counts.onExclusion()
rc.buckets[rc.index(age)].onExclusion()
}
}
func (rc *rollingCounts) clear() {
rc.Counts.clear()
rc.age = 0
for i := range rc.buckets {
rc.buckets[i].clear()
}
}
func (rc *rollingCounts) roll() {
rc.age++
if len(rc.buckets) == 0 {
return
}
current := rc.current()
rc.subtract(current)
rc.buckets[current].clear()
}
func (rc *rollingCounts) subtract(oldest uint64) {
length := uint64(len(rc.buckets))
if length == 0 {
return
}
oldest = oldest % length
bucket := rc.buckets[oldest]
totalSuccesses := bucket.ConsecutiveSuccesses
for i := uint64(1); i < length; i++ {
idx := (oldest + i) % length
totalSuccesses += rc.buckets[idx].TotalSuccesses
}
if rc.ConsecutiveSuccesses == totalSuccesses {
if rc.ConsecutiveSuccesses > bucket.ConsecutiveSuccesses {
rc.ConsecutiveSuccesses -= bucket.ConsecutiveSuccesses
} else {
rc.ConsecutiveSuccesses = 0
}
}
totalFailures := bucket.ConsecutiveFailures
for i := uint64(1); i < length; i++ {
idx := (oldest + i) % length
totalFailures += rc.buckets[idx].TotalFailures
}
if rc.ConsecutiveFailures == totalFailures {
if rc.ConsecutiveFailures > bucket.ConsecutiveFailures {
rc.ConsecutiveFailures -= bucket.ConsecutiveFailures
} else {
rc.ConsecutiveFailures = 0
}
}
if rc.Requests > bucket.Requests {
rc.Requests -= bucket.Requests
} else {
rc.Requests = 0
}
if rc.TotalSuccesses > bucket.TotalSuccesses {
rc.TotalSuccesses -= bucket.TotalSuccesses
} else {
rc.TotalSuccesses = 0
}
if rc.TotalFailures > bucket.TotalFailures {
rc.TotalFailures -= bucket.TotalFailures
} else {
rc.TotalFailures = 0
}
if rc.TotalExclusions > bucket.TotalExclusions {
rc.TotalExclusions -= bucket.TotalExclusions
} else {
rc.TotalExclusions = 0
}
}
func (rc *rollingCounts) grow(age uint64) {
if age <= rc.age {
return
}
diff := age - rc.age
if diff >= uint64(len(rc.buckets)) {
rc.clear()
rc.age = age
} else {
for range diff {
rc.roll()
}
}
}
func (rc *rollingCounts) bucketAt(index int) Counts {
bucketLen := len(rc.buckets)
if bucketLen == 0 {
return Counts{}
}
idx := (index%bucketLen + bucketLen) % bucketLen
if idx < 0 {
return Counts{}
}
bucketIndex := (rc.current() + uint64(idx)) % uint64(bucketLen)
return rc.buckets[bucketIndex]
}