AspNetCoreRateLimit icon indicating copy to clipboard operation
AspNetCoreRateLimit copied to clipboard

How to check current client rate limit usage and limits during runtime?

Open whentotrade opened this issue 4 years ago • 3 comments

Question/Request only: Is there a way to read the current used rate-limit counter per time-limit rule and set rate limit for a given client / client id?

E.g. showing a user the applied limits per endpoint and the already used count against these limits? As this is done with most API based services where you can see the limit per client used count.

I can make a call against each endpoint to check that? But is there a way to read this information per client from the internals somewhere? Any hint would be welcome.

whentotrade avatar Oct 11 '20 07:10 whentotrade

@whentotrade This may not be exactly what you are looking for but if you are using Application Insights you can have it listen for the ILogger Information messages from the rate-limiting middleware. This can be done through appsettings.json.

e.g.

"Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" }, "ApplicationInsights": { "LogLevel": { "Default": "Warning", "AspNetCoreRateLimit": "Information" } } }

In this example, we are telling Application Insights to record Warning (and above), except for when it comes to AspNetCoreRateLimit where we record Information (and above).

This lets you see the failure in Application Insights with a trace explaining why the request was blocked. I.e. what rule was applied.

g4mb10r avatar Oct 30 '20 23:10 g4mb10r

thanks for the response, but yes, it is not what I am looking for. Using API from a customer perspective, you should have an option to see what is your current consumed api call amount and the still available counts until your limits are reached. Right now, we can not get this information - would be a nice enhancement

whentotrade avatar Oct 06 '21 18:10 whentotrade

Did one work around: I am storing requests counters into Redis, as we do have micro services.

When you make requests to end point, Rate limit will add one key in redis which will have counter value.

To get those counter you need to first create key for particular client

$"{options.RateLimitCounterPrefix}{requestIdentity.ClientId}{rule.Period}" + $"{requestIdentity.HttpVerb}_{requestIdentity.Path}";

Example : crlc_client1_1d_get_/api/values

Now you need to hash this key using :

        var bytes = Encoding.UTF8.GetBytes(key);

        using var algorithm = new SHA1Managed();
        var hash = algorithm.ComputeHash(bytes);

        var RedisKey = Convert.ToBase64String(hash);

Using above key you can find count value for particular endpoint.

Let me know if you need your help

image

aa-development-portal avatar Oct 29 '21 05:10 aa-development-portal