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
-
The time complexity of
SimpleRateThrottle.allow_request
algorithm for history cleanup is O(n) which it's suitable for applications withcache_key
s 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.path
for 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/min
if we receive 99% of the requests in somewhere between 0 to 30 seconds, and the rest 10% at 59 to 60, the
history
of 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_request
as:Beta Was this translation helpful? Give feedback.
All reactions