Cache common version checks (#4673)

* Cache common version checks

We check core version quite frequently in the code, and its a bit expensive to do
all the comparsions everywhere. Since its mostly the same check happening over and
over we can cache it

* Cache common version checks

We check core version quite frequently in the code, and its a bit expensive to do
all the comparsions everywhere. Since its mostly the same check happening over and
over we can cache it

* fix import
This commit is contained in:
J. Nick Koston 2023-11-07 15:14:09 -06:00 committed by GitHub
parent 87385cf28e
commit 7cd7259992
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 6 deletions

View File

@ -85,6 +85,7 @@ from ..docker.const import Capabilities
from ..exceptions import AddonsNotSupportedError from ..exceptions import AddonsNotSupportedError
from ..jobs.const import JOB_GROUP_ADDON from ..jobs.const import JOB_GROUP_ADDON
from ..jobs.job_group import JobGroup from ..jobs.job_group import JobGroup
from ..utils import version_is_new_enough
from .const import ATTR_BACKUP, ATTR_CODENOTARY, AddonBackupMode from .const import ATTR_BACKUP, ATTR_CODENOTARY, AddonBackupMode
from .options import AddonOptions, UiOptions from .options import AddonOptions, UiOptions
from .validate import RE_SERVICE, RE_VOLUME from .validate import RE_SERVICE, RE_VOLUME
@ -645,7 +646,9 @@ class AddonModel(JobGroup, ABC):
# Home Assistant # Home Assistant
version: AwesomeVersion | None = config.get(ATTR_HOMEASSISTANT) version: AwesomeVersion | None = config.get(ATTR_HOMEASSISTANT)
with suppress(AwesomeVersionException, TypeError): with suppress(AwesomeVersionException, TypeError):
if self.sys_homeassistant.version < version: if version and not version_is_new_enough(
self.sys_homeassistant.version, version
):
raise AddonsNotSupportedError( raise AddonsNotSupportedError(
f"Add-on {self.slug} not supported on this system, requires Home Assistant version {version} or greater", f"Add-on {self.slug} not supported on this system, requires Home Assistant version {version} or greater",
logger, logger,

View File

@ -19,6 +19,7 @@ from ...const import (
CoreState, CoreState,
) )
from ...coresys import CoreSys, CoreSysAttributes from ...coresys import CoreSys, CoreSysAttributes
from ...utils import version_is_new_enough
from ..utils import api_return_error, excract_supervisor_token from ..utils import api_return_error, excract_supervisor_token
_LOGGER: logging.Logger = logging.getLogger(__name__) _LOGGER: logging.Logger = logging.getLogger(__name__)
@ -273,9 +274,8 @@ class SecurityMiddleware(CoreSysAttributes):
@middleware @middleware
async def core_proxy(self, request: Request, handler: RequestHandler) -> Response: async def core_proxy(self, request: Request, handler: RequestHandler) -> Response:
"""Validate user from Core API proxy.""" """Validate user from Core API proxy."""
if ( if request[REQUEST_FROM] != self.sys_homeassistant or version_is_new_enough(
request[REQUEST_FROM] != self.sys_homeassistant self.sys_homeassistant.version, _CORE_VERSION
or self.sys_homeassistant.version >= _CORE_VERSION
): ):
return await handler(request) return await handler(request)

View File

@ -13,7 +13,7 @@ from ..coresys import CoreSys, CoreSysAttributes
from ..exceptions import HomeAssistantAPIError, HomeAssistantAuthError from ..exceptions import HomeAssistantAPIError, HomeAssistantAuthError
from ..jobs.const import JobExecutionLimit from ..jobs.const import JobExecutionLimit
from ..jobs.decorator import Job from ..jobs.decorator import Job
from ..utils import check_port from ..utils import check_port, version_is_new_enough
from .const import LANDINGPAGE from .const import LANDINGPAGE
_LOGGER: logging.Logger = logging.getLogger(__name__) _LOGGER: logging.Logger = logging.getLogger(__name__)
@ -152,7 +152,9 @@ class HomeAssistantAPI(CoreSysAttributes):
# get_core_state is available since 2023.8.0 and preferred # get_core_state is available since 2023.8.0 and preferred
# since it is significantly faster than get_config because # since it is significantly faster than get_config because
# it does not require serializing the entire config # it does not require serializing the entire config
if self.sys_homeassistant.version >= GET_CORE_STATE_MIN_VERSION: if version_is_new_enough(
self.sys_homeassistant.version, GET_CORE_STATE_MIN_VERSION
):
data = await self.get_core_state() data = await self.get_core_state()
else: else:
data = await self.get_config() data = await self.get_config()

View File

@ -1,5 +1,6 @@
"""Tools file for Supervisor.""" """Tools file for Supervisor."""
import asyncio import asyncio
from functools import lru_cache
from ipaddress import IPv4Address from ipaddress import IPv4Address
import logging import logging
import os import os
@ -9,6 +10,8 @@ import socket
from tempfile import TemporaryDirectory from tempfile import TemporaryDirectory
from typing import Any from typing import Any
from awesomeversion import AwesomeVersion
_LOGGER: logging.Logger = logging.getLogger(__name__) _LOGGER: logging.Logger = logging.getLogger(__name__)
RE_STRING: re.Pattern = re.compile(r"\x1b(\[.*?[@-~]|\].*?(\x07|\x1b\\))") RE_STRING: re.Pattern = re.compile(r"\x1b(\[.*?[@-~]|\].*?(\x07|\x1b\\))")
@ -134,3 +137,11 @@ def clean_env() -> dict[str, str]:
if value := os.environ.get(key): if value := os.environ.get(key):
new_env[key] = value new_env[key] = value
return new_env return new_env
@lru_cache
def version_is_new_enough(
version: AwesomeVersion, want_version: AwesomeVersion
) -> bool:
"""Return True if the given version is new enough."""
return version >= want_version