Add periodic connection refresh

This commit is contained in:
Javier Cuevas
2024-05-23 17:06:04 +02:00
parent 8bd5a4e367
commit 31801a436c
2 changed files with 16 additions and 0 deletions
+14
View File
@@ -241,6 +241,20 @@ class AbstractUser(ABC):
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:
+2
View File
@@ -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: