perf: Deduplicate concurrent Home Assistant registry list fetches (#2650)
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { errorToConsole } from '../../../utils/basic';
|
||||
import { OnceRunner } from '../../../utils/concurrency/once-runner';
|
||||
import type { HomeAssistant } from '../../types';
|
||||
import { homeAssistantWSRequest } from '../../ws-request';
|
||||
import {
|
||||
@@ -10,7 +11,7 @@ import {
|
||||
|
||||
export class DeviceRegistryManager {
|
||||
private _cache: DeviceCache;
|
||||
private _fetchedDeviceList = false;
|
||||
private _deviceListFetch = new OnceRunner();
|
||||
|
||||
constructor(cache: DeviceCache) {
|
||||
this._cache = cache;
|
||||
@@ -35,22 +36,21 @@ export class DeviceRegistryManager {
|
||||
}
|
||||
|
||||
private async _fetchDeviceList(hass: HomeAssistant): Promise<void> {
|
||||
if (this._fetchedDeviceList) {
|
||||
return;
|
||||
}
|
||||
|
||||
let deviceList: DeviceList | null = null;
|
||||
try {
|
||||
deviceList = await homeAssistantWSRequest<DeviceList>(hass, deviceListSchema, {
|
||||
type: 'config/device_registry/list',
|
||||
await this._deviceListFetch.run(async () => {
|
||||
const deviceList = await homeAssistantWSRequest<DeviceList>(
|
||||
hass,
|
||||
deviceListSchema,
|
||||
{
|
||||
type: 'config/device_registry/list',
|
||||
},
|
||||
);
|
||||
deviceList.forEach((device) => {
|
||||
this._cache.set(device.id, device);
|
||||
});
|
||||
});
|
||||
} catch (e) {
|
||||
errorToConsole(e);
|
||||
return;
|
||||
}
|
||||
deviceList.forEach((device) => {
|
||||
this._cache.set(device.id, device);
|
||||
});
|
||||
this._fetchedDeviceList = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { errorToConsole } from '../../../utils/basic.js';
|
||||
import { OnceRunner } from '../../../utils/concurrency/once-runner.js';
|
||||
import type { HomeAssistant } from '../../types.js';
|
||||
import { homeAssistantWSRequest } from '../../ws-request.js';
|
||||
import {
|
||||
@@ -16,7 +17,7 @@ import {
|
||||
|
||||
export class EntityRegistryManagerLive implements EntityRegistryManager {
|
||||
private _cache: EntityCache;
|
||||
private _fetchedEntityList = false;
|
||||
private _entityListFetch = new OnceRunner();
|
||||
|
||||
constructor(cache: EntityCache) {
|
||||
this._cache = cache;
|
||||
@@ -69,22 +70,21 @@ export class EntityRegistryManagerLive implements EntityRegistryManager {
|
||||
}
|
||||
|
||||
public async fetchEntityList(hass: HomeAssistant): Promise<void> {
|
||||
if (this._fetchedEntityList) {
|
||||
return;
|
||||
}
|
||||
|
||||
let entityList: EntityList | null = null;
|
||||
try {
|
||||
entityList = await homeAssistantWSRequest<EntityList>(hass, entityListSchema, {
|
||||
type: 'config/entity_registry/list',
|
||||
await this._entityListFetch.run(async () => {
|
||||
const entityList = await homeAssistantWSRequest<EntityList>(
|
||||
hass,
|
||||
entityListSchema,
|
||||
{
|
||||
type: 'config/entity_registry/list',
|
||||
},
|
||||
);
|
||||
entityList.forEach((entity) => {
|
||||
this._cache.set(entity.entity_id, entity);
|
||||
});
|
||||
});
|
||||
} catch (e) {
|
||||
errorToConsole(e);
|
||||
return;
|
||||
}
|
||||
entityList.forEach((entity) => {
|
||||
this._cache.set(entity.entity_id, entity);
|
||||
});
|
||||
this._fetchedEntityList = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
export type Work = () => Promise<void>;
|
||||
|
||||
/**
|
||||
* Runs asynchronous work at most once, and shares it while it is in flight.
|
||||
*
|
||||
* The first call starts the work. Callers that arrive while it is still running
|
||||
* wait on that same run rather than starting their own, so ten concurrent
|
||||
* callers make one request instead of ten identical ones. Once the work has
|
||||
* succeeded, later calls return immediately without running it again.
|
||||
*
|
||||
* A failure is *not* remembered: every caller waiting on the failed run sees
|
||||
* the rejection, and the next call starts a fresh attempt.
|
||||
*
|
||||
* Only the first caller's `work` ever runs. Callers that join an in-flight run
|
||||
* have their own `work` discarded, so every caller must pass work that is
|
||||
* interchangeable with the others'.
|
||||
*/
|
||||
export class OnceRunner {
|
||||
private _succeeded = false;
|
||||
private _inFlight: Promise<void> | null = null;
|
||||
|
||||
public async run(work: Work): Promise<void> {
|
||||
if (this._succeeded) {
|
||||
return;
|
||||
}
|
||||
this._inFlight ??= this._runOnce(work);
|
||||
|
||||
const inFlight = this._inFlight;
|
||||
try {
|
||||
await inFlight;
|
||||
} finally {
|
||||
// Clear only the run this caller waited on. A caller that joined the same
|
||||
// failed run may resume after a later caller has already started a fresh
|
||||
// one, which must not be discarded.
|
||||
if (this._inFlight === inFlight) {
|
||||
this._inFlight = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async _runOnce(work: Work): Promise<void> {
|
||||
await work();
|
||||
this._succeeded = true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user