Merge pull request #747 from poolifier/multiple-functions
[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 } 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 run time instead of average run time.
38 *
39 * @defaultValue false
40 */
41 medRunTime?: boolean
42 }
43
44 /**
45 * Pool worker tasks usage statistics requirements.
46 *
47 * @internal
48 */
49 export interface RequiredStatistics {
50 /**
51 * Require tasks run time.
52 */
53 runTime: boolean
54 /**
55 * Require tasks average run time.
56 */
57 avgRunTime: boolean
58 /**
59 * Require tasks median run time.
60 */
61 medRunTime: boolean
62 }
63
64 /**
65 * Worker choice strategy interface.
66 */
67 export interface IWorkerChoiceStrategy {
68 /**
69 * Required tasks usage statistics.
70 */
71 readonly requiredStatistics: RequiredStatistics
72 /**
73 * Resets strategy internals (counters, statistics, etc.).
74 */
75 reset: () => boolean
76 /**
77 * Chooses a worker node in the pool and returns its key.
78 */
79 choose: () => number
80 /**
81 * Removes a worker node key from strategy internals.
82 *
83 * @param workerNodeKey - The worker node key.
84 */
85 remove: (workerNodeKey: number) => boolean
86 /**
87 * Sets the worker choice strategy options.
88 *
89 * @param opts - The worker choice strategy options.
90 */
91 setOptions: (opts: WorkerChoiceStrategyOptions) => void
92 }