From dd7e5d2ee08d427f53e2356c39142e188c437ba3 Mon Sep 17 00:00:00 2001 From: Anner Visser Date: Tue, 27 Jun 2023 14:49:19 +0200 Subject: [PATCH] Include nodeId in key in cluster connectionpool, correctly refreshing connections when nodeId changes Fixes #1778 --- lib/cluster/index.ts | 1 + lib/cluster/util.ts | 14 +++++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/cluster/index.ts b/lib/cluster/index.ts index 8419b1a3..9a514c0e 100644 --- a/lib/cluster/index.ts +++ b/lib/cluster/index.ts @@ -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); } diff --git a/lib/cluster/util.ts b/lib/cluster/util.ts index 4b12b955..99dccbf8 100644 --- a/lib/cluster/util.ts +++ b/lib/cluster/util.ts @@ -10,6 +10,7 @@ export interface RedisOptions { host: string; username?: string; password?: string; + nodeId?: string; [key: string]: any; } @@ -25,17 +26,20 @@ 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 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], }; }