Merge branch 'master' of github.com:poolifier/poolifier
[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 medRunTime?: boolean
40 }
41
42 /**
43 * Pool worker tasks usage statistics requirements.
44 */
45 export interface RequiredStatistics {
46 runTime: boolean
47 avgRunTime: boolean
48 medRunTime: boolean
49 }
50
51 /**
52 * Worker choice strategy interface.
53 */
54 export interface IWorkerChoiceStrategy {
55 /**
56 * Required tasks usage statistics.
57 */
58 readonly requiredStatistics: RequiredStatistics
59 /**
60 * Resets strategy internals (counters, statistics, etc.).
61 */
62 reset: () => boolean
63 /**
64 * Chooses a worker node in the pool and returns its key.
65 */
66 choose: () => number
67 /**
68 * Removes a worker node key from strategy internals.
69 *
70 * @param workerNodeKey - The worker node key.
71 */
72 remove: (workerNodeKey: number) => boolean
73 }