We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
如何实现多个拦截器 另外,可以发现 gRPC 本身居然只能设置一个拦截器,难道所有的逻辑都只能写在一起?
翻看grpc源码可以发现,在调用grpc.NewServer()时候,该函数会用到chainUnaryServerInterceptors函数,这个函数会将Server中opts的unaryInt拦截器和chainUnaryInts拦截器集合,一起打包成一个拦截器链再放回unaryInt字段。 所以grpc原生是支持多拦截器的
grpc.NewServer()
chainUnaryServerInterceptors
Server
opts
unaryInt
chainUnaryInts
chainUnaryServerInterceptors函数源码如下:
func chainUnaryServerInterceptors(s *Server) { // Prepend opts.unaryInt to the chaining interceptors if it exists, since unaryInt will // be executed before any other chained interceptors. interceptors := s.opts.chainUnaryInts if s.opts.unaryInt != nil { interceptors = append([]UnaryServerInterceptor{s.opts.unaryInt}, s.opts.chainUnaryInts...) } var chainedInt UnaryServerInterceptor if len(interceptors) == 0 { chainedInt = nil } else if len(interceptors) == 1 { chainedInt = interceptors[0] } else { chainedInt = func(ctx context.Context, req interface{}, info *UnaryServerInfo, handler UnaryHandler) (interface{}, error) { return interceptors[0](ctx, req, info, getChainUnaryHandler(interceptors, 0, info, handler)) } } s.opts.unaryInt = chainedInt }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
翻看grpc源码可以发现,在调用
grpc.NewServer()
时候,该函数会用到chainUnaryServerInterceptors
函数,这个函数会将Server
中opts
的unaryInt
拦截器和chainUnaryInts
拦截器集合,一起打包成一个拦截器链再放回unaryInt
字段。所以grpc原生是支持多拦截器的
chainUnaryServerInterceptors
函数源码如下:The text was updated successfully, but these errors were encountered: