gRPC server timeout
setup gRPC server timeout add interceptor
code:go
func TimeoutUnaryInterceptor(timeout time.Duration) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
return handler(ctx, req)
}
}
operation check
code:go
// at handler
func Handle(ctx context.Context, req Request) (Response, error) {
waitCancel(ctx)
...
}
func waitCancel(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
}
}
}