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