perf: remove unneeded class indirection for dynamic pool in worker
[poolifier.git] / src / pools / cluster / dynamic.ts
CommitLineData
7c0ba920 1import { PoolType } from '../pool-internal'
c97c7edb 2import type { ClusterPoolOptions } from './fixed'
325f50bc 3import { FixedClusterPool } from './fixed'
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`.
729c563d 10 *
38e795c1
JB
11 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
12 * @typeParam Response - Type of response of execution. This can only be serializable data.
325f50bc
S
13 * @author [Christopher Quadflieg](https://github.com/Shinigami92)
14 * @since 2.0.0
4ade5f1f 15 */
325f50bc 16export class DynamicClusterPool<
deb85c12
JB
17 Data = unknown,
18 Response = unknown
325f50bc 19> extends FixedClusterPool<Data, Response> {
4ade5f1f 20 /**
729c563d
S
21 * Constructs a new poolifier dynamic cluster pool.
22 *
38e795c1
JB
23 * @param min - Minimum number of workers which are always active.
24 * @param max - Maximum number of workers that can be created by this pool.
25 * @param filePath - Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
26 * @param opts - Options for this dynamic cluster pool.
4ade5f1f
S
27 */
28 public constructor (
c97c7edb 29 min: number,
c2ade475 30 private readonly max: number,
31b90205 31 filePath: string,
ed6dd37f 32 opts: ClusterPoolOptions = {}
4ade5f1f 33 ) {
31b90205 34 super(min, filePath, opts)
4ade5f1f
S
35 }
36
38e795c1 37 /** {@inheritDoc} */
7c0ba920
JB
38 public get type (): PoolType {
39 return PoolType.DYNAMIC
40 }
41
38e795c1 42 /** {@inheritDoc} */
c2ade475 43 public get full (): boolean {
e65c6cd9 44 return this.workers.length === this.max
4ade5f1f 45 }
c2ade475
JB
46
47 /** {@inheritDoc} */
48 public get busy (): boolean {
49 return this.full && this.findFreeWorkerKey() === -1
50 }
4ade5f1f 51}