This commit is contained in:
calzoneman 2013-09-06 15:53:23 -05:00
parent c2cd04f760
commit 6aecb32c89
3 changed files with 65 additions and 6 deletions

View File

@ -1,3 +1,10 @@
Fri Sep 6 15:51 2013 CDT
* lib/utilities.js: Tweak the throttle code for rate limiters to fix
incorrect behavior of the burst cap after the cooldown period has
been passed
* tests/rateLimiter.js: Write a couple quick test cases to ensure that
rate limiting is handled properly
Thu Sep 5 22:52 2013 CDT Thu Sep 5 22:52 2013 CDT
* www/assets/js/callbacks.js: Disable the channel registration button * www/assets/js/callbacks.js: Disable the channel registration button
and change its text while a registration attempt is being processed and change its text while a registration attempt is being processed

View File

@ -76,16 +76,16 @@ module.exports = {
if (isNaN(cooldown)) if (isNaN(cooldown))
cooldown = burst / sustained; cooldown = burst / sustained;
// Haven't reached burst cap yet, allow // Cooled down, allow and clear buffer
if (this.count < burst) { if (this.lastTime < Date.now() - cooldown*1000) {
this.count++; this.count = 1;
this.lastTime = Date.now(); this.lastTime = Date.now();
return false; return false;
} }
// Cooled down, allow and clear buffer // Haven't reached burst cap yet, allow
if (this.lastTime < Date.now() - cooldown*1000) { if (this.count < burst) {
this.count = 0; this.count++;
this.lastTime = Date.now(); this.lastTime = Date.now();
return false; return false;
} }

52
tests/rateLimiter.js Normal file
View File

@ -0,0 +1,52 @@
var $util = require('../lib/utilities.js');
function testBurst() {
var lim = $util.newRateLimiter();
var params = {
burst: 10,
sustained: 2
};
for (var i = 0; i < 10; i++) {
if (lim.throttle(params)) {
console.log("[FAIL] Burst: Unexpected throttle");
return;
}
}
if (!lim.throttle(params)) {
console.log("[FAIL] Burst: didn't throttle after exceeding burst amount");
return;
}
console.log("[PASS] Burst");
}
function testBurstAndWait() {
var lim = $util.newRateLimiter();
var params = {
burst: 10,
sustained: 2
};
for (var i = 0; i < 9; i++) {
if (lim.throttle(params)) {
console.log("[FAIL] Burst & Wait: Unexpected throttle");
return;
}
}
// Wait a while and try some more
setTimeout(function () {
for (var i = 9; i < 17; i++) {
if (lim.throttle(params)) {
console.log("[FAIL] Burst & Wait: Unexpected throttle");
return;
}
}
console.log("[PASS] Burst & Wait");
}, 6000);
}
testBurst();
testBurstAndWait();