timeoutinterceptor.go 457 B

123456789101112131415161718
  1. package serverinterceptors
  2. import (
  3. "context"
  4. "time"
  5. "github.com/tal-tech/go-zero/core/contextx"
  6. "google.golang.org/grpc"
  7. )
  8. func UnaryTimeoutInterceptor(timeout time.Duration) grpc.UnaryServerInterceptor {
  9. return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
  10. handler grpc.UnaryHandler) (resp interface{}, err error) {
  11. ctx, cancel := contextx.ShrinkDeadline(ctx, timeout)
  12. defer cancel()
  13. return handler(ctx, req)
  14. }
  15. }