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

Even distribution over time #11

Closed objectiveSee closed 9 years ago

objectiveSee commented 10 years ago

If I specify 20 / minute, and I have thousands of requests per second (for example). Right now I see 20 events happen right away, then nothing for 1 minute, then another burst of 20 events. It would be nice to specify an option for an even distribution so that one event would fire every 3 seconds rather than bursting every minute.

Thanks!

colinclancy commented 9 years ago

You can already do this. Just do the math yourself. Don't specify 20/min, specify 1 / 3s

jhurliman commented 9 years ago

@objectiveSee I think this can be accomplished as @Belenus said by specifying a smaller number with a more granular time scale. If I'm missing the picture, a mock code example of how you would expect to use the library would be helpful and we can determine if there is additional functionality that would be nice to add.

MariusRumpf commented 9 years ago

This works with 20 requests per minute, but if you have a limit of 500 per day this is not a solution. This would mean I have a setting of 0.005787 per second. :+1: for an even distribution setting.

colinclancy commented 9 years ago

@MariusRumpf This can be achieved, again, with a little more work on your side. 500 requests per day is equivalent to 1 request every 172.8 seconds(172800 in milliseconds).

This will instantiate a limiter at an evenly distributed rate of 500requests/24hours:

var limiter = new RateLimiter(1, 172800);
jhurliman commented 9 years ago

Since I believe @Belenus's answer addresses this question I'm closing the ticket. Feel free to reopen if I overlooked something.