mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-03 00:38:40 +08:00
842c4d81cc
Co-authored-by: hanzijian <hanzijian@52tt.com> Co-authored-by: Kevin Wan <wanjunfeng@gmail.com>
47 lines
1.0 KiB
Go
47 lines
1.0 KiB
Go
package clientinterceptors
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
// TimeoutInterceptor is an interceptor that controls timeout.
|
|
func TimeoutInterceptor(timeout time.Duration) grpc.UnaryClientInterceptor {
|
|
return func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn,
|
|
invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
|
|
t := getTimeoutByCallOptions(opts, timeout)
|
|
if t <= 0 {
|
|
return invoker(ctx, method, req, reply, cc, opts...)
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, t)
|
|
defer cancel()
|
|
|
|
return invoker(ctx, method, req, reply, cc, opts...)
|
|
}
|
|
}
|
|
|
|
func getTimeoutByCallOptions(callOptions []grpc.CallOption, defaultTimeout time.Duration) time.Duration {
|
|
for _, callOption := range callOptions {
|
|
if o, ok := callOption.(TimeoutCallOption); ok {
|
|
return o.timeout
|
|
}
|
|
}
|
|
|
|
return defaultTimeout
|
|
}
|
|
|
|
type TimeoutCallOption struct {
|
|
grpc.EmptyCallOption
|
|
|
|
timeout time.Duration
|
|
}
|
|
|
|
func WithTimeoutCallOption(timeout time.Duration) grpc.CallOption {
|
|
return TimeoutCallOption{
|
|
timeout: timeout,
|
|
}
|
|
}
|