forked from go-chassis/go-chassis
/
transfer.go
127 lines (117 loc) · 4.47 KB
/
transfer.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
package archaius
import (
"github.com/go-chassis/go-chassis/control"
"github.com/go-chassis/go-chassis/core/common"
"github.com/go-chassis/go-chassis/core/config"
"github.com/go-chassis/go-chassis/core/config/model"
"github.com/go-chassis/go-chassis/core/lager"
"github.com/go-chassis/go-chassis/core/loadbalancer"
"github.com/go-chassis/go-chassis/pkg/backoff"
"github.com/go-chassis/go-chassis/third_party/forked/afex/hystrix-go/hystrix"
"strings"
)
//SaveToLBCache save configs
func SaveToLBCache(raw *model.LoadBalancing) {
lager.Logger.Debug("Loading lb config from archaius into cache")
saveDefaultLB(raw)
for k, v := range raw.AnyService {
saveEachLB(k, v)
}
}
func saveDefaultLB(raw *model.LoadBalancing) {
c := control.LoadBalancingConfig{
Strategy: raw.Strategy["name"],
RetryEnabled: raw.RetryEnabled,
RetryOnSame: raw.RetryOnSame,
RetryOnNext: raw.RetryOnNext,
BackOffKind: raw.Backoff.Kind,
BackOffMin: raw.Backoff.MinMs,
BackOffMax: raw.Backoff.MaxMs,
SessionTimeoutInSeconds: raw.SessionStickinessRule.SessionTimeoutInSeconds,
SuccessiveFailedTimes: raw.SessionStickinessRule.SuccessiveFailedTimes,
}
setDefaultLBValue(&c)
LBConfigCache.Set("", c, 0)
}
func saveEachLB(k string, raw model.LoadBalancingSpec) {
c := control.LoadBalancingConfig{
Strategy: raw.Strategy["name"],
RetryEnabled: raw.RetryEnabled,
RetryOnSame: raw.RetryOnSame,
RetryOnNext: raw.RetryOnNext,
BackOffKind: raw.Backoff.Kind,
BackOffMin: raw.Backoff.MinMs,
BackOffMax: raw.Backoff.MaxMs,
SessionTimeoutInSeconds: raw.SessionStickinessRule.SessionTimeoutInSeconds,
SuccessiveFailedTimes: raw.SessionStickinessRule.SuccessiveFailedTimes,
}
setDefaultLBValue(&c)
LBConfigCache.Set(k, c, 0)
}
func setDefaultLBValue(c *control.LoadBalancingConfig) {
if c.Strategy == "" {
c.Strategy = loadbalancer.StrategyRoundRobin
}
if c.BackOffKind == "" {
c.BackOffKind = backoff.DefaultBackOffKind
}
}
//SaveToCBCache save configs
func SaveToCBCache(raw *model.HystrixConfig) {
lager.Logger.Debug("Loading cb config from archaius into cache")
saveEachCB("", common.Consumer)
saveEachCB("", common.Provider)
// even though the input is duplicated in below loops but service name list is not certain unless we loop all namespace
for k := range raw.IsolationProperties.Consumer.AnyService {
saveEachCB(k, common.Consumer)
}
for k := range raw.IsolationProperties.Provider.AnyService {
saveEachCB(k, common.Provider)
}
for k := range raw.CircuitBreakerProperties.Consumer.AnyService {
saveEachCB(k, common.Consumer)
}
for k := range raw.CircuitBreakerProperties.Provider.AnyService {
saveEachCB(k, common.Provider)
}
for k := range raw.FallbackPolicyProperties.Consumer.AnyService {
saveEachCB(k, common.Consumer)
}
for k := range raw.FallbackPolicyProperties.Provider.AnyService {
saveEachCB(k, common.Provider)
}
for k := range raw.FallbackProperties.Consumer.AnyService {
saveEachCB(k, common.Consumer)
}
for k := range raw.FallbackProperties.Provider.AnyService {
saveEachCB(k, common.Provider)
}
}
func saveEachCB(serviceName, serviceType string) {
command := serviceType
if serviceName != "" {
command = strings.Join([]string{serviceType, serviceName}, ".")
}
c := hystrix.CommandConfig{
ForceFallback: config.GetForceFallback(serviceName, serviceType),
TimeoutEnabled: config.GetTimeoutEnabled(serviceName, serviceType),
Timeout: config.GetTimeout(command, serviceType),
MaxConcurrentRequests: config.GetMaxConcurrentRequests(command, serviceType),
ErrorPercentThreshold: config.GetErrorPercentThreshold(command, serviceType),
RequestVolumeThreshold: config.GetRequestVolumeThreshold(command, serviceType),
SleepWindow: config.GetSleepWindow(command, serviceType),
ForceClose: config.GetForceClose(serviceName, serviceType),
ForceOpen: config.GetForceOpen(serviceName, serviceType),
CircuitBreakerEnabled: config.GetCircuitBreakerEnabled(command, serviceType),
}
lager.Logger.Infof("save circuit breaker config [%#v] for [%s] ", c, serviceName)
CBConfigCache.Set(GetCBCacheKey(serviceName, serviceType), c, 0)
}
//GetCBCacheKey generate cache key
func GetCBCacheKey(serviceName, serviceType string) string {
key := serviceType
if serviceName != "" {
key = serviceType + ":" + serviceName
}
return key
}