Skip to content

Commit fb648bd

Browse files
author
myxy99
committed
优化Hystrix
1 parent 80d55b0 commit fb648bd

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

xgrpc/client/interceptor.go

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -179,20 +179,21 @@ func XAidUnaryClientInterceptor() grpc.UnaryClientInterceptor {
179179

180180
func HystrixUnaryClientIntercept(timeout, maxConcurrentRequests, requestVolumeThreshold, errorPercentThreshold, sleepWindow int,
181181
fallback func(context.Context, error) error) grpc.UnaryClientInterceptor {
182+
// Timeout: 1000 // 超时时间设置 单位毫秒
183+
// MaxConcurrentRequests: 1, // 最大请求数
184+
// RequestVolumeThreshold: 2, // 默认20,如果错误超过该次数,才开始计算错误百分比
185+
// ErrorPercentThreshold: 50, // 错误百分比,默认50,即50%
186+
// SleepWindow: 5000, // 过多长时间,熔断器再次检测是否开启。单位毫秒
187+
config := hystrix.CommandConfig{
188+
Timeout: timeout,
189+
MaxConcurrentRequests: maxConcurrentRequests,
190+
RequestVolumeThreshold: requestVolumeThreshold,
191+
ErrorPercentThreshold: errorPercentThreshold,
192+
SleepWindow: sleepWindow,
193+
}
182194
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
183195
name := "GUC" + method
184-
// Timeout: 1000 // 超时时间设置 单位毫秒
185-
// MaxConcurrentRequests: 1, // 最大请求数
186-
// RequestVolumeThreshold: 2, // 默认20,如果错误超过该次数,才开始计算错误百分比
187-
// ErrorPercentThreshold: 50, // 错误百分比,默认50,即50%
188-
// SleepWindow: 5000, // 过多长时间,熔断器再次检测是否开启。单位毫秒
189-
hystrix.ConfigureCommand(name, hystrix.CommandConfig{
190-
Timeout: timeout,
191-
MaxConcurrentRequests: maxConcurrentRequests,
192-
RequestVolumeThreshold: requestVolumeThreshold,
193-
ErrorPercentThreshold: errorPercentThreshold,
194-
SleepWindow: sleepWindow,
195-
})
196+
hystrix.ConfigureCommand(name, config)
196197
return hystrix.DoC(ctx, name, func(ctx context.Context) error {
197198
return invoker(ctx, method, req, reply, cc, opts...)
198199
}, fallback)
@@ -201,20 +202,21 @@ func HystrixUnaryClientIntercept(timeout, maxConcurrentRequests, requestVolumeTh
201202

202203
func HystrixStreamClientInterceptor(timeout, maxConcurrentRequests, requestVolumeThreshold, errorPercentThreshold, sleepWindow int,
203204
fallback func(context.Context, error) error) grpc.StreamClientInterceptor {
205+
// Timeout: 1000 // 超时时间设置 单位毫秒
206+
// MaxConcurrentRequests: 1, // 最大请求数
207+
// RequestVolumeThreshold: 2, // 默认20,如果错误超过该次数,才开始计算错误百分比
208+
// ErrorPercentThreshold: 50, // 错误百分比,默认50,即50%
209+
// SleepWindow: 5000, // 过多长时间,熔断器再次检测是否开启。单位毫秒
210+
config := hystrix.CommandConfig{
211+
Timeout: timeout,
212+
MaxConcurrentRequests: maxConcurrentRequests,
213+
RequestVolumeThreshold: requestVolumeThreshold,
214+
ErrorPercentThreshold: errorPercentThreshold,
215+
SleepWindow: sleepWindow,
216+
}
204217
return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (cs grpc.ClientStream, err error) {
205218
name := "GSC" + method
206-
// Timeout: 1000 // 超时时间设置 单位毫秒
207-
// MaxConcurrentRequests: 1, // 最大请求数
208-
// RequestVolumeThreshold: 2, // 默认20,如果错误超过该次数,才开始计算错误百分比
209-
// ErrorPercentThreshold: 50, // 错误百分比,默认50,即50%
210-
// SleepWindow: 5000, // 过多长时间,熔断器再次检测是否开启。单位毫秒
211-
hystrix.ConfigureCommand(name, hystrix.CommandConfig{
212-
Timeout: timeout,
213-
MaxConcurrentRequests: maxConcurrentRequests,
214-
RequestVolumeThreshold: requestVolumeThreshold,
215-
ErrorPercentThreshold: errorPercentThreshold,
216-
SleepWindow: sleepWindow,
217-
})
219+
hystrix.ConfigureCommand(name, config)
218220
err = hystrix.DoC(ctx, name, func(ctx context.Context) error {
219221
cs, err = streamer(ctx, desc, cc, method, opts...)
220222
return err

0 commit comments

Comments
 (0)