refactor: switch Date.now() -> performance.now() where appropriate
[poolifier.git] / src / pools / pool-internal.ts
CommitLineData
78099a15 1import type { CircularArray } from '../circular-array'
a35560ba 2import type { IPool } from './pool'
ea7a90d3 3import type { IPoolWorker } from './pool-worker'
a35560ba 4
7c0ba920 5/**
b4904890 6 * Internal pool types.
d99ba5a8
JB
7 *
8 * @enum
7c0ba920
JB
9 */
10export enum PoolType {
11 FIXED = 'fixed',
12 DYNAMIC = 'dynamic'
13}
14
bf9549ae 15/**
b4904890 16 * Internal tasks usage statistics.
bf9549ae
JB
17 */
18export interface TasksUsage {
19 run: number
20 running: number
21 runTime: number
78099a15 22 runTimeHistory: CircularArray<number>
bf9549ae 23 avgRunTime: number
78099a15 24 medRunTime: number
2740a743 25 error: number
bf9549ae
JB
26}
27
ffcbbad8
JB
28/**
29 * Internal worker type.
30 *
4579a142 31 * @typeParam Worker - Type of worker type items which manages this pool.
ffcbbad8
JB
32 */
33export interface WorkerType<Worker extends IPoolWorker> {
34 worker: Worker
35 tasksUsage: TasksUsage
36}
37
a35560ba
S
38/**
39 * Internal contract definition for a poolifier pool.
40 *
38e795c1 41 * @typeParam Worker - Type of worker which manages this pool.
9cd39dd4
JB
42 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
43 * @typeParam Response - Type of response of execution. This can only be serializable data.
a35560ba
S
44 */
45export interface IPoolInternal<
ea7a90d3 46 Worker extends IPoolWorker,
a35560ba
S
47 Data = unknown,
48 Response = unknown
49> extends IPool<Data, Response> {
50 /**
c2ade475 51 * Pool worker type items array.
a35560ba 52 */
e65c6cd9 53 readonly workers: Array<WorkerType<Worker>>
ea7a90d3 54
a35560ba 55 /**
7c0ba920 56 * Pool type.
a35560ba 57 *
7c0ba920 58 * If it is `'dynamic'`, it provides the `max` property.
a35560ba 59 */
7c0ba920 60 readonly type: PoolType
ff5e76e1 61
7c0ba920 62 /**
c2ade475 63 * Whether the pool is full or not.
7c0ba920 64 *
c2ade475 65 * The pool filling boolean status.
7c0ba920 66 */
c2ade475 67 readonly full: boolean
7c0ba920
JB
68
69 /**
c2ade475
JB
70 * Whether the pool is busy or not.
71 *
72 * The pool busyness boolean status.
7c0ba920 73 */
c2ade475 74 readonly busy: boolean
7c0ba920
JB
75
76 /**
c923ce56 77 * Finds a free worker key based on the number of tasks the worker has applied.
7c0ba920 78 *
c923ce56 79 * If a worker is found with `0` running tasks, it is detected as free and its key is returned.
7c0ba920 80 *
dba3384e 81 * If no free worker is found, `-1` is returned.
7c0ba920 82 *
bf90656c 83 * @returns A worker key if there is one, `-1` otherwise.
7c0ba920 84 */
bf90656c 85 findFreeWorkerKey: () => number
a35560ba 86}