[#1639] qos: Add interceptors for limiting active RPCs
Signed-off-by: Aleksey Savchuk <a.savchuk@yadro.com>
This commit is contained in:
parent
92b0059989
commit
1d4b0defa5
3 changed files with 86 additions and 0 deletions
43
internal/qos/grpc.go
Normal file
43
internal/qos/grpc.go
Normal file
|
@ -0,0 +1,43 @@
|
|||
package qos
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-qos/limiting"
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-qos/tagging"
|
||||
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
func NewMaxActiveRPCLimiterUnaryServerInterceptor(lr *limiting.Limiter) grpc.UnaryServerInterceptor {
|
||||
return func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
|
||||
if tag, ok := tagging.IOTagFromContext(ctx); ok && tag == IOTagCritical.String() {
|
||||
return handler(ctx, req)
|
||||
}
|
||||
|
||||
release, ok := lr.TryAcquire(info.FullMethod)
|
||||
if !ok {
|
||||
return nil, new(apistatus.ResourceExhausted)
|
||||
}
|
||||
defer release()
|
||||
|
||||
return handler(ctx, req)
|
||||
}
|
||||
}
|
||||
|
||||
//nolint:contextcheck
|
||||
func NewMaxActiveRPCLimiterStreamServerInterceptor(lr *limiting.Limiter) grpc.StreamServerInterceptor {
|
||||
return func(srv any, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
|
||||
if tag, ok := tagging.IOTagFromContext(ss.Context()); ok && tag == IOTagCritical.String() {
|
||||
return handler(srv, ss)
|
||||
}
|
||||
|
||||
release, ok := lr.TryAcquire(info.FullMethod)
|
||||
if !ok {
|
||||
return new(apistatus.ResourceExhausted)
|
||||
}
|
||||
defer release()
|
||||
|
||||
return handler(srv, ss)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue