mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Reduce latency in storage by making the tasks eager (#111500)
* Reduce latancy to load storage by making the task eager This changes the semantics a bit under the hood because it can raise sooner which means we do not store the task as _load_task if it raises right away. That means concurrent calls that result in failure are likely to try again now which will be a tiny performance hit for this case. * fix * will now finish in time
This commit is contained in:
@@ -133,12 +133,16 @@ class Store(Generic[_T]):
|
||||
Will ensure that when a call comes in while another one is in progress,
|
||||
the second call will wait and return the result of the first call.
|
||||
"""
|
||||
if self._load_task is None:
|
||||
self._load_task = self.hass.async_create_task(
|
||||
self._async_load(), f"Storage load {self.key}"
|
||||
)
|
||||
if self._load_task:
|
||||
return await self._load_task
|
||||
|
||||
return await self._load_task
|
||||
load_task = self.hass.async_create_task(
|
||||
self._async_load(), f"Storage load {self.key}", eager_start=True
|
||||
)
|
||||
if not load_task.done():
|
||||
# Only set the load task if it didn't complete immediately
|
||||
self._load_task = load_task
|
||||
return await load_task
|
||||
|
||||
async def _async_load(self) -> _T | None:
|
||||
"""Load the data and ensure the task is removed."""
|
||||
@@ -318,7 +322,9 @@ class Store(Generic[_T]):
|
||||
# wrote. Reschedule the timer to the next write time.
|
||||
self._async_reschedule_delayed_write(self._next_write_time)
|
||||
return
|
||||
self.hass.async_create_task(self._async_callback_delayed_write())
|
||||
self.hass.async_create_task(
|
||||
self._async_callback_delayed_write(), eager_start=True
|
||||
)
|
||||
|
||||
@callback
|
||||
def _async_ensure_final_write_listener(self) -> None:
|
||||
|
||||
Reference in New Issue
Block a user