forked from wothing/worpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterceptor.go
26 lines (22 loc) · 875 Bytes
/
interceptor.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
// Copyright (c) 2016 Wothing Co., Ltd. All rights reserved.
package worpc
import (
"golang.org/x/net/context"
"google.golang.org/grpc"
)
// UnaryInterceptorChain build the multi interceptors into one interceptor chain.
func UnaryInterceptorChain(interceptors ...grpc.UnaryServerInterceptor) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
chain := handler
for i := len(interceptors) - 1; i >= 0; i-- {
chain = build(interceptors[i], chain, info)
}
return chain(ctx, req)
}
}
// build is the interceptor chain helper
func build(c grpc.UnaryServerInterceptor, n grpc.UnaryHandler, info *grpc.UnaryServerInfo) grpc.UnaryHandler {
return func(ctx context.Context, req interface{}) (interface{}, error) {
return c(ctx, req, info, n)
}
}