From 31801a436cf0c33c15c376bc2c3d41945518b525 Mon Sep 17 00:00:00 2001 From: Javier Cuevas Date: Thu, 23 May 2024 17:06:04 +0200 Subject: [PATCH 1/5] Add periodic connection refresh --- mautrix_telegram/abstract_user.py | 14 ++++++++++++++ mautrix_telegram/example-config.yaml | 2 ++ 2 files changed, 16 insertions(+) diff --git a/mautrix_telegram/abstract_user.py b/mautrix_telegram/abstract_user.py index 2eb96260..ef1df456 100644 --- a/mautrix_telegram/abstract_user.py +++ b/mautrix_telegram/abstract_user.py @@ -241,6 +241,20 @@ async def _init_client(self) -> None: use_ipv6=self.config["telegram.connection.use_ipv6"], ) self.client.add_event_handler(self._update_catch) + self._schedule_reconnect() + + def _schedule_reconnect(self) -> None: + reconnect_interval = self.config["telegram.force_refresh_interval_seconds"] + if not reconnect_interval or reconnect_interval == 0: + return + refresh_time = time.time() + reconnect_interval + self.log.info("Scheduling forced reconnect in %d seconds. Connection will be refreshed at %s", reconnect_interval, time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(refresh_time))) + self.loop.call_later(reconnect_interval, lambda: asyncio.create_task(self._reconnect())) + + async def _reconnect(self) -> None: + self.log.info("Reconnecting to Telegram...") + await self.stop() + await self.start() @abstractmethod async def on_signed_out(self, err: UnauthorizedError | AuthKeyError) -> None: diff --git a/mautrix_telegram/example-config.yaml b/mautrix_telegram/example-config.yaml index b23083e4..75da8958 100644 --- a/mautrix_telegram/example-config.yaml +++ b/mautrix_telegram/example-config.yaml @@ -581,6 +581,8 @@ telegram: # Should incoming updates be handled sequentially to make sure order is preserved on Matrix? sequential_updates: true exit_on_update_error: false + # Interval to force refresh the connection (full reconnect), default is 1 day. Set 0 to disable force refreshes. + force_refresh_interval_seconds: 10 # Telethon connection options. connection: From 716222a6716a6e0fadbce40dc381126d00fc48dd Mon Sep 17 00:00:00 2001 From: Javier Cuevas Date: Thu, 23 May 2024 17:18:06 +0200 Subject: [PATCH 2/5] Format to pass linting --- mautrix_telegram/abstract_user.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mautrix_telegram/abstract_user.py b/mautrix_telegram/abstract_user.py index ef1df456..cb480ccb 100644 --- a/mautrix_telegram/abstract_user.py +++ b/mautrix_telegram/abstract_user.py @@ -248,7 +248,11 @@ def _schedule_reconnect(self) -> None: if not reconnect_interval or reconnect_interval == 0: return refresh_time = time.time() + reconnect_interval - self.log.info("Scheduling forced reconnect in %d seconds. Connection will be refreshed at %s", reconnect_interval, time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(refresh_time))) + self.log.info( + "Scheduling forced reconnect in %d seconds. Connection will be refreshed at %s", + reconnect_interval, + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(refresh_time)), + ) self.loop.call_later(reconnect_interval, lambda: asyncio.create_task(self._reconnect())) async def _reconnect(self) -> None: From a35f6abfd17ed0671184fd5813ee52dd6e07ee83 Mon Sep 17 00:00:00 2001 From: Javier Cuevas Date: Fri, 24 May 2024 09:36:03 +0200 Subject: [PATCH 3/5] Change default for force_refresh_interval_seconds (disabled by default) --- mautrix_telegram/example-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mautrix_telegram/example-config.yaml b/mautrix_telegram/example-config.yaml index 75da8958..06b0e946 100644 --- a/mautrix_telegram/example-config.yaml +++ b/mautrix_telegram/example-config.yaml @@ -581,8 +581,8 @@ telegram: # Should incoming updates be handled sequentially to make sure order is preserved on Matrix? sequential_updates: true exit_on_update_error: false - # Interval to force refresh the connection (full reconnect), default is 1 day. Set 0 to disable force refreshes. - force_refresh_interval_seconds: 10 + # Interval to force refresh the connection (full reconnect). 0 disables it. + force_refresh_interval_seconds: 0 # Telethon connection options. connection: From 4b25e855e0a3ea5d8bc74a8b432fc4434637a86c Mon Sep 17 00:00:00 2001 From: Javier Cuevas Date: Fri, 24 May 2024 09:36:09 +0200 Subject: [PATCH 4/5] Add force_refresh_interval_seconds to config.py --- mautrix_telegram/config.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mautrix_telegram/config.py b/mautrix_telegram/config.py index 14148881..84ed3d07 100644 --- a/mautrix_telegram/config.py +++ b/mautrix_telegram/config.py @@ -264,6 +264,7 @@ def do_update(self, helper: ConfigUpdateHelper) -> None: copy("telegram.catch_up") copy("telegram.sequential_updates") copy("telegram.exit_on_update_error") + copy("telegram.force_refresh_interval_seconds") copy("telegram.connection.timeout") copy("telegram.connection.retries") From 6418202118483a3ed00d03bada3aa1027094d22d Mon Sep 17 00:00:00 2001 From: Javier Cuevas Date: Fri, 24 May 2024 09:39:20 +0200 Subject: [PATCH 5/5] Update mautrix_telegram/abstract_user.py Co-authored-by: Tulir Asokan --- mautrix_telegram/abstract_user.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mautrix_telegram/abstract_user.py b/mautrix_telegram/abstract_user.py index cb480ccb..d3edf7ce 100644 --- a/mautrix_telegram/abstract_user.py +++ b/mautrix_telegram/abstract_user.py @@ -253,7 +253,7 @@ def _schedule_reconnect(self) -> None: reconnect_interval, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(refresh_time)), ) - self.loop.call_later(reconnect_interval, lambda: asyncio.create_task(self._reconnect())) + self.loop.call_later(reconnect_interval, lambda: background_task.create(self._reconnect())) async def _reconnect(self) -> None: self.log.info("Reconnecting to Telegram...")