From a0c2495c4296a1d5d35c0332bdb4098b6e6e3615 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Thu, 31 Oct 2019 11:24:19 +0200 Subject: [PATCH] Improvements to network connections on startup --- src/client.js | 38 +++++++++++++++++++++++----------- test/tests/customhighlights.js | 12 ++++++++--- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/client.js b/src/client.js index b6316dff..dc19685c 100644 --- a/src/client.js +++ b/src/client.js @@ -61,7 +61,6 @@ function Client(manager, name, config = {}) { }); const client = this; - let delay = 0; if (!Helper.config.public && client.config.log) { if (Helper.config.messageStorage.includes("sqlite")) { @@ -77,12 +76,11 @@ function Client(manager, name, config = {}) { } } - (client.config.networks || []).forEach((n) => { - setTimeout(function() { - client.connect(n); - }, delay); - delay += 1000; - }); + (client.config.networks || []).forEach((network) => client.connect(network, true)); + + // Networks are stored directly in the client object + // We don't need to keep it in the config object + delete client.config.networks; if (typeof client.config.sessions !== "object") { client.config.sessions = {}; @@ -106,6 +104,21 @@ function Client(manager, name, config = {}) { if (client.name) { log.info(`User ${colors.bold(client.name)} loaded`); + + // Networks are created instantly, but to reduce server load on startup + // We randomize the IRC connections and channel log loading + let delay = manager.clients.length * 500; + client.networks.forEach((network) => { + setTimeout(() => { + network.channels.forEach((channel) => channel.loadMessages(client, network)); + + if (!network.userDisconnected && network.irc) { + network.irc.connect(); + } + }, delay); + + delay += 1000 + Math.floor(Math.random() * 1000); + }); } } @@ -143,7 +156,7 @@ Client.prototype.find = function(channelId) { return false; }; -Client.prototype.connect = function(args) { +Client.prototype.connect = function(args, isStartup = false) { const client = this; let channels = []; @@ -240,13 +253,14 @@ Client.prototype.connect = function(args) { }), true ); - } else { + } else if (!isStartup) { network.irc.connect(); } - client.save(); - - channels.forEach((channel) => channel.loadMessages(client, network)); + if (!isStartup) { + client.save(); + channels.forEach((channel) => channel.loadMessages(client, network)); + } }; Client.prototype.generateToken = function(callback) { diff --git a/test/tests/customhighlights.js b/test/tests/customhighlights.js index 43e161ff..9d4d37d8 100644 --- a/test/tests/customhighlights.js +++ b/test/tests/customhighlights.js @@ -10,9 +10,15 @@ describe("Custom highlights", function() { let userLoadedLog = ""; stub(log, "info").callsFake(TestUtil.sanitizeLog((str) => (userLoadedLog += str))); - const client = new Client({}, "test", { - clientSettings: {highlights: "foo, @all, sp ace , 고"}, - }); + const client = new Client( + { + clients: [], + }, + "test", + { + clientSettings: {highlights: "foo, @all, sp ace , 고"}, + } + ); log.info.restore(); expect(userLoadedLog).to.equal("User test loaded\n");