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.5k stars 132 forks source link

millisecond throttel not working; is only trotteling up to 1 second. #28

Closed awb99 closed 7 years ago

awb99 commented 8 years ago

I use a new RateLimiter (1, 8000) and thought this means that I want to throttle up to a frequency of 1 every 8 seconds.

However, it seems to reduce it to once every second.

I think it is important to be able to select this higher number.

jhurliman commented 8 years ago

Hi, thanks for the report. Do you have a snippet of runnable code that I can use to troubleshoot this with?

awb99 commented 8 years ago

the pings will appear in second intervals, even though I specified 8 seocnds intervals.

var RateLimiter = require('limiter').RateLimiter;
// Limit understands 'second', 'minute', 'day', or a number of milliseconds
var limiter2 = new RateLimiter(1, '8000');

function ping (i) {
  limiter2.removeTokens(1, (err, remainingRequests) => {
      console.log("ping.. " + i);
  })
}

for (var i=0;i<100;i++) {
    ping (i)
}
perryh commented 8 years ago

@awb99 pass in a number for your interval param e.g.

new RateLimiter(1, 8000);

If you pass '8000' it will fall out of this switch and default to 1s. https://github.com/jhurliman/node-rate-limiter/blob/master/lib/tokenBucket.js#L20-L33

jhurliman commented 7 years ago

Closing this as @perryh's response should solve the issue