Skip to content

Rate Limiter

Ollie Jennings edited this page Sep 10, 2020 · 1 revision

Did you build a custom rate-limiter for this library?

No, we use Bottleneck which is well tested and production used rate-limiter. It also comes with some nifty features that we can make use of.


How do you setup the rate-limiter based on the limits return from the Riot API?

Imagine we get the response headers:

X-App-Rate-Limit: 100:1,1000:10

This can be broken down into the following rate limits:

  • 100 calls per 1 second
  • 1,000 calls per 10 seconds

Therefore we would initialize 2 rate limiters to handle both rate limit windows. The first rate limiter would be initialized with the follow Bottleneck configuration:

{
  "reservoir": 100,
  "reservoirRefreshAmount": 100,
  "reservoirRefreshInterval": 1000,
  "minTime": 10, // this time between each request so we can 'spread' the requests over the window 
}

And the second rate limiter would be initialized with:

{
  "reservoir": 1000,
  "reservoirRefreshAmount": 1000,
  "reservoirRefreshInterval": 10000,
  "minTime": 10, // this time between each request so we can 'spread' the requests over the window 
}

We then chain the rate-limiters together via: rateLimiter1.chain(rateLimiter2). This means that when we use rateLimiter1, the rate-limits of rateLimiter2 will also be affected and respected by rateLimiter1, therfore achieving both rate limits outlined above.

The same above is done for the method rate limits as well, which is also chained to the app rate limiter above.


How do you synchronize the rate limiters with the Rate-Limit Counts?

Once the first request has been made, we use the response headers to setup our rate-limiters. We use the X-App-Rate-Limit-Count header to tell us how much of our resevior we have already used. After every subsequent response that is successful, we do not update the rate-limiters based on the count headers as we assume the that our rate-limiters will track everything correctly.

However if we receive a 429 response, we then update the reseviors of all affected rate-limiters in order to synchronize with the Riot API. We also then ready the Retry-After header to tell us how long to backoff for.

Clone this wiki locally