-
Notifications
You must be signed in to change notification settings - Fork 8
/
stat.go
150 lines (130 loc) · 2.64 KB
/
stat.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
package breaker
import (
"sync"
)
// Summary represents statistics of Breaker.
type Summary interface {
// Total is total attempts
Total() uint32
// Success is success count
Success() uint32
// Failure is failure count
Failure() uint32
// Intercept is failure count with ErrCircuitOpen
Intercept() uint32
// Reject is failure count with ErrMaxConcurrency
Reject() uint32
// Timeout is failure count with ErrTimeout
Timeout() uint32
// FallbackSuccess is success count by fallback
FallbackSuccess() uint32
// FallbackFailure is failure count by fallback
FallbackFailure() uint32
}
type Counter interface {
Summary
// Failure is consecutive failure count
ConsecutiveFailure() uint32
}
type summary struct {
total uint32
success uint32
failure uint32
intercept uint32
reject uint32
timeout uint32
fallbackSuccess uint32
fallbackFailure uint32
}
func (s summary) Total() uint32 {
return s.total
}
func (s summary) Success() uint32 {
return s.success
}
func (s summary) Failure() uint32 {
return s.failure
}
func (s summary) Intercept() uint32 {
return s.intercept
}
func (s summary) Reject() uint32 {
return s.reject
}
func (s summary) Timeout() uint32 {
return s.timeout
}
func (s summary) FallbackSuccess() uint32 {
return s.fallbackSuccess
}
func (s summary) FallbackFailure() uint32 {
return s.fallbackFailure
}
func (s summary) reset() {
s.total = 0
s.success = 0
s.failure = 0
s.reject = 0
s.timeout = 0
s.fallbackSuccess = 0
s.fallbackFailure = 0
}
type counter struct {
summary
consecutiveFailure uint32
}
func (c counter) ConsecutiveFailure() uint32 {
return c.consecutiveFailure
}
func (c counter) reset() {
c.summary.reset()
c.consecutiveFailure = 0
}
type statistics struct {
sync.Mutex
summary
counter
}
func (s *statistics) reset(summary, counter bool) {
s.Lock()
if summary {
s.summary.reset()
}
if counter {
s.counter.reset()
}
s.Unlock()
}
func (s *statistics) update(final, middle error, fallback bool) {
s.Lock()
s.summary.total++
s.counter.total++
if final == nil {
s.summary.success++
s.counter.success++
s.consecutiveFailure = 0
if fallback {
s.summary.fallbackSuccess++
s.counter.fallbackSuccess++
}
} else {
s.summary.failure++
s.counter.failure++
s.consecutiveFailure++
if fallback {
s.summary.fallbackFailure++
s.counter.fallbackFailure++
}
if final == ErrCircuitOpen {
s.summary.intercept++
s.counter.intercept++
} else if middle == ErrMaxConcurrency {
s.summary.reject++
s.counter.reject++
} else if middle == ErrTimeout {
s.summary.timeout++
s.counter.timeout++
}
}
s.Unlock()
}