forked from icexin/brpc-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intercepter.go
28 lines (23 loc) · 847 Bytes
/
intercepter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package brpc
import (
"context"
"google.golang.org/grpc"
)
func toUnaryHandler(info *grpc.UnaryServerInfo, handler grpc.UnaryHandler, inter grpc.UnaryServerInterceptor) grpc.UnaryHandler {
return func(ctx context.Context, req interface{}) (interface{}, error) {
return inter(ctx, req, info, handler)
}
}
func ChainIntercepter(ints ...grpc.UnaryServerInterceptor) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
if len(ints) == 0 {
return handler(ctx, req)
}
return ints[0](ctx, req, info, toUnaryHandler(info, handler, ChainIntercepter(ints[1:]...)))
}
}
func WithInterceptor(interceptor grpc.UnaryServerInterceptor) ServerOption {
return BServerOption(func(o *ServerOptions) {
o.Interceptor = interceptor
})
}