perf: drastically reduce worker nodes array lookups
[poolifier.git] / src / pools / thread / fixed.ts
CommitLineData
fc3e6586 1import {
85aeb3f3
JB
2 type MessageChannel,
3 type MessagePort,
fc3e6586 4 SHARE_ENV,
65d7a1c9 5 Worker,
90082c8c 6 type WorkerOptions,
65d7a1c9 7 isMainThread
fc3e6586 8} from 'node:worker_threads'
e102732c 9import type { MessageValue } from '../../utility-types'
c97c7edb 10import { AbstractPool } from '../abstract-pool'
4b628b48
JB
11import { type PoolOptions, type PoolType, PoolTypes } from '../pool'
12import { type WorkerType, WorkerTypes } from '../worker'
4ade5f1f 13
90082c8c
JB
14/**
15 * Options for a poolifier thread pool.
16 */
17export interface ThreadPoolOptions extends PoolOptions<Worker> {
18 /**
19 * Worker options.
20 *
21 * @see https://nodejs.org/api/worker_threads.html#new-workerfilename-options
22 */
23 workerOptions?: WorkerOptions
24}
25
4ade5f1f 26/**
729c563d
S
27 * A thread pool with a fixed number of threads.
28 *
e102732c
JB
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.
4ade5f1f
S
31 * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
32 * @since 0.0.1
33 */
d3c8a1a8 34export class FixedThreadPool<
deb85c12
JB
35 Data = unknown,
36 Response = unknown
e102732c 37> extends AbstractPool<Worker, Data, Response> {
4ade5f1f 38 /**
729c563d
S
39 * Constructs a new poolifier fixed thread pool.
40 *
38e795c1
JB
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.
4ade5f1f
S
44 */
45 public constructor (
5c5a1fb7 46 numberOfThreads: number,
c97c7edb 47 filePath: string,
90082c8c 48 protected readonly opts: ThreadPoolOptions = {}
4ade5f1f 49 ) {
5c5a1fb7 50 super(numberOfThreads, filePath, opts)
c97c7edb 51 }
4ade5f1f 52
afc003b2 53 /** @inheritDoc */
c97c7edb
S
54 protected isMain (): boolean {
55 return isMainThread
4ade5f1f
S
56 }
57
afc003b2 58 /** @inheritDoc */
aa9eede8
JB
59 protected async destroyWorkerNode (workerNodeKey: number): Promise<void> {
60 const workerNode = this.workerNodes[workerNodeKey]
61 const worker = workerNode.worker
62 this.sendToWorker(workerNodeKey, { kill: true, workerId: worker.threadId })
63 workerNode.closeChannel()
c97c7edb 64 await worker.terminate()
4ade5f1f
S
65 }
66
afc003b2 67 /** @inheritDoc */
aa9eede8
JB
68 protected sendToWorker (
69 workerNodeKey: number,
70 message: MessageValue<Data>
71 ): void {
85aeb3f3 72 (
aa9eede8 73 this.getWorkerInfo(workerNodeKey).messageChannel as MessageChannel
85aeb3f3
JB
74 ).port1.postMessage(message)
75 }
76
77 /** @inheritDoc */
aa9eede8
JB
78 protected sendStartupMessageToWorker (workerNodeKey: number): void {
79 const worker = this.workerNodes[workerNodeKey].worker
85aeb3f3 80 const port2: MessagePort = (
aa9eede8 81 this.getWorkerInfo(workerNodeKey).messageChannel as MessageChannel
85aeb3f3
JB
82 ).port2
83 worker.postMessage(
84 {
85 ready: false,
a038b517 86 workerId: worker.threadId,
85aeb3f3
JB
87 port: port2
88 },
89 [port2]
90 )
91 }
92
93 /** @inheritDoc */
94 protected registerWorkerMessageListener<Message extends Data | Response>(
aa9eede8 95 workerNodeKey: number,
85aeb3f3
JB
96 listener: (message: MessageValue<Message>) => void
97 ): void {
98 (
aa9eede8 99 this.getWorkerInfo(workerNodeKey).messageChannel as MessageChannel
85aeb3f3 100 ).port1.on('message', listener)
4ade5f1f
S
101 }
102
afc003b2 103 /** @inheritDoc */
e102732c 104 protected createWorker (): Worker {
c97c7edb 105 return new Worker(this.filePath, {
90082c8c
JB
106 env: SHARE_ENV,
107 ...this.opts.workerOptions
4ade5f1f 108 })
c97c7edb
S
109 }
110
afc003b2 111 /** @inheritDoc */
8881ae32 112 protected get type (): PoolType {
6b27d407 113 return PoolTypes.fixed
7c0ba920
JB
114 }
115
184855e6
JB
116 /** @inheritDoc */
117 protected get worker (): WorkerType {
118 return WorkerTypes.thread
119 }
120
08f3f44c 121 /** @inheritDoc */
6b27d407
JB
122 protected get minSize (): number {
123 return this.numberOfWorkers
124 }
125
126 /** @inheritDoc */
127 protected get maxSize (): number {
08f3f44c
JB
128 return this.numberOfWorkers
129 }
130
afc003b2 131 /** @inheritDoc */
c319c66b 132 protected get busy (): boolean {
c2ade475 133 return this.internalBusy()
7c0ba920 134 }
4ade5f1f 135}