014e3e8057333e97395bfc0b1a9ecea3efad7db2
[poolifier.git] / src / pools / cluster / dynamic.ts
1 import { PoolEvents, type PoolType, PoolTypes } from '../pool.js'
2 import { checkDynamicPoolSize } from '../utils.js'
3 import { type ClusterPoolOptions, FixedClusterPool } from './fixed.js'
4
5 /**
6 * A cluster pool with a dynamic number of workers, but a guaranteed minimum number of workers.
7 *
8 * This cluster pool creates new workers when the others are busy, up to the maximum number of workers.
9 * When the maximum number of workers is reached and workers are busy, an event is emitted. If you want to listen to this event, use the pool's `emitter`.
10 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
11 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
12 * @author [Christopher Quadflieg](https://github.com/Shinigami92)
13 * @since 2.0.0
14 */
15 export class DynamicClusterPool<
16 Data = unknown,
17 Response = unknown
18 > extends FixedClusterPool<Data, Response> {
19 /**
20 * Whether the pool empty event has been emitted or not
21 */
22 private emptyEventEmitted: boolean
23
24 /**
25 * Whether the pool full event has been emitted or not.
26 */
27 private fullEventEmitted: boolean
28
29 /**
30 * Constructs a new poolifier dynamic cluster pool.
31 * @param min - Minimum number of workers which are always active.
32 * @param max - Maximum number of workers that can be created by this pool.
33 * @param filePath - Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
34 * @param opts - Options for this dynamic cluster pool.
35 */
36 public constructor (
37 min: number,
38 max: number,
39 filePath: string,
40 opts: ClusterPoolOptions = {}
41 ) {
42 super(min, filePath, opts, max)
43 checkDynamicPoolSize(
44 this.minimumNumberOfWorkers,
45 this.maximumNumberOfWorkers
46 )
47 this.emptyEventEmitted = false
48 this.fullEventEmitted = false
49 }
50
51 /** @inheritDoc */
52 protected override checkAndEmitDynamicWorkerCreationEvents (): void {
53 if (this.emitter != null) {
54 if (!this.fullEventEmitted && this.full) {
55 this.emitter.emit(PoolEvents.full, this.info)
56 this.fullEventEmitted = true
57 }
58 if (this.emptyEventEmitted && !this.empty) {
59 this.emptyEventEmitted = false
60 }
61 }
62 }
63
64 /** @inheritDoc */
65 protected override checkAndEmitDynamicWorkerDestructionEvents (): void {
66 if (this.emitter != null) {
67 if (this.fullEventEmitted && !this.full) {
68 this.emitter.emit(PoolEvents.fullEnd, this.info)
69 this.fullEventEmitted = false
70 }
71 if (!this.emptyEventEmitted && this.empty) {
72 this.emitter.emit(PoolEvents.empty, this.info)
73 this.emptyEventEmitted = true
74 }
75 }
76 }
77
78 /** @inheritDoc */
79 protected override shallCreateDynamicWorker (): boolean {
80 return (!this.full && this.internalBusy()) || this.empty
81 }
82
83 /** @inheritDoc */
84 protected override get backPressure (): boolean {
85 return this.full && this.internalBackPressure()
86 }
87
88 /** @inheritDoc */
89 protected override get busy (): boolean {
90 return this.full && this.internalBusy()
91 }
92
93 /**
94 * Whether the pool is empty or not.
95 * @returns The pool emptiness boolean status.
96 */
97 private get empty (): boolean {
98 return (
99 this.minimumNumberOfWorkers === 0 &&
100 this.workerNodes.length === this.minimumNumberOfWorkers
101 )
102 }
103
104 /**
105 * Whether the pool is full or not.
106 * @returns The pool fullness boolean status.
107 */
108 private get full (): boolean {
109 return (
110 this.workerNodes.length >=
111 (this.maximumNumberOfWorkers ?? this.minimumNumberOfWorkers)
112 )
113 }
114
115 /** @inheritDoc */
116 protected override get type (): PoolType {
117 return PoolTypes.dynamic
118 }
119 }