timeoutinterceptor.go 435 B

12345678910111213141516171819
  1. package serverinterceptors
  2. import (
  3. "context"
  4. "time"
  5. "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. }