-
Notifications
You must be signed in to change notification settings - Fork 2
/
trip.go
50 lines (43 loc) · 1.13 KB
/
trip.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
package service_breaker
//when error occur, determine whether the breaker should be opened.
type TripStrategyFunc func(Metrics) bool
//according to consecutive fail
func ConsecutiveFailTripFunc(threshold uint64) TripStrategyFunc {
return func(m Metrics) bool {
return m.ConsecutiveFail >= threshold
}
}
//according to fail
func FailTripFunc(threshold uint64) TripStrategyFunc {
return func(m Metrics) bool {
return m.CountFail >= threshold
}
}
//according to fail rate
func FailRateTripFunc(rate float64, minCalls uint64) TripStrategyFunc {
return func(m Metrics) bool {
var currRate float64
if m.CountAll != 0 {
currRate = float64(m.CountFail) / float64(m.CountAll)
}
return m.CountAll >= minCalls && currRate >= rate
}
}
const (
ConsecutiveFailTrip = iota + 1
FailTrip
FailRateTrip
)
//choose trip
func ChooseTrip(op *TripStrategyOption) TripStrategyFunc {
switch op.Strategy {
case ConsecutiveFailTrip:
return ConsecutiveFailTripFunc(op.ConsecutiveFailThreshold)
case FailTrip:
return FailTripFunc(op.FailThreshold)
case FailRateTrip:
fallthrough
default:
return FailRateTripFunc(op.FailRate, op.MinCall)
}
}