Add retry delay for reconnection and requests

This commit is contained in:
Painor 2018-10-26 10:17:38 +01:00
parent 5beaf46e09
commit fd5a3d5f9a
2 changed files with 20 additions and 1 deletions

View File

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

View File

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