From: Jérôme Benoit Date: Mon, 25 Jan 2021 11:17:08 +0000 (+0100) Subject: Add proper worker set elements reference counting. X-Git-Tag: v1.0.1-0~114 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=c045d9a97ebdf3747b82746b733b0c60a7db3f32;p=e-mobility-charging-stations-simulator.git Add proper worker set elements reference counting. Signed-off-by: Jérôme Benoit --- diff --git a/package.json b/package.json index 8a4d7a29..36cea67b 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "build:clean": "rimraf ./dist", "lint": "cross-env TIMING=1 eslint --ext .js,.ts src", "lint:fix": "cross-env TIMING=1 eslint --fix --ext .js,.ts src", + "import-sort": "npx import-sort-cli --write 'src/**/*.ts{,x}'", "tsc": "tsc", "test": "echo \"Error: no tests implemented\" && exit 1", "clinic:clean": "clinic clean", diff --git a/src/charging-station/StationWorker.ts b/src/charging-station/StationWorker.ts index c0e85748..b22d8932 100644 --- a/src/charging-station/StationWorker.ts +++ b/src/charging-station/StationWorker.ts @@ -2,7 +2,7 @@ import { isMainThread, parentPort, workerData } from 'worker_threads'; import ChargingStation from './ChargingStation'; import Utils from '../utils/Utils'; -import { WorkerEvents } from '../types/WorkerEvents'; +import { WorkerEvents } from '../types/Worker'; if (!isMainThread) { // Add listener to start charging station from main thread diff --git a/src/start.ts b/src/start.ts index 90eb5161..7f582222 100644 --- a/src/start.ts +++ b/src/start.ts @@ -1,5 +1,5 @@ import Configuration from './utils/Configuration'; -import WorkerData from './types/WorkerData'; +import { WorkerData } from './types/Worker'; import WorkerFactory from './worker/WorkerFactory'; import Wrk from './worker/Wrk'; diff --git a/src/types/Worker.ts b/src/types/Worker.ts new file mode 100644 index 00000000..bc93d76e --- /dev/null +++ b/src/types/Worker.ts @@ -0,0 +1,17 @@ +import { Worker } from 'worker_threads'; + +// FIXME: make it more generic +export interface WorkerData { + index: number; + templateFile: string; +} + +export interface WorkerSetElement { + worker: Worker, + numberOfWorkerElements: number +} + +export enum WorkerEvents { + START_WORKER_ELEMENT = 'startWorkerElement', +} + diff --git a/src/types/WorkerData.ts b/src/types/WorkerData.ts deleted file mode 100644 index a7bc2c27..00000000 --- a/src/types/WorkerData.ts +++ /dev/null @@ -1,5 +0,0 @@ -// FIXME: make it more generic -export default interface WorkerData { - index: number; - templateFile: string; -} diff --git a/src/types/WorkerEvents.ts b/src/types/WorkerEvents.ts deleted file mode 100644 index 07a4e421..00000000 --- a/src/types/WorkerEvents.ts +++ /dev/null @@ -1,4 +0,0 @@ - -export enum WorkerEvents { - START_WORKER_ELEMENT = 'startWorkerElement', -} diff --git a/src/worker/WorkerPool.ts b/src/worker/WorkerPool.ts index f1a6cc1f..5b9415c8 100644 --- a/src/worker/WorkerPool.ts +++ b/src/worker/WorkerPool.ts @@ -2,7 +2,7 @@ import Configuration from '../utils/Configuration'; import Constants from '../utils/Constants'; import Pool from 'worker-threads-pool'; import Utils from '../utils/Utils'; -import WorkerData from '../types/WorkerData'; +import { WorkerData } from '../types/Worker'; import Wrk from './Wrk'; export default class WorkerPool extends Wrk { diff --git a/src/worker/WorkerSet.ts b/src/worker/WorkerSet.ts index af753c8f..50d099d8 100644 --- a/src/worker/WorkerSet.ts +++ b/src/worker/WorkerSet.ts @@ -1,14 +1,13 @@ +import { WorkerData, WorkerEvents, WorkerSetElement } from '../types/Worker'; + import Constants from '../utils/Constants'; import Utils from '../utils/Utils'; import { Worker } from 'worker_threads'; -import WorkerData from '../types/WorkerData'; -import { WorkerEvents } from '../types/WorkerEvents'; import Wrk from './Wrk'; export default class WorkerSet extends Wrk { public maxElementsPerWorker: number; - private workers: Set; - private lastWorkerNumberOfElements: number; + private workers: Set; /** * Create a new `WorkerSet`. @@ -18,9 +17,8 @@ export default class WorkerSet extends Wrk { */ constructor(workerScript: string, maxElementsPerWorker = 1) { super(workerScript); - this.workers = new Set(); + this.workers = new Set(); this.maxElementsPerWorker = maxElementsPerWorker; - this.lastWorkerNumberOfElements = 0; } get size(): number { @@ -36,14 +34,13 @@ export default class WorkerSet extends Wrk { if (!this.workers) { throw Error('Cannot add a WorkerSet element: workers set does not exist'); } - if (this.lastWorkerNumberOfElements >= this.maxElementsPerWorker) { + if (this.getLastWorkerSetElement().numberOfWorkerElements >= this.maxElementsPerWorker) { void this.startWorker(); - this.lastWorkerNumberOfElements = 0; // Start worker sequentially to optimize memory at startup void Utils.sleep(Constants.START_WORKER_DELAY); } this.getLastWorker().postMessage({ id: WorkerEvents.START_WORKER_ELEMENT, workerData: elementData }); - this.lastWorkerNumberOfElements++; + this.getLastWorkerSetElement().numberOfWorkerElements++; } /** @@ -72,14 +69,18 @@ export default class WorkerSet extends Wrk { reject(new Error(`Worker stopped with exit code ${code}`)); } }); - this.workers.add(worker); + this.workers.add({ worker, numberOfWorkerElements: 0 }); }); } - private getLastWorker(): Worker { - let worker: Worker; + private getLastWorkerSetElement(): WorkerSetElement { + let workerSetElement: WorkerSetElement; // eslint-disable-next-line no-empty - for (worker of this.workers) { } - return worker; + for (workerSetElement of this.workers) { } + return workerSetElement; + } + + private getLastWorker(): Worker { + return this.getLastWorkerSetElement().worker; } } diff --git a/src/worker/Wrk.ts b/src/worker/Wrk.ts index bea6d67f..02589a59 100644 --- a/src/worker/Wrk.ts +++ b/src/worker/Wrk.ts @@ -1,4 +1,4 @@ -import WorkerData from '../types/WorkerData'; +import { WorkerData } from '../types/Worker'; export default abstract class Wrk { protected workerScript: string;