Improve the performance of SimpleRateThrottle #9471
Unanswered
mohsen-mahmoodi
asked this question in
Ideas & Suggestions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The time complexity of
SimpleRateThrottle.allow_requestalgorithm for history cleanup is O(n) which it's suitable for applications withcache_keys that have high cardinality.In my case, I was going to create a custom throttling class, and the cache_key is a specific part of the
request.pathfor example:Another example can be users coming from a single ISP when we're using
AnonRateThrottle.Let's imagine we use a throttling rate like
1000000/minif we receive 99% of the requests in somewhere between 0 to 30 seconds, and the rest 10% at 59 to 60, the
historyof requests will be updated in the cache for another minute. Now after we receive another request in the next 30 seconds the history cleanup algorithm will do 1000000 comparisons, or O(num_requests).If we use bisect_right to drop the right of the list we can reduce this complexity to O(log(n)) simply.
We need a descending bisect right implementation:
and to rewrite the
SimpleRateThrottle.allow_requestas:Beta Was this translation helpful? Give feedback.
All reactions