From 1b2f07dfa292afcb15817edc77bb32c3b71baf38 Mon Sep 17 00:00:00 2001 From: Nick Mills-Barrett Date: Mon, 13 Nov 2023 18:21:39 +0000 Subject: [PATCH] Add quick retries when connecting to Telegram (#941) * Add quick 5 retries when connecting to Telegram * Fix attempt initialisation Co-authored-by: Tulir Asokan * Log only when retrying Co-authored-by: Tulir Asokan --------- Co-authored-by: Tulir Asokan --- mautrix_telegram/abstract_user.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/mautrix_telegram/abstract_user.py b/mautrix_telegram/abstract_user.py index 7556b3cb..3930fc87 100644 --- a/mautrix_telegram/abstract_user.py +++ b/mautrix_telegram/abstract_user.py @@ -306,7 +306,16 @@ async def has_full_access(self, allow_bot: bool = False) -> bool: async def start(self, delete_unless_authenticated: bool = False) -> AbstractUser: if not self.client: await self._init_client() - await self.client.connect() + attempts = 1 + while True: + try: + await self.client.connect() + except Exception: + attempts += 1 + if attempts > 10: + raise + self.log.exception("Exception connecting to Telegram, retrying in 5s...") + await asyncio.sleep(5) self.log.debug(f"{'Bot' if self.is_relaybot else self.mxid} connected: {self.connected}") return self