Merge branch 'master' 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 * 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 * Worker choice strategy options.
40 */
41 export interface WorkerChoiceStrategyOptions {
42 /**
43 * Use tasks median runtime instead of average runtime.
44 *
45 * @defaultValue false
46 */
47 medRunTime?: boolean
48 /**
49 * Use tasks median wait time instead of average runtime.
50 *
51 * @defaultValue false
52 */
53 medWaitTime?: boolean
54 /**
55 * Worker weights to use for weighted round robin worker selection strategy.
56 * Weight is the tasks maximum average or median runtime in milliseconds.
57 *
58 * @defaultValue Computed worker weights automatically given the CPU performance.
59 */
60 weights?: Record<number, number>
61 }
62
63 /**
64 * Pool worker tasks usage statistics requirements.
65 *
66 * @internal
67 */
68 export interface RequiredStatistics {
69 /**
70 * Require tasks runtime.
71 */
72 runTime: boolean
73 /**
74 * Require tasks average runtime.
75 */
76 avgRunTime: boolean
77 /**
78 * Require tasks median runtime.
79 */
80 medRunTime: boolean
81 /**
82 * Require tasks wait time.
83 */
84 waitTime: boolean
85 /**
86 * Require tasks average wait time.
87 */
88 avgWaitTime: boolean
89 /**
90 * Require tasks median wait time.
91 */
92 medWaitTime: boolean
93 /**
94 * Event loop utilization.
95 */
96 elu: boolean
97 }
98
99 /**
100 * Worker choice strategy interface.
101 */
102 export interface IWorkerChoiceStrategy {
103 /**
104 * Required tasks usage statistics.
105 */
106 readonly requiredStatistics: RequiredStatistics
107 /**
108 * Resets strategy internals.
109 *
110 * @returns `true` if the reset is successful, `false` otherwise.
111 */
112 reset: () => boolean
113 /**
114 * Updates the worker node key strategy internals.
115 *
116 * @returns `true` if the update is successful, `false` otherwise.
117 */
118 update: (workerNodeKey: number) => boolean
119 /**
120 * Chooses a worker node in the pool and returns its key.
121 *
122 * @returns The worker node key.
123 */
124 choose: () => number
125 /**
126 * Removes the worker node key from strategy internals.
127 *
128 * @param workerNodeKey - The worker node key.
129 * @returns `true` if the worker node key is removed, `false` otherwise.
130 */
131 remove: (workerNodeKey: number) => boolean
132 /**
133 * Sets the worker choice strategy options.
134 *
135 * @param opts - The worker choice strategy options.
136 */
137 setOptions: (opts: WorkerChoiceStrategyOptions) => void
138 }