mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-24 09:40:24 +08:00
20 lines
435 B
Go
20 lines
435 B
Go
|
package serverinterceptors
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"time"
|
||
|
|
||
|
"zero/core/contextx"
|
||
|
|
||
|
"google.golang.org/grpc"
|
||
|
)
|
||
|
|
||
|
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)
|
||
|
}
|
||
|
}
|