SGrondin / bottleneck

Job scheduler and rate limiter, supports Clustering
MIT License
1.82k stars 75 forks source link

Rate limit 20 jobs per second #141

Open Spyes opened 4 years ago

Spyes commented 4 years ago

I'm trying to rate limit requests to an express route, and I want to limit it to 20 jobs per second, any more jobs and I return 429 Too Many Requests, but I'm not sure I understand the configuration exactly...

{
  highWater: 0,
  reservoir: 20,
  reservoirRefreshAmount: 20,
  reservoirRefreshInterval: 1 * 1000
}

If I understand it correctly, this will set the reservoir to 20 every 1000ms, and because highWater is set to 0, it will drop any request that overflow from the reservoir. Please correct me if I'm wrong :) Thanks! And great library, by the way - amazing work!

jamesliupenn commented 4 years ago

@Spyes - I think that is the right approach if you wish to drop any request that overflows the 20 limitations every 1000ms.

I'd also suggest using maxConcurrent and minTime as recommended to better stagger the requests.