Switched JSON serialization to orjson for faster encoding and decoding.
CI / Formatting (push) Failing after 4s
CI / Linting (push) Successful in 4s
CI / Tests (Python 3.12) (push) Successful in 12s
CI / Tests (Python 3.13) (push) Successful in 12s
CI / Tests (Python 3.14) (push) Successful in 10s
CI / Type Checking (push) Successful in 9s
CI / Spelling (push) Successful in 5s
Audit / Dependencies (push) Successful in 7s

This commit is contained in:
2026-04-05 10:19:07 -04:00
parent 2d2214c671
commit 100cbc3bef
8 changed files with 76 additions and 14 deletions
+4 -4
View File
@@ -16,11 +16,11 @@
from __future__ import annotations
import json
import logging
from typing import TYPE_CHECKING, Any
import aiohttp
import orjson
from aiohttp import ContentTypeError
from markupsafe import escape as _escape_html
@@ -38,7 +38,7 @@ def _extract_error(text: str) -> str:
:return: The extracted error string.
"""
try:
data = json.loads(text)
data = orjson.loads(text)
except (ValueError, TypeError):
return text
if isinstance(data, dict):
@@ -256,7 +256,7 @@ class OwncastClient:
self._logger.debug("POST %s -> %d", endpoint, response.status)
if response.content_type == "application/json":
try:
result = await response.json()
result = await response.json(loads=orjson.loads)
except (ValueError, ContentTypeError):
text = await response.text()
self._logger.error(f"Invalid JSON on POST {endpoint}: {text}")
@@ -314,7 +314,7 @@ class OwncastClient:
raise OwncastError(response.status, message)
self._logger.debug("GET %s -> %d", endpoint, response.status)
try:
result = await response.json()
result = await response.json(loads=orjson.loads)
except (ValueError, ContentTypeError):
text = await response.text()
self._logger.error(f"Invalid JSON on GET {endpoint}: {text}")
+3 -2
View File
@@ -20,9 +20,10 @@ Serializes ffmpeg/ffprobe execution via semaphores.
from __future__ import annotations
import asyncio
import json
from typing import TYPE_CHECKING
import orjson
if TYPE_CHECKING:
import logging
from pathlib import Path
@@ -230,5 +231,5 @@ class ProcessingManager:
str(file_path),
)
data = json.loads(stdout)
data = orjson.loads(stdout)
return float(data["format"]["duration"])
@@ -23,13 +23,13 @@ from __future__ import annotations
import asyncio
import contextlib
import json
import secrets
from html.parser import HTMLParser
from pathlib import Path
from typing import TYPE_CHECKING
import emoji
import orjson
from aiohttp import web
if TYPE_CHECKING:
@@ -203,7 +203,7 @@ async def _broadcast(
:param events: The event data dicts to broadcast.
:param logger: Logger instance from the module context.
"""
payload = "".join(f"data: {json.dumps(event)}\n\n" for event in events).encode()
payload = b"".join(b"data: " + orjson.dumps(event) + b"\n\n" for event in events)
snapshot = list(clients)
results = await asyncio.gather(
*(client.write(payload) for client in snapshot),
+2 -1
View File
@@ -22,6 +22,7 @@ from collections.abc import Awaitable, Callable, Coroutine
from pathlib import Path
from typing import TYPE_CHECKING, Any
import orjson
from aiohttp import web
from . import __version__
@@ -143,7 +144,7 @@ class HttpServer:
async def _handle_webhook(self, request: web.Request) -> web.Response:
"""Handle incoming webhook requests from Owncast."""
try:
data = await request.json()
data = await request.json(loads=orjson.loads)
except ValueError as e:
# Invalid JSON received. This shouldn't happen
# with legitimate Owncast webhooks.
+5 -1
View File
@@ -26,6 +26,7 @@ from collections import defaultdict
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, cast, overload
import orjson
from aiohttp import web
from aiohttp.web import DynamicResource
@@ -608,7 +609,10 @@ class RouteDispatcher:
if isinstance(result, web.StreamResponse):
return result
if isinstance(result, dict):
return web.json_response(result)
return web.Response(
body=orjson.dumps(result),
content_type="application/json",
)
# pragma: no branch — defensive against untyped handlers
mod_logger.error( # type: ignore[unreachable]
f"Route handler '{route_info.full_path}' returned "