-
Notifications
You must be signed in to change notification settings - Fork 55
/
util.go
223 lines (207 loc) · 8.13 KB
/
util.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
* Tencent is pleased to support the open source community by making polaris-go available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package data
import (
"fmt"
"time"
"github.com/polarismesh/polaris-go/pkg/clock"
"github.com/polarismesh/polaris-go/pkg/config"
"github.com/polarismesh/polaris-go/pkg/log"
"github.com/polarismesh/polaris-go/pkg/model"
"github.com/polarismesh/polaris-go/pkg/plugin"
"github.com/polarismesh/polaris-go/pkg/plugin/circuitbreaker"
"github.com/polarismesh/polaris-go/pkg/plugin/common"
"github.com/polarismesh/polaris-go/pkg/plugin/configconnector"
"github.com/polarismesh/polaris-go/pkg/plugin/configfilter"
"github.com/polarismesh/polaris-go/pkg/plugin/healthcheck"
"github.com/polarismesh/polaris-go/pkg/plugin/loadbalancer"
"github.com/polarismesh/polaris-go/pkg/plugin/localregistry"
statreporter "github.com/polarismesh/polaris-go/pkg/plugin/metrics"
"github.com/polarismesh/polaris-go/pkg/plugin/serverconnector"
"github.com/polarismesh/polaris-go/pkg/plugin/servicerouter"
)
// GetServerConnector 加载连接器插件
func GetServerConnector(
cfg config.Configuration, supplier plugin.Supplier) (serverconnector.ServerConnector, error) {
// 加载服务端连接器
protocol := cfg.GetGlobal().GetServerConnector().GetProtocol()
targetPlugin, err := supplier.GetPlugin(common.TypeServerConnector, protocol)
if err != nil {
return nil, err
}
return targetPlugin.(serverconnector.ServerConnector), nil
}
// GetConfigConnector 加载配置中心连接器插件
func GetConfigConnector(
cfg config.Configuration, supplier plugin.Supplier) (configconnector.ConfigConnector, error) {
// 加载配置中心连接器
protocol := cfg.GetConfigFile().GetConfigConnectorConfig().GetProtocol()
targetPlugin, err := supplier.GetPlugin(common.TypeConfigConnector, protocol)
if err != nil {
return nil, err
}
return targetPlugin.(configconnector.ConfigConnector), nil
}
// GetConfigFilterChain 加载配置过滤链
func GetConfigFilterChain(cfg config.Configuration, supplier plugin.Supplier) (configfilter.Chain, error) {
chain := make(configfilter.Chain, 0)
chainNames := cfg.GetConfigFile().GetConfigFilterConfig().GetChain()
for _, name := range chainNames {
targetPlugin, err := supplier.GetPlugin(common.TypeConfigFilter, name)
if err != nil {
return nil, err
}
chain = append(chain, targetPlugin.(configfilter.ConfigFilter))
}
return chain, nil
}
// GetRegistry 加载本地缓存插件
func GetRegistry(cfg config.Configuration, supplier plugin.Supplier) (localregistry.LocalRegistry, error) {
localCacheType := cfg.GetConsumer().GetLocalCache().GetType()
targetPlugin, err := supplier.GetPlugin(common.TypeLocalRegistry, localCacheType)
if err != nil {
return nil, err
}
return targetPlugin.(localregistry.LocalRegistry), nil
}
// GetCircuitBreakers 获取熔断插件链
func GetCircuitBreakers(
cfg config.Configuration, supplier plugin.Supplier) ([]circuitbreaker.InstanceCircuitBreaker, error) {
cbChain := cfg.GetConsumer().GetCircuitBreaker().GetChain()
when := cfg.GetConsumer().GetHealthCheck().GetWhen()
var hasHealthCheckBreaker bool
cbreakers := make([]circuitbreaker.InstanceCircuitBreaker, 0, len(cbChain))
if len(cbChain) > 0 {
for _, cbName := range cbChain {
if cbName == config.DefaultCircuitBreakerErrCheck {
hasHealthCheckBreaker = true
}
targetPlugin, err := supplier.GetPlugin(common.TypeCircuitBreaker, cbName)
if err != nil {
return nil, err
}
cbreakers = append(cbreakers, targetPlugin.(circuitbreaker.InstanceCircuitBreaker))
}
}
if when == config.HealthCheckAlways && !hasHealthCheckBreaker {
targetPlugin, err := supplier.GetPlugin(common.TypeCircuitBreaker, config.DefaultCircuitBreakerErrCheck)
if err != nil {
return nil, err
}
cbreakers = append(cbreakers, targetPlugin.(circuitbreaker.InstanceCircuitBreaker))
}
return cbreakers, nil
}
// GetHealthCheckers 获取健康探测插件列表
func GetHealthCheckers(cfg config.Configuration, supplier plugin.Supplier) ([]healthcheck.HealthChecker, error) {
names := cfg.GetConsumer().GetHealthCheck().GetChain()
healthCheckers := make([]healthcheck.HealthChecker, 0, len(names))
if len(names) > 0 {
for _, name := range names {
targetPlugin, err := supplier.GetPlugin(common.TypeHealthCheck, name)
if err != nil {
return nil, err
}
healthCheckers = append(healthCheckers, targetPlugin.(healthcheck.HealthChecker))
}
}
return healthCheckers, nil
}
// GetServiceRouterChain 获取服务路由插件链
func GetServiceRouterChain(cfg config.Configuration, supplier plugin.Supplier) (*servicerouter.RouterChain, error) {
filterChain := cfg.GetConsumer().GetServiceRouter().GetChain()
filters := &servicerouter.RouterChain{
Chain: make([]servicerouter.ServiceRouter, 0, len(filterChain)),
}
if len(filterChain) > 0 {
for _, filter := range filterChain {
targetPlugin, err := supplier.GetPlugin(common.TypeServiceRouter, filter)
if err != nil {
return nil, err
}
filters.Chain = append(filters.Chain, targetPlugin.(servicerouter.ServiceRouter))
}
}
return filters, nil
}
// GetStatReporterChain 获取统计上报插件
func GetStatReporterChain(cfg config.Configuration, supplier plugin.Supplier) ([]statreporter.StatReporter, error) {
if !cfg.GetGlobal().GetStatReporter().IsEnable() {
return make([]statreporter.StatReporter, 0), nil
}
reporterNames := cfg.GetGlobal().GetStatReporter().GetChain()
reporterChain := make([]statreporter.StatReporter, 0, len(reporterNames))
if len(reporterNames) > 0 {
for _, reporter := range reporterNames {
targetPlugin, err := supplier.GetPlugin(common.TypeStatReporter, reporter)
if err != nil {
return nil, err
}
reporterChain = append(reporterChain, targetPlugin.(statreporter.StatReporter))
}
}
return reporterChain, nil
}
// GetLoadBalancer 获取负载均衡插件
func GetLoadBalancer(cfg config.Configuration, supplier plugin.Supplier) (loadbalancer.LoadBalancer, error) {
lbType := cfg.GetConsumer().GetLoadbalancer().GetType()
targetPlugin, err := supplier.GetPlugin(common.TypeLoadBalancer, lbType)
if err != nil {
return nil, err
}
return targetPlugin.(loadbalancer.LoadBalancer), nil
}
// GetLoadBalancerByLbType 获取负载均衡插件
func GetLoadBalancerByLbType(lbType string, supplier plugin.Supplier) (loadbalancer.LoadBalancer, error) {
targetPlugin, err := supplier.GetPlugin(common.TypeLoadBalancer, lbType)
if err != nil {
return nil, err
}
return targetPlugin.(loadbalancer.LoadBalancer), nil
}
// SingleInvoke 同步调用的通用方法定义
type SingleInvoke func(request interface{}) (interface{}, error)
// RetrySyncCall 通用的带重试的同步调用逻辑
func RetrySyncCall(name string, svcKey *model.ServiceKey,
request interface{}, call SingleInvoke, param *model.ControlParam) (interface{}, model.SDKError) {
retryTimes := -1
var resp interface{}
var err error
retryInterval := param.RetryInterval
for retryTimes < param.MaxRetry {
startTime := clock.GetClock().Now()
resp, err = call(request)
consumeTime := clock.GetClock().Now().Sub(startTime)
if err == nil {
return resp, nil
}
sdkErr, ok := err.(model.SDKError)
if !ok || !sdkErr.ErrorCode().Retryable() {
return resp, sdkErr
}
retryTimes++
if retryTimes >= param.MaxRetry {
break
}
time.Sleep(retryInterval)
log.GetBaseLogger().Warnf("retry %s for timeout, consume time %v,"+
" Namespace: %s, Service: %s, retry times: %d",
name, consumeTime, svcKey.Namespace, svcKey.Service, retryTimes)
}
return resp, model.NewSDKError(model.ErrCodeAPITimeoutError, err,
fmt.Sprintf("fail to do %s after retry %v times", name, retryTimes))
}