1 import type { Worker
} from
'cluster'
2 import type { JSONValue
} from
'../../utility-types'
3 import { isKillBehavior
, KillBehaviors
} from
'../../worker/worker-options'
4 import type { ClusterPoolOptions
} from
'./fixed'
5 import { FixedClusterPool
} from
'./fixed'
8 * A cluster pool with a dynamic number of workers, but a guaranteed minimum number of workers.
10 * This cluster pool creates new workers when the others are busy, up to the maximum number of workers.
11 * When the maximum number of workers is reached, an event is emitted. If you want to listen to this event, use the pool's `emitter`.
13 * @template Data Type of data sent to the worker.
14 * @template Response Type of response of execution.
16 * @author [Christopher Quadflieg](https://github.com/Shinigami92)
19 export class DynamicClusterPool
<
20 Data
extends JSONValue
= JSONValue
,
21 Response
extends JSONValue
= JSONValue
22 > extends FixedClusterPool
<Data
, Response
> {
24 * Constructs a new poolifier dynamic cluster pool.
26 * @param min Minimum number of workers which are always active.
27 * @param max Maximum number of workers that can be created by this pool.
28 * @param filename Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
29 * @param opts Options for this fixed cluster pool. Default: `{ maxTasks: 1000 }`
33 public readonly max
: number,
35 opts
: ClusterPoolOptions
= { maxTasks
: 1000 }
37 super(min
, filename
, opts
)
41 * Choose a worker for the next task.
43 * It will first check for and return an idle worker.
44 * If all workers are busy, then it will try to create a new one up to the `max` worker count.
45 * If the max worker count is reached, the emitter will emit a `FullPool` event and it will fall back to using a round robin algorithm to distribute the load.
47 * @returns Cluster worker.
49 protected chooseWorker (): Worker
{
50 for (const [worker
, numberOfTasks
] of this.tasks
) {
51 if (numberOfTasks
=== 0) {
52 // A worker is free, use it
57 if (this.workers
.length
=== this.max
) {
58 this.emitter
.emit('FullPool')
59 return super.chooseWorker()
62 // All workers are busy, create a new worker
63 const workerCreated
= this.createAndSetupWorker()
64 this.registerWorkerMessageListener
<Data
>(workerCreated
, message
=> {
65 const tasksInProgress
= this.tasks
.get(workerCreated
)
67 isKillBehavior(KillBehaviors
.HARD
, message
.kill
) ||
70 // Kill received from the worker, means that no new tasks are submitted to that worker for a while ( > maxInactiveTime)
71 this.sendToWorker(workerCreated
, { kill
: 1 })
72 void this.destroyWorker(workerCreated
)