perf: drastically reduce worker nodes array lookups
[poolifier.git] / src / pools / cluster / fixed.ts
CommitLineData
059438ff 1import cluster, { type ClusterSettings, type Worker } from 'node:cluster'
deb85c12 2import type { MessageValue } from '../../utility-types'
c97c7edb 3import { AbstractPool } from '../abstract-pool'
4b628b48
JB
4import { type PoolOptions, type PoolType, PoolTypes } from '../pool'
5import { type WorkerType, WorkerTypes } from '../worker'
4ade5f1f 6
729c563d
S
7/**
8 * Options for a poolifier cluster pool.
9 */
c97c7edb 10export interface ClusterPoolOptions extends PoolOptions<Worker> {
325f50bc
S
11 /**
12 * Key/value pairs to add to worker process environment.
13 *
14 * @see https://nodejs.org/api/cluster.html#cluster_cluster_fork_env
15 */
82f36766 16 env?: Record<string, unknown>
1a76932b
JB
17 /**
18 * Cluster settings.
19 *
20 * @see https://nodejs.org/api/cluster.html#cluster_cluster_settings
21 */
22 settings?: ClusterSettings
4ade5f1f
S
23}
24
25/**
729c563d
S
26 * A cluster pool with a fixed number of workers.
27 *
e102732c
JB
28 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
29 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
325f50bc
S
30 * @author [Christopher Quadflieg](https://github.com/Shinigami92)
31 * @since 2.0.0
4ade5f1f 32 */
d3c8a1a8 33export class FixedClusterPool<
deb85c12
JB
34 Data = unknown,
35 Response = unknown
d3c8a1a8 36> extends AbstractPool<Worker, Data, Response> {
4ade5f1f 37 /**
729c563d
S
38 * Constructs a new poolifier fixed cluster pool.
39 *
38e795c1
JB
40 * @param numberOfWorkers - Number of workers for this pool.
41 * @param filePath - Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
42 * @param opts - Options for this fixed cluster pool.
4ade5f1f
S
43 */
44 public constructor (
5c5a1fb7 45 numberOfWorkers: number,
c97c7edb 46 filePath: string,
dea903a8 47 protected readonly opts: ClusterPoolOptions = {}
4ade5f1f 48 ) {
5c5a1fb7 49 super(numberOfWorkers, filePath, opts)
c97c7edb 50 }
4ade5f1f 51
afc003b2 52 /** @inheritDoc */
c97c7edb 53 protected setupHook (): void {
1a76932b 54 cluster.setupPrimary({ ...this.opts.settings, exec: this.filePath })
c97c7edb 55 }
325f50bc 56
afc003b2 57 /** @inheritDoc */
c97c7edb 58 protected isMain (): boolean {
7e0d447f 59 return cluster.isPrimary
4ade5f1f
S
60 }
61
afc003b2 62 /** @inheritDoc */
aa9eede8
JB
63 protected destroyWorkerNode (workerNodeKey: number): void {
64 const worker = this.workerNodes[workerNodeKey].worker
65 this.sendToWorker(workerNodeKey, { kill: true, workerId: worker.id })
5b59d53e
JB
66 worker.on('disconnect', () => {
67 worker.kill()
68 })
69 worker.disconnect()
4ade5f1f
S
70 }
71
afc003b2 72 /** @inheritDoc */
aa9eede8
JB
73 protected sendToWorker (
74 workerNodeKey: number,
75 message: MessageValue<Data>
76 ): void {
77 this.workerNodes[workerNodeKey].worker.send(message)
4ade5f1f
S
78 }
79
85aeb3f3 80 /** @inheritDoc */
aa9eede8
JB
81 protected sendStartupMessageToWorker (workerNodeKey: number): void {
82 this.sendToWorker(workerNodeKey, {
85aeb3f3 83 ready: false,
aa9eede8 84 workerId: this.workerNodes[workerNodeKey].worker.id
85aeb3f3
JB
85 })
86 }
87
88 /** @inheritDoc */
89 protected registerWorkerMessageListener<Message extends Data | Response>(
aa9eede8 90 workerNodeKey: number,
85aeb3f3
JB
91 listener: (message: MessageValue<Message>) => void
92 ): void {
aa9eede8 93 this.workerNodes[workerNodeKey].worker.on('message', listener)
85aeb3f3
JB
94 }
95
afc003b2 96 /** @inheritDoc */
280c2a77 97 protected createWorker (): Worker {
7e0d447f 98 return cluster.fork(this.opts.env)
4ade5f1f
S
99 }
100
afc003b2 101 /** @inheritDoc */
8881ae32 102 protected get type (): PoolType {
6b27d407 103 return PoolTypes.fixed
7c0ba920
JB
104 }
105
184855e6
JB
106 /** @inheritDoc */
107 protected get worker (): WorkerType {
108 return WorkerTypes.cluster
109 }
110
08f3f44c 111 /** @inheritDoc */
6b27d407
JB
112 protected get minSize (): number {
113 return this.numberOfWorkers
114 }
115
116 /** @inheritDoc */
117 protected get maxSize (): number {
08f3f44c
JB
118 return this.numberOfWorkers
119 }
120
afc003b2 121 /** @inheritDoc */
c319c66b 122 protected get busy (): boolean {
c2ade475 123 return this.internalBusy()
7c0ba920 124 }
4ade5f1f 125}