forked from TrueCloudLab/frostfs-node
[#1639] qos: Add interceptors for limiting active RPCs
Signed-off-by: Aleksey Savchuk <a.savchuk@yadro.com>
This commit is contained in:
parent
c6be169634
commit
04d8c16d3a
1 changed files with 35 additions and 0 deletions
|
@ -3,7 +3,9 @@ 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"
|
||||
)
|
||||
|
||||
|
@ -49,3 +51,36 @@ func NewAdjustOutgoingIOTagStreamClientInterceptor() grpc.StreamClientIntercepto
|
|||
return streamer(ctx, desc, cc, method, opts...)
|
||||
}
|
||||
}
|
||||
|
||||
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
Reference in a new issue