From: Jérôme Benoit Date: Sat, 23 Jan 2021 06:52:53 +0000 (+0100) Subject: Add WorkerPool file X-Git-Tag: v1.0.1-0~123 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=78399acb675c5482ec3f87c0536f2f7082d63f5d;p=e-mobility-charging-stations-simulator.git Add WorkerPool file Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/WorkerPool.ts b/src/charging-station/WorkerPool.ts new file mode 100644 index 00000000..40c284e0 --- /dev/null +++ b/src/charging-station/WorkerPool.ts @@ -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; + } +}