Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep track of number of connections in an integer #530

Merged
merged 2 commits into from
Dec 8, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions lib/irc/ClientPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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() {
Expand All @@ -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;
Expand Down