refactor: align worker choice strategy options namespace
[poolifier.git] / src / pools / selection-strategies / selection-strategies-types.ts
CommitLineData
bdaf31cd
JB
1/**
2 * Enumeration of worker choice strategies.
3 */
4export const WorkerChoiceStrategies = Object.freeze({
5 /**
6 * Round robin worker selection strategy.
7 */
8 ROUND_ROBIN: 'ROUND_ROBIN',
9 /**
e4543b14 10 * Least used worker selection strategy.
bdaf31cd 11 */
e4543b14 12 LEAST_USED: 'LEAST_USED',
168c526f 13 /**
e4543b14 14 * Least busy worker selection strategy.
168c526f 15 */
e4543b14 16 LEAST_BUSY: 'LEAST_BUSY',
23ff945a
JB
17 /**
18 * Fair share worker selection strategy.
19 */
20 FAIR_SHARE: 'FAIR_SHARE',
b3432a63
JB
21 /**
22 * Weighted round robin worker selection strategy.
23 */
feec6e8c
JB
24 WEIGHTED_ROUND_ROBIN: 'WEIGHTED_ROUND_ROBIN',
25 /**
26 * Interleaved weighted round robin worker selection strategy.
d3127e84
JB
27 *
28 * @experimental
feec6e8c
JB
29 */
30 INTERLEAVED_WEIGHTED_ROUND_ROBIN: 'INTERLEAVED_WEIGHTED_ROUND_ROBIN'
bdaf31cd
JB
31} as const)
32
33/**
34 * Worker choice strategy.
35 */
36export type WorkerChoiceStrategy = keyof typeof WorkerChoiceStrategies
37
932fc8be
JB
38/**
39 * Measurement options.
40 */
41interface MeasurementOptions {
42 /**
43 * Set measurement median.
44 */
45 median: boolean
46}
47
ff733df7
JB
48/**
49 * Worker choice strategy options.
50 */
51export interface WorkerChoiceStrategyOptions {
b0623665 52 /**
932fc8be 53 * Runtime options.
d29bce7c 54 *
932fc8be 55 * @defaultValue \{ median: false \}
b0623665 56 */
932fc8be 57 runTime?: MeasurementOptions
0567595a 58 /**
932fc8be 59 * Wait time options.
0567595a 60 *
932fc8be 61 * @defaultValue \{ median: false \}
0567595a 62 */
932fc8be 63 waitTime?: MeasurementOptions
08f3f44c
JB
64 /**
65 * Worker weights to use for weighted round robin worker selection strategy.
66 * Weight is the tasks maximum average or median runtime in milliseconds.
67 *
68 * @defaultValue Computed worker weights automatically given the CPU performance.
69 */
70 weights?: Record<number, number>
ff733df7
JB
71}
72
10fcfaf4 73/**
932fc8be 74 * Measurement statistics requirements.
71ebe93b
JB
75 *
76 * @internal
10fcfaf4 77 */
932fc8be 78interface MeasurementStatisticsRequirements {
243a550a 79 /**
932fc8be 80 * Require measurement aggregate.
243a550a 81 */
932fc8be 82 aggregate: boolean
243a550a 83 /**
932fc8be 84 * Require measurement average.
243a550a 85 */
932fc8be 86 average: boolean
0567595a 87 /**
932fc8be 88 * Require measurement median.
0567595a 89 */
932fc8be
JB
90 median: boolean
91}
92
93/**
94 * Pool worker node worker usage statistics requirements.
95 *
96 * @internal
97 */
98export interface TaskStatisticsRequirements {
0567595a 99 /**
932fc8be 100 * Tasks runtime requirements.
0567595a 101 */
932fc8be 102 runTime: MeasurementStatisticsRequirements
0567595a 103 /**
932fc8be 104 * Tasks wait time requirements.
0567595a 105 */
932fc8be 106 waitTime: MeasurementStatisticsRequirements
62c15a68
JB
107 /**
108 * Event loop utilization.
109 */
110 elu: boolean
10fcfaf4
JB
111}
112
bdaf31cd
JB
113/**
114 * Worker choice strategy interface.
bdaf31cd 115 */
17393ac8 116export interface IWorkerChoiceStrategy {
10fcfaf4 117 /**
87de9ff5 118 * Tasks statistics requirements.
10fcfaf4 119 */
87de9ff5 120 readonly taskStatisticsRequirements: TaskStatisticsRequirements
ea7a90d3 121 /**
138d29a8 122 * Resets strategy internals.
a4958de2
JB
123 *
124 * @returns `true` if the reset is successful, `false` otherwise.
ea7a90d3 125 */
78cea37e 126 reset: () => boolean
138d29a8 127 /**
c7e196ba 128 * Updates the worker node key strategy internals.
138d29a8
JB
129 *
130 * @returns `true` if the update is successful, `false` otherwise.
131 */
a4958de2 132 update: (workerNodeKey: number) => boolean
bdaf31cd 133 /**
f06e48d8 134 * Chooses a worker node in the pool and returns its key.
a4958de2
JB
135 *
136 * @returns The worker node key.
bdaf31cd 137 */
c923ce56 138 choose: () => number
97a2abc3 139 /**
c7e196ba 140 * Removes the worker node key from strategy internals.
97a2abc3 141 *
f06e48d8 142 * @param workerNodeKey - The worker node key.
138d29a8 143 * @returns `true` if the worker node key is removed, `false` otherwise.
97a2abc3 144 */
f06e48d8 145 remove: (workerNodeKey: number) => boolean
a20f0ba5
JB
146 /**
147 * Sets the worker choice strategy options.
148 *
149 * @param opts - The worker choice strategy options.
150 */
151 setOptions: (opts: WorkerChoiceStrategyOptions) => void
bdaf31cd 152}