From 3afabdd7c05eba244a047f10ed5a00aac02541e4 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Tue, 18 Jan 2022 18:21:56 +0100 Subject: [PATCH] Remove auto-reconnect callback It's an abstraction leak. The client should know to refetch updates if a long period passed without them on its own. --- telethon/_client/telegrambaseclient.py | 1 - telethon/_client/telegramclient.py | 4 --- telethon/_client/updates.py | 44 -------------------------- telethon/_network/mtprotosender.py | 7 +--- 4 files changed, 1 insertion(+), 55 deletions(-) diff --git a/telethon/_client/telegrambaseclient.py b/telethon/_client/telegrambaseclient.py index 9fbf3f14..62dc09b0 100644 --- a/telethon/_client/telegrambaseclient.py +++ b/telethon/_client/telegrambaseclient.py @@ -190,7 +190,6 @@ def init( auto_reconnect=self._auto_reconnect, connect_timeout=self._connect_timeout, update_callback=self._handle_update, - auto_reconnect_callback=self._handle_auto_reconnect ) # Cache ``{dc_id: (_ExportState, MTProtoSender)}`` for all borrowed senders. diff --git a/telethon/_client/telegramclient.py b/telethon/_client/telegramclient.py index 5f555b06..412e7b61 100644 --- a/telethon/_client/telegramclient.py +++ b/telethon/_client/telegramclient.py @@ -3513,10 +3513,6 @@ class TelegramClient: def _handle_update(self: 'TelegramClient', update): pass - @forward_call(updates._handle_auto_reconnect) - async def _handle_auto_reconnect(self: 'TelegramClient'): - pass - @forward_call(auth._update_session_state) async def _update_session_state(self, user, *, save=True): pass diff --git a/telethon/_client/updates.py b/telethon/_client/updates.py index 92515dae..4b8e29cb 100644 --- a/telethon/_client/updates.py +++ b/telethon/_client/updates.py @@ -347,50 +347,6 @@ async def _get_difference(self: 'TelegramClient', update, entities, channel_id, itertools.chain(result.users, result.chats) }) -async def _handle_auto_reconnect(self: 'TelegramClient'): - # TODO Catch-up - # For now we make a high-level request to let Telegram - # know we are still interested in receiving more updates. - try: - await self.get_me() - except Exception as e: - self._log[__name__].warning('Error executing high-level request ' - 'after reconnect: %s: %s', type(e), e) - - return - try: - self._log[__name__].info( - 'Asking for the current state after reconnect...') - - # TODO consider: - # If there aren't many updates while the client is disconnected - # (I tried with up to 20), Telegram seems to send them without - # asking for them (via updates.getDifference). - # - # On disconnection, the library should probably set a "need - # difference" or "catching up" flag so that any new updates are - # ignored, and then the library should call updates.getDifference - # itself to fetch them. - # - # In any case (either there are too many updates and Telegram - # didn't send them, or there isn't a lot and Telegram sent them - # but we dropped them), we fetch the new difference to get all - # missed updates. I feel like this would be the best solution. - - # If a disconnection occurs, the old known state will be - # the latest one we were aware of, so we can catch up since - # the most recent state we were aware of. - await self.catch_up() - - self._log[__name__].info('Successfully fetched missed updates') - except RpcError as e: - self._log[__name__].warning('Failed to get missed updates after ' - 'reconnect: %r', e) - except Exception: - self._log[__name__].exception( - 'Unhandled exception while getting update difference after reconnect') - - class EventBuilderDict: """ Helper "dictionary" to return events from types and cache them. diff --git a/telethon/_network/mtprotosender.py b/telethon/_network/mtprotosender.py index 92438502..b05137e3 100644 --- a/telethon/_network/mtprotosender.py +++ b/telethon/_network/mtprotosender.py @@ -37,7 +37,7 @@ class MTProtoSender: """ def __init__(self, *, loggers, retries=5, delay=1, auto_reconnect=True, connect_timeout=None, - update_callback=None, auto_reconnect_callback=None): + update_callback=None): self._connection = None self._loggers = loggers self._log = loggers[__name__] @@ -46,7 +46,6 @@ class MTProtoSender: self._auto_reconnect = auto_reconnect self._connect_timeout = connect_timeout self._update_callback = update_callback - self._auto_reconnect_callback = auto_reconnect_callback self._connect_lock = asyncio.Lock() self._ping = None @@ -373,10 +372,6 @@ class MTProtoSender: else: self._send_queue.extend(self._pending_state.values()) self._pending_state.clear() - - if self._auto_reconnect_callback: - asyncio.create_task(self._auto_reconnect_callback()) - break else: ok = False