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
.on('exit', () => {
70 this.sendToWorker(workerNodeKey
, { kill
: true, workerId
: worker
.threadId
})
71 workerNode
.closeChannel()
72 await worker
.terminate()
77 protected sendToWorker (
78 workerNodeKey
: number,
79 message
: MessageValue
<Data
>,
80 transferList
?: TransferListItem
[]
83 this.getWorkerInfo(workerNodeKey
).messageChannel
as MessageChannel
84 ).port1
.postMessage(message
, transferList
)
88 protected sendStartupMessageToWorker (workerNodeKey
: number): void {
89 const worker
= this.workerNodes
[workerNodeKey
].worker
90 const port2
: MessagePort
= (
91 this.getWorkerInfo(workerNodeKey
).messageChannel
as MessageChannel
96 workerId
: worker
.threadId
,
104 protected registerWorkerMessageListener
<Message
extends Data
| Response
>(
105 workerNodeKey
: number,
106 listener
: (message
: MessageValue
<Message
>) => void
109 this.getWorkerInfo(workerNodeKey
).messageChannel
as MessageChannel
110 ).port1
.on('message', listener
)
114 protected createWorker (): Worker
{
115 return new Worker(this.filePath
, {
117 ...this.opts
.workerOptions
122 protected get
type (): PoolType
{
123 return PoolTypes
.fixed
127 protected get
worker (): WorkerType
{
128 return WorkerTypes
.thread
132 protected get
minSize (): number {
133 return this.numberOfWorkers
137 protected get
maxSize (): number {
138 return this.numberOfWorkers
142 protected get
busy (): boolean {
143 return this.internalBusy()