Skip to content

Commit

Permalink
Add incremental backoff (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsayre authored Jan 11, 2025
1 parent 1938cb0 commit dffd597
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions pyheos/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
CLI_PORT: Final = 1255
SEPARATOR: Final = "\r\n"
SEPARATOR_BYTES: Final = SEPARATOR.encode()
MAX_RECONNECT_DELAY = 600

_LOGGER: Final = logging.getLogger(__name__)

Expand Down Expand Up @@ -317,20 +318,25 @@ async def _heart_beat_handler(self) -> None:

async def _attempt_reconnect(self) -> None:
"""Attempt to reconnect after disconnection from error."""
self._state = ConnectionState.RECONNECTING
attempts = 0
unlimited_attempts = self._reconnect_max_attempts == 0
self._state = ConnectionState.RECONNECTING
delay = min(self._reconnect_delay, MAX_RECONNECT_DELAY)
while (attempts < self._reconnect_max_attempts) or unlimited_attempts:
try:
await asyncio.sleep(self._reconnect_delay)
_LOGGER.debug(
"Attempting to reconnect to %s in %s seconds", self._host, delay
)
await asyncio.sleep(delay)
await self.connect()
except HeosError as err:
attempts += 1
delay = min(delay * 2, MAX_RECONNECT_DELAY)
_LOGGER.debug(
f"Failed reconnect attempt {attempts} to {self._host}: {err}"
"Failed reconnect attempt %s to %s: %s", attempts, self._host, err
)
else:
return
return # This never actually hits as the task is cancelled when the connection is established, but it's here for completeness.

async def _on_connected(self) -> None:
"""Handle when the connection is established."""
Expand Down

0 comments on commit dffd597

Please sign in to comment.