Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions homeassistant/components/http/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from homeassistant.helpers import storage
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.network import NoURLAvailableError, get_url
from homeassistant.helpers.start import async_at_started
from homeassistant.helpers.typing import ConfigType
from homeassistant.loader import bind_hass
from homeassistant.setup import async_start_setup, async_when_setup_or_start
Expand Down Expand Up @@ -513,6 +514,19 @@ def _create_emergency_ssl_context(self) -> ssl.SSLContext:
context.load_cert_chain(cert_pem.name, key_pem.name)
return context

def optimize_resources(self) -> None:
"""Optimize resources."""
# aiohttp does a linear search to locate resources to server.
# re-sort the resource table to move the cheaper resources to the front
# so the frontend loads faster
_resource_cost = {
web.PlainResource: 2,
web.DynamicResource: 10,
}
self.app.router._resources.sort( # pylint: disable=protected-access
key=lambda resource: _resource_cost.get(type(resource), 0) # type: ignore[arg-type]
)

async def start(self) -> None:
"""Start the aiohttp server."""
# Aiohttp freezes apps after start so that no changes can be made.
Expand Down Expand Up @@ -551,8 +565,15 @@ async def start_http_server_and_save_config(
hass: HomeAssistant, conf: dict, server: HomeAssistantHTTP
) -> None:
"""Startup the http server and save the config."""
server.optimize_resources()
await server.start()

async def _reoptimize(hass: HomeAssistant) -> None:
"""Reoptimize resources."""
server.optimize_resources()

async_at_started(hass, _reoptimize)

# If we are set up successful, we store the HTTP settings for safe mode.
store: storage.Store[dict[str, Any]] = storage.Store(
hass, STORAGE_VERSION, STORAGE_KEY
Expand Down