From 78399acb675c5482ec3f87c0536f2f7082d63f5d Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 23 Jan 2021 07:52:53 +0100 Subject: [PATCH] Add WorkerPool file MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/WorkerPool.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/charging-station/WorkerPool.ts 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; + } +} -- 2.34.1