build(deps-dev): apply updates
[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 /**
737c6d97 10 * Less used worker selection strategy.
bdaf31cd 11 */
737c6d97 12 LESS_USED: 'LESS_USED',
168c526f
JB
13 /**
14 * Less busy worker selection strategy.
15 */
16 LESS_BUSY: 'LESS_BUSY',
23ff945a
JB
17 /**
18 * Fair share worker selection strategy.
19 */
20 FAIR_SHARE: 'FAIR_SHARE',
b3432a63
JB
21 /**
22 * Weighted round robin worker selection strategy.
23 */
24 WEIGHTED_ROUND_ROBIN: 'WEIGHTED_ROUND_ROBIN'
bdaf31cd
JB
25} as const)
26
27/**
28 * Worker choice strategy.
29 */
30export type WorkerChoiceStrategy = keyof typeof WorkerChoiceStrategies
31
ff733df7
JB
32/**
33 * Worker choice strategy options.
34 */
35export interface WorkerChoiceStrategyOptions {
b0623665
JB
36 /**
37 * Use tasks median run time instead of average run time.
d29bce7c
JB
38 *
39 * @defaultValue false
b0623665 40 */
ff733df7 41 medRunTime?: boolean
08f3f44c
JB
42 /**
43 * Worker weights to use for weighted round robin worker selection strategy.
44 * Weight is the tasks maximum average or median runtime in milliseconds.
45 *
46 * @defaultValue Computed worker weights automatically given the CPU performance.
47 */
48 weights?: Record<number, number>
ff733df7
JB
49}
50
10fcfaf4 51/**
9cd39dd4 52 * Pool worker tasks usage statistics requirements.
71ebe93b
JB
53 *
54 * @internal
10fcfaf4 55 */
78cea37e 56export interface RequiredStatistics {
243a550a
JB
57 /**
58 * Require tasks run time.
59 */
10fcfaf4 60 runTime: boolean
243a550a
JB
61 /**
62 * Require tasks average run time.
63 */
c6bd2650 64 avgRunTime: boolean
243a550a
JB
65 /**
66 * Require tasks median run time.
67 */
78099a15 68 medRunTime: boolean
10fcfaf4
JB
69}
70
bdaf31cd
JB
71/**
72 * Worker choice strategy interface.
bdaf31cd 73 */
17393ac8 74export interface IWorkerChoiceStrategy {
10fcfaf4 75 /**
f06e48d8 76 * Required tasks usage statistics.
10fcfaf4 77 */
ea7a90d3
JB
78 readonly requiredStatistics: RequiredStatistics
79 /**
138d29a8 80 * Resets strategy internals.
a4958de2
JB
81 *
82 * @returns `true` if the reset is successful, `false` otherwise.
ea7a90d3 83 */
78cea37e 84 reset: () => boolean
138d29a8 85 /**
c7e196ba 86 * Updates the worker node key strategy internals.
138d29a8
JB
87 *
88 * @returns `true` if the update is successful, `false` otherwise.
89 */
a4958de2 90 update: (workerNodeKey: number) => boolean
bdaf31cd 91 /**
f06e48d8 92 * Chooses a worker node in the pool and returns its key.
a4958de2
JB
93 *
94 * @returns The worker node key.
bdaf31cd 95 */
c923ce56 96 choose: () => number
97a2abc3 97 /**
c7e196ba 98 * Removes the worker node key from strategy internals.
97a2abc3 99 *
f06e48d8 100 * @param workerNodeKey - The worker node key.
138d29a8 101 * @returns `true` if the worker node key is removed, `false` otherwise.
97a2abc3 102 */
f06e48d8 103 remove: (workerNodeKey: number) => boolean
a20f0ba5
JB
104 /**
105 * Sets the worker choice strategy options.
106 *
107 * @param opts - The worker choice strategy options.
108 */
109 setOptions: (opts: WorkerChoiceStrategyOptions) => void
bdaf31cd 110}