mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-06-30 10:23:08 +03:00
Attempt at cleaning up reconnection logic
This commit is contained in:
parent
f3e2887452
commit
07cb001854
|
@ -254,9 +254,6 @@ class TelegramBareClient:
|
||||||
connects to the new data center.
|
connects to the new data center.
|
||||||
"""
|
"""
|
||||||
if new_dc is None:
|
if new_dc is None:
|
||||||
# Assume we are disconnected due to some error, so connect again
|
|
||||||
with self._reconnect_lock:
|
|
||||||
# Another thread may have connected again, so check that first
|
|
||||||
if self.is_connected():
|
if self.is_connected():
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -430,7 +427,16 @@ class TelegramBareClient:
|
||||||
)
|
)
|
||||||
if result is None:
|
if result is None:
|
||||||
sleep(1)
|
sleep(1)
|
||||||
|
self._logger.debug('RPC failed. Attempting reconnection.')
|
||||||
|
# The ReadThread has priority when attempting reconnection,
|
||||||
|
# since this thread is constantly running while __call__ is
|
||||||
|
# only done sometimes. Here try connecting only once/retry.
|
||||||
|
if sender == self._sender:
|
||||||
|
if not self._reconnect_lock.locked():
|
||||||
|
with self._reconnect_lock:
|
||||||
self._reconnect()
|
self._reconnect()
|
||||||
|
else:
|
||||||
|
sender.connect()
|
||||||
else:
|
else:
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
@ -494,21 +500,12 @@ class TelegramBareClient:
|
||||||
pass # We will just retry
|
pass # We will just retry
|
||||||
|
|
||||||
except ConnectionResetError:
|
except ConnectionResetError:
|
||||||
if not self._user_connected or self._reconnect_lock.locked():
|
if self._user_connected:
|
||||||
# Only attempt reconnecting if the user called connect and not
|
# Server disconnected us, __call__ will try reconnecting.
|
||||||
# reconnecting already.
|
|
||||||
raise
|
|
||||||
|
|
||||||
self._logger.debug('Server disconnected us. Reconnecting and '
|
|
||||||
'resending request...')
|
|
||||||
|
|
||||||
if sender != self._sender:
|
|
||||||
# TODO Try reconnecting forever too?
|
|
||||||
sender.connect()
|
|
||||||
else:
|
|
||||||
while self._user_connected and not self._reconnect():
|
|
||||||
sleep(0.1) # Retry forever until we can send the request
|
|
||||||
return None
|
return None
|
||||||
|
else:
|
||||||
|
# User never called .connect(), so raise this error.
|
||||||
|
raise
|
||||||
|
|
||||||
if init_connection:
|
if init_connection:
|
||||||
# We initialized the connection successfully, even if
|
# We initialized the connection successfully, even if
|
||||||
|
@ -828,6 +825,7 @@ class TelegramBareClient:
|
||||||
pass
|
pass
|
||||||
except ConnectionResetError:
|
except ConnectionResetError:
|
||||||
self._logger.debug('Server disconnected us. Reconnecting...')
|
self._logger.debug('Server disconnected us. Reconnecting...')
|
||||||
|
with self._reconnect_lock:
|
||||||
while self._user_connected and not self._reconnect():
|
while self._user_connected and not self._reconnect():
|
||||||
sleep(0.1) # Retry forever, this is instant messaging
|
sleep(0.1) # Retry forever, this is instant messaging
|
||||||
|
|
||||||
|
@ -864,12 +862,7 @@ class TelegramBareClient:
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
break
|
break
|
||||||
except ImportError:
|
except ImportError:
|
||||||
"Not using PySocks, so it can't be a socket error"
|
"Not using PySocks, so it can't be a proxy error"
|
||||||
|
|
||||||
# If something strange happens we don't want to enter an
|
|
||||||
# infinite loop where all we do is raise an exception, so
|
|
||||||
# add a little sleep to avoid the CPU usage going mad.
|
|
||||||
sleep(0.1)
|
|
||||||
|
|
||||||
self._recv_thread = None
|
self._recv_thread = None
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user