Unify retry loops in mtprotosender._connect

Now the retry count is not twice its value.
This commit is contained in:
Lonami Exo 2019-10-24 13:40:09 +02:00
parent 3039915ce9
commit 08b78f0c47

View File

@ -197,20 +197,26 @@ class MTProtoSender:
receive loops. receive loops.
""" """
self._log.info('Connecting to %s...', self._connection) self._log.info('Connecting to %s...', self._connection)
for attempt in retry_range(self._retries):
if await self._try_connect(attempt):
break
else:
raise ConnectionError('Connection to Telegram failed %d time(s)', self._retries)
if not self.auth_key: connected = False
for attempt in retry_range(self._retries): for attempt in retry_range(self._retries):
if await self._try_gen_auth_key(attempt): if not connected:
break connected = await self._try_connect(attempt)
else: if not connected:
e = ConnectionError('auth_key generation failed %d time(s)', self._retries) continue # skip auth key generation until we're connected
await self._disconnect(error=e)
raise e if not self.auth_key:
if not await self._try_gen_auth_key(attempt):
continue # keep retrying until we have the auth key
break # all steps done, break retry loop
else:
if not connected:
raise ConnectionError('Connection to Telegram failed %d time(s)', self._retries)
e = ConnectionError('auth_key generation failed %d time(s)', self._retries)
await self._disconnect(error=e)
raise e
self._log.debug('Starting send loop') self._log.debug('Starting send loop')
self._send_loop_handle = self._loop.create_task(self._send_loop()) self._send_loop_handle = self._loop.create_task(self._send_loop())