From fd5a3d5f9abd35f5609be5f8d47dad4dbc692062 Mon Sep 17 00:00:00 2001 From: Painor Date: Fri, 26 Oct 2018 10:17:38 +0100 Subject: [PATCH] Add retry delay for reconnection and requests --- telethon/client/telegrambaseclient.py | 9 +++++++++ telethon/network/mtprotosender.py | 12 +++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/telethon/client/telegrambaseclient.py b/telethon/client/telegrambaseclient.py index 22a5ac29..3490e0d4 100644 --- a/telethon/client/telegrambaseclient.py +++ b/telethon/client/telegrambaseclient.py @@ -89,6 +89,12 @@ class TelegramBaseClient(abc.ABC): retries, but this is not recommended, since the program can get stuck in an infinite loop. + retry_delay (`float`, optional): + The delay should wait between each retry in seconds, either + on the initial connection or when Telegram disconnects us + or when sending requests. + May be set to a false-y value (``0`` or ``None``) for no delays. + auto_reconnect (`bool`, optional): Whether reconnection should be retried `connection_retries` times automatically if Telegram disconnects us or not. @@ -150,6 +156,7 @@ class TelegramBaseClient(abc.ABC): timeout=10, request_retries=5, connection_retries=5, + retry_delay=0, auto_reconnect=True, sequential_updates=False, flood_sleep_threshold=60, @@ -210,6 +217,7 @@ class TelegramBaseClient(abc.ABC): self._request_retries = request_retries or sys.maxsize self._connection_retries = connection_retries or sys.maxsize + self._retry_delay = retry_delay or 0 self._proxy = proxy self._timeout = timeout self._auto_reconnect = auto_reconnect @@ -237,6 +245,7 @@ class TelegramBaseClient(abc.ABC): self._sender = MTProtoSender( self.session.auth_key, self._loop, retries=self._connection_retries, + delay=self._retry_delay, auto_reconnect=self._auto_reconnect, connect_timeout=self._timeout, update_callback=self._handle_update, diff --git a/telethon/network/mtprotosender.py b/telethon/network/mtprotosender.py index 8c4eeb4b..1433b2f8 100644 --- a/telethon/network/mtprotosender.py +++ b/telethon/network/mtprotosender.py @@ -60,11 +60,12 @@ class MTProtoSender: key exists yet. """ def __init__(self, auth_key, loop, *, - retries=5, auto_reconnect=True, connect_timeout=None, + retries=5, delay=0, auto_reconnect=True, connect_timeout=None, update_callback=None, auto_reconnect_callback=None): self._connection = None self._loop = loop self._retries = retries + self._delay = delay self._auto_reconnect = auto_reconnect self._connect_timeout = connect_timeout self._update_callback = update_callback @@ -215,6 +216,9 @@ class MTProtoSender: except (ConnectionError, asyncio.TimeoutError) as e: __log__.warning('Attempt {} at connecting failed: {}: {}' .format(retry, type(e).__name__, e)) + __log__.warning('Sleeping for {} seconds on failed attempt {}:' + .format(self._delay,retry)) + await asyncio.sleep(self._delay) else: break else: @@ -234,6 +238,9 @@ class MTProtoSender: except (SecurityError, AssertionError) as e: __log__.warning('Attempt {} at new auth_key failed: {}' .format(retry, e)) + __log__.warning('Sleeping for {} seconds on failed attempt {}:' + .format(self._delay,retry)) + await asyncio.sleep(self._delay) else: e = ConnectionError('auth_key generation failed {} times' .format(self._retries)) @@ -314,6 +321,9 @@ class MTProtoSender: await self._connect() except ConnectionError: __log__.info('Failed reconnection retry %d/%d', retry, retries) + __log__.warning('Sleeping for {} seconds on failed attempt {}:' + .format(self._delay,retry)) + await asyncio.sleep(self._delay) else: self._send_queue.extend(self._pending_state.values()) self._pending_state.clear()