mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-05-31 11:43:07 +03:00
Fix automatic reconnection by unblocking the send queue
This commit is contained in:
parent
56ddaaee6f
commit
bb2cacd525
|
@ -19,6 +19,12 @@ from ..tl.types import (
|
||||||
__log__ = logging.getLogger(__name__)
|
__log__ = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
# Place this object in the send queue when a reconnection is needed
|
||||||
|
# so there is an item to read and we can early quit the loop, since
|
||||||
|
# without this it will block until there's something in the queue.
|
||||||
|
_reconnect_sentinel = object()
|
||||||
|
|
||||||
|
|
||||||
# TODO Create some kind of "ReconnectionPolicy" that allows specifying
|
# TODO Create some kind of "ReconnectionPolicy" that allows specifying
|
||||||
# what should be done in case of some errors, with some sane defaults.
|
# what should be done in case of some errors, with some sane defaults.
|
||||||
# For instance, should all messages be set with an error upon network
|
# For instance, should all messages be set with an error upon network
|
||||||
|
@ -249,6 +255,7 @@ class MTProtoSender:
|
||||||
Cleanly disconnects and then reconnects.
|
Cleanly disconnects and then reconnects.
|
||||||
"""
|
"""
|
||||||
self._reconnecting = True
|
self._reconnecting = True
|
||||||
|
self._send_queue.put_nowait(_reconnect_sentinel)
|
||||||
|
|
||||||
__log__.debug('Awaiting for the send loop before reconnecting...')
|
__log__.debug('Awaiting for the send loop before reconnecting...')
|
||||||
await self._send_loop_handle
|
await self._send_loop_handle
|
||||||
|
@ -296,6 +303,9 @@ class MTProtoSender:
|
||||||
self._pending_ack.clear()
|
self._pending_ack.clear()
|
||||||
|
|
||||||
messages = await self._send_queue.get()
|
messages = await self._send_queue.get()
|
||||||
|
if messages == _reconnect_sentinel and self._reconnecting:
|
||||||
|
break
|
||||||
|
|
||||||
if isinstance(messages, list):
|
if isinstance(messages, list):
|
||||||
message = self.state.create_message(MessageContainer(messages))
|
message = self.state.create_message(MessageContainer(messages))
|
||||||
self._pending_messages[message.msg_id] = message
|
self._pending_messages[message.msg_id] = message
|
||||||
|
|
Loading…
Reference in New Issue
Block a user