Skip to content

Commit

Permalink
Include nodeId in key in cluster connectionpool, correctly refreshing…
Browse files Browse the repository at this point in the history
… connections when nodeId changes

Fixes #1778
  • Loading branch information
annervisser committed Jun 27, 2023
1 parent 9c17550 commit ef7bb4a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/cluster/ConnectionPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class ConnectionPool extends EventEmitter {
* Find or create a connection to the node
*/
findOrCreate(node: RedisOptions, readOnly = false): Redis {
const key = getNodeKey(node);
const key = this.getNodeKey(node);
readOnly = Boolean(readOnly);

if (this.specifiedOptions[key]) {
Expand Down Expand Up @@ -113,7 +113,7 @@ export default class ConnectionPool extends EventEmitter {
debug("Reset with %O", nodes);
const newNodes = {};
nodes.forEach((node) => {
const key = getNodeKey(node);
const key = this.getNodeKey(node);

// Don't override the existing (master) node
// when the current one is slave.
Expand Down Expand Up @@ -147,4 +147,8 @@ export default class ConnectionPool extends EventEmitter {
delete nodes.master[key];
delete nodes.slave[key];
}

private getNodeKey(options: RedisOptions) {
return getNodeKey(options) + ':' + options.nodeId;
}
}
1 change: 1 addition & 0 deletions lib/cluster/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,7 @@ class Cluster extends Commander {
port: items[j][1],
});
node.readOnly = j !== 2;
node.nodeId = items[j][2];
nodes.push(node);
keys.push(node.host + ":" + node.port);
}
Expand Down
1 change: 1 addition & 0 deletions lib/cluster/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface RedisOptions {
host: string;
username?: string;
password?: string;
nodeId?: string;
[key: string]: any;
}

Expand Down

0 comments on commit ef7bb4a

Please sign in to comment.