mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-02-18 12:30:59 +03:00
Retry on connection/security errors
This commit is contained in:
parent
92b606a3e8
commit
e36517845a
|
@ -39,11 +39,12 @@ class MTProtoSender:
|
||||||
A new authorization key will be generated on connection if no other
|
A new authorization key will be generated on connection if no other
|
||||||
key exists yet.
|
key exists yet.
|
||||||
"""
|
"""
|
||||||
def __init__(self, session):
|
def __init__(self, session, retries=5):
|
||||||
self.session = session
|
self.session = session
|
||||||
self._connection = ConnectionTcpFull()
|
self._connection = ConnectionTcpFull()
|
||||||
self._ip = None
|
self._ip = None
|
||||||
self._port = None
|
self._port = None
|
||||||
|
self._retries = retries
|
||||||
|
|
||||||
# Whether the user has explicitly connected or disconnected.
|
# Whether the user has explicitly connected or disconnected.
|
||||||
#
|
#
|
||||||
|
@ -109,14 +110,31 @@ class MTProtoSender:
|
||||||
await self._connect()
|
await self._connect()
|
||||||
|
|
||||||
async def _connect(self):
|
async def _connect(self):
|
||||||
|
_last_error = ConnectionError()
|
||||||
|
for _ in range(self._retries):
|
||||||
|
try:
|
||||||
async with self._send_lock:
|
async with self._send_lock:
|
||||||
await self._connection.connect(self._ip, self._port)
|
await self._connection.connect(self._ip, self._port)
|
||||||
|
except OSError as e:
|
||||||
|
_last_error = e
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
raise _last_error
|
||||||
|
|
||||||
# TODO Handle SecurityError, AssertionError
|
|
||||||
if self.session.auth_key is None:
|
if self.session.auth_key is None:
|
||||||
|
_last_error = SecurityError()
|
||||||
plain = MTProtoPlainSender(self._connection)
|
plain = MTProtoPlainSender(self._connection)
|
||||||
|
for _ in range(self._retries):
|
||||||
|
try:
|
||||||
self.session.auth_key, self.session.time_offset =\
|
self.session.auth_key, self.session.time_offset =\
|
||||||
await authenticator.do_authentication(plain)
|
await authenticator.do_authentication(plain)
|
||||||
|
except (SecurityError, AssertionError) as e:
|
||||||
|
_last_error = e
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
raise _last_error
|
||||||
|
|
||||||
self._send_loop_handle = asyncio.ensure_future(self._send_loop())
|
self._send_loop_handle = asyncio.ensure_future(self._send_loop())
|
||||||
self._recv_loop_handle = asyncio.ensure_future(self._recv_loop())
|
self._recv_loop_handle = asyncio.ensure_future(self._recv_loop())
|
||||||
|
@ -146,8 +164,6 @@ class MTProtoSender:
|
||||||
try:
|
try:
|
||||||
async with self._send_lock:
|
async with self._send_lock:
|
||||||
await self._connection.close()
|
await self._connection.close()
|
||||||
except:
|
|
||||||
__log__.exception('Ignoring exception upon disconnection')
|
|
||||||
finally:
|
finally:
|
||||||
for message in self._pending_messages.values():
|
for message in self._pending_messages.values():
|
||||||
message.future.cancel()
|
message.future.cancel()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user