You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
769 B
28 lines
769 B
2 years ago
|
package mw
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
|
||
|
"google.golang.org/grpc"
|
||
|
)
|
||
|
|
||
|
func InterceptChain(intercepts ...grpc.UnaryServerInterceptor) grpc.UnaryServerInterceptor {
|
||
|
l := len(intercepts)
|
||
|
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
|
||
|
chain := func(currentInter grpc.UnaryServerInterceptor, currentHandler grpc.UnaryHandler) grpc.UnaryHandler {
|
||
|
return func(currentCtx context.Context, currentReq interface{}) (interface{}, error) {
|
||
|
return currentInter(
|
||
|
currentCtx,
|
||
|
currentReq,
|
||
|
info,
|
||
|
currentHandler)
|
||
|
}
|
||
|
}
|
||
|
chainHandler := handler
|
||
|
for i := l - 1; i >= 0; i-- {
|
||
|
chainHandler = chain(intercepts[i], chainHandler)
|
||
|
}
|
||
|
return chainHandler(ctx, req)
|
||
|
}
|
||
|
}
|