564dbf54a91adec20804bc5598e800e8301e930a
1 import { checkDynamicPoolSize
} from
'../utils.js'
2 import { PoolEvents
, type PoolType
, PoolTypes
} from
'../pool.js'
3 import { type ClusterPoolOptions
, FixedClusterPool
} from
'./fixed.js'
6 * A cluster pool with a dynamic number of workers, but a guaranteed minimum number of workers.
8 * This cluster pool creates new workers when the others are busy, up to the maximum number of workers.
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`.
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 [Christopher Quadflieg](https://github.com/Shinigami92)
16 export class DynamicClusterPool
<
19 > extends FixedClusterPool
<Data
, Response
> {
21 * Constructs a new poolifier dynamic cluster pool.
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.
32 opts
: ClusterPoolOptions
= {}
34 super(min
, filePath
, opts
, max
)
36 this.minimumNumberOfWorkers
,
37 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
38 this.maximumNumberOfWorkers
!
43 protected shallCreateDynamicWorker (): boolean {
45 (!this.full
&& this.internalBusy()) ||
46 (this.minimumNumberOfWorkers
=== 0 && this.workerNodes
.length
=== 0)
51 protected checkAndEmitDynamicWorkerCreationEvents (): void {
53 this.emitter
?.emit(PoolEvents
.full
, this.info
)
58 protected get
type (): PoolType
{
59 return PoolTypes
.dynamic
63 protected get
busy (): boolean {
64 return this.full
&& this.internalBusy()