| 1 | import { type PoolType, PoolTypes } from '../pool' |
| 2 | import { type ClusterPoolOptions, FixedClusterPool } from './fixed' |
| 3 | |
| 4 | /** |
| 5 | * A cluster pool with a dynamic number of workers, but a guaranteed minimum number of workers. |
| 6 | * |
| 7 | * This cluster pool creates new workers when the others are busy, up to the maximum number of workers. |
| 8 | * 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`. |
| 9 | * |
| 10 | * @typeParam Data - Type of data sent to the worker. This can only be serializable data. |
| 11 | * @typeParam Response - Type of execution response. This can only be serializable data. |
| 12 | * @author [Christopher Quadflieg](https://github.com/Shinigami92) |
| 13 | * @since 2.0.0 |
| 14 | */ |
| 15 | export class DynamicClusterPool< |
| 16 | Data = unknown, |
| 17 | Response = unknown |
| 18 | > extends FixedClusterPool<Data, Response> { |
| 19 | /** |
| 20 | * Constructs a new poolifier dynamic cluster pool. |
| 21 | * |
| 22 | * @param min - Minimum number of workers which are always active. |
| 23 | * @param max - Maximum number of workers that can be created by this pool. |
| 24 | * @param filePath - Path to an implementation of a `ClusterWorker` file, which can be relative or absolute. |
| 25 | * @param opts - Options for this dynamic cluster pool. |
| 26 | */ |
| 27 | public constructor ( |
| 28 | min: number, |
| 29 | public readonly max: number, |
| 30 | filePath: string, |
| 31 | opts: ClusterPoolOptions = {} |
| 32 | ) { |
| 33 | super(min, filePath, opts) |
| 34 | } |
| 35 | |
| 36 | /** @inheritDoc */ |
| 37 | protected get type (): PoolType { |
| 38 | return PoolTypes.dynamic |
| 39 | } |
| 40 | |
| 41 | /** @inheritDoc */ |
| 42 | protected get maxSize (): number { |
| 43 | return this.max |
| 44 | } |
| 45 | |
| 46 | /** @inheritDoc */ |
| 47 | protected get full (): boolean { |
| 48 | return this.workerNodes.length >= this.max |
| 49 | } |
| 50 | |
| 51 | /** @inheritDoc */ |
| 52 | protected get busy (): boolean { |
| 53 | return this.full && this.internalBusy() |
| 54 | } |
| 55 | } |