fix: prepare code to fix pool internal IPC for cluster worker
[poolifier.git] / src / pools / cluster / dynamic.ts
CommitLineData
6b27d407
JB
1import { type PoolType, PoolTypes } from '../pool'
2import { type ClusterPoolOptions, FixedClusterPool } from './fixed'
f045358d 3
4ade5f1f 4/**
729c563d 5 * A cluster pool with a dynamic number of workers, but a guaranteed minimum number of workers.
4ade5f1f 6 *
729c563d 7 * This cluster pool creates new workers when the others are busy, up to the maximum number of workers.
9cd39dd4 8 * 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`.
729c563d 9 *
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
S
20 * Constructs a new poolifier dynamic cluster pool.
21 *
38e795c1
JB
22 * @param min - Minimum number of workers which are always active.
23 * @param max - Maximum number of workers that can be created by this pool.
24 * @param filePath - Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
25 * @param opts - Options for this dynamic cluster pool.
4ade5f1f
S
26 */
27 public constructor (
c97c7edb 28 min: number,
b4213b7f 29 protected readonly max: number,
31b90205 30 filePath: string,
ed6dd37f 31 opts: ClusterPoolOptions = {}
4ade5f1f 32 ) {
31b90205 33 super(min, filePath, opts)
4ade5f1f
S
34 }
35
afc003b2 36 /** @inheritDoc */
8881ae32 37 protected get type (): PoolType {
6b27d407 38 return PoolTypes.dynamic
7c0ba920
JB
39 }
40
08f3f44c 41 /** @inheritDoc */
6b27d407 42 protected get maxSize (): number {
08f3f44c
JB
43 return this.max
44 }
45
afc003b2 46 /** @inheritDoc */
c319c66b 47 protected get busy (): boolean {
0527b6db 48 return this.full && this.internalBusy()
c2ade475 49 }
4ade5f1f 50}