python-musicbrainzngs
python-musicbrainzngs copied to clipboard
Invalid rate limiting sleeping due to error in computation
When the rate limiter needs to sleep, it computes the duration to sleep before making a new request with the following computation:
(1.0 - self.remaining_requests) * (limit_requests / limit_interval)
Which, if we rename the variables to something more readable:
(1.0 - self.remaining_requests) * (reqs_per_interval / interval_sec)
However, this computation is wrong if reqs_per_interval != interval_sec.
For example, suppose we want 10 reqs per 0.1 seconds:
- We should wait around 0.01 second to be able to do 1 req
self.reqs_per_interval / self.interval_sec = 10 / 0.1 = 100- So currently, we will wait 100s instead of 0.1s
- Depending on the values, we may even sleep less than required, and thus we will not follow the rate limits!
The solution is instead to use the following computation:
(1.0 - self.remaining_requests) * (interval_sec / reqs_per_interval)