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