whoops (#1271)
This commit is contained in:
@@ -20,6 +20,7 @@ from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import aiohttp
|
||||
import zstandard as zstd
|
||||
from aiohttp import web
|
||||
from dotenv import load_dotenv
|
||||
|
||||
@@ -73,6 +74,8 @@ HOP_BY_HOP_HEADERS = {
|
||||
CONNECTED_WEBSOCKETS: set[web.WebSocketResponse] = set()
|
||||
CONNECTED_LOCK = asyncio.Lock()
|
||||
|
||||
_compressor = zstd.ZstdCompressor(level=3)
|
||||
|
||||
|
||||
def _auth_ok(request: web.Request) -> bool:
|
||||
if not SETTINGS.bearer_token:
|
||||
@@ -201,7 +204,8 @@ async def websocket_handler(request: web.Request) -> web.WebSocketResponse:
|
||||
|
||||
|
||||
async def _broadcast(envelope: dict[str, Any]) -> None:
|
||||
serialized = json.dumps(envelope, ensure_ascii=False, separators=(",", ":"))
|
||||
raw = json.dumps(envelope, ensure_ascii=False, separators=(",", ":")).encode("utf-8")
|
||||
payload = _compressor.compress(raw)
|
||||
async with CONNECTED_LOCK:
|
||||
targets = list(CONNECTED_WEBSOCKETS)
|
||||
|
||||
@@ -215,7 +219,7 @@ async def _broadcast(envelope: dict[str, Any]) -> None:
|
||||
dead: list[web.WebSocketResponse] = []
|
||||
for ws in targets:
|
||||
try:
|
||||
await ws.send_str(serialized)
|
||||
await ws.send_bytes(payload)
|
||||
except Exception as exc:
|
||||
logger.warning(
|
||||
"Failed to send websocket envelope",
|
||||
@@ -233,6 +237,8 @@ async def _broadcast(envelope: dict[str, Any]) -> None:
|
||||
extra={
|
||||
"event_type": envelope.get("type"),
|
||||
"clients": len(targets) - len(dead),
|
||||
"raw_bytes": len(raw),
|
||||
"compressed_bytes": len(payload),
|
||||
"payload_keys": list((envelope.get("payload") or {}).keys())[:8],
|
||||
},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user