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

Setting up the rate limit #21

Closed boboci9 closed 7 years ago

boboci9 commented 9 years ago

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!

lennerd commented 8 years ago

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

t3chguy commented 8 years ago

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: :+1:] 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 https://github.com/jhurliman/node-rate-limiter/issues/21#issuecomment-147673860 .

jhurliman commented 8 years ago

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.