Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
whentotrade opened this issue Oct 11, 2020 · 3 comments
Open

Comments

@whentotrade
Copy link

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.

@g4mb10r
Copy link

g4mb10r commented Oct 30, 2020

@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.

@whentotrade
Copy link
Author

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

@aa-development-portal
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants