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

Is there a way to read the current rate limit. #2080

Open
p4paul opened this issue Jan 17, 2024 · 3 comments · May be fixed by #2081
Open

Is there a way to read the current rate limit. #2080

p4paul opened this issue Jan 17, 2024 · 3 comments · May be fixed by #2081

Comments

@p4paul
Copy link

p4paul commented Jan 17, 2024

I was looking to access the current rate limit count and add it to the Header...

For example: Rate limit 30/second, called 25 times, 58 seconds to reset.

X-RateLimit-Limit → 30
X-RateLimit-Remaining → 25
X-RateLimit-Reset → 58

using it something like...

path("api/{ver}/login", () ->
    post((ctx -> {
        NaiveRateLimit.requestPerTimeUnit(ctx, 30, TimeUnit.MINUTES);
        ConcurrentHashMap<TimeUnit, RateLimiter> limiters = RateLimitUtil.INSTANCE.getLimiters();
        RateLimiter rateLimiter = limiters.get(TimeUnit.MINUTES);
        // RateLimiter's method 'keyToRequestCount' is private!
        ...
    }), Role.ALL()));
@tipsy
Copy link
Member

tipsy commented Jan 17, 2024

@p4paul this should be easy enough to add as a feature, would you like to add it?

@p4paul
Copy link
Author

p4paul commented Jan 18, 2024

Hi @tipsy it would be great to access the limit, hit count and remaining time (for the header fields I mentioned above).

I guess you could expose $requestLimit for the given context? Can there be more than one keyFunction for a context? If so perhaps the whole ConcurrentMap keyToRequestCount?

Not sure how I would determine the seconds/milliseconds left until the count is reset. If the executor scheduler runs on the start of each timeUnit I guess a rounding up to the next time unit and subtracting the current time would work.

@p4paul
Copy link
Author

p4paul commented Jan 18, 2024

I'll experiment - never used Kotlin before...

@p4paul p4paul linked a pull request Jan 22, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants