jhurliman / node-rate-limiter

A generic rate limiter for node.js. Useful for API clients, web crawling, or other tasks that need to be throttled
MIT License
1.51k stars 135 forks source link

Possible to adjust concurrency? #38

Closed zeke closed 7 years ago

zeke commented 7 years ago

Hi. I'm hitting the GitHub API using this setting:

const limiter = new RateLimiter(5000, 'hour')

This is within GitHub's documented limit, but I'm still getting throttled because limiter is making so many concurrent requests. I think this is happening because limiter comes on strong at first and uses some kind of backoff mechanism to taper down the requests over the course of the hour. Is that right?

Is there a way to limit the number of concurrent requests?

Here's one way I'm thinking I could approximate it:

const limiter = new RateLimiter(Math.floor(5000/60), 'minute')
// or
const limiter = new RateLimiter(Math.floor(5000/60/60), 'second')
jhurliman commented 7 years ago

The best way to handle this is by using the lower level TokenBucket API. It allows burst rate and fill rate to be configured separately, and you can nest them so an 80/minute bucket can have a parent with a 5000/hour limit.

zeke commented 7 years ago

Thanks for the info @jhurliman. I ended up having pretty good luck just using a smaller time increment.