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.
This commit is contained in:
Lonami Exo 2022-01-18 18:21:56 +01:00
parent 7142734fb4
commit 3afabdd7c0
4 changed files with 1 additions and 55 deletions

View File

@ -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.

View File

@ -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

View File

@ -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.

View File

@ -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