1 import { type PoolType
, PoolTypes
} from
'../pool'
2 import { checkDynamicPoolSize
} from
'../utils'
3 import { FixedThreadPool
, type ThreadPoolOptions
} from
'./fixed'
6 * A thread pool with a dynamic number of threads, but a guaranteed minimum number of threads.
8 * This thread pool creates new threads when the others are busy, up to the maximum number of threads.
9 * 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`.
11 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
12 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
13 * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
16 export class DynamicThreadPool
<
19 > extends FixedThreadPool
<Data
, Response
> {
21 * Constructs a new poolifier dynamic thread pool.
23 * @param min - Minimum number of threads which are always active.
24 * @param max - Maximum number of threads that can be created by this pool.
25 * @param filePath - Path to an implementation of a `ThreadWorker` file, which can be relative or absolute.
26 * @param opts - Options for this dynamic thread pool.
30 protected readonly max
: number,
32 opts
: ThreadPoolOptions
= {}
34 super(min
, filePath
, opts
)
35 checkDynamicPoolSize(this.numberOfWorkers
, this.max
)
39 protected get
type (): PoolType
{
40 return PoolTypes
.dynamic
44 protected get
busy (): boolean {
45 return this.full
&& this.internalBusy()