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