modified retry range

This commit is contained in:
yash-dk 2020-09-12 18:46:02 +05:30
parent 7163c17808
commit 73dd8fa69e
2 changed files with 28 additions and 29 deletions

View File

@ -95,13 +95,16 @@ def strip_text(text, entities):
return text return text
def retry_range(retries): def retry_range(retries, reconnect=False):
""" """
Generates an integer sequence starting from 1. If `retries` is Generates an integer sequence starting from 1. If `retries` is
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`.
""" """
if retries == 0 and not reconnect:
yield 1
attempt = 0 attempt = 0
while attempt != retries: while attempt != retries:
yield 1 + attempt yield 1 + attempt

View File

@ -358,36 +358,32 @@ 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
if retries:
for attempt in retry_range(retries):
try:
await self._connect()
except (IOError, asyncio.TimeoutError) as e:
last_error = e
self._log.info('Failed reconnection attempt %d with %s',
attempt, e.__class__.__name__)
await asyncio.sleep(self._delay) attempt = 0
except Exception as e: for attempt in retry_range(retries,reconnect=True):
last_error = e try:
self._log.exception('Unexpected exception reconnecting on ' await self._connect()
'attempt %d', attempt) except (IOError, asyncio.TimeoutError) as e:
last_error = e
await asyncio.sleep(self._delay) self._log.info('Failed reconnection attempt %d with %s',
else: attempt, e.__class__.__name__)
self._send_queue.extend(self._pending_state.values()) await asyncio.sleep(self._delay)
self._pending_state.clear() except Exception as e:
last_error = e
if self._auto_reconnect_callback: self._log.exception('Unexpected exception reconnecting on '
asyncio.get_event_loop().create_task(self._auto_reconnect_callback()) 'attempt %d', attempt)
await asyncio.sleep(self._delay)
break
else: else:
self._log.error('Automatic reconnection failed %d time(s)', attempt) self._send_queue.extend(self._pending_state.values())
await self._disconnect(error=last_error.with_traceback(None)) self._pending_state.clear()
if self._auto_reconnect_callback:
asyncio.get_event_loop().create_task(self._auto_reconnect_callback())
break
else: else:
self._log.error('Automatic reconnection is False.') self._log.error('Automatic reconnection failed %d time(s)', attempt)
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."""