From: Jérôme Benoit Date: Tue, 2 May 2023 21:22:51 +0000 (+0200) Subject: refactor: cleanup async lock API X-Git-Tag: v1.2.12~14 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=dd485b567f2f1b595c8241fa2cf74ba0c27911f0;p=e-mobility-charging-stations-simulator.git refactor: cleanup async lock API Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 9855c8ca..f1895163 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -1594,9 +1594,7 @@ export class ChargingStation { .update(JSON.stringify(configurationData)) .digest('hex'); if (this.configurationFileHash !== configurationHash) { - const asyncLock = AsyncLock.getInstance(AsyncLockType.configuration); - asyncLock - .acquire() + AsyncLock.acquire(AsyncLockType.configuration) .then(() => { configurationData.configurationHash = configurationHash; const measureId = `${FileType.ChargingStationConfiguration} write`; @@ -1618,7 +1616,7 @@ export class ChargingStation { ); }) .finally(() => { - asyncLock.release().catch(Constants.EMPTY_FUNCTION); + AsyncLock.release(AsyncLockType.configuration).catch(Constants.EMPTY_FUNCTION); }); } else { logger.debug( diff --git a/src/performance/storage/JsonFileStorage.ts b/src/performance/storage/JsonFileStorage.ts index 5effae90..fb706267 100644 --- a/src/performance/storage/JsonFileStorage.ts +++ b/src/performance/storage/JsonFileStorage.ts @@ -16,9 +16,7 @@ export class JsonFileStorage extends Storage { public storePerformanceStatistics(performanceStatistics: Statistics): void { this.checkPerformanceRecordsFile(); - const asyncLock = AsyncLock.getInstance(AsyncLockType.performance); - asyncLock - .acquire() + AsyncLock.acquire(AsyncLockType.performance) .then(() => { const fileData = fs.readFileSync(this.dbName, 'utf8'); const performanceRecords: Statistics[] = fileData @@ -40,7 +38,7 @@ export class JsonFileStorage extends Storage { ); }) .finally(() => { - asyncLock.release().catch(Constants.EMPTY_FUNCTION); + AsyncLock.release(AsyncLockType.performance).catch(Constants.EMPTY_FUNCTION); }); } diff --git a/src/utils/AsyncLock.ts b/src/utils/AsyncLock.ts index 53907ce4..7f44bd25 100644 --- a/src/utils/AsyncLock.ts +++ b/src/utils/AsyncLock.ts @@ -6,7 +6,7 @@ export enum AsyncLockType { } export class AsyncLock { - private static readonly instances = new Map(); + private static readonly asyncLocks = new Map(); private acquired: boolean; private readonly resolveQueue: ((value: void | PromiseLike) => void)[]; @@ -15,32 +15,34 @@ export class AsyncLock { this.resolveQueue = []; } - public static getInstance(type: AsyncLockType): AsyncLock { - if (!AsyncLock.instances.has(type)) { - AsyncLock.instances.set(type, new AsyncLock(type)); - } - return AsyncLock.instances.get(type); - } - - public async acquire(): Promise { - if (!this.acquired) { - this.acquired = true; + public static async acquire(type: AsyncLockType): Promise { + const asyncLock = AsyncLock.getAsyncLock(type); + if (!asyncLock.acquired) { + asyncLock.acquired = true; } else { return new Promise((resolve) => { - this.resolveQueue.push(resolve); + asyncLock.resolveQueue.push(resolve); }); } } - public async release(): Promise { - if (this.resolveQueue.length === 0 && this.acquired) { - this.acquired = false; + public static async release(type: AsyncLockType): Promise { + const asyncLock = AsyncLock.getAsyncLock(type); + if (asyncLock.resolveQueue.length === 0 && asyncLock.acquired) { + asyncLock.acquired = false; return; } - const queuedResolve = this.resolveQueue.shift(); + const queuedResolve = asyncLock.resolveQueue.shift(); return new Promise((resolve) => { queuedResolve(); resolve(); }); } + + private static getAsyncLock(type: AsyncLockType): AsyncLock { + if (!AsyncLock.asyncLocks.has(type)) { + AsyncLock.asyncLocks.set(type, new AsyncLock(type)); + } + return AsyncLock.asyncLocks.get(type); + } }