1 import type { PoolOptions
} from
'../pool'
2 import { PoolType
} from
'../pool'
3 import type { ThreadWorkerWithMessageChannel
} from
'./fixed'
4 import { FixedThreadPool
} from
'./fixed'
7 * A thread pool with a dynamic number of threads, but a guaranteed minimum number of threads.
9 * This thread pool creates new threads when the others are busy, up to the maximum number of threads.
10 * 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`.
12 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
13 * @typeParam Response - Type of response of execution. This can only be serializable data.
14 * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
17 export class DynamicThreadPool
<
20 > extends FixedThreadPool
<Data
, Response
> {
22 * Constructs a new poolifier dynamic thread pool.
24 * @param min - Minimum number of threads which are always active.
25 * @param max - Maximum number of threads that can be created by this pool.
26 * @param filePath - Path to an implementation of a `ThreadWorker` file, which can be relative or absolute.
27 * @param opts - Options for this dynamic thread pool.
31 public readonly max
: number,
33 opts
: PoolOptions
<ThreadWorkerWithMessageChannel
> = {}
35 super(min
, filePath
, opts
)
39 public get
type (): PoolType
{
40 return PoolType
.DYNAMIC
44 protected get
full (): boolean {
45 return this.workerNodes
.length
=== this.max
49 protected get
busy (): boolean {
50 return this.full
&& this.internalBusy()