From 1ec87cba14b5e6b910f98074483dabec4c42bddd Mon Sep 17 00:00:00 2001 From: vvzvlad Date: Tue, 29 Apr 2025 19:48:11 +0400 Subject: [PATCH] refactor TelegramClient: simplify disconnection handling --- telegram_client.py | 46 ++++++---------------------------------------- 1 file changed, 6 insertions(+), 40 deletions(-) diff --git a/telegram_client.py b/telegram_client.py index 05799b8..b8ecdb2 100644 --- a/telegram_client.py +++ b/telegram_client.py @@ -13,7 +13,6 @@ import os import asyncio import sys import signal -import time import uvloop asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) @@ -33,11 +32,6 @@ class TelegramClient: api_hash=settings["tg_api_hash"], workdir=settings["session_path"], ) - self.disconnect_count = 0 - self.max_disconnects = 3 - self.last_disconnect_time = 0 - self.disconnect_window = 60 # Reset counter if disconnects are more than 60 seconds apart - self.reconnect_delay = 5 # Wait 5 seconds before reconnecting self._setup_connection_handlers() def _ensure_session_directory(self): @@ -54,46 +48,18 @@ class TelegramClient: logger.info("connection_handlers: connection handlers set up") async def _on_disconnect(self, _client): - """Handles disconnection from Telegram servers with auto-reconnect""" - current_time = time.time() - - # Reset counter if disconnects are far apart - if self.last_disconnect_time > 0 and current_time - self.last_disconnect_time > self.disconnect_window: - logger.info(f"connection_handler: resetting disconnect counter (last disconnect was {current_time - self.last_disconnect_time:.1f}s ago)") - self.disconnect_count = 0 - - self.last_disconnect_time = current_time - self.disconnect_count += 1 - - logger.warning(f"connection_handler: connection lost (#{self.disconnect_count})") - - if self.disconnect_count >= self.max_disconnects: - logger.critical(f"connection_handler: reached disconnect limit ({self.max_disconnects}) within {self.disconnect_window}s window, terminating") - # Explicitly stop the client before exit - if self.client.is_connected: - await self.client.stop() - # Exit with error code - sys.exit(1) - - # Attempt to reconnect only if we haven't reached max_disconnects - logger.info(f"connection_handler: attempting reconnection in {self.reconnect_delay}s...") - await asyncio.sleep(self.reconnect_delay) - - try: - if not self.client.is_connected: - await self.client.start() - logger.info("connection_handler: reconnection successful") - except Exception as e: - logger.error(f"connection_handler: reconnection failed: {str(e)}") + """Handles disconnection by just exiting the program""" + logger.error("connection_handler: connection lost, terminating program") + # Force exit with error code + sys.exit(1) async def start(self): try: if not self.client.is_connected: await self.client.start() logger.info("Telegram client connected successfully") - # Reset disconnect counter on successful connection - self.disconnect_count = 0 logger.info("connection_handler: connection established") except Exception as e: logger.error(f"Failed to start Telegram client: {str(e)}") - raise + # Force exit on any connection error + sys.exit(1)