Improve JSDoc comments (#130)
[poolifier.git] / src / pools / cluster / dynamic.ts
1 import type { Worker } from 'cluster'
2 import type { JSONValue, MessageValue } from '../../utility-types'
3 import type { ClusterPoolOptions } from './fixed'
4 import { FixedClusterPool } from './fixed'
5
6 /**
7 * A cluster pool with a dynamic number of workers, but a guaranteed minimum number of workers.
8 *
9 * This cluster pool creates new workers when the others are busy, up to the maximum number of workers.
10 * 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`.
11 *
12 * @template Data Type of data sent to the worker.
13 * @template Response Type of response of execution.
14 *
15 * @author [Christopher Quadflieg](https://github.com/Shinigami92)
16 * @since 2.0.0
17 */
18 export class DynamicClusterPool<
19 Data extends JSONValue = JSONValue,
20 Response extends JSONValue = JSONValue
21 > extends FixedClusterPool<Data, Response> {
22 /**
23 * Constructs a new poolifier dynamic cluster pool.
24 *
25 * @param min Minimum number of workers which are always active.
26 * @param max Maximum number of workers that can be created by this pool.
27 * @param filename Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
28 * @param opts Options for this fixed cluster pool. Default: `{ maxTasks: 1000 }`
29 */
30 public constructor (
31 min: number,
32 public readonly max: number,
33 filename: string,
34 opts: ClusterPoolOptions = { maxTasks: 1000 }
35 ) {
36 super(min, filename, opts)
37 }
38
39 /**
40 * Choose a worker for the next task.
41 *
42 * It will first check for and return an idle worker.
43 * If all workers are busy, then it will try to create a new one up to the `max` worker count.
44 * 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.
45 */
46 protected chooseWorker (): Worker {
47 let worker: Worker | undefined
48 for (const entry of this.tasks) {
49 if (entry[1] === 0) {
50 worker = entry[0]
51 break
52 }
53 }
54
55 if (worker) {
56 // A worker is free, use it
57 return worker
58 } else {
59 if (this.workers.length === this.max) {
60 this.emitter.emit('FullPool')
61 return super.chooseWorker()
62 }
63 // All workers are busy, create a new worker
64 const worker = this.internalNewWorker()
65 worker.on('message', (message: MessageValue<Data>) => {
66 if (message.kill) {
67 this.sendToWorker(worker, { kill: 1 })
68 void this.destroyWorker(worker)
69 this.removeWorker(worker)
70 }
71 })
72 return worker
73 }
74 }
75 }