| 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 | * Worker choice strategy options. |
| 34 | */ |
| 35 | export interface WorkerChoiceStrategyOptions { |
| 36 | /** |
| 37 | * Use tasks median run time instead of average run time. |
| 38 | * |
| 39 | * @defaultValue false |
| 40 | */ |
| 41 | medRunTime?: boolean |
| 42 | /** |
| 43 | * Worker weights to use for weighted round robin worker selection strategy. |
| 44 | * Weight is the tasks maximum average or median runtime in milliseconds. |
| 45 | * |
| 46 | * @defaultValue Computed worker weights automatically given the CPU performance. |
| 47 | */ |
| 48 | weights?: Record<number, number> |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Pool worker tasks usage statistics requirements. |
| 53 | * |
| 54 | * @internal |
| 55 | */ |
| 56 | export interface RequiredStatistics { |
| 57 | /** |
| 58 | * Require tasks run time. |
| 59 | */ |
| 60 | runTime: boolean |
| 61 | /** |
| 62 | * Require tasks average run time. |
| 63 | */ |
| 64 | avgRunTime: boolean |
| 65 | /** |
| 66 | * Require tasks median run time. |
| 67 | */ |
| 68 | medRunTime: boolean |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Worker choice strategy interface. |
| 73 | */ |
| 74 | export interface IWorkerChoiceStrategy { |
| 75 | /** |
| 76 | * Required tasks usage statistics. |
| 77 | */ |
| 78 | readonly requiredStatistics: RequiredStatistics |
| 79 | /** |
| 80 | * Resets strategy internals. |
| 81 | * |
| 82 | * @returns `true` if the reset is successful, `false` otherwise. |
| 83 | */ |
| 84 | reset: () => boolean |
| 85 | /** |
| 86 | * Updates worker node strategy internals. |
| 87 | * |
| 88 | * @returns `true` if the update is successful, `false` otherwise. |
| 89 | */ |
| 90 | update: (workerNodeKey: number) => boolean |
| 91 | /** |
| 92 | * Chooses a worker node in the pool and returns its key. |
| 93 | * |
| 94 | * @returns The worker node key. |
| 95 | */ |
| 96 | choose: () => number |
| 97 | /** |
| 98 | * Removes a worker node key from strategy internals. |
| 99 | * |
| 100 | * @param workerNodeKey - The worker node key. |
| 101 | * @returns `true` if the worker node key is removed, `false` otherwise. |
| 102 | */ |
| 103 | remove: (workerNodeKey: number) => boolean |
| 104 | /** |
| 105 | * Sets the worker choice strategy options. |
| 106 | * |
| 107 | * @param opts - The worker choice strategy options. |
| 108 | */ |
| 109 | setOptions: (opts: WorkerChoiceStrategyOptions) => void |
| 110 | } |