Fix round robin load balancing

This commit is contained in:
sukhman
2025-09-23 08:58:12 +05:30
parent d268393812
commit 3363f57338

View File

@ -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