Refactor reconnection policy handling to use None for no retry instead of False

This commit is contained in:
Jahongir Qurbonov 2025-09-15 13:02:34 +05:00
parent 6e4ee71c32
commit 5fe17a17e2
No known key found for this signature in database
GPG Key ID: 256976CED13D5F2D
2 changed files with 7 additions and 10 deletions

View File

@ -10,16 +10,15 @@ class ReconnectionPolicy(ABC):
""" """
@abstractmethod @abstractmethod
def should_retry(self, attempts: int) -> bool | float: def should_retry(self, attempts: int) -> float | None:
""" """
Determines whether the client should retry the connection attempt. Determines whether the client should retry the connection attempt.
""" """
pass
class NoReconnect(ReconnectionPolicy): class NoReconnect(ReconnectionPolicy):
def should_retry(self, attempts: int) -> bool: def should_retry(self, attempts: int) -> float | None:
return False return None
class FixedReconnect(ReconnectionPolicy): class FixedReconnect(ReconnectionPolicy):
@ -29,8 +28,8 @@ class FixedReconnect(ReconnectionPolicy):
self.max_attempts = attempts self.max_attempts = attempts
self.delay = delay self.delay = delay
def should_retry(self, attempts: int) -> bool | float: def should_retry(self, attempts: int) -> float | None:
if attempts < self.max_attempts: if attempts < self.max_attempts:
return self.delay return self.delay
return False return None

View File

@ -310,10 +310,8 @@ class Sender:
delay = self._reconnection_policy.should_retry(attempts) delay = self._reconnection_policy.should_retry(attempts)
if delay: if delay is not None:
if delay is not True: await asyncio.sleep(delay)
await asyncio.sleep(delay)
continue
else: else:
self._logger.error( self._logger.error(
f"auto-reconnect failed {attempts} time(s); giving up" f"auto-reconnect failed {attempts} time(s); giving up"