Tweak rate limiting on the playlist

This commit is contained in:
Calvin Montgomery 2013-08-28 19:25:53 -05:00
parent 202c2ec467
commit 02887958cb
3 changed files with 61 additions and 6 deletions

View file

@ -1227,11 +1227,20 @@ Channel.prototype.tryQueue = function(user, data) {
return;
}
if(user.rank < Rank.Moderator
&& this.leader != user
&& user.noflood("queue", 3)) {
return;
} else if (user.rank < Rank.Siteadmin && user.noflood("queue", 0.5)) {
var limit = {
burst: 3,
sustained: 1
};
if (user.rank >= Rank.Moderator || this.leader == user) {
limit = {
burst: 10,
sustained: 2
};
}
if (user.queueLimiter.throttle(limit)) {
user.socket.emit("queueFail", "You are adding videos too quickly");
return;
}

View file

@ -31,6 +31,7 @@ var User = function(socket, Server) {
this.muted = false;
this.throttle = {};
this.flooded = {};
this.queueLimiter = $util.newRateLimiter();
this.profile = {
image: "",
text: ""

View file

@ -53,5 +53,50 @@ module.exports = {
return [m, s].join(":");
return [h, m, s].join(":");
},
newRateLimiter: function () {
return {
count: 0,
lastTime: 0,
throttle: function (opts) {
if (typeof opts === "undefined")
opts = {};
var burst = +opts.burst,
sustained = +opts.sustained,
cooldown = +opts.cooldown;
if (isNaN(burst))
burst = 10;
if (isNaN(sustained))
sustained = 2;
if (isNaN(cooldown))
cooldown = burst / sustained;
// Haven't reached burst cap yet, allow
if (this.count < burst) {
this.count++;
this.lastTime = Date.now();
return false;
}
// Cooled down, allow and clear buffer
if (this.lastTime < Date.now() - cooldown*1000) {
this.count = 0;
this.lastTime = Date.now();
return false;
}
var diff = Date.now() - this.lastTime;
if (diff < 1000/sustained)
return true;
this.lastTime = Date.now();
return false;
}
};
}
};