1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

Refactor rate limit helper to track time in seconds (#113898)

* Refactor rate limit helper to track time in seconds

Currently we created datetime and timedelta objects to enforce the
rate limit. When the rate limit was being hit hard, this got expensive.

We now use floats everywhere instead as they are much cheaper which
is important when we are running up against a rate limit, which is
by definition a hot path

The rate limit helper is currently only used for templates and
we do not have any code in the code base that directly passes
in a rate limit so the impact to custom components is expected
to be negligible if any

* misesd two
This commit is contained in:
J. Nick Koston
2024-03-20 13:49:37 -10:00
committed by GitHub
parent b311fe2a0f
commit b574220247
7 changed files with 53 additions and 68 deletions

View File

@@ -131,8 +131,8 @@ _T = TypeVar("_T")
_R = TypeVar("_R")
_P = ParamSpec("_P")
ALL_STATES_RATE_LIMIT = timedelta(minutes=1)
DOMAIN_STATES_RATE_LIMIT = timedelta(seconds=1)
ALL_STATES_RATE_LIMIT = 60 # seconds
DOMAIN_STATES_RATE_LIMIT = 1 # seconds
_render_info: ContextVar[RenderInfo | None] = ContextVar("_render_info", default=None)
@@ -374,7 +374,7 @@ class RenderInfo:
self.domains: collections.abc.Set[str] = set()
self.domains_lifecycle: collections.abc.Set[str] = set()
self.entities: collections.abc.Set[str] = set()
self.rate_limit: timedelta | None = None
self.rate_limit: float | None = None
self.has_time = False
def __repr__(self) -> str: