1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
// Copyright (c) The go-grpc-middleware Authors.
// Licensed under the Apache License 2.0.
package timeout
import (
"context"
"time"
"google.golang.org/grpc"
)
// UnaryClientInterceptor returns a new unary client interceptor that sets a timeout on the request context.
func UnaryClientInterceptor(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 {
timedCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
return invoker(timedCtx, method, req, reply, cc, opts...)
}
}
|