alisaifee / limits

Rate limiting using various strategies and storage backends such as redis & memcached
https://limits.readthedocs.org
MIT License
423 stars 59 forks source link

Ability to 'undo' last hit on exception response #186

Closed ttamg closed 9 months ago

ttamg commented 1 year ago

Great package and very useful thanks.

My use case is where I use the rate limiter at the start of a long-running request, but want to only count the 'hit' if the response does not fail and return an exception. If a client hits the API and gets and error response then they should not have to 'pay for it' that time.

I see a workaround for this is to use the .test() on the way in and at the end of my call use the full .hit() to increment the rate limiter. This feels a bit clunky and hits Redis twice.

Instead is there a way to do this by applying the .hit() at the start of the request and then if an exception is raised, catch that and call another .hit() with a cost of -1 or similar? I can and will test it but thought I would raise as a feature query / request.

Thanks. Matt

alisaifee commented 11 months ago

At the moment using the test method would be the way to go - this is used in the Flask-Limiter library to do conditional rate limiting (pretty much what it looks like you're trying to do). The extra hit to the backend is indeed unfortunate but the implementation of the moving window strategy is not so simple in terms of implementing a deduction.

alisaifee commented 9 months ago

Closing due to inactivity