Commit | Line | Data |
---|---|---|
78399acb JB |
1 | import { Worker, WorkerOptions } from 'worker_threads'; |
2 | ||
3 | import Pool from 'worker-threads-pool'; | |
4 | ||
5 | export default class WorkerPool { | |
6 | public static maxConcurrentWorkers: number; | |
7 | private static instance: Pool; | |
8 | ||
9 | private constructor() { } | |
10 | ||
11 | public static getInstance(): Pool { | |
12 | if (!WorkerPool.instance) { | |
13 | WorkerPool.instance = new Pool({ max: WorkerPool.maxConcurrentWorkers }); | |
14 | } | |
15 | return WorkerPool.instance; | |
16 | } | |
17 | ||
18 | public static acquire(filename: string, options: WorkerOptions, callback: (error: Error | null, worker: Worker) => void): void { | |
19 | WorkerPool.getInstance().acquire(filename, options, callback); | |
20 | } | |
21 | ||
22 | public static getPoolSize(): number { | |
23 | return WorkerPool.getInstance().size; | |
24 | } | |
25 | } |