From ad01144af21291fc229db5459d486ee5bc067d4b Mon Sep 17 00:00:00 2001 From: david hodgetts Date: Wed, 19 Jul 2023 15:11:17 +0200 Subject: [PATCH 1/2] fixes Manager failing to properly remove event listeners from Connection --- src/manager.ts | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/manager.ts b/src/manager.ts index a8c4dbc..c8a9315 100644 --- a/src/manager.ts +++ b/src/manager.ts @@ -20,12 +20,21 @@ export class Manager extends EventEmitter { private timeoutConnectRetries: number; private retryTimeout?: ReturnType = undefined; + private connectionCallbacks: Map void> = new Map void> + constructor(options: LogstashTransportOptions, connection: IConnection) { super(); this.options = options; this.connection = connection; this.logQueue = new Array(); + this.connectionCallbacks.set(ConnectionEvents.Connected, this.onConnected.bind(this)); + this.connectionCallbacks.set(ConnectionEvents.Closed, this.onConnectionClosed.bind(this)); + this.connectionCallbacks.set(ConnectionEvents.ClosedByServer, this.onConnectionError.bind(this)); + this.connectionCallbacks.set(ConnectionEvents.Error, this.onConnectionError.bind(this)); + this.connectionCallbacks.set(ConnectionEvents.Timeout, this.onConnectionError.bind(this)); + this.connectionCallbacks.set(ConnectionEvents.Drain, this.flush.bind(this)); + // Connection retry attributes this.retries = 0; this.maxConnectRetries = options?.max_connect_retries ?? 4; @@ -33,21 +42,21 @@ export class Manager extends EventEmitter { } private addEventListeners() { - this.connection.once(ConnectionEvents.Connected, this.onConnected.bind(this)); - this.connection.once(ConnectionEvents.Closed, this.onConnectionClosed.bind(this)); - this.connection.once(ConnectionEvents.ClosedByServer, this.onConnectionError.bind(this)); - this.connection.once(ConnectionEvents.Error, this.onConnectionError.bind(this)); - this.connection.once(ConnectionEvents.Timeout, this.onConnectionError.bind(this)); - this.connection.on(ConnectionEvents.Drain, this.flush.bind(this)); + this.connection.once(ConnectionEvents.Connected, this.connectionCallbacks.get(ConnectionEvents.Connected)!); + this.connection.once(ConnectionEvents.Closed, this.connectionCallbacks.get(ConnectionEvents.Closed)!); + this.connection.once(ConnectionEvents.ClosedByServer, this.connectionCallbacks.get(ConnectionEvents.ClosedByServer)!); + this.connection.once(ConnectionEvents.Error, this.connectionCallbacks.get(ConnectionEvents.Error)!); + this.connection.once(ConnectionEvents.Timeout, this.connectionCallbacks.get(ConnectionEvents.Timeout)!); + this.connection.on(ConnectionEvents.Drain, this.connectionCallbacks.get(ConnectionEvents.Drain)!); } private removeEventListeners() { - this.connection.off(ConnectionEvents.Connected, this.onConnected.bind(this)); - this.connection.off(ConnectionEvents.Closed, this.onConnectionClosed.bind(this)); - this.connection.off(ConnectionEvents.ClosedByServer, this.onConnectionError.bind(this)); - this.connection.off(ConnectionEvents.Error, this.onConnectionError.bind(this)); - this.connection.off(ConnectionEvents.Timeout, this.onConnectionError.bind(this)); - this.connection.off(ConnectionEvents.Drain, this.flush.bind(this)); + this.connection.off(ConnectionEvents.Connected, this.connectionCallbacks.get(ConnectionEvents.Connected)!); + this.connection.off(ConnectionEvents.Closed, this.connectionCallbacks.get(ConnectionEvents.Closed)!); + this.connection.off(ConnectionEvents.ClosedByServer, this.connectionCallbacks.get(ConnectionEvents.ClosedByServer)!); + this.connection.off(ConnectionEvents.Error, this.connectionCallbacks.get(ConnectionEvents.Error)!); + this.connection.off(ConnectionEvents.Timeout, this.connectionCallbacks.get(ConnectionEvents.Timeout)!); + this.connection.off(ConnectionEvents.Drain, this.connectionCallbacks.get(ConnectionEvents.Drain)!); } private onConnected() { From 10a91f7e08f2089ecf26653a6a937d610e829e1d Mon Sep 17 00:00:00 2001 From: david hodgetts Date: Wed, 19 Jul 2023 15:47:51 +0200 Subject: [PATCH 2/2] lib rebuilt --- lib/manager.js | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/lib/manager.js b/lib/manager.js index 4eca35e..0180feb 100644 --- a/lib/manager.js +++ b/lib/manager.js @@ -19,9 +19,16 @@ class Manager extends _events.EventEmitter { _defineProperty(this, "maxConnectRetries", void 0); _defineProperty(this, "timeoutConnectRetries", void 0); _defineProperty(this, "retryTimeout", undefined); + _defineProperty(this, "connectionCallbacks", new Map()); this.options = options; this.connection = connection; this.logQueue = new Array(); + this.connectionCallbacks.set(_connection.ConnectionEvents.Connected, this.onConnected.bind(this)); + this.connectionCallbacks.set(_connection.ConnectionEvents.Closed, this.onConnectionClosed.bind(this)); + this.connectionCallbacks.set(_connection.ConnectionEvents.ClosedByServer, this.onConnectionError.bind(this)); + this.connectionCallbacks.set(_connection.ConnectionEvents.Error, this.onConnectionError.bind(this)); + this.connectionCallbacks.set(_connection.ConnectionEvents.Timeout, this.onConnectionError.bind(this)); + this.connectionCallbacks.set(_connection.ConnectionEvents.Drain, this.flush.bind(this)); // Connection retry attributes this.retries = 0; @@ -29,20 +36,20 @@ class Manager extends _events.EventEmitter { this.timeoutConnectRetries = (_options$timeout_conn = options === null || options === void 0 ? void 0 : options.timeout_connect_retries) !== null && _options$timeout_conn !== void 0 ? _options$timeout_conn : 100; } addEventListeners() { - this.connection.once(_connection.ConnectionEvents.Connected, this.onConnected.bind(this)); - this.connection.once(_connection.ConnectionEvents.Closed, this.onConnectionClosed.bind(this)); - this.connection.once(_connection.ConnectionEvents.ClosedByServer, this.onConnectionError.bind(this)); - this.connection.once(_connection.ConnectionEvents.Error, this.onConnectionError.bind(this)); - this.connection.once(_connection.ConnectionEvents.Timeout, this.onConnectionError.bind(this)); - this.connection.on(_connection.ConnectionEvents.Drain, this.flush.bind(this)); + this.connection.once(_connection.ConnectionEvents.Connected, this.connectionCallbacks.get(_connection.ConnectionEvents.Connected)); + this.connection.once(_connection.ConnectionEvents.Closed, this.connectionCallbacks.get(_connection.ConnectionEvents.Closed)); + this.connection.once(_connection.ConnectionEvents.ClosedByServer, this.connectionCallbacks.get(_connection.ConnectionEvents.ClosedByServer)); + this.connection.once(_connection.ConnectionEvents.Error, this.connectionCallbacks.get(_connection.ConnectionEvents.Error)); + this.connection.once(_connection.ConnectionEvents.Timeout, this.connectionCallbacks.get(_connection.ConnectionEvents.Timeout)); + this.connection.on(_connection.ConnectionEvents.Drain, this.connectionCallbacks.get(_connection.ConnectionEvents.Drain)); } removeEventListeners() { - this.connection.off(_connection.ConnectionEvents.Connected, this.onConnected.bind(this)); - this.connection.off(_connection.ConnectionEvents.Closed, this.onConnectionClosed.bind(this)); - this.connection.off(_connection.ConnectionEvents.ClosedByServer, this.onConnectionError.bind(this)); - this.connection.off(_connection.ConnectionEvents.Error, this.onConnectionError.bind(this)); - this.connection.off(_connection.ConnectionEvents.Timeout, this.onConnectionError.bind(this)); - this.connection.off(_connection.ConnectionEvents.Drain, this.flush.bind(this)); + this.connection.off(_connection.ConnectionEvents.Connected, this.connectionCallbacks.get(_connection.ConnectionEvents.Connected)); + this.connection.off(_connection.ConnectionEvents.Closed, this.connectionCallbacks.get(_connection.ConnectionEvents.Closed)); + this.connection.off(_connection.ConnectionEvents.ClosedByServer, this.connectionCallbacks.get(_connection.ConnectionEvents.ClosedByServer)); + this.connection.off(_connection.ConnectionEvents.Error, this.connectionCallbacks.get(_connection.ConnectionEvents.Error)); + this.connection.off(_connection.ConnectionEvents.Timeout, this.connectionCallbacks.get(_connection.ConnectionEvents.Timeout)); + this.connection.off(_connection.ConnectionEvents.Drain, this.connectionCallbacks.get(_connection.ConnectionEvents.Drain)); } onConnected() { this.emit('connected');