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