perf: drastically reduce worker nodes array lookups
[poolifier.git] / src / pools / cluster / fixed.ts
1 import cluster, { type ClusterSettings, type Worker } from 'node:cluster'
2 import type { MessageValue } from '../../utility-types'
3 import { AbstractPool } from '../abstract-pool'
4 import { type PoolOptions, type PoolType, PoolTypes } from '../pool'
5 import { type WorkerType, WorkerTypes } from '../worker'
6
7 /**
8 * Options for a poolifier cluster pool.
9 */
10 export interface ClusterPoolOptions extends PoolOptions<Worker> {
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 */
16 env?: Record<string, unknown>
17 /**
18 * Cluster settings.
19 *
20 * @see https://nodejs.org/api/cluster.html#cluster_cluster_settings
21 */
22 settings?: ClusterSettings
23 }
24
25 /**
26 * A cluster pool with a fixed number of workers.
27 *
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.
30 * @author [Christopher Quadflieg](https://github.com/Shinigami92)
31 * @since 2.0.0
32 */
33 export class FixedClusterPool<
34 Data = unknown,
35 Response = unknown
36 > extends AbstractPool<Worker, Data, Response> {
37 /**
38 * Constructs a new poolifier fixed cluster pool.
39 *
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.
43 */
44 public constructor (
45 numberOfWorkers: number,
46 filePath: string,
47 protected readonly opts: ClusterPoolOptions = {}
48 ) {
49 super(numberOfWorkers, filePath, opts)
50 }
51
52 /** @inheritDoc */
53 protected setupHook (): void {
54 cluster.setupPrimary({ ...this.opts.settings, exec: this.filePath })
55 }
56
57 /** @inheritDoc */
58 protected isMain (): boolean {
59 return cluster.isPrimary
60 }
61
62 /** @inheritDoc */
63 protected destroyWorkerNode (workerNodeKey: number): void {
64 const worker = this.workerNodes[workerNodeKey].worker
65 this.sendToWorker(workerNodeKey, { kill: true, workerId: worker.id })
66 worker.on('disconnect', () => {
67 worker.kill()
68 })
69 worker.disconnect()
70 }
71
72 /** @inheritDoc */
73 protected sendToWorker (
74 workerNodeKey: number,
75 message: MessageValue<Data>
76 ): void {
77 this.workerNodes[workerNodeKey].worker.send(message)
78 }
79
80 /** @inheritDoc */
81 protected sendStartupMessageToWorker (workerNodeKey: number): void {
82 this.sendToWorker(workerNodeKey, {
83 ready: false,
84 workerId: this.workerNodes[workerNodeKey].worker.id
85 })
86 }
87
88 /** @inheritDoc */
89 protected registerWorkerMessageListener<Message extends Data | Response>(
90 workerNodeKey: number,
91 listener: (message: MessageValue<Message>) => void
92 ): void {
93 this.workerNodes[workerNodeKey].worker.on('message', listener)
94 }
95
96 /** @inheritDoc */
97 protected createWorker (): Worker {
98 return cluster.fork(this.opts.env)
99 }
100
101 /** @inheritDoc */
102 protected get type (): PoolType {
103 return PoolTypes.fixed
104 }
105
106 /** @inheritDoc */
107 protected get worker (): WorkerType {
108 return WorkerTypes.cluster
109 }
110
111 /** @inheritDoc */
112 protected get minSize (): number {
113 return this.numberOfWorkers
114 }
115
116 /** @inheritDoc */
117 protected get maxSize (): number {
118 return this.numberOfWorkers
119 }
120
121 /** @inheritDoc */
122 protected get busy (): boolean {
123 return this.internalBusy()
124 }
125 }