| 1 | import type { CircularArray } from '../circular-array' |
| 2 | import type { IPool } from './pool' |
| 3 | import type { IPoolWorker } from './pool-worker' |
| 4 | |
| 5 | /** |
| 6 | * Internal pool types. |
| 7 | * |
| 8 | * @enum |
| 9 | */ |
| 10 | export enum PoolType { |
| 11 | FIXED = 'fixed', |
| 12 | DYNAMIC = 'dynamic' |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Internal tasks usage statistics. |
| 17 | */ |
| 18 | export interface TasksUsage { |
| 19 | run: number |
| 20 | running: number |
| 21 | runTime: number |
| 22 | runTimeHistory: CircularArray<number> |
| 23 | avgRunTime: number |
| 24 | medRunTime: number |
| 25 | error: number |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Internal worker type. |
| 30 | * |
| 31 | * @typeParam Worker - Type of worker type items which manages this pool. |
| 32 | */ |
| 33 | export interface WorkerType<Worker extends IPoolWorker> { |
| 34 | worker: Worker |
| 35 | tasksUsage: TasksUsage |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Internal contract definition for a poolifier pool. |
| 40 | * |
| 41 | * @typeParam Worker - Type of worker which manages this pool. |
| 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. |
| 44 | */ |
| 45 | export interface IPoolInternal< |
| 46 | Worker extends IPoolWorker, |
| 47 | Data = unknown, |
| 48 | Response = unknown |
| 49 | > extends IPool<Data, Response> { |
| 50 | /** |
| 51 | * Pool worker type items array. |
| 52 | */ |
| 53 | readonly workers: Array<WorkerType<Worker>> |
| 54 | |
| 55 | /** |
| 56 | * Pool type. |
| 57 | * |
| 58 | * If it is `'dynamic'`, it provides the `max` property. |
| 59 | */ |
| 60 | readonly type: PoolType |
| 61 | |
| 62 | /** |
| 63 | * Whether the pool is full or not. |
| 64 | * |
| 65 | * The pool filling boolean status. |
| 66 | */ |
| 67 | readonly full: boolean |
| 68 | |
| 69 | /** |
| 70 | * Whether the pool is busy or not. |
| 71 | * |
| 72 | * The pool busyness boolean status. |
| 73 | */ |
| 74 | readonly busy: boolean |
| 75 | |
| 76 | /** |
| 77 | * Finds a free worker key based on the number of tasks the worker has applied. |
| 78 | * |
| 79 | * If a worker is found with `0` running tasks, it is detected as free and its key is returned. |
| 80 | * |
| 81 | * If no free worker is found, `-1` is returned. |
| 82 | * |
| 83 | * @returns A worker key if there is one, `-1` otherwise. |
| 84 | */ |
| 85 | findFreeWorkerKey: () => number |
| 86 | } |