2020-07-26 17:09:05 +08:00
|
|
|
package serverinterceptors
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2020-08-08 16:40:10 +08:00
|
|
|
"github.com/tal-tech/go-zero/core/contextx"
|
2020-07-26 17:09:05 +08:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
2021-03-01 23:52:44 +08:00
|
|
|
// UnaryTimeoutInterceptor returns a func that sets timeout to incoming unary requests.
|
2020-07-26 17:09:05 +08:00
|
|
|
func UnaryTimeoutInterceptor(timeout time.Duration) grpc.UnaryServerInterceptor {
|
|
|
|
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
|
|
|
|
handler grpc.UnaryHandler) (resp interface{}, err error) {
|
|
|
|
ctx, cancel := contextx.ShrinkDeadline(ctx, timeout)
|
|
|
|
defer cancel()
|
|
|
|
return handler(ctx, req)
|
|
|
|
}
|
|
|
|
}
|