Skip to content

Commit

Permalink
fixes Manager failing to properly remove event listeners from Connect…
Browse files Browse the repository at this point in the history
…ion (#80)

* fixes Manager failing to properly remove event listeners from Connection
  • Loading branch information
david-hodgetts committed Jul 28, 2023
1 parent 15f3519 commit 0b602cc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
31 changes: 19 additions & 12 deletions lib/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,37 @@ 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;
this.maxConnectRetries = (_options$max_connect_ = options === null || options === void 0 ? void 0 : options.max_connect_retries) !== null && _options$max_connect_ !== void 0 ? _options$max_connect_ : 4;
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');
Expand Down
33 changes: 21 additions & 12 deletions src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,43 @@ export class Manager extends EventEmitter {
private timeoutConnectRetries: number;
private retryTimeout?: ReturnType<typeof setTimeout> = undefined;

private connectionCallbacks: Map<ConnectionEvents, (e:Error) => void> = new Map<ConnectionEvents, () => 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;
this.timeoutConnectRetries = options?.timeout_connect_retries ?? 100;
}

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() {
Expand Down

0 comments on commit 0b602cc

Please sign in to comment.