From cd37478e31b65ca2d993b69d2e39102ae63be324 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Mon, 2 Dec 2019 18:32:31 +0100 Subject: [PATCH] Don't send pings unless the connection is made This will hopefully avoid batching tens of ping requests which we don't care about their results. --- telethon/client/updates.py | 6 ++++++ telethon/network/mtprotosender.py | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/telethon/client/updates.py b/telethon/client/updates.py index d4098870..3f1f7043 100644 --- a/telethon/client/updates.py +++ b/telethon/client/updates.py @@ -335,6 +335,12 @@ class UpdateMethods: except Exception: continue # Any disconnected exception should be ignored + # Don't bother sending pings until the low-level connection is + # ready, otherwise a lot of pings will be batched to be sent upon + # reconnect, when we really don't care about that. + if not self._sender._transport_connected(): + continue + # We also don't really care about their result. # Just send them periodically. try: diff --git a/telethon/network/mtprotosender.py b/telethon/network/mtprotosender.py index 77c7fb30..6c935c73 100644 --- a/telethon/network/mtprotosender.py +++ b/telethon/network/mtprotosender.py @@ -128,6 +128,13 @@ class MTProtoSender: def is_connected(self): return self._user_connected + def _transport_connected(self): + return ( + not self._reconnecting + and self._connection is not None + and self._connection._connected + ) + async def disconnect(self): """ Cleanly disconnects the instance from the network, cancels