Merge branch 'elu-strategy' of github.com:poolifier/poolifier into elu-strategy
[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 * Least ELU worker selection strategy.
19 *
20 * @experimental
21 */
22 LEAST_ELU: 'LEAST_ELU',
23 /**
24 * Fair share worker selection strategy.
25 */
26 FAIR_SHARE: 'FAIR_SHARE',
27 /**
28 * Weighted round robin worker selection strategy.
29 */
30 WEIGHTED_ROUND_ROBIN: 'WEIGHTED_ROUND_ROBIN',
31 /**
32 * Interleaved weighted round robin worker selection strategy.
33 *
34 * @experimental
35 */
36 INTERLEAVED_WEIGHTED_ROUND_ROBIN: 'INTERLEAVED_WEIGHTED_ROUND_ROBIN'
37 } as const)
38
39 /**
40 * Worker choice strategy.
41 */
42 export type WorkerChoiceStrategy = keyof typeof WorkerChoiceStrategies
43
44 /**
45 * Worker choice strategy options.
46 */
47 export interface WorkerChoiceStrategyOptions {
48 /**
49 * Use tasks median runtime instead of average runtime.
50 *
51 * @defaultValue false
52 */
53 medRunTime?: boolean
54 /**
55 * Use tasks median wait time instead of average runtime.
56 *
57 * @defaultValue false
58 */
59 medWaitTime?: boolean
60 /**
61 * Worker weights to use for weighted round robin worker selection strategy.
62 * Weight is the tasks maximum average or median runtime in milliseconds.
63 *
64 * @defaultValue Computed worker weights automatically given the CPU performance.
65 */
66 weights?: Record<number, number>
67 }
68
69 /**
70 * Pool worker node worker usage statistics requirements.
71 *
72 * @internal
73 */
74 export interface TaskStatisticsRequirements {
75 /**
76 * Require tasks runtime.
77 */
78 runTime: boolean
79 /**
80 * Require tasks average runtime.
81 */
82 avgRunTime: boolean
83 /**
84 * Require tasks median runtime.
85 */
86 medRunTime: boolean
87 /**
88 * Require tasks wait time.
89 */
90 waitTime: boolean
91 /**
92 * Require tasks average wait time.
93 */
94 avgWaitTime: boolean
95 /**
96 * Require tasks median wait time.
97 */
98 medWaitTime: boolean
99 /**
100 * Event loop utilization.
101 */
102 elu: boolean
103 }
104
105 /**
106 * Worker choice strategy interface.
107 */
108 export interface IWorkerChoiceStrategy {
109 /**
110 * Tasks statistics requirements.
111 */
112 readonly taskStatisticsRequirements: TaskStatisticsRequirements
113 /**
114 * Resets strategy internals.
115 *
116 * @returns `true` if the reset is successful, `false` otherwise.
117 */
118 reset: () => boolean
119 /**
120 * Updates the worker node key strategy internals.
121 *
122 * @returns `true` if the update is successful, `false` otherwise.
123 */
124 update: (workerNodeKey: number) => boolean
125 /**
126 * Chooses a worker node in the pool and returns its key.
127 *
128 * @returns The worker node key.
129 */
130 choose: () => number
131 /**
132 * Removes the worker node key from strategy internals.
133 *
134 * @param workerNodeKey - The worker node key.
135 * @returns `true` if the worker node key is removed, `false` otherwise.
136 */
137 remove: (workerNodeKey: number) => boolean
138 /**
139 * Sets the worker choice strategy options.
140 *
141 * @param opts - The worker choice strategy options.
142 */
143 setOptions: (opts: WorkerChoiceStrategyOptions) => void
144 }