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