perf: use worker key instead of worker instance
[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 error: number
21 }
22
23 /**
24 * Internal worker type.
25 *
26 * @typeParam Worker - Type of worker which manages this pool.
27 */
28 export interface WorkerType<Worker extends IPoolWorker> {
29 worker: Worker
30 tasksUsage: TasksUsage
31 }
32
33 /**
34 * Internal contract definition for a poolifier pool.
35 *
36 * @typeParam Worker - Type of worker which manages this pool.
37 * @typeParam Data - Type of data sent to the worker.
38 * @typeParam Response - Type of response of execution.
39 */
40 export interface IPoolInternal<
41 Worker extends IPoolWorker,
42 Data = unknown,
43 Response = unknown
44 > extends IPool<Data, Response> {
45 /**
46 * Pool workers item array.
47 */
48 readonly workers: Array<WorkerType<Worker>>
49
50 /**
51 * Pool type.
52 *
53 * If it is `'dynamic'`, it provides the `max` property.
54 */
55 readonly type: PoolType
56
57 /**
58 * Whether the pool is busy or not.
59 *
60 * The pool busyness boolean status.
61 */
62 readonly busy: boolean
63
64 /**
65 * Number of tasks currently concurrently running.
66 */
67 readonly numberOfRunningTasks: number
68
69 /**
70 * Finds a free worker based on the number of tasks the worker has applied.
71 *
72 * If a worker is found with `0` running tasks, it is detected as free and returned.
73 *
74 * If no free worker is found, `false` is returned.
75 *
76 * @returns A free worker if there is one, otherwise `false`.
77 */
78 findFreeWorker: () => Worker | false
79
80 /**
81 * Gets worker tasks usage.
82 *
83 * @param worker - The worker.
84 * @returns The tasks usage on the worker.
85 */
86 getWorkerTasksUsage: (worker: Worker) => TasksUsage | undefined
87 }