refactor: align worker choice strategy options namespace
[poolifier.git] / src / pools / selection-strategies / selection-strategies-types.ts
1 /**
2 * Enumeration of worker choice strategies.
3 */
4 export const WorkerChoiceStrategies = Object.freeze({
5 /**
6 * Round robin worker selection strategy.
7 */
8 ROUND_ROBIN: 'ROUND_ROBIN',
9 /**
10 * Least used worker selection strategy.
11 */
12 LEAST_USED: 'LEAST_USED',
13 /**
14 * Least busy worker selection strategy.
15 */
16 LEAST_BUSY: 'LEAST_BUSY',
17 /**
18 * Fair share worker selection strategy.
19 */
20 FAIR_SHARE: 'FAIR_SHARE',
21 /**
22 * Weighted round robin worker selection strategy.
23 */
24 WEIGHTED_ROUND_ROBIN: 'WEIGHTED_ROUND_ROBIN',
25 /**
26 * Interleaved weighted round robin worker selection strategy.
27 *
28 * @experimental
29 */
30 INTERLEAVED_WEIGHTED_ROUND_ROBIN: 'INTERLEAVED_WEIGHTED_ROUND_ROBIN'
31 } as const)
32
33 /**
34 * Worker choice strategy.
35 */
36 export type WorkerChoiceStrategy = keyof typeof WorkerChoiceStrategies
37
38 /**
39 * Measurement options.
40 */
41 interface MeasurementOptions {
42 /**
43 * Set measurement median.
44 */
45 median: boolean
46 }
47
48 /**
49 * Worker choice strategy options.
50 */
51 export interface WorkerChoiceStrategyOptions {
52 /**
53 * Runtime options.
54 *
55 * @defaultValue \{ median: false \}
56 */
57 runTime?: MeasurementOptions
58 /**
59 * Wait time options.
60 *
61 * @defaultValue \{ median: false \}
62 */
63 waitTime?: MeasurementOptions
64 /**
65 * Worker weights to use for weighted round robin worker selection strategy.
66 * Weight is the tasks maximum average or median runtime in milliseconds.
67 *
68 * @defaultValue Computed worker weights automatically given the CPU performance.
69 */
70 weights?: Record<number, number>
71 }
72
73 /**
74 * Measurement statistics requirements.
75 *
76 * @internal
77 */
78 interface MeasurementStatisticsRequirements {
79 /**
80 * Require measurement aggregate.
81 */
82 aggregate: boolean
83 /**
84 * Require measurement average.
85 */
86 average: boolean
87 /**
88 * Require measurement median.
89 */
90 median: boolean
91 }
92
93 /**
94 * Pool worker node worker usage statistics requirements.
95 *
96 * @internal
97 */
98 export interface TaskStatisticsRequirements {
99 /**
100 * Tasks runtime requirements.
101 */
102 runTime: MeasurementStatisticsRequirements
103 /**
104 * Tasks wait time requirements.
105 */
106 waitTime: MeasurementStatisticsRequirements
107 /**
108 * Event loop utilization.
109 */
110 elu: boolean
111 }
112
113 /**
114 * Worker choice strategy interface.
115 */
116 export interface IWorkerChoiceStrategy {
117 /**
118 * Tasks statistics requirements.
119 */
120 readonly taskStatisticsRequirements: TaskStatisticsRequirements
121 /**
122 * Resets strategy internals.
123 *
124 * @returns `true` if the reset is successful, `false` otherwise.
125 */
126 reset: () => boolean
127 /**
128 * Updates the worker node key strategy internals.
129 *
130 * @returns `true` if the update is successful, `false` otherwise.
131 */
132 update: (workerNodeKey: number) => boolean
133 /**
134 * Chooses a worker node in the pool and returns its key.
135 *
136 * @returns The worker node key.
137 */
138 choose: () => number
139 /**
140 * Removes the worker node key from strategy internals.
141 *
142 * @param workerNodeKey - The worker node key.
143 * @returns `true` if the worker node key is removed, `false` otherwise.
144 */
145 remove: (workerNodeKey: number) => boolean
146 /**
147 * Sets the worker choice strategy options.
148 *
149 * @param opts - The worker choice strategy options.
150 */
151 setOptions: (opts: WorkerChoiceStrategyOptions) => void
152 }