From: Jérôme Benoit Date: Mon, 11 Dec 2023 21:12:32 +0000 (+0100) Subject: perf: remove AsyncResource usage X-Git-Tag: v1.2.30~23 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=d1ff85994f2ca20b45a7e7b6f07d0a90d98eaa54;p=e-mobility-charging-stations-simulator.git perf: remove AsyncResource usage Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index 12cf7ee4..95b2c678 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -1,7 +1,5 @@ // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved. -import { AsyncResource } from 'node:async_hooks'; - import { hoursToMilliseconds, secondsToMilliseconds } from 'date-fns'; import type { ChargingStation } from './ChargingStation'; @@ -31,9 +29,7 @@ import { sleep, } from '../utils'; -const moduleName = 'AutomaticTransactionGenerator'; - -export class AutomaticTransactionGenerator extends AsyncResource { +export class AutomaticTransactionGenerator { private static readonly instances: Map = new Map< string, AutomaticTransactionGenerator @@ -46,7 +42,6 @@ export class AutomaticTransactionGenerator extends AsyncResource { private readonly chargingStation: ChargingStation; private constructor(chargingStation: ChargingStation) { - super(moduleName); this.started = false; this.starting = false; this.stopping = false; @@ -109,14 +104,7 @@ export class AutomaticTransactionGenerator extends AsyncResource { throw new BaseError(`Connector ${connectorId} does not exist`); } if (this.connectorsStatus.get(connectorId)?.start === false) { - this.runInAsyncScope( - this.internalStartConnector.bind(this) as ( - this: AutomaticTransactionGenerator, - ...args: unknown[] - ) => Promise, - this, - connectorId, - ).catch(Constants.EMPTY_FUNCTION); + this.internalStartConnector(connectorId).catch(Constants.EMPTY_FUNCTION); } else if (this.connectorsStatus.get(connectorId)?.start === true) { logger.warn(`${this.logPrefix(connectorId)} is already started on connector`); } diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 8faf05cf..6dae5c8a 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -445,14 +445,9 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { commandPayload: ResetRequest, ): GenericResponse { const { type } = commandPayload; - this.runInAsyncScope( - chargingStation.reset.bind(chargingStation) as ( - this: ChargingStation, - ...args: unknown[] - ) => Promise, - chargingStation, - `${type}Reset` as OCPP16StopTransactionReason, - ).catch(Constants.EMPTY_FUNCTION); + chargingStation + .reset(`${type}Reset` as OCPP16StopTransactionReason) + .catch(Constants.EMPTY_FUNCTION); logger.info( `${chargingStation.logPrefix()} ${type} reset command received, simulating it. The station will be back online in ${formatDurationMilliSeconds( chargingStation.stationInfo.resetTime!, @@ -1122,25 +1117,11 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { retrieveDate = convertToDate(retrieveDate)!; const now = Date.now(); if (retrieveDate?.getTime() <= now) { - this.runInAsyncScope( - this.updateFirmwareSimulation.bind(this) as ( - this: OCPP16IncomingRequestService, - ...args: unknown[] - ) => Promise, - this, - chargingStation, - ).catch(Constants.EMPTY_FUNCTION); + this.updateFirmwareSimulation(chargingStation).catch(Constants.EMPTY_FUNCTION); } else { setTimeout( () => { - this.runInAsyncScope( - this.updateFirmwareSimulation.bind(this) as ( - this: OCPP16IncomingRequestService, - ...args: unknown[] - ) => Promise, - this, - chargingStation, - ).catch(Constants.EMPTY_FUNCTION); + this.updateFirmwareSimulation(chargingStation).catch(Constants.EMPTY_FUNCTION); }, retrieveDate?.getTime() - now, ); diff --git a/src/charging-station/ocpp/OCPPIncomingRequestService.ts b/src/charging-station/ocpp/OCPPIncomingRequestService.ts index 4b27f9d8..c93129bc 100644 --- a/src/charging-station/ocpp/OCPPIncomingRequestService.ts +++ b/src/charging-station/ocpp/OCPPIncomingRequestService.ts @@ -1,5 +1,3 @@ -import { AsyncResource } from 'node:async_hooks'; - import Ajv, { type JSONSchemaType, type ValidateFunction } from 'ajv'; import ajvFormats from 'ajv-formats'; @@ -18,7 +16,7 @@ import { logger, setDefaultErrorParams } from '../../utils'; const moduleName = 'OCPPIncomingRequestService'; -export abstract class OCPPIncomingRequestService extends AsyncResource { +export abstract class OCPPIncomingRequestService { private static instance: OCPPIncomingRequestService | null = null; private readonly version: OCPPVersion; private readonly ajv: Ajv; @@ -26,7 +24,6 @@ export abstract class OCPPIncomingRequestService extends AsyncResource { protected abstract jsonSchemas: Map>; protected constructor(version: OCPPVersion) { - super(moduleName); this.version = version; this.ajv = new Ajv({ keywords: ['javaType'],