Skip to content

Commit

Permalink
fix: add second arg to "node error" to know which node failed (#793)
Browse files Browse the repository at this point in the history
Fixes: #774
  • Loading branch information
luin authored Feb 3, 2019
1 parent 85b5326 commit 6049f6c
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ reconnecting | emits after `close` when a reconnection will be made. The argumen
end | emits after `close` when no more reconnections will be made.
+node | emits when a new node is connected.
-node | emits when a node is disconnected.
node error | emits when an error occurs when connecting to a node
node error | emits when an error occurs when connecting to a node. The second argument indicates the address of the node.

### Password
Setting the `password` option to access password-protected clusters:
Expand Down
9 changes: 5 additions & 4 deletions lib/cluster/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ class Cluster extends EventEmitter {
this.connectionPool.on('drain', () => {
this.setStatus('close')
})
this.connectionPool.on('nodeError', (error) => {
this.emit('node error', error)
this.connectionPool.on('nodeError', (error, key) => {
this.emit('node error', error, key)
})

this.subscriber = new ClusterSubscriber(this.connectionPool, this)
Expand Down Expand Up @@ -374,7 +374,8 @@ class Cluster extends EventEmitter {
return wrapper(error)
}
const node = nodes[index]
debug('getting slot cache from %s:%s', node.options.host, node.options.port)
const key = `${node.options.host}:${node.options.port}`
debug('getting slot cache from %s', key)
_this.getInfoFromNode(node, function (err) {
switch (_this.status) {
case 'close':
Expand All @@ -384,7 +385,7 @@ class Cluster extends EventEmitter {
return wrapper(new Error('Cluster is disconnecting.'))
}
if (err) {
_this.emit('node error', err)
_this.emit('node error', err, key)
lastNodeError = err
tryNode(index + 1)
} else {
Expand Down
3 changes: 2 additions & 1 deletion test/functional/cluster/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,9 @@ describe('cluster:connect', function () {
}
});

cluster.once('node error', function (err) {
cluster.once('node error', function (err, key) {
expect(err.message).to.eql(errorMessage);
expect(['127.0.0.1:30001', '127.0.0.1:30002']).to.include(key)
checkDone();
});

Expand Down
1 change: 0 additions & 1 deletion test/mocha.opts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
--reporter spec
--recursive
--growl

0 comments on commit 6049f6c

Please sign in to comment.