Add proper worker set elements reference counting.
authorJérôme Benoit <jerome.benoit@sap.com>
Mon, 25 Jan 2021 11:17:08 +0000 (12:17 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Mon, 25 Jan 2021 11:17:08 +0000 (12:17 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
package.json
src/charging-station/StationWorker.ts
src/start.ts
src/types/Worker.ts [new file with mode: 0644]
src/types/WorkerData.ts [deleted file]
src/types/WorkerEvents.ts [deleted file]
src/worker/WorkerPool.ts
src/worker/WorkerSet.ts
src/worker/Wrk.ts

index 8a4d7a29a79ea90d93d66b86554faf54f0f75859..36cea67b28334f159b0b5bae85e2818f86b4800d 100644 (file)
@@ -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",
index c0e85748ec0918b2f184d3561ee1f30100eddfb8..b22d8932731356198cd64818b9b3b9cc77699e19 100644 (file)
@@ -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
index 90eb5161fc4c85b3c3a6330e6398b7d82102a2ef..7f5822225cfe260d3073a12ce536f31dde881d18 100644 (file)
@@ -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 (file)
index 0000000..bc93d76
--- /dev/null
@@ -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 (file)
index a7bc2c2..0000000
+++ /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 (file)
index 07a4e42..0000000
+++ /dev/null
@@ -1,4 +0,0 @@
-
-export enum WorkerEvents {
-  START_WORKER_ELEMENT = 'startWorkerElement',
-}
index f1a6cc1f7a7acd0fc3e60bce255b5b3a4e2b3a48..5b9415c8d40bd69a2d55ad8a7cfa46b7a1bac83e 100644 (file)
@@ -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 {
index af753c8fc8dc8f1129e0d4ed2779234554414566..50d099d856633ff5cbc8ad6c5d1f57813a750b1e 100644 (file)
@@ -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<Worker>;
-  private lastWorkerNumberOfElements: number;
+  private workers: Set<WorkerSetElement>;
 
   /**
    * 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<Worker>();
+    this.workers = new Set<WorkerSetElement>();
     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;
   }
 }
index bea6d67f9b6bfdf86756cb060ad42d812ecf44e4..02589a593d7a7ecbf5bb416caa1168d0f67b2eec 100644 (file)
@@ -1,4 +1,4 @@
-import WorkerData from '../types/WorkerData';
+import { WorkerData } from '../types/Worker';
 
 export default abstract class Wrk {
   protected workerScript: string;