feat: add less busy worker choice strategy
[poolifier.git] / src / pools / selection-strategies / selection-strategies-types.ts
1 import type { IPoolWorker } from '../pool-worker'
2
3 /**
4 * Enumeration of worker choice strategies.
5 */
6 export const WorkerChoiceStrategies = Object.freeze({
7 /**
8 * Round robin worker selection strategy.
9 */
10 ROUND_ROBIN: 'ROUND_ROBIN',
11 /**
12 * Less used worker selection strategy.
13 */
14 LESS_USED: 'LESS_USED',
15 /**
16 * Less busy worker selection strategy.
17 */
18 LESS_BUSY: 'LESS_BUSY',
19 /**
20 * Fair share worker selection strategy.
21 */
22 FAIR_SHARE: 'FAIR_SHARE',
23 /**
24 * Weighted round robin worker selection strategy.
25 */
26 WEIGHTED_ROUND_ROBIN: 'WEIGHTED_ROUND_ROBIN'
27 } as const)
28
29 /**
30 * Worker choice strategy.
31 */
32 export type WorkerChoiceStrategy = keyof typeof WorkerChoiceStrategies
33
34 /**
35 * Pool tasks usage statistics requirements.
36 */
37 export interface RequiredStatistics {
38 runTime: boolean
39 }
40
41 /**
42 * Worker choice strategy interface.
43 *
44 * @typeParam Worker - Type of worker which manages the strategy.
45 */
46 export interface IWorkerChoiceStrategy<Worker extends IPoolWorker> {
47 /**
48 * Is the pool attached to the strategy dynamic?.
49 */
50 readonly isDynamicPool: boolean
51 /**
52 * Required pool tasks usage statistics.
53 */
54 readonly requiredStatistics: RequiredStatistics
55 /**
56 * Resets strategy internals (counters, statistics, etc.).
57 */
58 reset: () => boolean
59 /**
60 * Chooses a worker in the pool.
61 */
62 choose: () => Worker
63 }