Skip to content

Commit 231504c

Browse files
committed
Include nodeId in key in cluster connectionpool, correctly refreshing connections when nodeId changes
Fixes #1778
1 parent 9c17550 commit 231504c

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

lib/cluster/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,7 @@ class Cluster extends Commander {
864864
port: items[j][1],
865865
});
866866
node.readOnly = j !== 2;
867+
node.nodeId = items[j][2];
867868
nodes.push(node);
868869
keys.push(node.host + ":" + node.port);
869870
}

lib/cluster/util.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface RedisOptions {
1010
host: string;
1111
username?: string;
1212
password?: string;
13+
nodeId?: string;
1314
[key: string]: any;
1415
}
1516

@@ -25,17 +26,25 @@ export interface GroupedSrvRecords {
2526
export function getNodeKey(node: RedisOptions): NodeKey {
2627
node.port = node.port || 6379;
2728
node.host = node.host || "127.0.0.1";
28-
return node.host + ":" + node.port;
29+
node.nodeId = node.nodeId || '0'
30+
return node.host + ":" + node.port + ":" + node.nodeId;
2931
}
3032

3133
export function nodeKeyToRedisOptions(nodeKey: NodeKey): RedisOptions {
32-
const portIndex = nodeKey.lastIndexOf(":");
33-
if (portIndex === -1) {
34+
const idIndex = nodeKey.lastIndexOf(":");
35+
if (idIndex === -1) {
3436
throw new Error(`Invalid node key ${nodeKey}`);
3537
}
38+
39+
const parts = nodeKey.split(':');
40+
if (parts.length < 3) {
41+
throw new Error(`Invalid node key ${nodeKey}`);
42+
}
43+
3644
return {
37-
host: nodeKey.slice(0, portIndex),
38-
port: Number(nodeKey.slice(portIndex + 1)),
45+
host: parts.slice(0, -2).join(':'),
46+
port: Number(parts[parts.length - 2]),
47+
nodeId: parts[parts.length - 1],
3948
};
4049
}
4150

0 commit comments

Comments
 (0)