-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcircuitbreaker.go
110 lines (89 loc) · 2.77 KB
/
circuitbreaker.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
/*
* enqueuestomp
*
* MIT License
*
* Copyright (c) 2020 Globo.com
*/
package enqueuestomp
import (
"fmt"
"github.com/afex/hystrix-go/hystrix"
)
const (
defaultTimeout = 10000
defaultMaxConcurrentRequests = 10000
defaultRequestVolumeThreshold = 100
defaultSleepWindow = 500
defaultErrorPercentThreshold = 5
)
type CircuitBreakerConfig struct {
// how long to wait for command to complete, in milliseconds
// Default is 10000
Timeout int
// how many commands of the same type can run at the same time
// Default is 10000
MaxConcurrentRequests int
// the minimum number of requests needed before a circuit can be tripped due to health
// Default is 100
RequestVolumeThreshold int
// how long, in milliseconds, to wait after a circuit opens before testing for recovery
// Default is 500
SleepWindow int
// causes circuits to open once the rolling measure of errors exceeds this percent of requests
// Default is 5
ErrorPercentThreshold int
}
func (cb *CircuitBreakerConfig) init() {
if cb.Timeout == 0 {
cb.Timeout = defaultTimeout
}
if cb.MaxConcurrentRequests == 0 {
cb.MaxConcurrentRequests = defaultMaxConcurrentRequests
}
if cb.RequestVolumeThreshold == 0 {
cb.RequestVolumeThreshold = defaultRequestVolumeThreshold
}
if cb.SleepWindow == 0 {
cb.SleepWindow = defaultSleepWindow
}
if cb.ErrorPercentThreshold == 0 {
cb.ErrorPercentThreshold = defaultErrorPercentThreshold
}
}
func (emq *EnqueueStompImpl) ConfigureCircuitBreaker(name string, cb CircuitBreakerConfig) {
emq.mu.Lock()
defer emq.mu.Unlock()
cb.init()
circuitName := emq.makeCircuitName(name)
hystrix.ConfigureCommand(circuitName, hystrix.CommandConfig{
Timeout: cb.Timeout,
MaxConcurrentRequests: cb.MaxConcurrentRequests,
RequestVolumeThreshold: cb.RequestVolumeThreshold,
SleepWindow: cb.SleepWindow,
ErrorPercentThreshold: cb.ErrorPercentThreshold,
})
emq.circuitNames[circuitName] = circuitName
}
func (emq *EnqueueStompImpl) sendWithCircuitBreaker(identifier string, destination string, body []byte, sc SendConfig) error {
circuitName := emq.makeCircuitName(sc.CircuitName)
err := hystrix.Do(circuitName, func() error {
emq.debugLogger(
"[enqueuestomp][%s] Send message with circuitBreaker: `%s` and destination: `%s` and body: `%s`",
identifier, sc.CircuitName, destination, body,
)
return emq.conn.Send(destination, sc.ContentType, body, sc.Options...)
}, nil)
return err
}
func (emq *EnqueueStompImpl) makeCircuitName(name string) string {
return fmt.Sprintf("%s::%s", name, emq.id)
}
func (emq *EnqueueStompImpl) hasCircuitBreaker(sc SendConfig) bool {
if sc.CircuitName == "" {
return false
}
circuitName := emq.makeCircuitName(sc.CircuitName)
_, found := emq.circuitNames[circuitName]
return found
}