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