1 Star 0 Fork 0

xiedongji/antgo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
.idea
core
cpts
helper
xdebug
xfile
xgen
xpassword
xreopen
xrpc
etcd.go
grpc_client.go
grpc_server.go
middleware.go
middleware_multi_wrap.go
xstring
xtoken
xutils
http
LICENSE
README.md
antgo.go
go.mod
go.sum
克隆/下载
middleware_multi_wrap.go 3.00 KB
一键复制 编辑 原始数据 按行查看 历史
xiedongji 提交于 2年前 . 框架
package xrpc
import (
"context"
"google.golang.org/grpc"
)
// 构建支持多个拦截器中间件
func ChainUnaryServer(interceptors ...grpc.UnaryServerInterceptor) grpc.UnaryServerInterceptor {
n := len(interceptors)
// Dummy interceptor maintained for backward compatibility to avoid returning nil.
if n == 0 {
return func(ctx context.Context, req interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
return handler(ctx, req)
}
}
// The degenerate case, just return the single wrapped interceptor directly.
if n == 1 {
return interceptors[0]
}
// Return a function which satisfies the interceptor interface, and which is
// a closure over the given list of interceptors to be chained.
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
currHandler := handler
// Iterate backwards through all interceptors except the first (outermost).
// Wrap each one in a function which satisfies the handler interface, but
// is also a closure over the `info` and `handler` parametert. Then pass
// each pseudo-handler to the next outer interceptor as the handler to be called.
for i := n - 1; i > 0; i-- {
// Rebind to loop-local vars so they can be closed over.
innerHandler, i := currHandler, i
currHandler = func(currentCtx context.Context, currentReq interface{}) (interface{}, error) {
return interceptors[i](currentCtx, currentReq, info, innerHandler)
}
}
// Finally return the result of calling the outermost interceptor with the
// outermost pseudo-handler created above as its handler.
return interceptors[0](ctx, req, info, currHandler)
}
}
// 流式拦截器
// ChainStreamServer creates a single interceptor out of a chain of many interceptort.
//
// Execution is done in left-to-right order, including passing of context.
// For example ChainUnaryServer(one, two, three) will execute one before two before three.
// If you want to pass context between interceptors, use WrapServerStream.
//
// While this can be useful in some scenarios, it is generally advisable to use google.golang.org/grpc.ChainStreamInterceptor directly.
func ChainStreamServer(interceptors ...grpc.StreamServerInterceptor) grpc.StreamServerInterceptor {
n := len(interceptors)
// Dummy interceptor maintained for backward compatibility to avoid returning nil.
if n == 0 {
return func(srv interface{}, stream grpc.ServerStream, _ *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
return handler(srv, stream)
}
}
if n == 1 {
return interceptors[0]
}
return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
currHandler := handler
for i := n - 1; i > 0; i-- {
innerHandler, i := currHandler, i
currHandler = func(currentSrv interface{}, currentStream grpc.ServerStream) error {
return interceptors[i](currentSrv, currentStream, info, innerHandler)
}
}
return interceptors[0](srv, stream, info, currHandler)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/xiedongji/antgo.git
git@gitee.com:xiedongji/antgo.git
xiedongji
antgo
antgo
v1.0.17

搜索帮助