Closed binarybehemoth closed 5 years ago
The sentence just above what you pasted explains that this example does not queue, it returns immediately if the rate limiter is exhausted. It's intended to show an example of where you don't want to queue and instead want to inform a requesting client that they've exceeded their rate limit and no action will be taken. response
would be part of a web server framework; this wasn't intended to be a complete runnable code example but just a small snippet showing one way to use the library.
I am confused after reading the example:
var RateLimiter = require('limiter').RateLimiter; var limiter = new RateLimiter(150, 'hour', true); // fire CB immediately
// Immediately send 429 header to client when rate limiting is in effect limiter.removeTokens(1, function(err, remainingRequests) { if (remainingRequests < 1) { response.writeHead(429, {'Content-Type': 'text/plain;charset=UTF-8'}); response.end('429 Too Many Requests - your IP is being rate limited'); } else { callMyMessageSendingFunction(...); } });
Where is 'response' defined? Don't we need to hook the limiter to a path with app.use()? How does the limiter know the incoming IP otherwise? Also, how do we queue up the messages to ensure correct order?