Skip to content

Invalid rate limiting sleeping due to error in computation #294

@nicomem

Description

@nicomem

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions