node-tokenthrottle icon indicating copy to clipboard operation
node-tokenthrottle copied to clipboard

Throttling fails when multiple calls are made in quick succession

Open weiyin opened this issue 10 years ago • 1 comments

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

weiyin avatar Apr 14 '15 23:04 weiyin

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.

carlfreeland avatar Mar 01 '17 23:03 carlfreeland