From 7f1dcd208d1d9b52af7f10499f7297f375dc6c16 Mon Sep 17 00:00:00 2001 From: David Baker Date: Fri, 8 Dec 2017 12:20:37 +0000 Subject: [PATCH 1/2] Keep track of number of connections in an integer Rather than filtering a list each time. This is pretty slow. --- lib/irc/ClientPool.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/irc/ClientPool.js b/lib/irc/ClientPool.js index 7314b2e32..82b948db3 100644 --- a/lib/irc/ClientPool.js +++ b/lib/irc/ClientPool.js @@ -28,6 +28,10 @@ function ClientPool(ircBridge) { // } // } } + // map of numbers of connected clients on each server + this._virtualClientCounts = { + // server_domain: number + }; } ClientPool.prototype.killAllClients = function() { @@ -77,6 +81,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 +98,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 +204,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 +226,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; From 8d4c6f1139054dc8485e9bfb57dcbf77bcbcdd8a Mon Sep 17 00:00:00 2001 From: David Baker Date: Fri, 8 Dec 2017 18:12:36 +0000 Subject: [PATCH 2/2] add comment --- lib/irc/ClientPool.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/irc/ClientPool.js b/lib/irc/ClientPool.js index 82b948db3..923e7c84e 100644 --- a/lib/irc/ClientPool.js +++ b/lib/irc/ClientPool.js @@ -29,6 +29,9 @@ 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 };