From 9b4040a32679e381c8ce53cbf2711f6b76532851 Mon Sep 17 00:00:00 2001 From: retoor Date: Sat, 14 Feb 2026 08:53:13 +0000 Subject: [PATCH] perf: increase websocket heartbeat to 300s and load subscriptions asynchronously in RPCView The heartbeat interval is raised from 30.0 to 300.0 seconds to reduce network overhead and improve connection efficiency. Subscription loading is moved into a background async task to prevent blocking the WebSocket handshake, enhancing responsiveness during initial connection setup. --- CHANGELOG.md | 8 ++++++++ pyproject.toml | 2 +- src/snek/view/rpc/__init__.py | 29 ++++++++++++++++------------- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 93be993b..65c72b13 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,14 @@ + + +## Version 1.36.0 - 2026-02-14 + +Increases the websocket heartbeat interval to reduce network overhead and improve connection efficiency. Loads subscriptions asynchronously to enhance responsiveness during data retrieval. + +**Changes:** 1 files, 29 lines +**Languages:** Python (29 lines) ## Version 1.35.0 - 2026-01-31 diff --git a/pyproject.toml b/pyproject.toml index ad2dc082..a6daa29e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "Snek" -version = "1.35.0" +version = "1.36.0" readme = "README.md" #license = { file = "LICENSE", content-type="text/markdown" } description = "Snek Chat Application by Molodetz" diff --git a/src/snek/view/rpc/__init__.py b/src/snek/view/rpc/__init__.py index 99120fb1..6dd644d4 100644 --- a/src/snek/view/rpc/__init__.py +++ b/src/snek/view/rpc/__init__.py @@ -155,7 +155,7 @@ class RPCView(BaseView): logger.debug(f"Schedule failed: {safe_str(ex)}") try: - ws = web.WebSocketResponse(heartbeat=30.0, autoping=True) + ws = web.WebSocketResponse(heartbeat=300.0, autoping=True) await ws.prepare(self.request) except Exception as ex: logger.warning(f"Failed to prepare WebSocket: {safe_str(ex)}") @@ -168,18 +168,21 @@ class RPCView(BaseView): user_uid = session.get("uid") if user_uid and self.services: await self.services.socket.add(ws, user_uid) - try: - async for subscription in self.services.channel_member.find( - user_uid=user_uid, - deleted_at=None, - is_banned=False, - ): - if subscription: - channel_uid = safe_get(subscription, "channel_uid") - if channel_uid: - await self.services.socket.subscribe(ws, channel_uid, user_uid) - except Exception as ex: - logger.warning(f"Failed to subscribe to channels: {safe_str(ex)}") + # Load subscriptions in background to avoid blocking WebSocket handshake + async def load_subscriptions(): + try: + async for subscription in self.services.channel_member.find( + user_uid=user_uid, + deleted_at=None, + is_banned=False, + ): + if subscription: + channel_uid = safe_get(subscription, "channel_uid") + if channel_uid: + await self.services.socket.subscribe(ws, channel_uid, user_uid) + except Exception as ex: + logger.warning(f"Failed to subscribe to channels: {safe_str(ex)}") + asyncio.create_task(load_subscriptions()) except Exception as ex: logger.warning(f"Session initialization failed: {safe_str(ex)}")