Skip to content

Commit 097938a

Browse files
committed
Improve async cluster connection initialization
1 parent 9aeec4c commit 097938a

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

redis/asyncio/cluster.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,37 +1317,35 @@ async def initialize(self) -> None:
13171317
port = int(primary_node[1])
13181318
host, port = self.remap_host_port(host, port)
13191319

1320+
nodes_for_slot = []
1321+
13201322
target_node = tmp_nodes_cache.get(get_node_name(host, port))
13211323
if not target_node:
13221324
target_node = ClusterNode(
13231325
host, port, PRIMARY, **self.connection_kwargs
13241326
)
13251327
# add this node to the nodes cache
13261328
tmp_nodes_cache[target_node.name] = target_node
1329+
nodes_for_slot.append(target_node)
1330+
1331+
replica_nodes = [slot[j] for j in range(3, len(slot))]
1332+
for replica_node in replica_nodes:
1333+
host = replica_node[0]
1334+
port = replica_node[1]
1335+
host, port = self.remap_host_port(host, port)
1336+
1337+
target_replica_node = tmp_nodes_cache.get(get_node_name(host, port))
1338+
if not target_replica_node:
1339+
target_replica_node = ClusterNode(
1340+
host, port, REPLICA, **self.connection_kwargs
1341+
)
1342+
nodes_for_slot.append(target_replica_node)
1343+
# add this node to the nodes cache
1344+
tmp_nodes_cache[target_replica_node.name] = target_replica_node
13271345

13281346
for i in range(int(slot[0]), int(slot[1]) + 1):
13291347
if i not in tmp_slots:
1330-
tmp_slots[i] = []
1331-
tmp_slots[i].append(target_node)
1332-
replica_nodes = [slot[j] for j in range(3, len(slot))]
1333-
1334-
for replica_node in replica_nodes:
1335-
host = replica_node[0]
1336-
port = replica_node[1]
1337-
host, port = self.remap_host_port(host, port)
1338-
1339-
target_replica_node = tmp_nodes_cache.get(
1340-
get_node_name(host, port)
1341-
)
1342-
if not target_replica_node:
1343-
target_replica_node = ClusterNode(
1344-
host, port, REPLICA, **self.connection_kwargs
1345-
)
1346-
tmp_slots[i].append(target_replica_node)
1347-
# add this node to the nodes cache
1348-
tmp_nodes_cache[target_replica_node.name] = (
1349-
target_replica_node
1350-
)
1348+
tmp_slots[i] = nodes_for_slot
13511349
else:
13521350
# Validate that 2 nodes want to use the same slot cache
13531351
# setup

0 commit comments

Comments
 (0)