chore: v2.6.30
[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',
058a9457
JB
17 /**
18 * Least ELU worker selection strategy.
19 *
20 * @experimental
21 */
22 LEAST_ELU: 'LEAST_ELU',
23ff945a
JB
23 /**
24 * Fair share worker selection strategy.
25 */
26 FAIR_SHARE: 'FAIR_SHARE',
b3432a63
JB
27 /**
28 * Weighted round robin worker selection strategy.
29 */
feec6e8c
JB
30 WEIGHTED_ROUND_ROBIN: 'WEIGHTED_ROUND_ROBIN',
31 /**
32 * Interleaved weighted round robin worker selection strategy.
d3127e84
JB
33 *
34 * @experimental
feec6e8c
JB
35 */
36 INTERLEAVED_WEIGHTED_ROUND_ROBIN: 'INTERLEAVED_WEIGHTED_ROUND_ROBIN'
bdaf31cd
JB
37} as const)
38
39/**
40 * Worker choice strategy.
41 */
42export type WorkerChoiceStrategy = keyof typeof WorkerChoiceStrategies
43
9adcefab
JB
44/**
45 * Enumeration of measurements.
46 */
47export const Measurements = Object.freeze({
48 runTime: 'runTime',
49 waitTime: 'waitTime',
50 elu: 'elu'
51} as const)
52
53/**
54 * Measurement.
55 */
56export type Measurement = keyof typeof Measurements
57
932fc8be
JB
58/**
59 * Measurement options.
60 */
9adcefab 61export interface MeasurementOptions {
932fc8be
JB
62 /**
63 * Set measurement median.
64 */
eb7bf744 65 readonly median: boolean
932fc8be
JB
66}
67
ff733df7
JB
68/**
69 * Worker choice strategy options.
70 */
71export interface WorkerChoiceStrategyOptions {
9adcefab 72 /**
8990357d
JB
73 * Number of worker choice retries to perform if no worker is eligible.
74 *
75 * @defaultValue 6
76 */
77 readonly choiceRetries?: number
78 /**
79 * Measurement to use in worker choice strategy supporting it.
9adcefab 80 */
eb7bf744 81 readonly measurement?: Measurement
b0623665 82 /**
932fc8be 83 * Runtime options.
d29bce7c 84 *
932fc8be 85 * @defaultValue \{ median: false \}
b0623665 86 */
eb7bf744 87 readonly runTime?: MeasurementOptions
0567595a 88 /**
932fc8be 89 * Wait time options.
0567595a 90 *
932fc8be 91 * @defaultValue \{ median: false \}
0567595a 92 */
eb7bf744 93 readonly waitTime?: MeasurementOptions
5df69fab
JB
94 /**
95 * Event loop utilization options.
96 *
97 * @defaultValue \{ median: false \}
98 */
eb7bf744 99 readonly elu?: MeasurementOptions
08f3f44c
JB
100 /**
101 * Worker weights to use for weighted round robin worker selection strategy.
102 * Weight is the tasks maximum average or median runtime in milliseconds.
103 *
104 * @defaultValue Computed worker weights automatically given the CPU performance.
105 */
eb7bf744 106 readonly weights?: Record<number, number>
ff733df7
JB
107}
108
10fcfaf4 109/**
932fc8be 110 * Measurement statistics requirements.
71ebe93b
JB
111 *
112 * @internal
10fcfaf4 113 */
9adcefab 114export interface MeasurementStatisticsRequirements {
243a550a 115 /**
64383951 116 * Requires measurement aggregate.
243a550a 117 */
932fc8be 118 aggregate: boolean
243a550a 119 /**
64383951 120 * Requires measurement average.
243a550a 121 */
932fc8be 122 average: boolean
0567595a 123 /**
64383951 124 * Requires measurement median.
0567595a 125 */
932fc8be
JB
126 median: boolean
127}
128
129/**
130 * Pool worker node worker usage statistics requirements.
131 *
132 * @internal
133 */
134export interface TaskStatisticsRequirements {
0567595a 135 /**
932fc8be 136 * Tasks runtime requirements.
0567595a 137 */
eb7bf744 138 readonly runTime: MeasurementStatisticsRequirements
0567595a 139 /**
932fc8be 140 * Tasks wait time requirements.
0567595a 141 */
eb7bf744 142 readonly waitTime: MeasurementStatisticsRequirements
62c15a68 143 /**
5df69fab 144 * Tasks event loop utilization requirements.
62c15a68 145 */
eb7bf744 146 readonly elu: MeasurementStatisticsRequirements
10fcfaf4
JB
147}
148
6c6afb84
JB
149/**
150 * Strategy policy.
247ce01e
JB
151 *
152 * @internal
6c6afb84
JB
153 */
154export interface StrategyPolicy {
155 /**
20016c79 156 * Expects direct usage of the newly created dynamic worker.
6c6afb84 157 */
eb7bf744 158 readonly useDynamicWorker: boolean
6c6afb84
JB
159}
160
bdaf31cd
JB
161/**
162 * Worker choice strategy interface.
922a7350
JB
163 *
164 * @internal
bdaf31cd 165 */
17393ac8 166export interface IWorkerChoiceStrategy {
6c6afb84
JB
167 /**
168 * Strategy policy.
169 */
170 readonly strategyPolicy: StrategyPolicy
10fcfaf4 171 /**
87de9ff5 172 * Tasks statistics requirements.
10fcfaf4 173 */
87de9ff5 174 readonly taskStatisticsRequirements: TaskStatisticsRequirements
ea7a90d3 175 /**
138d29a8 176 * Resets strategy internals.
a4958de2
JB
177 *
178 * @returns `true` if the reset is successful, `false` otherwise.
ea7a90d3 179 */
4b628b48 180 readonly reset: () => boolean
138d29a8 181 /**
c7e196ba 182 * Updates the worker node key strategy internals.
138d29a8
JB
183 *
184 * @returns `true` if the update is successful, `false` otherwise.
185 */
4b628b48 186 readonly update: (workerNodeKey: number) => boolean
bdaf31cd 187 /**
f06e48d8 188 * Chooses a worker node in the pool and returns its key.
a4958de2
JB
189 *
190 * @returns The worker node key.
bdaf31cd 191 */
4b628b48 192 readonly choose: () => number
97a2abc3 193 /**
c7e196ba 194 * Removes the worker node key from strategy internals.
97a2abc3 195 *
f06e48d8 196 * @param workerNodeKey - The worker node key.
138d29a8 197 * @returns `true` if the worker node key is removed, `false` otherwise.
97a2abc3 198 */
4b628b48 199 readonly remove: (workerNodeKey: number) => boolean
a20f0ba5
JB
200 /**
201 * Sets the worker choice strategy options.
202 *
203 * @param opts - The worker choice strategy options.
204 */
4b628b48 205 readonly setOptions: (opts: WorkerChoiceStrategyOptions) => void
bdaf31cd 206}