refactor: limit pool internals public exposure
[poolifier.git] / src / pools / thread / dynamic.ts
CommitLineData
6b27d407
JB
1import { type PoolOptions, type PoolType, PoolTypes } from '../pool'
2import { FixedThreadPool, type ThreadWorkerWithMessageChannel } from './fixed'
f045358d 3
4ade5f1f 4/**
729c563d 5 * A thread pool with a dynamic number of threads, but a guaranteed minimum number of threads.
4ade5f1f 6 *
729c563d 7 * This thread pool creates new threads when the others are busy, up to the maximum number of threads.
9cd39dd4 8 * When the maximum number of threads 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 *
38e795c1 10 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
02706357 11 * @typeParam Response - Type of execution response. This can only be serializable data.
4ade5f1f
S
12 * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
13 * @since 0.0.1
14 */
60fbd6d6 15export class DynamicThreadPool<
deb85c12
JB
16 Data = unknown,
17 Response = unknown
4ade5f1f 18> extends FixedThreadPool<Data, Response> {
4ade5f1f 19 /**
729c563d
S
20 * Constructs a new poolifier dynamic thread pool.
21 *
38e795c1
JB
22 * @param min - Minimum number of threads which are always active.
23 * @param max - Maximum number of threads that can be created by this pool.
24 * @param filePath - Path to an implementation of a `ThreadWorker` file, which can be relative or absolute.
25 * @param opts - Options for this dynamic thread pool.
4ade5f1f
S
26 */
27 public constructor (
c97c7edb 28 min: number,
b4213b7f 29 protected readonly max: number,
31b90205 30 filePath: string,
1927ee67 31 opts: PoolOptions<ThreadWorkerWithMessageChannel> = {}
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
afc003b2 41 /** @inheritDoc */
18bfcd0b
JB
42 protected get maxSize (): number {
43 return this.max
4ade5f1f 44 }
c2ade475 45
08f3f44c 46 /** @inheritDoc */
18bfcd0b
JB
47 protected get full (): boolean {
48 return this.workerNodes.length >= this.max
08f3f44c
JB
49 }
50
afc003b2 51 /** @inheritDoc */
c319c66b 52 protected get busy (): boolean {
0527b6db 53 return this.full && this.internalBusy()
c2ade475 54 }
4ade5f1f 55}