forked from mantzas/patron
/
breaker.go
197 lines (164 loc) · 3.91 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
// Package circuitbreaker provides a circuit breaker pattern implementation.
package circuitbreaker
import (
"errors"
"math"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
)
// OpenError defines a open error.
type OpenError struct{}
func (oe OpenError) Error() string {
return "circuit is open"
}
type status int
const (
close status = iota // nolint:predeclared
open
)
var (
tsFuture = int64(math.MaxInt64)
errOpen = new(OpenError)
breakerCounter *prometheus.CounterVec
statusMap = map[status]string{close: "close", open: "open"}
)
func init() {
breakerCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "reliability",
Subsystem: "circuit_breaker",
Name: "errors",
Help: "Circuit breaker status, classified by name and status",
},
[]string{"name", "status"},
)
prometheus.MustRegister(breakerCounter)
}
func breakerCounterInc(name string, st status) {
breakerCounter.WithLabelValues(name, statusMap[st]).Inc()
}
// Setting definition.
type Setting struct {
// The threshold for the circuit to open.
FailureThreshold uint
// The timeout after which we set the state to half-open and allow a retry.
RetryTimeout time.Duration
// The threshold of the retry successes which returns the state to open.
RetrySuccessThreshold uint
// The threshold of how many retry executions are allowed when the status is half-open.
MaxRetryExecutionThreshold uint
}
// Action function to execute in circuit breaker.
type Action func() (interface{}, error)
// CircuitBreaker implementation.
type CircuitBreaker struct {
name string
set Setting
sync.RWMutex
status status
executions uint
failures uint
retries uint
nextRetry int64
}
// New constructor.
func New(name string, s Setting) (*CircuitBreaker, error) {
if name == "" {
return nil, errors.New("name is required")
}
if s.MaxRetryExecutionThreshold < s.RetrySuccessThreshold {
return nil, errors.New("max retry has to be greater than the retry threshold")
}
return &CircuitBreaker{
name: name,
set: s,
status: close,
executions: 0,
failures: 0,
retries: 0,
nextRetry: tsFuture,
}, nil
}
func (cb *CircuitBreaker) isHalfOpen() bool {
cb.RLock()
defer cb.RUnlock()
if cb.status == open && cb.nextRetry <= time.Now().UnixNano() {
return true
}
return false
}
func (cb *CircuitBreaker) isOpen() bool {
cb.RLock()
defer cb.RUnlock()
if cb.status == open && cb.nextRetry > time.Now().UnixNano() {
return true
}
return false
}
func (cb *CircuitBreaker) isClose() bool {
cb.RLock()
defer cb.RUnlock()
return cb.status == close
}
// Execute the function enclosed.
func (cb *CircuitBreaker) Execute(act Action) (interface{}, error) {
if cb.isOpen() {
return nil, errOpen
}
resp, err := act()
if err != nil {
cb.incFailure()
return nil, err
}
cb.incSuccess()
return resp, err
}
func (cb *CircuitBreaker) incFailure() {
// allow closed and half open to transition to open
if cb.isOpen() {
return
}
cb.Lock()
defer cb.Unlock()
cb.failures++
if cb.status == close && cb.failures >= cb.set.FailureThreshold {
cb.transitionToOpen()
return
}
cb.executions++
if cb.executions < cb.set.MaxRetryExecutionThreshold {
return
}
cb.transitionToOpen()
}
func (cb *CircuitBreaker) incSuccess() {
// allow only half open in order to transition to closed
if !cb.isHalfOpen() {
return
}
cb.Lock()
defer cb.Unlock()
cb.retries++
cb.executions++
if cb.retries < cb.set.RetrySuccessThreshold {
return
}
cb.transitionToClose()
}
func (cb *CircuitBreaker) transitionToOpen() {
cb.status = open
cb.failures = 0
cb.executions = 0
cb.retries = 0
cb.nextRetry = time.Now().Add(cb.set.RetryTimeout).UnixNano()
breakerCounterInc(cb.name, cb.status)
}
func (cb *CircuitBreaker) transitionToClose() {
cb.status = close
cb.failures = 0
cb.executions = 0
cb.retries = 0
cb.nextRetry = tsFuture
breakerCounterInc(cb.name, cb.status)
}