fix reconnect and retries

This commit is contained in:
yash-dk 2020-09-12 14:58:44 +05:30
parent 2a114917f1
commit a1eacac3c3
2 changed files with 29 additions and 24 deletions

View File

@ -101,11 +101,11 @@ def retry_range(retries):
not a zero or a positive integer value, the sequence will be not a zero or a positive integer value, the sequence will be
infinite, otherwise it will end at `retries + 1`. infinite, otherwise it will end at `retries + 1`.
""" """
yield 1
attempt = 0 attempt = 0
while attempt != retries: while attempt != retries:
attempt += 1
yield 1 + attempt yield 1 + attempt
attempt += 1
async def _maybe_await(value): async def _maybe_await(value):

View File

@ -217,6 +217,7 @@ class MTProtoSender:
self._log.info('Connecting to %s...', self._connection) self._log.info('Connecting to %s...', self._connection)
connected = False connected = False
attempt = 0
for attempt in retry_range(self._retries): for attempt in retry_range(self._retries):
if not connected: if not connected:
connected = await self._try_connect(attempt) connected = await self._try_connect(attempt)
@ -357,32 +358,36 @@ class MTProtoSender:
self._state.reset() self._state.reset()
retries = self._retries if self._auto_reconnect else 0 retries = self._retries if self._auto_reconnect else 0
for attempt in retry_range(retries): if retries:
try: for attempt in retry_range(retries):
await self._connect() try:
except (IOError, asyncio.TimeoutError) as e: await self._connect()
last_error = e except (IOError, asyncio.TimeoutError) as e:
self._log.info('Failed reconnection attempt %d with %s', last_error = e
attempt, e.__class__.__name__) self._log.info('Failed reconnection attempt %d with %s',
attempt, e.__class__.__name__)
await asyncio.sleep(self._delay) await asyncio.sleep(self._delay)
except Exception as e: except Exception as e:
last_error = e last_error = e
self._log.exception('Unexpected exception reconnecting on ' self._log.exception('Unexpected exception reconnecting on '
'attempt %d', attempt) 'attempt %d', attempt)
await asyncio.sleep(self._delay) await asyncio.sleep(self._delay)
else:
self._send_queue.extend(self._pending_state.values())
self._pending_state.clear()
if self._auto_reconnect_callback:
asyncio.get_event_loop().create_task(self._auto_reconnect_callback())
break
else: else:
self._send_queue.extend(self._pending_state.values()) self._log.error('Automatic reconnection failed %d time(s)', attempt)
self._pending_state.clear() await self._disconnect(error=last_error.with_traceback(None))
if self._auto_reconnect_callback:
asyncio.get_event_loop().create_task(self._auto_reconnect_callback())
break
else: else:
self._log.error('Automatic reconnection failed %d time(s)', attempt) self._log.error('Automatic reconnection is False.')
await self._disconnect(error=last_error.with_traceback(None)) await self._disconnect(error=last_error.with_traceback(None))
def _start_reconnect(self, error): def _start_reconnect(self, error):
"""Starts a reconnection in the background.""" """Starts a reconnection in the background."""