docs: add scope on pool public API
[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
JB
72 /**
73 * Measurement to use for worker choice strategy.
74 */
eb7bf744 75 readonly measurement?: Measurement
b0623665 76 /**
932fc8be 77 * Runtime options.
d29bce7c 78 *
932fc8be 79 * @defaultValue \{ median: false \}
b0623665 80 */
eb7bf744 81 readonly runTime?: MeasurementOptions
0567595a 82 /**
932fc8be 83 * Wait time options.
0567595a 84 *
932fc8be 85 * @defaultValue \{ median: false \}
0567595a 86 */
eb7bf744 87 readonly waitTime?: MeasurementOptions
5df69fab
JB
88 /**
89 * Event loop utilization options.
90 *
91 * @defaultValue \{ median: false \}
92 */
eb7bf744 93 readonly elu?: MeasurementOptions
08f3f44c
JB
94 /**
95 * Worker weights to use for weighted round robin worker selection strategy.
96 * Weight is the tasks maximum average or median runtime in milliseconds.
97 *
98 * @defaultValue Computed worker weights automatically given the CPU performance.
99 */
eb7bf744 100 readonly weights?: Record<number, number>
ff733df7
JB
101}
102
10fcfaf4 103/**
932fc8be 104 * Measurement statistics requirements.
71ebe93b
JB
105 *
106 * @internal
10fcfaf4 107 */
9adcefab 108export interface MeasurementStatisticsRequirements {
243a550a 109 /**
64383951 110 * Requires measurement aggregate.
243a550a 111 */
932fc8be 112 aggregate: boolean
243a550a 113 /**
64383951 114 * Requires measurement average.
243a550a 115 */
932fc8be 116 average: boolean
0567595a 117 /**
64383951 118 * Requires measurement median.
0567595a 119 */
932fc8be
JB
120 median: boolean
121}
122
123/**
124 * Pool worker node worker usage statistics requirements.
125 *
126 * @internal
127 */
128export interface TaskStatisticsRequirements {
0567595a 129 /**
932fc8be 130 * Tasks runtime requirements.
0567595a 131 */
eb7bf744 132 readonly runTime: MeasurementStatisticsRequirements
0567595a 133 /**
932fc8be 134 * Tasks wait time requirements.
0567595a 135 */
eb7bf744 136 readonly waitTime: MeasurementStatisticsRequirements
62c15a68 137 /**
5df69fab 138 * Tasks event loop utilization requirements.
62c15a68 139 */
eb7bf744 140 readonly elu: MeasurementStatisticsRequirements
10fcfaf4
JB
141}
142
6c6afb84
JB
143/**
144 * Strategy policy.
247ce01e
JB
145 *
146 * @internal
6c6afb84
JB
147 */
148export interface StrategyPolicy {
149 /**
20016c79 150 * Expects direct usage of the newly created dynamic worker.
6c6afb84 151 */
eb7bf744 152 readonly useDynamicWorker: boolean
6c6afb84
JB
153}
154
bdaf31cd
JB
155/**
156 * Worker choice strategy interface.
922a7350
JB
157 *
158 * @internal
bdaf31cd 159 */
17393ac8 160export interface IWorkerChoiceStrategy {
6c6afb84
JB
161 /**
162 * Strategy policy.
163 */
164 readonly strategyPolicy: StrategyPolicy
10fcfaf4 165 /**
87de9ff5 166 * Tasks statistics requirements.
10fcfaf4 167 */
87de9ff5 168 readonly taskStatisticsRequirements: TaskStatisticsRequirements
ea7a90d3 169 /**
138d29a8 170 * Resets strategy internals.
a4958de2
JB
171 *
172 * @returns `true` if the reset is successful, `false` otherwise.
ea7a90d3 173 */
4b628b48 174 readonly reset: () => boolean
138d29a8 175 /**
c7e196ba 176 * Updates the worker node key strategy internals.
138d29a8
JB
177 *
178 * @returns `true` if the update is successful, `false` otherwise.
179 */
4b628b48 180 readonly update: (workerNodeKey: number) => boolean
bdaf31cd 181 /**
f06e48d8 182 * Chooses a worker node in the pool and returns its key.
a4958de2
JB
183 *
184 * @returns The worker node key.
bdaf31cd 185 */
4b628b48 186 readonly choose: () => number
97a2abc3 187 /**
c7e196ba 188 * Removes the worker node key from strategy internals.
97a2abc3 189 *
f06e48d8 190 * @param workerNodeKey - The worker node key.
138d29a8 191 * @returns `true` if the worker node key is removed, `false` otherwise.
97a2abc3 192 */
4b628b48 193 readonly remove: (workerNodeKey: number) => boolean
a20f0ba5
JB
194 /**
195 * Sets the worker choice strategy options.
196 *
197 * @param opts - The worker choice strategy options.
198 */
4b628b48 199 readonly setOptions: (opts: WorkerChoiceStrategyOptions) => void
bdaf31cd 200}