From 1de8b76303c811c796d2e3233cef79c97654889a Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Fri, 19 Feb 2021 14:28:19 +0100 Subject: [PATCH] Emit resume instead of connect on resumed connections Raise "resume" instead of "connect" when a connection is re-established after a connection drop. This prevents logic meant to resynchronize state after a full reconnection from being invoked whenever the connection was temporarily dropped. --- docs/writing-plugins.md | 2 +- packages/lib/link/connectors.js | 2 +- packages/lib/plugin.js | 28 ++++++++++++++++++---------- packages/master/master.js | 4 ++-- packages/slave/slave.js | 2 +- test/integration/link.js | 6 +++--- 6 files changed, 26 insertions(+), 18 deletions(-) diff --git a/docs/writing-plugins.md b/docs/writing-plugins.md index ae5ab831..5e007b35 100644 --- a/docs/writing-plugins.md +++ b/docs/writing-plugins.md @@ -621,7 +621,7 @@ until the connection is re-established. This means that if your plugin sends a lot of events or requests, they can end up being queued up in a buffer for a long time and sent out all at once. To avoid this you should be throtteling and/or stopping your requests/events after `drop` -has been raised, and continue back as normal when `connect` is raised. +has been raised, and continue back as normal when `resume` is raised. Collecting Statistics diff --git a/packages/lib/link/connectors.js b/packages/lib/link/connectors.js index ccf24904..f3881d26 100644 --- a/packages/lib/link/connectors.js +++ b/packages/lib/link/connectors.js @@ -404,7 +404,7 @@ class WebSocketClientConnector extends WebSocketBaseConnector { } this._startedReconnect = null; this._connected = true; - this.emit("connect"); + this.emit("resume"); } else if (type === "invalidate") { this._heartbeatInterval = null; diff --git a/packages/lib/plugin.js b/packages/lib/plugin.js index 8e9aaafa..ddaf50e0 100644 --- a/packages/lib/plugin.js +++ b/packages/lib/plugin.js @@ -148,13 +148,12 @@ class BaseInstancePlugin { /** * Called when an event on the master connection happens * - * The event param may be one of connect, drop and close and has the - * following meaning: + * The event param may be one of connect, drop, resume and close and has + * the following meaning: * * ##### connect * - * Invoked when a new connection to the master has been established, or - * the existing one that have previously been dropped is re-established. + * Invoked when a new connection to the master has been established. * * ##### drop * @@ -166,6 +165,11 @@ class BaseInstancePlugin { * memory on the slave and sent all in one go when the connection is * re-established again. * + * ##### resume + * + * Invoked when the connection that had previously dropped is + * re-established. + * * ##### close * * Invoked when the connection to the master has been closed. This @@ -173,7 +177,7 @@ class BaseInstancePlugin { * send any messages that goes to or via the master server after the * connection has been closed and before a new one is established. * - * @param {string} event - one of connect, drop, and close + * @param {string} event - one of connect, drop, resumeand close */ onMasterConnectionEvent(event) { } @@ -293,13 +297,12 @@ class BaseMasterPlugin { /** * Called when an event on a slave connection happens * - * The event param may be one of connect, drop and close and has the - * following meaning: + * The event param may be one of connect, drop, resume and close and has + * the following meaning: * * ##### connect * - * Invoked when a new connection to the slave has been established, or - * an existing one that had previously been dropped is re-established. + * Invoked when a new connection to the slave has been established. * * ##### drop * @@ -311,13 +314,18 @@ class BaseMasterPlugin { * memory on the master and sent all in one go when the connection is * re-established again. * + * ##### resume + * + * Invoked when a connection that had previously been dropped is + * re-established + * * ##### close * * Invoked when the connection to the slave has been closed. * * @param {module:master/master~SlaveConnection} connection - * The connection the event occured on. - * @param {string} event - one of connect, drop, and close + * @param {string} event - one of connect, drop, resume and close */ onSlaveConnectionEvent(connection, event) { } diff --git a/packages/master/master.js b/packages/master/master.js index 048a661b..e5a2a1aa 100755 --- a/packages/master/master.js +++ b/packages/master/master.js @@ -1168,7 +1168,7 @@ class SlaveConnection extends BaseConnection { } }); - for (let event of ["connect", "drop", "close"]) { + for (let event of ["connect", "drop", "resume", "close"]) { // eslint-disable-next-line no-loop-func this.connector.on(event, () => { for (let masterPlugin of masterPlugins.values()) { @@ -1397,7 +1397,7 @@ class WebSocketServerConnector extends libLink.WebSocketBaseConnector { for (let message of this._sendBuffer) { this._socket.send(JSON.stringify(message)); } - this.emit("connect"); + this.emit("resume"); } setTimeout(timeout) { diff --git a/packages/slave/slave.js b/packages/slave/slave.js index e220d6b6..6c0dfc30 100755 --- a/packages/slave/slave.js +++ b/packages/slave/slave.js @@ -1028,7 +1028,7 @@ class Slave extends libLink.Link { } }); - for (let event of ["connect", "drop", "close"]) { + for (let event of ["connect", "drop", "resume", "close"]) { this.connector.on(event, () => { for (let instanceConnection of this.instanceConnections.values()) { libLink.messages.masterConnectionEvent.send(instanceConnection, { event }); diff --git a/test/integration/link.js b/test/integration/link.js index 731fef97..39848e16 100644 --- a/test/integration/link.js +++ b/test/integration/link.js @@ -34,17 +34,17 @@ describe("Integration of lib/link", function() { it("should reconnect on connection lost", async function() { controlConnector._socket.close(1008, "Test"); - await events.once(controlConnector, "connect"); + await events.once(controlConnector, "resume"); }); it("should reconnect on connection terminated", async function() { controlConnector._socket.terminate(); - await events.once(controlConnector, "connect"); + await events.once(controlConnector, "resume"); }); it("should reconnect on connection heartbeat timeout", async function() { controlConnector.stopHeartbeat(); - await events.once(controlConnector, "connect"); + await events.once(controlConnector, "resume"); }); it("should invalidate on reconnection with bad session token", async function() {