src/pools/selection-strategies/weighted-round-robin-choice-strategy.ts -> src/pools...
[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
bf9549ae
JB
13/**
14 * Tasks usage statistics.
15 */
16export interface TasksUsage {
17 run: number
18 running: number
19 runTime: number
20 avgRunTime: number
21}
22
a35560ba
S
23/**
24 * Internal poolifier pool emitter.
25 */
26export class PoolEmitter extends EventEmitter {}
27
28/**
29 * Internal contract definition for a poolifier pool.
30 *
31 * @template Worker Type of worker which manages this pool.
32 * @template Data Type of data sent to the worker.
33 * @template Response Type of response of execution.
34 */
35export interface IPoolInternal<
bdaf31cd 36 Worker extends AbstractPoolWorker,
a35560ba
S
37 Data = unknown,
38 Response = unknown
39> extends IPool<Data, Response> {
40 /**
41 * List of currently available workers.
42 */
43 readonly workers: Worker[]
44
a35560ba
S
45 /**
46 * Emitter on which events can be listened to.
47 *
48 * Events that can currently be listened to:
49 *
330c983e 50 * - `'busy'`
a35560ba 51 */
7c0ba920 52 readonly emitter?: PoolEmitter
a35560ba 53
a35560ba 54 /**
7c0ba920 55 * Pool type.
a35560ba 56 *
7c0ba920 57 * If it is `'dynamic'`, it provides the `max` property.
a35560ba 58 */
7c0ba920 59 readonly type: PoolType
ff5e76e1
JB
60
61 /**
62 * Maximum number of workers that can be created by this pool.
63 */
64 readonly max?: number
7c0ba920
JB
65
66 /**
67 * Whether the pool is busy or not.
68 *
69 * The pool busyness boolean status.
70 */
71 readonly busy: boolean
72
73 /**
74 * Number of tasks currently concurrently running.
75 */
76 readonly numberOfRunningTasks: number
77
78 /**
bdaf31cd 79 * Find a free worker based on the number of tasks the worker has applied.
7c0ba920 80 *
bdaf31cd 81 * If a worker is found with `0` running tasks, it is detected as free and returned.
7c0ba920 82 *
bdaf31cd 83 * If no free worker is found, `false` is returned.
7c0ba920 84 *
bdaf31cd 85 * @returns A free worker if there is one, otherwise `false`.
7c0ba920 86 */
bdaf31cd
JB
87 findFreeWorker(): Worker | false
88
89 /**
90 * Get worker index.
91 *
92 * @param worker The worker.
93 * @returns The worker index.
94 */
95 getWorkerIndex(worker: Worker): number
96
97 /**
98 * Get worker running tasks.
99 *
100 * @param worker The worker.
101 * @returns The number of tasks currently running on the worker.
102 */
103 getWorkerRunningTasks(worker: Worker): number | undefined
bf9549ae
JB
104
105 /**
106 * Get worker average tasks run time.
107 *
108 * @param worker The worker.
109 * @returns The average tasks run time on the worker.
110 */
111 getWorkerAverageTasksRunTime(worker: Worker): number | undefined
a35560ba 112}