build(deps-dev): apply updates
[poolifier.git] / src / pools / cluster / dynamic.ts
CommitLineData
d35e5717 1import { PoolEvents, type PoolType, PoolTypes } from '../pool.js'
ded253e2 2import { checkDynamicPoolSize } from '../utils.js'
d35e5717 3import { type ClusterPoolOptions, FixedClusterPool } from './fixed.js'
f045358d 4
4ade5f1f 5/**
729c563d 6 * A cluster pool with a dynamic number of workers, but a guaranteed minimum number of workers.
4ade5f1f 7 *
729c563d 8 * This cluster pool creates new workers when the others are busy, up to the maximum number of workers.
9cd39dd4 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`.
e102732c
JB
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.
325f50bc
S
12 * @author [Christopher Quadflieg](https://github.com/Shinigami92)
13 * @since 2.0.0
4ade5f1f 14 */
325f50bc 15export class DynamicClusterPool<
deb85c12
JB
16 Data = unknown,
17 Response = unknown
325f50bc 18> extends FixedClusterPool<Data, Response> {
4ade5f1f 19 /**
729c563d 20 * Constructs a new poolifier dynamic cluster pool.
38e795c1
JB
21 * @param min - Minimum number of workers which are always active.
22 * @param max - Maximum number of workers that can be created by this pool.
23 * @param filePath - Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
24 * @param opts - Options for this dynamic cluster pool.
4ade5f1f
S
25 */
26 public constructor (
c97c7edb 27 min: number,
26ce26ca 28 max: number,
31b90205 29 filePath: string,
2889bd70 30 opts: ClusterPoolOptions = {}
4ade5f1f 31 ) {
26ce26ca
JB
32 super(min, filePath, opts, max)
33 checkDynamicPoolSize(
34 this.minimumNumberOfWorkers,
c63a35a0 35 this.maximumNumberOfWorkers
26ce26ca 36 )
4ade5f1f
S
37 }
38
9d9fb7b6
JB
39 /** @inheritDoc */
40 protected shallCreateDynamicWorker (): boolean {
8e8d9101 41 return (!this.full && this.internalBusy()) || this.empty
9d9fb7b6
JB
42 }
43
d0878034
JB
44 /** @inheritDoc */
45 protected checkAndEmitDynamicWorkerCreationEvents (): void {
46 if (this.full) {
47 this.emitter?.emit(PoolEvents.full, this.info)
48 }
49 }
50
afc003b2 51 /** @inheritDoc */
8881ae32 52 protected get type (): PoolType {
6b27d407 53 return PoolTypes.dynamic
7c0ba920
JB
54 }
55
afc003b2 56 /** @inheritDoc */
c319c66b 57 protected get busy (): boolean {
0527b6db 58 return this.full && this.internalBusy()
c2ade475 59 }
4ade5f1f 60}