.NET 8 Concurrency limiter to different rpc calls
Asked Answered
P

1

0

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?

Pent answered 16/7 at 10:52 Comment(0)
I
1

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.

Implicit answered 17/7 at 22:13 Comment(2)
I actually read about this attribute, but according to what I've read here: learn.microsoft.com/en-us/aspnet/core/performance/… it seems like this attribute is used only for web api controller and actions, and not for grpc? Maybe I got it wrongPent
@Pent I have tested it with GRPC. AFAIK there are quite a lot of attributes which are shared between different "flavors" of ASP.NET Core. P.S. - the article does not mention GRPC at all as far as I can see, including the RequireRateLimiting call.Implicit

© 2022 - 2024 — McMap. All rights reserved.