499b8e489e744a3fad453391b8e4e664c90eac94
[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 recently used worker selection strategy.
13 */
14 LESS_RECENTLY_USED: 'LESS_RECENTLY_USED',
15 /**
16 * Fair share worker selection strategy.
17 */
18 FAIR_SHARE: 'FAIR_SHARE',
19 /**
20 * Weighted round robin worker selection strategy.
21 */
22 WEIGHTED_ROUND_ROBIN: 'WEIGHTED_ROUND_ROBIN'
23 } as const)
24
25 /**
26 * Worker choice strategy.
27 */
28 export type WorkerChoiceStrategy = keyof typeof WorkerChoiceStrategies
29
30 /**
31 * Pool tasks usage statistics requirements.
32 */
33 export type RequiredStatistics = {
34 runTime: boolean
35 }
36
37 /**
38 * Worker choice strategy interface.
39 *
40 * @template Worker Type of worker which manages the strategy.
41 */
42 export interface IWorkerChoiceStrategy<Worker extends IPoolWorker> {
43 /**
44 * Is the pool attached to the strategy dynamic?.
45 */
46 readonly isDynamicPool: boolean
47 /**
48 * Required pool tasks usage statistics.
49 */
50 readonly requiredStatistics: RequiredStatistics
51 /**
52 * Resets strategy internals (counters, statistics, etc.).
53 */
54 reset(): boolean
55 /**
56 * Chooses a worker in the pool.
57 */
58 choose(): Worker
59 }