Skip to content

Commit

Permalink
Back off reconnect delay on failures
Browse files Browse the repository at this point in the history
  • Loading branch information
jjlawren committed Nov 27, 2019
1 parent c60761d commit 91dbf2a
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions plexwebsocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,13 @@ def _get_uri(plex_server):
async def listen(self):
"""Open a persistent websocket connection and act on events."""
self._active = True
failed_attempts = 0
while self._active:
try:
async with self.session.ws_connect(self.uri, heartbeat=15, ssl=self._ssl) as ws_client:
async with self.session.ws_connect(
self.uri, heartbeat=15, ssl=self._ssl
) as ws_client:
failed_attempts = 0
self._current_task = asyncio.Task.current_task()
_LOGGER.debug("Websocket connected")
self.callback()
Expand All @@ -78,8 +82,18 @@ async def listen(self):
self.callback()

except aiohttp.client_exceptions.ClientConnectorError as e:
_LOGGER.error("Websocket connection refused: %s", e)
await asyncio.sleep(10)
if failed_attempts > 4:
retry_delay = 300
elif failed_attempts > 0:
retry_delay = 2 ** (failed_attempts - 1) * 30
else:
retry_delay = 10
failed_attempts += 1

_LOGGER.error(
"Websocket connection refused, retrying in %ss: %s", retry_delay, e
)
await asyncio.sleep(retry_delay)
else:
_LOGGER.debug("Websocket disconnected")
if self._active:
Expand Down

0 comments on commit 91dbf2a

Please sign in to comment.