014e3e8057333e97395bfc0b1a9ecea3efad7db2
1 import { PoolEvents
, type PoolType
, PoolTypes
} from
'../pool.js'
2 import { checkDynamicPoolSize
} from
'../utils.js'
3 import { type ClusterPoolOptions
, FixedClusterPool
} from
'./fixed.js'
6 * A cluster pool with a dynamic number of workers, but a guaranteed minimum number of workers.
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)
15 export class DynamicClusterPool
<
18 > extends FixedClusterPool
<Data
, Response
> {
20 * Whether the pool empty event has been emitted or not
22 private emptyEventEmitted
: boolean
25 * Whether the pool full event has been emitted or not.
27 private fullEventEmitted
: boolean
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.
40 opts
: ClusterPoolOptions
= {}
42 super(min
, filePath
, opts
, max
)
44 this.minimumNumberOfWorkers
,
45 this.maximumNumberOfWorkers
47 this.emptyEventEmitted
= false
48 this.fullEventEmitted
= false
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
58 if (this.emptyEventEmitted
&& !this.empty
) {
59 this.emptyEventEmitted
= false
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
71 if (!this.emptyEventEmitted
&& this.empty
) {
72 this.emitter
.emit(PoolEvents
.empty
, this.info
)
73 this.emptyEventEmitted
= true
79 protected override
shallCreateDynamicWorker (): boolean {
80 return (!this.full
&& this.internalBusy()) || this.empty
84 protected override get
backPressure (): boolean {
85 return this.full
&& this.internalBackPressure()
89 protected override get
busy (): boolean {
90 return this.full
&& this.internalBusy()
94 * Whether the pool is empty or not.
95 * @returns The pool emptiness boolean status.
97 private get
empty (): boolean {
99 this.minimumNumberOfWorkers
=== 0 &&
100 this.workerNodes
.length
=== this.minimumNumberOfWorkers
105 * Whether the pool is full or not.
106 * @returns The pool fullness boolean status.
108 private get
full (): boolean {
110 this.workerNodes
.length
>=
111 (this.maximumNumberOfWorkers
?? this.minimumNumberOfWorkers
)
116 protected override get
type (): PoolType
{
117 return PoolTypes
.dynamic