Go gRPC:基于每个RPC对API进行速率限制
我正在寻找一种方法来分别对高粒度的RPC进行分级限制,令我沮丧的是,对于这个问题没有太多的选择。我正在尝试用gRPC替换RESTAPI,对我来说,最重要的特性之一是能够为每个路由添加中间件。不幸的是,仅将中间件应用于整个服务器Go gRPC:基于每个RPC对API进行速率限制,go,grpc,Go,Grpc,我正在寻找一种方法来分别对高粒度的RPC进行分级限制,令我沮丧的是,对于这个问题没有太多的选择。我正在尝试用gRPC替换RESTAPI,对我来说,最重要的特性之一是能够为每个路由添加中间件。不幸的是,仅将中间件应用于整个服务器 在我的想象中,一个理想的gRPC速率限制中间件将使用类似的技巧,其中proto文件将包含速率限制本身的配置。我想我可以发布一个片段,以供参考,在实践中使用WithUnaryServerChain和一元拦截器 其想法是向服务器添加一个grpc.UnaryServerInfo
在我的想象中,一个理想的gRPC速率限制中间件将使用类似的技巧,其中proto文件将包含速率限制本身的配置。我想我可以发布一个片段,以供参考,在实践中使用
WithUnaryServerChain
和一元拦截器
其想法是向服务器添加一个grpc.UnaryServerInfo
实例,该实例将通过*grpc.UnaryServerInfo
调用。此对象导出字段FullMethod
,该字段保存被调用的RPC方法的限定名
在拦截器中,您可以在实际调用RPC处理程序之前实现任意代码,包括特定于RPC的速率限制逻辑
// import grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
// import "google.golang.org/grpc"
grpcServer := grpc.NewServer(
// WithUnaryServerChain is a grpc.Server config option that accepts multiple unary interceptors.
grpc_middleware.WithUnaryServerChain(
// UnaryServerInterceptor provides a hook to intercept the execution of a unary RPC on the server. info
// contains all the information of this RPC the interceptor can operate on. And handler is the wrapper
// of the service method implementation. It is the responsibility of the interceptor to invoke handler
// to complete the RPC.
grpc.UnaryInterceptor(func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
// FullMethod is the full RPC method string, i.e., /package.service/method.
switch info.FullMethod {
case "/mypackage.someservice/DoThings":
// ... rate limiting code
// if all is good, then call the handler
return handler(ctx, req)
}
}),
// other `grpc.ServerOption` opts
),
)
你有没有考虑过实施你自己的计划
go-grpc中间件
构建在拦截器之上。实现您自己的可以允许您根据请求信息指定速率限制。这是一个有趣的想法,但我不确定是否有办法区分通过拦截器调用的RPC。但是似乎很有希望。找到了一些应该使速率限制成为可能的东西,UnaryServerInfo.FullMethod似乎为RPCY显示了一个非常独特的标识符。如果需要,您还应该能够检查req
参数以找到更具体的信息。