diff --git a/lib/irc/ClientPool.js b/lib/irc/ClientPool.js index 7314b2e32..923e7c84e 100644 --- a/lib/irc/ClientPool.js +++ b/lib/irc/ClientPool.js @@ -28,6 +28,13 @@ function ClientPool(ircBridge) { // } // } } + // map of numbers of connected clients on each server + // Counting these is quite expensive because we have to + // ignore entries where the value is undefined. Instead, + // just keep track of how many we have. + this._virtualClientCounts = { + // server_domain: number + }; } ClientPool.prototype.killAllClients = function() { @@ -77,6 +84,7 @@ ClientPool.prototype.createIrcClient = function(ircClientConfig, matrixUser, isB nicks: Object.create(null), userIds: Object.create(null) }; + this._virtualClientCounts[server.domain] = 0; } if (isBot) { this._botClients[server.domain] = bridgedClient; @@ -93,6 +101,7 @@ ClientPool.prototype.createIrcClient = function(ircClientConfig, matrixUser, isB // connected yet, else we could spawn 2 clients for a single user if this // function is called quickly. this._virtualClients[server.domain].userIds[bridgedClient.userId] = bridgedClient; + this._virtualClientCounts[server.domain] = this._virtualClientCounts[server.domain] + 1; // Does this server have a max clients limit? If so, check if the limit is // reached and start cycling based on oldest time. @@ -198,14 +207,7 @@ ClientPool.prototype._checkClientLimit = function(server) { ClientPool.prototype._getNumberOfConnections = function(server) { if (!server || !this._virtualClients[server.domain]) { return 0; } - - var connectedNickMap = this._virtualClients[server.domain].nicks; - - var numConnectedNicks = Object.keys(connectedNickMap).filter(function(nick) { - return Boolean(connectedNickMap[nick]); // remove 'undefined' values - }).length; - - return numConnectedNicks; + return this._virtualClientCounts[server.domain]; }; ClientPool.prototype.countTotalConnections = function() { @@ -227,6 +229,7 @@ ClientPool.prototype._removeBridgedClient = function(bridgedClient) { var server = bridgedClient.server; this._virtualClients[server.domain].userIds[bridgedClient.userId] = undefined; this._virtualClients[server.domain].nicks[bridgedClient.nick] = undefined; + this._virtualClientCounts[server.domain] = this._virtualClientCounts[server.domain] - 1; if (bridgedClient.isBot) { this._botClients[server.domain] = undefined;