Add dynamic worker choice strategy change at runtime
[poolifier.git] / src / pools / pool-internal.ts
CommitLineData
a35560ba 1import EventEmitter from 'events'
bdaf31cd 2import type { AbstractPoolWorker } from './abstract-pool-worker'
a35560ba
S
3import type { IPool } from './pool'
4
7c0ba920
JB
5/**
6 * Pool types.
7 */
8export enum PoolType {
9 FIXED = 'fixed',
10 DYNAMIC = 'dynamic'
11}
12
a35560ba
S
13/**
14 * Internal poolifier pool emitter.
15 */
16export class PoolEmitter extends EventEmitter {}
17
18/**
19 * Internal contract definition for a poolifier pool.
20 *
21 * @template Worker Type of worker which manages this pool.
22 * @template Data Type of data sent to the worker.
23 * @template Response Type of response of execution.
24 */
25export interface IPoolInternal<
bdaf31cd 26 Worker extends AbstractPoolWorker,
a35560ba
S
27 Data = unknown,
28 Response = unknown
29> extends IPool<Data, Response> {
30 /**
31 * List of currently available workers.
32 */
33 readonly workers: Worker[]
34
35 /**
36 * The tasks map.
37 *
38 * - `key`: The `Worker`
39 * - `value`: Number of tasks currently in progress on the worker.
40 */
41 readonly tasks: Map<Worker, number>
42
43 /**
44 * Emitter on which events can be listened to.
45 *
46 * Events that can currently be listened to:
47 *
330c983e 48 * - `'busy'`
a35560ba 49 */
7c0ba920 50 readonly emitter?: PoolEmitter
a35560ba 51
a35560ba 52 /**
7c0ba920 53 * Pool type.
a35560ba 54 *
7c0ba920 55 * If it is `'dynamic'`, it provides the `max` property.
a35560ba 56 */
7c0ba920 57 readonly type: PoolType
ff5e76e1
JB
58
59 /**
60 * Maximum number of workers that can be created by this pool.
61 */
62 readonly max?: number
7c0ba920
JB
63
64 /**
65 * Whether the pool is busy or not.
66 *
67 * The pool busyness boolean status.
68 */
69 readonly busy: boolean
70
71 /**
72 * Number of tasks currently concurrently running.
73 */
74 readonly numberOfRunningTasks: number
75
76 /**
bdaf31cd 77 * Find a free worker based on the number of tasks the worker has applied.
7c0ba920 78 *
bdaf31cd 79 * If a worker is found with `0` running tasks, it is detected as free and returned.
7c0ba920 80 *
bdaf31cd 81 * If no free worker is found, `false` is returned.
7c0ba920 82 *
bdaf31cd 83 * @returns A free worker if there is one, otherwise `false`.
7c0ba920 84 */
bdaf31cd
JB
85 findFreeWorker(): Worker | false
86
87 /**
88 * Get worker index.
89 *
90 * @param worker The worker.
91 * @returns The worker index.
92 */
93 getWorkerIndex(worker: Worker): number
94
95 /**
96 * Get worker running tasks.
97 *
98 * @param worker The worker.
99 * @returns The number of tasks currently running on the worker.
100 */
101 getWorkerRunningTasks(worker: Worker): number | undefined
a35560ba 102}