Skip to content

Commit

Permalink
fix(package): resolve hanging retry connection timeout by introducing…
Browse files Browse the repository at this point in the history
… cancelable timeout
  • Loading branch information
CS-BTurner committed Aug 19, 2020
1 parent 6b512e8 commit e37dd1a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
25 changes: 17 additions & 8 deletions src/AmqpConnectionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ export default class AmqpConnectionManager extends EventEmitter {
if(this._closed) { return Promise.resolve(); }
this._closed = true;

if(this._cancelRetriesHandler) {
this._cancelRetriesHandler();
this._cancelRetriesHandler = null;
}

return Promise.resolve(this._connectPromise).then(() => {
return Promise.all(this._channels.map(channel => channel.close()))
.catch(function() {
Expand Down Expand Up @@ -175,10 +180,14 @@ export default class AmqpConnectionManager extends EventEmitter {
this._currentConnection = null;
this.emit('disconnect', { err });

wait(this.reconnectTimeInSeconds * 1000)
.then(() => this._connect())
// `_connect()` should never throw.
.catch(neverThrows);
const handle = wait(this.reconnectTimeInSeconds * 1000);
this._cancelRetriesHandler = handle.cancel;

handle
.promise()
.then(() => this._connect())
// `_connect()` should never throw.
.catch(neverThrows);
});

this._connectPromise = null;
Expand All @@ -195,10 +204,10 @@ export default class AmqpConnectionManager extends EventEmitter {
this._connectPromise = null;

// TODO: Probably want to try right away here, especially if there are multiple brokers to try...
return wait(this.reconnectTimeInSeconds * 1000)
.then(() => {
return this._connect();
});
const handle = wait(this.reconnectTimeInSeconds * 1000);
this._cancelRetriesHandler = handle.cancel;

return handle.promise().then(() => this._connect());
});

return this._connectPromise;
Expand Down
13 changes: 10 additions & 3 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
export function wait(timeInMs) {
return new Promise(function(resolve) {
return setTimeout(resolve, timeInMs);
let timeoutHandle;

const promise = () => {
return new Promise(function (resolve) {
timeoutHandle = setTimeout(resolve, timeInMs);
return timeoutHandle;
});
}
};

return { promise, cancel: () => clearTimeout(timeoutHandle) };
}

0 comments on commit e37dd1a

Please sign in to comment.