Add throttling of usercount frames

This commit is contained in:
calzoneman 2016-06-18 00:32:50 -07:00
parent e4decbc34f
commit 056b2a48ea
4 changed files with 20 additions and 3 deletions

View File

@ -2,7 +2,7 @@
"author": "Calvin Montgomery", "author": "Calvin Montgomery",
"name": "CyTube", "name": "CyTube",
"description": "Online media synchronizer and chat", "description": "Online media synchronizer and chat",
"version": "3.17.0", "version": "3.17.1",
"repository": { "repository": {
"url": "http://github.com/calzoneman/sync" "url": "http://github.com/calzoneman/sync"
}, },
@ -27,6 +27,7 @@
"http-errors": "^1.3.1", "http-errors": "^1.3.1",
"jade": "^1.11.0", "jade": "^1.11.0",
"json-typecheck": "^0.1.3", "json-typecheck": "^0.1.3",
"lodash": "^4.13.1",
"morgan": "^1.6.1", "morgan": "^1.6.1",
"mysql": "^2.9.0", "mysql": "^2.9.0",
"nodemailer": "^1.4.0", "nodemailer": "^1.4.0",

View File

@ -11,6 +11,9 @@ import * as ChannelStore from '../channel-storage/channelstore';
import { ChannelStateSizeError } from '../errors'; import { ChannelStateSizeError } from '../errors';
import Promise from 'bluebird'; import Promise from 'bluebird';
import { EventEmitter } from 'events'; import { EventEmitter } from 'events';
import { throttle } from '../util/throttle';
const USERCOUNT_THROTTLE = 10000;
class ReferenceCounter { class ReferenceCounter {
constructor(channel) { constructor(channel) {
@ -81,6 +84,9 @@ function Channel(name) {
this.users = []; this.users = [];
this.refCounter = new ReferenceCounter(this); this.refCounter = new ReferenceCounter(this);
this.flags = 0; this.flags = 0;
this.broadcastUsercount = throttle(() => {
this.broadcastAll("usercount", this.users.length);
}, USERCOUNT_THROTTLE);
var self = this; var self = this;
db.channels.load(this, function (err) { db.channels.load(this, function (err) {
if (err && err !== "Channel is not registered") { if (err && err !== "Channel is not registered") {
@ -403,7 +409,7 @@ Channel.prototype.acceptUser = function (user) {
}); });
this.sendUserlist([user]); this.sendUserlist([user]);
this.sendUsercount(this.users); this.broadcastUsercount();
if (!this.is(Flags.C_REGISTERED)) { if (!this.is(Flags.C_REGISTERED)) {
user.socket.emit("channelNotRegistered"); user.socket.emit("channelNotRegistered");
} }
@ -434,7 +440,7 @@ Channel.prototype.partUser = function (user) {
Object.keys(this.modules).forEach(function (m) { Object.keys(this.modules).forEach(function (m) {
self.modules[m].onUserPart(user); self.modules[m].onUserPart(user);
}); });
this.sendUsercount(this.users); this.broadcastUsercount();
this.refCounter.unref("Channel::user"); this.refCounter.unref("Channel::user");
user.die(); user.die();

View File

@ -258,6 +258,7 @@ Server.prototype.unloadChannel = function (chan) {
} }
Logger.syslog.log("Unloaded channel " + chan.name); Logger.syslog.log("Unloaded channel " + chan.name);
chan.broadcastUsercount.cancel();
// Empty all outward references from the channel // Empty all outward references from the channel
var keys = Object.keys(chan); var keys = Object.keys(chan);
for (var i in keys) { for (var i in keys) {

9
src/util/throttle.js Normal file
View File

@ -0,0 +1,9 @@
import lo from 'lodash';
export function throttle(fn, timeout) {
return lo.debounce(fn, timeout, {
leading: true,
trailing: true,
maxWait: timeout
});
}