Skip to content

Commit 210916f

Browse files
authored
Adjust device client connection failure backoff and logging (#19)
- When the connection fails, immediately retry once and only backoff starting with the second attempt - Adjust logging for recoverable connection failures: log as info for error 1/reconnected, debug for error >1
1 parent a8a4005 commit 210916f

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

letpot/deviceclient.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ async def _connect(self) -> None:
212212
tls_insecure=False,
213213
websocket_path="/mqttwss",
214214
) as client:
215+
if connection_attempts >= 1:
216+
_LOGGER.info("Reconnected to MQTT broker")
217+
215218
self._client = client
216219
self._message_id = 0
217220
connection_attempts = 0
@@ -239,14 +242,16 @@ async def _connect(self) -> None:
239242
raise auth_exception from err
240243

241244
connection_attempts += 1
242-
reconnect_interval = min(connection_attempts * 15, 600)
243-
_LOGGER.error(
244-
"MQTT error, reconnecting in %i seconds: %s",
245-
reconnect_interval,
246-
err,
247-
)
248-
249-
await asyncio.sleep(reconnect_interval)
245+
if connection_attempts == 1:
246+
_LOGGER.info("MQTT connection error, reconnecting...: %s", err)
247+
else:
248+
reconnect_interval = min((connection_attempts - 1) * 15, 600)
249+
_LOGGER.debug(
250+
"MQTT connection error, retrying in %i seconds: %s",
251+
reconnect_interval,
252+
err,
253+
)
254+
await asyncio.sleep(reconnect_interval)
250255
finally:
251256
self._client = None
252257
if self._connected is not None:

0 commit comments

Comments
 (0)