Skip to content

Commit

Permalink
Improve async cluster connection initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
gerzse committed Jul 8, 2024
1 parent 9aeec4c commit 097938a
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions redis/asyncio/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -1317,37 +1317,35 @@ async def initialize(self) -> None:
port = int(primary_node[1])
host, port = self.remap_host_port(host, port)

nodes_for_slot = []

target_node = tmp_nodes_cache.get(get_node_name(host, port))
if not target_node:
target_node = ClusterNode(
host, port, PRIMARY, **self.connection_kwargs
)
# add this node to the nodes cache
tmp_nodes_cache[target_node.name] = target_node
nodes_for_slot.append(target_node)

replica_nodes = [slot[j] for j in range(3, len(slot))]
for replica_node in replica_nodes:
host = replica_node[0]
port = replica_node[1]
host, port = self.remap_host_port(host, port)

target_replica_node = tmp_nodes_cache.get(get_node_name(host, port))
if not target_replica_node:
target_replica_node = ClusterNode(
host, port, REPLICA, **self.connection_kwargs
)
nodes_for_slot.append(target_replica_node)
# add this node to the nodes cache
tmp_nodes_cache[target_replica_node.name] = target_replica_node

for i in range(int(slot[0]), int(slot[1]) + 1):
if i not in tmp_slots:
tmp_slots[i] = []
tmp_slots[i].append(target_node)
replica_nodes = [slot[j] for j in range(3, len(slot))]

for replica_node in replica_nodes:
host = replica_node[0]
port = replica_node[1]
host, port = self.remap_host_port(host, port)

target_replica_node = tmp_nodes_cache.get(
get_node_name(host, port)
)
if not target_replica_node:
target_replica_node = ClusterNode(
host, port, REPLICA, **self.connection_kwargs
)
tmp_slots[i].append(target_replica_node)
# add this node to the nodes cache
tmp_nodes_cache[target_replica_node.name] = (
target_replica_node
)
tmp_slots[i] = nodes_for_slot
else:
# Validate that 2 nodes want to use the same slot cache
# setup
Expand Down

0 comments on commit 097938a

Please sign in to comment.