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.flushTasksQueue(workerNodeKey
)
62 // FIXME: wait for tasks to be finished
63 const workerNode
= this.workerNodes
[workerNodeKey
]
64 const worker
= workerNode
.worker
65 const waitWorkerExit
= new Promise
<void>(resolve
=> {
66 worker
.once('exit', () => {
70 await this.sendKillMessageToWorker(workerNodeKey
)
71 workerNode
.closeChannel()
72 await worker
.terminate()
77 protected sendToWorker (
78 workerNodeKey
: number,
79 message
: MessageValue
<Data
>,
80 transferList
?: TransferListItem
[]
83 this.workerNodes
[workerNodeKey
].messageChannel
as MessageChannel
85 { ...message
, workerId
: this.workerNodes
[workerNodeKey
].info
.id
},
91 protected sendStartupMessageToWorker (workerNodeKey
: number): void {
92 const workerNode
= this.workerNodes
[workerNodeKey
]
93 const port2
: MessagePort
= (workerNode
.messageChannel
as MessageChannel
)
95 workerNode
.worker
.postMessage(
98 workerId
: workerNode
.info
.id
,
106 protected registerWorkerMessageListener
<Message
extends Data
| Response
>(
107 workerNodeKey
: number,
108 listener
: (message
: MessageValue
<Message
>) => void
111 this.workerNodes
[workerNodeKey
].messageChannel
as MessageChannel
112 ).port1
.on('message', listener
)
116 protected registerOnceWorkerMessageListener
<Message
extends Data
| Response
>(
117 workerNodeKey
: number,
118 listener
: (message
: MessageValue
<Message
>) => void
121 this.workerNodes
[workerNodeKey
].messageChannel
as MessageChannel
122 ).port1
.once('message', listener
)
126 protected deregisterWorkerMessageListener
<Message
extends Data
| Response
>(
127 workerNodeKey
: number,
128 listener
: (message
: MessageValue
<Message
>) => void
131 this.workerNodes
[workerNodeKey
].messageChannel
as MessageChannel
132 ).port1
.off('message', listener
)
136 protected createWorker (): Worker
{
137 return new Worker(this.filePath
, {
139 ...this.opts
.workerOptions
144 protected get
type (): PoolType
{
145 return PoolTypes
.fixed
149 protected get
worker (): WorkerType
{
150 return WorkerTypes
.thread
154 protected get
busy (): boolean {
155 return this.internalBusy()