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 redis#1778
  • Loading branch information
annervisser committed Jun 27, 2023
1 parent 9c17550 commit 231504c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/cluster/index.ts
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
19 changes: 14 additions & 5 deletions lib/cluster/util.ts
Expand Up @@ -10,6 +10,7 @@ export interface RedisOptions {
host: string;
username?: string;
password?: string;
nodeId?: string;
[key: string]: any;
}

Expand All @@ -25,17 +26,25 @@ export interface GroupedSrvRecords {
export function getNodeKey(node: RedisOptions): NodeKey {
node.port = node.port || 6379;
node.host = node.host || "127.0.0.1";
return node.host + ":" + node.port;
node.nodeId = node.nodeId || '0'
return node.host + ":" + node.port + ":" + node.nodeId;
}

export function nodeKeyToRedisOptions(nodeKey: NodeKey): RedisOptions {
const portIndex = nodeKey.lastIndexOf(":");
if (portIndex === -1) {
const idIndex = nodeKey.lastIndexOf(":");
if (idIndex === -1) {
throw new Error(`Invalid node key ${nodeKey}`);
}

const parts = nodeKey.split(':');
if (parts.length < 3) {
throw new Error(`Invalid node key ${nodeKey}`);
}

return {
host: nodeKey.slice(0, portIndex),
port: Number(nodeKey.slice(portIndex + 1)),
host: parts.slice(0, -2).join(':'),
port: Number(parts[parts.length - 2]),
nodeId: parts[parts.length - 1],
};
}

Expand Down

0 comments on commit 231504c

Please sign in to comment.