node-tokenthrottle
node-tokenthrottle copied to clipboard
Throttling fails when multiple calls are made in quick succession
Hi, thanks for creating an excellent module. I am encountering one issue, where if multiple calls are made before the callbacks settle, then the throttling can fail. Example:
var myThrottle = require('tokenthrottle')({
rate: 1,
window: 10 * 1000
});
function getToken() {
myThrottle.rateLimit('abc', function(err, limited) {
if (err) console.log(err);
else console.log(limited);
});
}
getToken();
getToken();
Expected output:
false
true
Actual output:
false
false
Yup, I ran into this too. The library is not usable for mid-to-high concurrency environments. It doesn't use concurrency-sensitive manipulation of the payload. If there are several concurrent requests in a node server, or multiple servers, they may each fetch the current limit, say, 100, then decrement to 99 and each write back 99 when it should be 97.