Skip to content

Commit d1f1bbe

Browse files
authored
Migrate to using aiohttp-fast-url-dispatcher (home-assistant#103656)
1 parent 1a6c3a4 commit d1f1bbe

File tree

7 files changed

+16
-44
lines changed

7 files changed

+16
-44
lines changed

homeassistant/components/http/__init__.py

+2-43
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@
1818
from aiohttp.web_exceptions import HTTPMovedPermanently, HTTPRedirection
1919
from aiohttp.web_log import AccessLogger
2020
from aiohttp.web_protocol import RequestHandler
21-
from aiohttp.web_urldispatcher import (
22-
AbstractResource,
23-
UrlDispatcher,
24-
UrlMappingMatchInfo,
25-
)
21+
from aiohttp_fast_url_dispatcher import FastUrlDispatcher, attach_fast_url_dispatcher
2622
from aiohttp_zlib_ng import enable_zlib_ng
2723
from cryptography import x509
2824
from cryptography.hazmat.primitives import hashes, serialization
@@ -321,7 +317,7 @@ def __init__(
321317
# By default aiohttp does a linear search for routing rules,
322318
# we have a lot of routes, so use a dict lookup with a fallback
323319
# to the linear search.
324-
self.app._router = FastUrlDispatcher()
320+
attach_fast_url_dispatcher(self.app, FastUrlDispatcher())
325321
self.hass = hass
326322
self.ssl_certificate = ssl_certificate
327323
self.ssl_peer_certificate = ssl_peer_certificate
@@ -587,40 +583,3 @@ async def start_http_server_and_save_config(
587583
]
588584

589585
store.async_delay_save(lambda: conf, SAVE_DELAY)
590-
591-
592-
class FastUrlDispatcher(UrlDispatcher):
593-
"""UrlDispatcher that uses a dict lookup for resolving."""
594-
595-
def __init__(self) -> None:
596-
"""Initialize the dispatcher."""
597-
super().__init__()
598-
self._resource_index: dict[str, list[AbstractResource]] = {}
599-
600-
def register_resource(self, resource: AbstractResource) -> None:
601-
"""Register a resource."""
602-
super().register_resource(resource)
603-
canonical = resource.canonical
604-
if "{" in canonical: # strip at the first { to allow for variables
605-
canonical = canonical.split("{")[0].rstrip("/")
606-
# There may be multiple resources for a canonical path
607-
# so we use a list to avoid falling back to a full linear search
608-
self._resource_index.setdefault(canonical, []).append(resource)
609-
610-
async def resolve(self, request: web.Request) -> UrlMappingMatchInfo:
611-
"""Resolve a request."""
612-
url_parts = request.rel_url.raw_parts
613-
resource_index = self._resource_index
614-
615-
# Walk the url parts looking for candidates
616-
for i in range(len(url_parts), 0, -1):
617-
url_part = "/" + "/".join(url_parts[1:i])
618-
if (resource_candidates := resource_index.get(url_part)) is not None:
619-
for candidate in resource_candidates:
620-
if (
621-
match_dict := (await candidate.resolve(request))[0]
622-
) is not None:
623-
return match_dict
624-
625-
# Finally, fallback to the linear search
626-
return await super().resolve(request)

homeassistant/components/http/manifest.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@
66
"integration_type": "system",
77
"iot_class": "local_push",
88
"quality_scale": "internal",
9-
"requirements": ["aiohttp_cors==0.7.0", "aiohttp-zlib-ng==0.1.1"]
9+
"requirements": [
10+
"aiohttp_cors==0.7.0",
11+
"aiohttp-fast-url-dispatcher==0.1.0",
12+
"aiohttp-zlib-ng==0.1.1"
13+
]
1014
}

homeassistant/package_constraints.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
aiodiscover==1.5.1
2+
aiohttp-fast-url-dispatcher==0.1.0
23
aiohttp-zlib-ng==0.1.1
34
aiohttp==3.8.5;python_version<'3.12'
45
aiohttp==3.9.0b0;python_version>='3.12'

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ dependencies = [
2626
"aiohttp==3.9.0b0;python_version>='3.12'",
2727
"aiohttp==3.8.5;python_version<'3.12'",
2828
"aiohttp_cors==0.7.0",
29+
"aiohttp-fast-url-dispatcher==0.1.0",
2930
"aiohttp-zlib-ng==0.1.1",
3031
"astral==2.2",
3132
"attrs==23.1.0",

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
aiohttp==3.9.0b0;python_version>='3.12'
55
aiohttp==3.8.5;python_version<'3.12'
66
aiohttp_cors==0.7.0
7+
aiohttp-fast-url-dispatcher==0.1.0
78
aiohttp-zlib-ng==0.1.1
89
astral==2.2
910
attrs==23.1.0

requirements_all.txt

+3
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ aioharmony==0.2.10
257257
# homeassistant.components.homekit_controller
258258
aiohomekit==3.0.9
259259

260+
# homeassistant.components.http
261+
aiohttp-fast-url-dispatcher==0.1.0
262+
260263
# homeassistant.components.http
261264
aiohttp-zlib-ng==0.1.1
262265

requirements_test_all.txt

+3
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ aioharmony==0.2.10
235235
# homeassistant.components.homekit_controller
236236
aiohomekit==3.0.9
237237

238+
# homeassistant.components.http
239+
aiohttp-fast-url-dispatcher==0.1.0
240+
238241
# homeassistant.components.http
239242
aiohttp-zlib-ng==0.1.1
240243

0 commit comments

Comments
 (0)