tomasbasham / ratelimit

API Rate Limit Decorator
MIT License
768 stars 159 forks source link

Jordan/improve algorithm and add soft limit #57

Closed JordanZwan closed 2 years ago

JordanZwan commented 2 years ago

This PR contains an improved rate limiting algorithm, as well as a soft limit feature.

The rate limiting algorithm replacing the fixed time windows with a token bucket approach instead, ensuring that no more endpoint requests than the imposed limit can happen in the given period (improving the current implementation, where twice the allowed number of requests is permitted (if the first half happened during the end of one time window, while the second half requesting at the start of the next time window).

The soft limiting feature provides the possibility to generate f.e. a warning when a rate-limited endpoint is close to hitting that limit. This can be used to act upon, before the endpoint has actually hit the rate limit and starts blocking new requests. This soft limiting is configured using two parameters: treshold, which is the fraction of remaining requests under which to trigger provided treshold_method (e.g. threshold=0.2 for triggering on requests with less than 20% remaining requests)