1 import { PoolEvents
, type PoolType
, PoolTypes
} from
'../pool.js'
2 import { checkDynamicPoolSize
} from
'../utils.js'
3 import { FixedThreadPool
, type ThreadPoolOptions
} from
'./fixed.js'
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`.
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.
12 * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
15 export class DynamicThreadPool
<
18 > extends FixedThreadPool
<Data
, Response
> {
20 * Constructs a new poolifier dynamic thread pool.
21 * @param min - Minimum number of threads which are always active.
22 * @param max - Maximum number of threads that can be created by this pool.
23 * @param filePath - Path to an implementation of a `ThreadWorker` file, which can be relative or absolute.
24 * @param opts - Options for this dynamic thread pool.
30 opts
: ThreadPoolOptions
= {}
32 super(min
, filePath
, opts
, max
)
34 this.minimumNumberOfWorkers
,
35 this.maximumNumberOfWorkers
40 protected shallCreateDynamicWorker (): boolean {
41 return (!this.full
&& this.internalBusy()) || this.empty
45 protected checkAndEmitDynamicWorkerCreationEvents (): void {
47 this.emitter
?.emit(PoolEvents
.full
, this.info
)
52 protected get
type (): PoolType
{
53 return PoolTypes
.dynamic
57 protected get
busy (): boolean {
58 return this.full
&& this.internalBusy()