Skip to content

Commit

Permalink
Merge pull request #79 from azzang/fix/too-many-connection-attempts
Browse files Browse the repository at this point in the history
fix: Prevent too many connection attempts on error
  • Loading branch information
jwalton authored Apr 1, 2019
2 parents a68f8b3 + 2760ce5 commit b377b89
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"chai-as-promised": "^7.1.1",
"chai-string": "^1.1.2",
"coveralls": "^3.0.0",
"cross-env": "^5.2.0",
"eslint": "^5.14.1",
"eslint-config-benbria": "^4.0.0",
"eslint-plugin-import": "^2.11.0",
Expand All @@ -47,9 +48,9 @@
"build": "babel -s -d lib src",
"clean": "rm -rf lib coverage",
"test": "npm run test:lint && npm run test:unittest",
"test:unittest": "NODE_ENV=test nyc mocha",
"test:unittest": "cross-env NODE_ENV=test nyc mocha",
"test:lint": "eslint src test",
"precommit:unittest": "BABEL_DISABLE_CACHE=1 NODE_ENV=test nyc mocha --reporter progress",
"precommit:unittest": "cross-env BABEL_DISABLE_CACHE=1 cross-env NODE_ENV=test nyc mocha --reporter progress",
"semantic-release": "semantic-release"
},
"husky": {
Expand Down
22 changes: 8 additions & 14 deletions src/AmqpConnectionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,21 +148,15 @@ export default class AmqpConnectionManager extends EventEmitter {

connection.on('unblocked', () => this.emit('unblocked'));

// Reconnect if the broker goes away.
connection.on('error', err =>
Promise.resolve()
.then(() =>this._currentConnection.close())
.catch(() => { /* Ignore */ })
.then(() => {
this._currentConnection = null;
this.emit('disconnect', { err });
return this._connect();
})
// `_connect()` should never throw.
.catch(neverThrows)
);
connection.on('error', (/* err */) => {
// if this event was emitted, then the connection was already closed,
// so no need to call #close here
// also, 'close' is emitted after 'error',
// so no need for work already done in 'close' handler
return Promise.resolve();
});

// Reconnect if the connection closes gracefully
// Reconnect if the connection closes
connection.on('close', err => {
this._currentConnection = null;
this.emit('disconnect', { err });
Expand Down
4 changes: 3 additions & 1 deletion test/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ export class FakeAmqp {
constructor() { this.reset(); }

kill() {
this.connection.emit('error', new Error("Died in a fire"));
const err = new Error("Died in a fire");
this.connection.emit('error', err);
this.connection.emit('close', err);
}

simulateRemoteClose() {
Expand Down

0 comments on commit b377b89

Please sign in to comment.