I got a gRPC service implemented using C# .NET8.
I’m setting a concurrency limiter like this:
services.AddRateLimiter(options =>
{
options.AddConcurrencyLimiter(policyName: concurrencyPolicyName, options =>
{
options.PermitLimit = 160;
options.QueueProcessingOrder = QueueProcessingOrder.OldestFirst;
options.QueueLimit = 160;
});
options.OnRejected = async (context, token) =>
{
context.HttpContext.Response.StatusCode = 503;
await context.HttpContext.Response.WriteAsync("The service is currently handling too many requests. Please try again later... ", cancellationToken: token);
};
});
.
.
.
endpoints.MapGrpcService<MyService>().RequireRateLimiting(concurrencyPolicyName);
In the proto file for the rpc calls, I got a single service with 2 different rpc calls, one is Set
and one is Get
:
service My_Service {
rpc Get (GetMessage) returns (GetReply);
rpc Set (SetMessage) returns (SetReply);
}
And in the C# side I’m overriding the service base class with MyService
class and implement Set
and Get
.
As I shown before, I know how to set a rate limiter for the entire service with the line:
endpoints.MapGrpcService<MyService>().RequireRateLimiting(concurrencyPolicyName);
Is there a way to add a different limit policy for Set
and Get
instead of setting the policy for the entire service?
You can use the EnableRateLimitingAttribute
with corresponding policy name applied to the method implementations (and remove the RequireRateLimiting
call):
[EnableRateLimiting("GetPolicyName")]
public override Task<GetReply> Get(GetMessage request, ServerCallContext context)
{
//...
}
[EnableRateLimiting("SetPolicyName")]
public override Task<SetReply> Set(SetMessage request, ServerCallContext context)
{
//...
}
Note that this attribute can be applied hierarchically – see the docs.
2