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