Merge branch 'master' into elu-strategy
[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
ff733df7
JB
44/**
45 * Worker choice strategy options.
46 */
47export interface WorkerChoiceStrategyOptions {
b0623665 48 /**
9e45c2c4 49 * Use tasks median runtime instead of average runtime.
d29bce7c
JB
50 *
51 * @defaultValue false
b0623665 52 */
ff733df7 53 medRunTime?: boolean
0567595a 54 /**
e6606302 55 * Use tasks median wait time instead of average runtime.
0567595a
JB
56 *
57 * @defaultValue false
58 */
59 medWaitTime?: boolean
08f3f44c
JB
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>
ff733df7
JB
67}
68
10fcfaf4 69/**
9cd39dd4 70 * Pool worker tasks usage statistics requirements.
71ebe93b
JB
71 *
72 * @internal
10fcfaf4 73 */
b6b32453 74export interface TaskStatistics {
243a550a 75 /**
9e775f96 76 * Require tasks runtime.
243a550a 77 */
10fcfaf4 78 runTime: boolean
243a550a 79 /**
9e775f96 80 * Require tasks average runtime.
243a550a 81 */
c6bd2650 82 avgRunTime: boolean
243a550a 83 /**
9e775f96 84 * Require tasks median runtime.
243a550a 85 */
78099a15 86 medRunTime: boolean
0567595a
JB
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
62c15a68
JB
99 /**
100 * Event loop utilization.
101 */
102 elu: boolean
10fcfaf4
JB
103}
104
bdaf31cd
JB
105/**
106 * Worker choice strategy interface.
bdaf31cd 107 */
17393ac8 108export interface IWorkerChoiceStrategy {
10fcfaf4 109 /**
b6b32453 110 * Required tasks statistics.
10fcfaf4 111 */
b6b32453 112 readonly taskStatistics: TaskStatistics
ea7a90d3 113 /**
138d29a8 114 * Resets strategy internals.
a4958de2
JB
115 *
116 * @returns `true` if the reset is successful, `false` otherwise.
ea7a90d3 117 */
78cea37e 118 reset: () => boolean
138d29a8 119 /**
c7e196ba 120 * Updates the worker node key strategy internals.
138d29a8
JB
121 *
122 * @returns `true` if the update is successful, `false` otherwise.
123 */
a4958de2 124 update: (workerNodeKey: number) => boolean
bdaf31cd 125 /**
f06e48d8 126 * Chooses a worker node in the pool and returns its key.
a4958de2
JB
127 *
128 * @returns The worker node key.
bdaf31cd 129 */
c923ce56 130 choose: () => number
97a2abc3 131 /**
c7e196ba 132 * Removes the worker node key from strategy internals.
97a2abc3 133 *
f06e48d8 134 * @param workerNodeKey - The worker node key.
138d29a8 135 * @returns `true` if the worker node key is removed, `false` otherwise.
97a2abc3 136 */
f06e48d8 137 remove: (workerNodeKey: number) => boolean
a20f0ba5
JB
138 /**
139 * Sets the worker choice strategy options.
140 *
141 * @param opts - The worker choice strategy options.
142 */
143 setOptions: (opts: WorkerChoiceStrategyOptions) => void
bdaf31cd 144}