| 1 | import type { IPoolInternal } from '../pool-internal' |
| 2 | import type { IPoolWorker } from '../pool-worker' |
| 3 | |
| 4 | /** |
| 5 | * Enumeration of worker choice strategies. |
| 6 | */ |
| 7 | export const WorkerChoiceStrategies = Object.freeze({ |
| 8 | /** |
| 9 | * Round robin worker selection strategy. |
| 10 | */ |
| 11 | ROUND_ROBIN: 'ROUND_ROBIN', |
| 12 | /** |
| 13 | * Less used worker selection strategy. |
| 14 | */ |
| 15 | LESS_USED: 'LESS_USED', |
| 16 | /** |
| 17 | * Less busy worker selection strategy. |
| 18 | */ |
| 19 | LESS_BUSY: 'LESS_BUSY', |
| 20 | /** |
| 21 | * Fair share worker selection strategy. |
| 22 | */ |
| 23 | FAIR_SHARE: 'FAIR_SHARE', |
| 24 | /** |
| 25 | * Weighted round robin worker selection strategy. |
| 26 | */ |
| 27 | WEIGHTED_ROUND_ROBIN: 'WEIGHTED_ROUND_ROBIN' |
| 28 | } as const) |
| 29 | |
| 30 | /** |
| 31 | * Worker choice strategy. |
| 32 | */ |
| 33 | export type WorkerChoiceStrategy = keyof typeof WorkerChoiceStrategies |
| 34 | |
| 35 | /** |
| 36 | * Pool worker tasks usage statistics requirements. |
| 37 | */ |
| 38 | export interface RequiredStatistics { |
| 39 | runTime: boolean |
| 40 | avgRunTime: boolean |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Worker choice strategy interface. |
| 45 | */ |
| 46 | export interface IWorkerChoiceStrategy< |
| 47 | Worker extends IPoolWorker, |
| 48 | Data = unknown, |
| 49 | Response = unknown |
| 50 | > { |
| 51 | /** |
| 52 | * The pool instance. |
| 53 | */ |
| 54 | readonly pool: IPoolInternal<Worker, Data, Response> |
| 55 | /** |
| 56 | * Is the pool attached to the strategy dynamic?. |
| 57 | */ |
| 58 | readonly isDynamicPool: boolean |
| 59 | /** |
| 60 | * Required pool tasks usage statistics. |
| 61 | */ |
| 62 | readonly requiredStatistics: RequiredStatistics |
| 63 | /** |
| 64 | * Resets strategy internals (counters, statistics, etc.). |
| 65 | */ |
| 66 | reset: () => boolean |
| 67 | /** |
| 68 | * Chooses a worker in the pool and returns its key. |
| 69 | */ |
| 70 | choose: () => number |
| 71 | /** |
| 72 | * Removes a worker reference from strategy internals. |
| 73 | * |
| 74 | * @param workerKey - The worker key. |
| 75 | */ |
| 76 | remove: (workerKey: number) => boolean |
| 77 | } |