6 } from
'node:worker_threads'
7 import type { MessageValue
} from
'../../utility-types'
8 import { AbstractPool
} from
'../abstract-pool'
18 * Options for a poolifier thread pool.
20 export interface ThreadPoolOptions
extends PoolOptions
<Worker
> {
24 * @see https://nodejs.org/api/worker_threads.html#new-workerfilename-options
26 workerOptions
?: WorkerOptions
30 * A thread pool with a fixed number of threads.
32 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
33 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
34 * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
37 export class FixedThreadPool
<
40 > extends AbstractPool
<Worker
, Data
, Response
> {
42 * Constructs a new poolifier fixed thread pool.
44 * @param numberOfThreads - Number of threads for this pool.
45 * @param filePath - Path to an implementation of a `ThreadWorker` file, which can be relative or absolute.
46 * @param opts - Options for this fixed thread pool.
49 numberOfThreads
: number,
51 protected readonly opts
: ThreadPoolOptions
= {}
53 super(numberOfThreads
, filePath
, opts
)
57 protected isMain (): boolean {
62 protected async destroyWorker (worker
: Worker
): Promise
<void> {
63 this.sendToWorker(worker
, { kill
: 1 })
64 await worker
.terminate()
68 protected sendToWorker (worker
: Worker
, message
: MessageValue
<Data
>): void {
69 worker
.postMessage(message
)
73 protected createWorker (): Worker
{
74 return new Worker(this.filePath
, {
76 ...this.opts
.workerOptions
81 protected get
type (): PoolType
{
82 return PoolTypes
.fixed
86 protected get
worker (): WorkerType
{
87 return WorkerTypes
.thread
91 protected get
minSize (): number {
92 return this.numberOfWorkers
96 protected get
maxSize (): number {
97 return this.numberOfWorkers
101 protected get
busy (): boolean {
102 return this.internalBusy()