Add WorkerPool file
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 23 Jan 2021 06:52:53 +0000 (07:52 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 23 Jan 2021 06:52:53 +0000 (07:52 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/WorkerPool.ts [new file with mode: 0644]

diff --git a/src/charging-station/WorkerPool.ts b/src/charging-station/WorkerPool.ts
new file mode 100644 (file)
index 0000000..40c284e
--- /dev/null
@@ -0,0 +1,25 @@
+import { Worker, WorkerOptions } from 'worker_threads';
+
+import Pool from 'worker-threads-pool';
+
+export default class WorkerPool {
+  public static maxConcurrentWorkers: number;
+  private static instance: Pool;
+
+  private constructor() { }
+
+  public static getInstance(): Pool {
+    if (!WorkerPool.instance) {
+      WorkerPool.instance = new Pool({ max: WorkerPool.maxConcurrentWorkers });
+    }
+    return WorkerPool.instance;
+  }
+
+  public static acquire(filename: string, options: WorkerOptions, callback: (error: Error | null, worker: Worker) => void): void {
+    WorkerPool.getInstance().acquire(filename, options, callback);
+  }
+
+  public static getPoolSize(): number {
+    return WorkerPool.getInstance().size;
+  }
+}