Serialize websocket event message once (#40453)
Since most of the json serialize work for the websocket was done multiple times for the same message, we can avoid the overhead of serializing the same message many times (once per websocket client) with a cache.
This commit is contained in:
parent
d82b97fbe1
commit
f0f817c361
5 changed files with 120 additions and 32 deletions
|
@ -77,7 +77,7 @@ def handle_subscribe_events(hass, connection, msg):
|
|||
):
|
||||
return
|
||||
|
||||
connection.send_message(messages.event_message(msg["id"], event))
|
||||
connection.send_message(messages.cached_event_message(msg["id"], event))
|
||||
|
||||
else:
|
||||
|
||||
|
@ -87,7 +87,7 @@ def handle_subscribe_events(hass, connection, msg):
|
|||
if event.event_type == EVENT_TIME_CHANGED:
|
||||
return
|
||||
|
||||
connection.send_message(messages.event_message(msg["id"], event.as_dict()))
|
||||
connection.send_message(messages.cached_event_message(msg["id"], event))
|
||||
|
||||
connection.subscriptions[msg["id"]] = hass.bus.async_listen(
|
||||
event_type, forward_events
|
||||
|
|
|
@ -11,17 +11,11 @@ from homeassistant.components.http import HomeAssistantView
|
|||
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers.event import async_call_later
|
||||
from homeassistant.util.json import (
|
||||
find_paths_unserializable_data,
|
||||
format_unserializable_data,
|
||||
)
|
||||
|
||||
from .auth import AuthPhase, auth_required_message
|
||||
from .const import (
|
||||
CANCELLATION_ERRORS,
|
||||
DATA_CONNECTIONS,
|
||||
ERR_UNKNOWN_ERROR,
|
||||
JSON_DUMP,
|
||||
MAX_PENDING_MSG,
|
||||
PENDING_MSG_PEAK,
|
||||
PENDING_MSG_PEAK_TIME,
|
||||
|
@ -30,7 +24,7 @@ from .const import (
|
|||
URL,
|
||||
)
|
||||
from .error import Disconnect
|
||||
from .messages import error_message
|
||||
from .messages import message_to_json
|
||||
|
||||
# mypy: allow-untyped-calls, allow-untyped-defs, no-check-untyped-defs
|
||||
|
||||
|
@ -72,27 +66,10 @@ class WebSocketHandler:
|
|||
|
||||
self._logger.debug("Sending %s", message)
|
||||
|
||||
if isinstance(message, str):
|
||||
await self.wsock.send_str(message)
|
||||
continue
|
||||
if not isinstance(message, str):
|
||||
message = message_to_json(message)
|
||||
|
||||
try:
|
||||
dumped = JSON_DUMP(message)
|
||||
except (ValueError, TypeError):
|
||||
await self.wsock.send_json(
|
||||
error_message(
|
||||
message["id"], ERR_UNKNOWN_ERROR, "Invalid JSON in response"
|
||||
)
|
||||
)
|
||||
self._logger.error(
|
||||
"Unable to serialize to JSON. Bad data found at %s",
|
||||
format_unserializable_data(
|
||||
find_paths_unserializable_data(message, dump=JSON_DUMP)
|
||||
),
|
||||
)
|
||||
continue
|
||||
|
||||
await self.wsock.send_str(dumped)
|
||||
await self.wsock.send_str(message)
|
||||
|
||||
# Clean up the peaker checker when we shut down the writer
|
||||
if self._peak_checker_unsub:
|
||||
|
|
|
@ -1,11 +1,21 @@
|
|||
"""Message templates for websocket commands."""
|
||||
|
||||
from functools import lru_cache
|
||||
import logging
|
||||
from typing import Any, Dict
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.core import Event
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.util.json import (
|
||||
find_paths_unserializable_data,
|
||||
format_unserializable_data,
|
||||
)
|
||||
|
||||
from . import const
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
# mypy: allow-untyped-defs
|
||||
|
||||
# Minimal requirements of a message
|
||||
|
@ -18,12 +28,12 @@ MINIMAL_MESSAGE_SCHEMA = vol.Schema(
|
|||
BASE_COMMAND_MESSAGE_SCHEMA = vol.Schema({vol.Required("id"): cv.positive_int})
|
||||
|
||||
|
||||
def result_message(iden, result=None):
|
||||
def result_message(iden: int, result: Any = None) -> Dict:
|
||||
"""Return a success result message."""
|
||||
return {"id": iden, "type": const.TYPE_RESULT, "success": True, "result": result}
|
||||
|
||||
|
||||
def error_message(iden, code, message):
|
||||
def error_message(iden: int, code: str, message: str) -> Dict:
|
||||
"""Return an error result message."""
|
||||
return {
|
||||
"id": iden,
|
||||
|
@ -33,6 +43,37 @@ def error_message(iden, code, message):
|
|||
}
|
||||
|
||||
|
||||
def event_message(iden, event):
|
||||
def event_message(iden: int, event: Any) -> Dict:
|
||||
"""Return an event message."""
|
||||
return {"id": iden, "type": "event", "event": event}
|
||||
|
||||
|
||||
@lru_cache(maxsize=128)
|
||||
def cached_event_message(iden: int, event: Event) -> str:
|
||||
"""Return an event message.
|
||||
|
||||
Serialize to json once per message.
|
||||
|
||||
Since we can have many clients connected that are
|
||||
all getting many of the same events (mostly state changed)
|
||||
we can avoid serializing the same data for each connection.
|
||||
"""
|
||||
return message_to_json(event_message(iden, event))
|
||||
|
||||
|
||||
def message_to_json(message: Any) -> str:
|
||||
"""Serialize a websocket message to json."""
|
||||
try:
|
||||
return const.JSON_DUMP(message)
|
||||
except (ValueError, TypeError):
|
||||
_LOGGER.error(
|
||||
"Unable to serialize to JSON. Bad data found at %s",
|
||||
format_unserializable_data(
|
||||
find_paths_unserializable_data(message, dump=const.JSON_DUMP)
|
||||
),
|
||||
)
|
||||
return const.JSON_DUMP(
|
||||
error_message(
|
||||
message["id"], const.ERR_UNKNOWN_ERROR, "Invalid JSON in response"
|
||||
)
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue