Jelajahi Sumber

zrpc client support block (#412)

HarryWang29 4 tahun lalu
induk
melakukan
d239952d2d
2 mengubah file dengan 4 tambahan dan 7 penghapusan
  1. 1 1
      zrpc/config.go
  2. 3 6
      zrpc/internal/clientinterceptors/timeoutinterceptor.go

+ 1 - 1
zrpc/config.go

@@ -25,7 +25,7 @@ type (
 		Endpoints []string        `json:",optional=!Etcd"`
 		App       string          `json:",optional"`
 		Token     string          `json:",optional"`
-		Timeout   int64           `json:",optional"`
+		Timeout   int64           `json:",default=2000"`
 	}
 )
 

+ 3 - 6
zrpc/internal/clientinterceptors/timeoutinterceptor.go

@@ -8,15 +8,12 @@ import (
 	"google.golang.org/grpc"
 )
 
-const defaultTimeout = time.Second * 2
-
 func TimeoutInterceptor(timeout time.Duration) grpc.UnaryClientInterceptor {
-	if timeout <= 0 {
-		timeout = defaultTimeout
-	}
-
 	return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn,
 		invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
+		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...)