From 086113c84319ad1c9bed8bdd16fbf9178597dc84 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Fri, 19 Feb 2021 10:44:39 +0800 Subject: [PATCH] prevent negative timeout settings (#482) * prevent negative timeout settings * fix misleading comment --- zrpc/config.go | 3 +-- zrpc/internal/clientinterceptors/timeoutinterceptor.go | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/zrpc/config.go b/zrpc/config.go index 3448301b..31c3675c 100644 --- a/zrpc/config.go +++ b/zrpc/config.go @@ -14,8 +14,7 @@ type ( Auth bool `json:",optional"` Redis redis.RedisKeyConf `json:",optional"` StrictControl bool `json:",optional"` - // pending forever is not allowed - // never set it to 0, if zero, the underlying will set to 2s automatically + // setting 0 means no timeout Timeout int64 `json:",default=2000"` CpuThreshold int64 `json:",default=900,range=[0:1000]"` } diff --git a/zrpc/internal/clientinterceptors/timeoutinterceptor.go b/zrpc/internal/clientinterceptors/timeoutinterceptor.go index 4b9800c4..86150e02 100644 --- a/zrpc/internal/clientinterceptors/timeoutinterceptor.go +++ b/zrpc/internal/clientinterceptors/timeoutinterceptor.go @@ -11,9 +11,10 @@ import ( func TimeoutInterceptor(timeout time.Duration) grpc.UnaryClientInterceptor { return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { - if timeout == 0 { + if timeout <= 0 { return invoker(ctx, method, req, reply, cc, opts...) } + ctx, cancel := contextx.ShrinkDeadline(ctx, timeout) defer cancel() return invoker(ctx, method, req, reply, cc, opts...)