diff --git a/telethon/_misc/helpers.py b/telethon/_misc/helpers.py index a3480007..c6c76b8e 100644 --- a/telethon/_misc/helpers.py +++ b/telethon/_misc/helpers.py @@ -102,23 +102,11 @@ def strip_text(text, entities): return text -def retry_range(retries, force_retry=True): +def retry_range(retries): """ - Generates an integer sequence starting from 1. If `retries` is - not a zero or a positive integer value, the sequence will be - infinite, otherwise it will end at `retries + 1`. + Generates an integer sequence starting from 1, always returning once, and adding the given retries. """ - - # We need at least one iteration even if the retries are 0 - # when force_retry is True. - if force_retry and not (retries is None or retries < 0): - retries += 1 - - attempt = 0 - while attempt != retries: - attempt += 1 - yield attempt - + return range(1, max(retries, 0) + 2) async def _maybe_await(value): diff --git a/telethon/_network/mtprotosender.py b/telethon/_network/mtprotosender.py index fb6650ce..84f2b2c0 100644 --- a/telethon/_network/mtprotosender.py +++ b/telethon/_network/mtprotosender.py @@ -249,9 +249,9 @@ class MTProtoSender: break # all steps done, break retry loop else: if not connected: - raise ConnectionError('Connection to Telegram failed {} time(s)'.format(self._retries)) + raise ConnectionError('Connection to Telegram failed {} time(s)'.format(1 + self._retries)) - e = ConnectionError('auth_key generation failed {} time(s)'.format(self._retries)) + e = ConnectionError('auth_key generation failed {} time(s)'.format(1 + self._retries)) await self._disconnect(error=e) raise e @@ -349,12 +349,11 @@ class MTProtoSender: # Start with a clean state (and thus session ID) to avoid old msgs self._state.reset() - retries = self._retries if self._auto_reconnect else 0 + retry_range = helpers.retry_range(self._retries) if self._auto_reconnect else range(0) attempt = 0 ok = True - # We're already "retrying" to connect, so we don't want to force retries - for attempt in helpers.retry_range(retries, force_retry=False): + for attempt in retry_range: try: await self._connect() except (IOError, asyncio.TimeoutError) as e: