From 3363f57338efbb7c4e0f3ae2c96388385a7cc8e7 Mon Sep 17 00:00:00 2001 From: sukhman Date: Tue, 23 Sep 2025 08:58:12 +0530 Subject: [PATCH] Fix round robin load balancing --- libp2p/relay/circuit_v2/transport.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/libp2p/relay/circuit_v2/transport.py b/libp2p/relay/circuit_v2/transport.py index 2ba0aa90..44e4e22d 100644 --- a/libp2p/relay/circuit_v2/transport.py +++ b/libp2p/relay/circuit_v2/transport.py @@ -90,6 +90,7 @@ class CircuitV2Transport(ITransport): discovery_interval=config.discovery_interval, max_relays=config.max_relays, ) + self.relay_counter = 0 # for round robin load balancing async def dial( self, @@ -231,13 +232,13 @@ class CircuitV2Transport(ITransport): other_relays.append(relay_id) # Return first available relay with reservation, or fallback to others + self.relay_counter += 1 if relays_with_reservations: return relays_with_reservations[ - attempts % len(relays_with_reservations) + (self.relay_counter-1) % len(relays_with_reservations) ] elif other_relays: - return other_relays[attempts % len(other_relays)] - + return other_relays[(self.relay_counter-1) % len(other_relays)] await trio.sleep(1) attempts += 1