| 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 worker type. |
| 24 | * |
| 25 | * @typeParam Worker - Type of worker which manages this pool. |
| 26 | */ |
| 27 | export interface WorkerType<Worker extends IPoolWorker> { |
| 28 | worker: Worker |
| 29 | tasksUsage: TasksUsage |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Internal contract definition for a poolifier pool. |
| 34 | * |
| 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. |
| 38 | */ |
| 39 | export interface IPoolInternal< |
| 40 | Worker extends IPoolWorker, |
| 41 | Data = unknown, |
| 42 | Response = unknown |
| 43 | > extends IPool<Data, Response> { |
| 44 | /** |
| 45 | * Map of workers. |
| 46 | */ |
| 47 | readonly workers: Map<number, WorkerType<Worker>> |
| 48 | |
| 49 | /** |
| 50 | * Pool type. |
| 51 | * |
| 52 | * If it is `'dynamic'`, it provides the `max` property. |
| 53 | */ |
| 54 | readonly type: PoolType |
| 55 | |
| 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 | /** |
| 69 | * Finds a free worker based on the number of tasks the worker has applied. |
| 70 | * |
| 71 | * If a worker is found with `0` running tasks, it is detected as free and returned. |
| 72 | * |
| 73 | * If no free worker is found, `false` is returned. |
| 74 | * |
| 75 | * @returns A free worker if there is one, otherwise `false`. |
| 76 | */ |
| 77 | findFreeWorker: () => Worker | false |
| 78 | |
| 79 | /** |
| 80 | * Gets worker running tasks. |
| 81 | * |
| 82 | * @param worker - The worker. |
| 83 | * @returns The number of tasks currently running on the worker. |
| 84 | */ |
| 85 | getWorkerRunningTasks: (worker: Worker) => number | undefined |
| 86 | |
| 87 | /** |
| 88 | * Gets worker average tasks runtime. |
| 89 | * |
| 90 | * @param worker - The worker. |
| 91 | * @returns The average tasks runtime on the worker. |
| 92 | */ |
| 93 | getWorkerAverageTasksRunTime: (worker: Worker) => number | undefined |
| 94 | } |