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