refactor: factor out dynamic worker creation
[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 */
65 median: boolean
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 */
75 measurement?: Measurement
b0623665 76 /**
932fc8be 77 * Runtime options.
d29bce7c 78 *
932fc8be 79 * @defaultValue \{ median: false \}
b0623665 80 */
932fc8be 81 runTime?: MeasurementOptions
0567595a 82 /**
932fc8be 83 * Wait time options.
0567595a 84 *
932fc8be 85 * @defaultValue \{ median: false \}
0567595a 86 */
932fc8be 87 waitTime?: MeasurementOptions
5df69fab
JB
88 /**
89 * Event loop utilization options.
90 *
91 * @defaultValue \{ median: false \}
92 */
93 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 */
100 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 /**
932fc8be 110 * Require measurement aggregate.
243a550a 111 */
932fc8be 112 aggregate: boolean
243a550a 113 /**
932fc8be 114 * Require measurement average.
243a550a 115 */
932fc8be 116 average: boolean
0567595a 117 /**
932fc8be 118 * Require 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 */
932fc8be 132 runTime: MeasurementStatisticsRequirements
0567595a 133 /**
932fc8be 134 * Tasks wait time requirements.
0567595a 135 */
932fc8be 136 waitTime: MeasurementStatisticsRequirements
62c15a68 137 /**
5df69fab 138 * Tasks event loop utilization requirements.
62c15a68 139 */
5df69fab 140 elu: MeasurementStatisticsRequirements
10fcfaf4
JB
141}
142
bdaf31cd
JB
143/**
144 * Worker choice strategy interface.
bdaf31cd 145 */
17393ac8 146export interface IWorkerChoiceStrategy {
10fcfaf4 147 /**
87de9ff5 148 * Tasks statistics requirements.
10fcfaf4 149 */
87de9ff5 150 readonly taskStatisticsRequirements: TaskStatisticsRequirements
ea7a90d3 151 /**
138d29a8 152 * Resets strategy internals.
a4958de2
JB
153 *
154 * @returns `true` if the reset is successful, `false` otherwise.
ea7a90d3 155 */
78cea37e 156 reset: () => boolean
138d29a8 157 /**
c7e196ba 158 * Updates the worker node key strategy internals.
138d29a8
JB
159 *
160 * @returns `true` if the update is successful, `false` otherwise.
161 */
a4958de2 162 update: (workerNodeKey: number) => boolean
bdaf31cd 163 /**
f06e48d8 164 * Chooses a worker node in the pool and returns its key.
a4958de2
JB
165 *
166 * @returns The worker node key.
bdaf31cd 167 */
c923ce56 168 choose: () => number
97a2abc3 169 /**
c7e196ba 170 * Removes the worker node key from strategy internals.
97a2abc3 171 *
f06e48d8 172 * @param workerNodeKey - The worker node key.
138d29a8 173 * @returns `true` if the worker node key is removed, `false` otherwise.
97a2abc3 174 */
f06e48d8 175 remove: (workerNodeKey: number) => boolean
a20f0ba5
JB
176 /**
177 * Sets the worker choice strategy options.
178 *
179 * @param opts - The worker choice strategy options.
180 */
181 setOptions: (opts: WorkerChoiceStrategyOptions) => void
bdaf31cd 182}