You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/zrpc/internal/clientinterceptors/timeoutinterceptor.go

22 lines
576 B
Go

4 years ago
package clientinterceptors
import (
"context"
"time"
"github.com/tal-tech/go-zero/core/contextx"
4 years ago
"google.golang.org/grpc"
)
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 {
return invoker(ctx, method, req, reply, cc, opts...)
}
4 years ago
ctx, cancel := contextx.ShrinkDeadline(ctx, timeout)
defer cancel()
return invoker(ctx, method, req, reply, cc, opts...)
}
}