Improve WS ping timeout handling

This commit is contained in:
DevilXD committed 2021-12-16 19:09:55 +01:00
1 parent f41ef63f85
commit 0cb2ae7fcd
2 files changed
+7 -3

No files matched your search

+1
View File
@@ -26,6 +26,7 @@ SETTINGS_PATH = "settings.json"
COOKIES_PATH = "cookies.pickle" COOKIES_PATH = "cookies.pickle"
# Intervals and Delays # Intervals and Delays
PING_INTERVAL = timedelta(minutes=3) PING_INTERVAL = timedelta(minutes=3)
PING_TIMEOUT = timedelta(seconds=10)
ONLINE_DELAY = timedelta(seconds=30) ONLINE_DELAY = timedelta(seconds=30)
# Tags # Tags
DROPS_ENABLED_TAG = "c2542d6d-cd10-4532-919b-3d19f30a768b" DROPS_ENABLED_TAG = "c2542d6d-cd10-4532-919b-3d19f30a768b"
+6 -3
View File
@@ -20,6 +20,7 @@ from constants import (
WebsocketTopic, WebsocketTopic,
WEBSOCKET_URL, WEBSOCKET_URL,
PING_INTERVAL, PING_INTERVAL,
PING_TIMEOUT,
MAX_WEBSOCKETS, MAX_WEBSOCKETS,
WS_TOPICS_LIMIT, WS_TOPICS_LIMIT,
) )
@@ -62,8 +63,8 @@ class Websocket:
# set when the topics changed # set when the topics changed
self._topics_changed = asyncio.Event() self._topics_changed = asyncio.Event()
# ping timestamps # ping timestamps
self._next_ping = time() self._next_ping: float = time()
self._max_pong = time() self._max_pong: float = self._next_ping + PING_TIMEOUT.total_seconds()
# main task, responsible for receiving messages, sending them, and websocket ping # main task, responsible for receiving messages, sending them, and websocket ping
self._handle_task: Optional[asyncio.Task[Any]] = None self._handle_task: Optional[asyncio.Task[Any]] = None
# topics stuff # topics stuff
@@ -79,6 +80,8 @@ class Websocket:
def request_reconnect(self): def request_reconnect(self):
ws_logger.warning(f"Websocket[{self._idx}] requested reconnect.") ws_logger.warning(f"Websocket[{self._idx}] requested reconnect.")
# reset our ping interval, so we send a PING after reconnect right away
self._next_ping = time()
self._reconnect_requested.set() self._reconnect_requested.set()
async def start(self): async def start(self):
@@ -152,7 +155,7 @@ class Websocket:
now = time() now = time()
if now >= self._next_ping: if now >= self._next_ping:
self._next_ping = now + PING_INTERVAL.total_seconds() self._next_ping = now + PING_INTERVAL.total_seconds()
self._max_pong = now + 10 # wait for a PONG for up to 10s self._max_pong = now + PING_TIMEOUT.total_seconds() # wait for a PONG for up to 10s
await self.send({"type": "PING"}) await self.send({"type": "PING"})
elif now >= self._max_pong: elif now >= self._max_pong:
# it's been more than 10s and there was no PONG # it's been more than 10s and there was no PONG