9 } from
'node:worker_threads'
10 import type { MessageValue
} from
'../../utility-types'
11 import { AbstractPool
} from
'../abstract-pool'
12 import { type PoolOptions
, type PoolType
, PoolTypes
} from
'../pool'
13 import { type WorkerType
, WorkerTypes
} from
'../worker'
16 * Options for a poolifier thread pool.
18 export interface ThreadPoolOptions
extends PoolOptions
<Worker
> {
22 * @see https://nodejs.org/api/worker_threads.html#new-workerfilename-options
24 workerOptions
?: WorkerOptions
28 * A thread pool with a fixed number of threads.
30 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
31 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
32 * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
35 export class FixedThreadPool
<
38 > extends AbstractPool
<Worker
, Data
, Response
> {
40 * Constructs a new poolifier fixed thread pool.
42 * @param numberOfThreads - Number of threads for this pool.
43 * @param filePath - Path to an implementation of a `ThreadWorker` file, which can be relative or absolute.
44 * @param opts - Options for this fixed thread pool.
47 numberOfThreads
: number,
49 protected readonly opts
: ThreadPoolOptions
= {}
51 super(numberOfThreads
, filePath
, opts
)
55 protected isMain (): boolean {
60 protected async destroyWorkerNode (workerNodeKey
: number): Promise
<void> {
61 this.flagWorkerNodeAsNotReady(workerNodeKey
)
62 this.flushTasksQueue(workerNodeKey
)
63 // FIXME: wait for tasks to be finished
64 const workerNode
= this.workerNodes
[workerNodeKey
]
65 const worker
= workerNode
.worker
66 const waitWorkerExit
= new Promise
<void>(resolve
=> {
67 worker
.once('exit', () => {
71 await this.sendKillMessageToWorker(workerNodeKey
)
72 workerNode
.closeChannel()
73 await worker
.terminate()
78 protected sendToWorker (
79 workerNodeKey
: number,
80 message
: MessageValue
<Data
>,
81 transferList
?: TransferListItem
[]
84 this.workerNodes
[workerNodeKey
]?.messageChannel
as MessageChannel
85 )?.port1
?.postMessage(
86 { ...message
, workerId
: this.getWorkerInfo(workerNodeKey
).id
},
92 protected sendStartupMessageToWorker (workerNodeKey
: number): void {
93 const workerNode
= this.workerNodes
[workerNodeKey
]
94 const port2
: MessagePort
= (workerNode
.messageChannel
as MessageChannel
)
96 workerNode
.worker
.postMessage(
99 workerId
: this.getWorkerInfo(workerNodeKey
).id
,
107 protected registerWorkerMessageListener
<Message
extends Data
| Response
>(
108 workerNodeKey
: number,
109 listener
: (message
: MessageValue
<Message
>) => void
112 this.workerNodes
[workerNodeKey
]?.messageChannel
as MessageChannel
113 ).port1
.on('message', listener
)
117 protected registerOnceWorkerMessageListener
<Message
extends Data
| Response
>(
118 workerNodeKey
: number,
119 listener
: (message
: MessageValue
<Message
>) => void
122 this.workerNodes
[workerNodeKey
]?.messageChannel
as MessageChannel
123 ).port1
.once('message', listener
)
127 protected deregisterWorkerMessageListener
<Message
extends Data
| Response
>(
128 workerNodeKey
: number,
129 listener
: (message
: MessageValue
<Message
>) => void
132 this.workerNodes
[workerNodeKey
]?.messageChannel
as MessageChannel
133 ).port1
.off('message', listener
)
137 protected createWorker (): Worker
{
138 return new Worker(this.filePath
, {
140 ...this.opts
.workerOptions
145 protected get
type (): PoolType
{
146 return PoolTypes
.fixed
150 protected get
worker (): WorkerType
{
151 return WorkerTypes
.thread
155 protected get
busy (): boolean {
156 return this.internalBusy()