jhurliman/node-rate-limiter

Setting up the rate limit

boboci9 opened this issue · 3 comments

Hi,

Great job with the package I just have some questions?

var RateLimiter = Npm.require('limiter').RateLimiter;
        var cache = Npm.require('memory-cache');
        checkBy = req.headers.app_id;
        if (cache.get(checkBy)){
            var cachedLimiter = cache.get(checkBy); 
            if (cachedLimiter.getTokensRemaining()>1){
                cachedLimiter.removeTokens(1, function(){});
                cache.put(checkBy, cachedLimiter, 60000);
                return;
            }
            throw new Error('Too many requests for the app_id: ' + checkBy + '!');
        }
        else {
            var cachedLimiter = new RateLimiter(10, 'sec'); 
            cache.put(checkBy, cachedLimiter, 60000); 
            return;
        }
  1. If I put var cachedLimiter = new RateLimiter(10, 'sec'); the rate limit is now working as expected I put 30 calls in the same second and about 25 gets passed and 5 gets rejected because of the limit although I don't want to allow more than 10 per second? Could you tell me what am I doing wrong?
  2. Can I activate two limits at once, one for 10 requests per second with 60 sec stop (this one I have done in the code above) and one for 400 requests per hour with an hour stop if they reach the second limit? Do I have to do this with a parentToken? If yes I couldn't find anywhere any description on how to use the parentToken?

Thank you very much in advance.

Best regards!

👍 for a guide how to use multiple rate limiters at once (e.g. one for requests per minute, one for requests per hour).

I guess its as simple as nesting their calls? Create one with limits and
replenishes of per-Minute and per-Hour and call one, then inside of that
call, call the other, inside of that call, call your desired function.

On 13 October 2015 at 11:21, Lennart Hildebrandt notifications@github.com
wrote:

[image: 👍] for a guide how to use multiple rate limiters at once (e.g.
one for requests per minute, one for requests per hour).


Reply to this email directly or view it on GitHub
#21 (comment)
.

One note, to simplify your code I would use the tryRemoveTokens method (see https://github.com/jhurliman/node-rate-limiter/blob/master/lib/rateLimiter.js#L98).

For the second question, there are two separate parts. One is the multiple rate limiters which you should be able to get by just creating two rate limiters. But since you are looking for custom logic when the rate limit is hit (disable for N seconds), you probably want to use the tryRemoveTokens call and run your custom logic (like setting a reEnableAt timestamp in the future) if that method returns false.