-
Notifications
You must be signed in to change notification settings - Fork 0
/
gobreak.go
107 lines (93 loc) · 3 KB
/
gobreak.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
package httpclient
import (
"sync"
"time"
"github.com/mitchellh/mapstructure"
"github.com/sony/gobreaker"
)
var (
circuitBreaker = map[string]*gobreaker.CircuitBreaker{}
circuitBreakerMutex = &sync.RWMutex{}
)
type settings struct {
Name string `mapstructure:"name"`
MaxRequests uint32 `mapstructure:"maxRequests"`
Interval uint32 `mapstructure:"interval"`
Timeout uint32 `mapstructure:"timeout"`
FailureRatio float64 `mapstructure:"failureRatio"`
ConsecutiveFailures uint32 `mapstructure:"consecutiveFailures"`
}
// 10s 连续失败100次熔断,1s后继续放量,如果失败立马进入开启状态
var defaultSetting = &settings{MaxRequests: 100, Interval: 10, Timeout: 1, FailureRatio: 1, ConsecutiveFailures: 100}
// InitCircuitBreaker 初始化熔断配置
// {
// "default":{
// "maxRequests":1,
// "interval":1,
// "timeout":10,
// "failureRatio":1,
// "consecutiveFailures":100
// }
//}
func InitCircuitBreaker(c []map[string]interface{}) {
// 从config Remote中获取熔断配置
d := make(map[string]*gobreaker.CircuitBreaker)
if c == nil {
return
}
for k := range c {
s := &settings{}
if err := mapstructure.Decode(k, s); err == nil {
d[s.Name] = newCircuitBreaker(s.Name, s)
}
}
circuitBreaker = d
httpGobreakGauge.Reset()
}
// GetCircuitBreaker returns a CircuitBreaker by name
func GetCircuitBreaker(name string) *gobreaker.CircuitBreaker {
circuitBreakerMutex.RLock()
cb, ok := circuitBreaker[name]
if !ok {
circuitBreakerMutex.RUnlock()
circuitBreakerMutex.Lock()
defer circuitBreakerMutex.Unlock()
// because we released the rlock before we obtained the exclusive lock,
// we need to double check that some other thread didn't beat us to
// creation.
if cb, ok := circuitBreaker[name]; ok {
return cb
}
cb = newCircuitBreaker(name, defaultSetting)
circuitBreaker[name] = cb
} else {
circuitBreakerMutex.RUnlock()
}
return cb
}
func newCircuitBreaker(name string, s *settings) *gobreaker.CircuitBreaker {
return gobreaker.NewCircuitBreaker(gobreaker.Settings{
Name: name,
MaxRequests: s.MaxRequests,
Interval: time.Duration(s.Interval) * time.Second,
Timeout: time.Duration(s.Timeout) * time.Second,
OnStateChange: onStateChange,
ReadyToTrip: readyToTrip(s.FailureRatio, s.ConsecutiveFailures),
})
}
func readyToTrip(failureRatio float64, consecutiveFailures uint32) func(counts gobreaker.Counts) bool {
return func(counts gobreaker.Counts) bool {
ratio := float64(counts.TotalFailures) / float64(counts.Requests)
return counts.ConsecutiveFailures >= consecutiveFailures && ratio >= failureRatio
}
}
func onStateChange(name string, from gobreaker.State, to gobreaker.State) {
switch to {
case gobreaker.StateOpen:
httpGobreakGauge.WithLabelValues(name).Set(1)
case gobreaker.StateClosed:
httpGobreakGauge.WithLabelValues(name).Set(0)
case gobreaker.StateHalfOpen:
httpGobreakGauge.WithLabelValues(name).Set(2)
}
}