8 } from
'node:worker_threads'
9 import type { MessageValue
} from
'../../utility-types'
10 import { AbstractPool
} from
'../abstract-pool'
11 import { type PoolOptions
, type PoolType
, PoolTypes
} from
'../pool'
12 import { type WorkerType
, WorkerTypes
} from
'../worker'
15 * Options for a poolifier thread pool.
17 export interface ThreadPoolOptions
extends PoolOptions
<Worker
> {
21 * @see https://nodejs.org/api/worker_threads.html#new-workerfilename-options
23 workerOptions
?: WorkerOptions
27 * A thread pool with a fixed number of threads.
29 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
30 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
31 * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
34 export class FixedThreadPool
<
37 > extends AbstractPool
<Worker
, Data
, Response
> {
39 * Constructs a new poolifier fixed thread pool.
41 * @param numberOfThreads - Number of threads for this pool.
42 * @param filePath - Path to an implementation of a `ThreadWorker` file, which can be relative or absolute.
43 * @param opts - Options for this fixed thread pool.
46 numberOfThreads
: number,
48 protected readonly opts
: ThreadPoolOptions
= {}
50 super(numberOfThreads
, filePath
, opts
)
54 protected isMain (): boolean {
59 protected async destroyWorkerNode (workerNodeKey
: number): Promise
<void> {
60 this.flushTasksQueue(workerNodeKey
)
61 // FIXME: wait for tasks to be finished
62 const workerNode
= this.workerNodes
[workerNodeKey
]
63 const worker
= workerNode
.worker
64 const workerExitPromise
= new Promise
<void>(resolve
=> {
65 worker
.on('exit', () => {
69 this.sendToWorker(workerNodeKey
, { kill
: true, workerId
: worker
.threadId
})
70 workerNode
.closeChannel()
71 await worker
.terminate()
72 await workerExitPromise
76 protected sendToWorker (
77 workerNodeKey
: number,
78 message
: MessageValue
<Data
>
81 this.getWorkerInfo(workerNodeKey
).messageChannel
as MessageChannel
82 ).port1
.postMessage(message
)
86 protected sendStartupMessageToWorker (workerNodeKey
: number): void {
87 const worker
= this.workerNodes
[workerNodeKey
].worker
88 const port2
: MessagePort
= (
89 this.getWorkerInfo(workerNodeKey
).messageChannel
as MessageChannel
94 workerId
: worker
.threadId
,
102 protected registerWorkerMessageListener
<Message
extends Data
| Response
>(
103 workerNodeKey
: number,
104 listener
: (message
: MessageValue
<Message
>) => void
107 this.getWorkerInfo(workerNodeKey
).messageChannel
as MessageChannel
108 ).port1
.on('message', listener
)
112 protected createWorker (): Worker
{
113 return new Worker(this.filePath
, {
115 ...this.opts
.workerOptions
120 protected get
type (): PoolType
{
121 return PoolTypes
.fixed
125 protected get
worker (): WorkerType
{
126 return WorkerTypes
.thread
130 protected get
minSize (): number {
131 return this.numberOfWorkers
135 protected get
maxSize (): number {
136 return this.numberOfWorkers
140 protected get
busy (): boolean {
141 return this.internalBusy()