Commit | Line | Data |
---|---|---|
bdaf31cd | 1 | import type { IPoolInternal } from '../pool-internal' |
ea7a90d3 | 2 | import type { IPoolWorker } from '../pool-worker' |
51fe3d3c JB |
3 | import { FairShareWorkerChoiceStrategy } from './fair-share-worker-choice-strategy' |
4 | import { LessBusyWorkerChoiceStrategy } from './less-busy-worker-choice-strategy' | |
5 | import { LessUsedWorkerChoiceStrategy } from './less-used-worker-choice-strategy' | |
6 | import { RoundRobinWorkerChoiceStrategy } from './round-robin-worker-choice-strategy' | |
bdaf31cd JB |
7 | import type { |
8 | IWorkerChoiceStrategy, | |
97a2abc3 | 9 | RequiredStatistics, |
bdaf31cd JB |
10 | WorkerChoiceStrategy |
11 | } from './selection-strategies-types' | |
12 | import { WorkerChoiceStrategies } from './selection-strategies-types' | |
51fe3d3c | 13 | import { WeightedRoundRobinWorkerChoiceStrategy } from './weighted-round-robin-worker-choice-strategy' |
bdaf31cd JB |
14 | |
15 | /** | |
16 | * The worker choice strategy context. | |
17 | * | |
38e795c1 JB |
18 | * @typeParam Worker - Type of worker. |
19 | * @typeParam Data - Type of data sent to the worker. This can only be serializable data. | |
20 | * @typeParam Response - Type of response of execution. This can only be serializable data. | |
bdaf31cd JB |
21 | */ |
22 | export class WorkerChoiceStrategyContext< | |
ea7a90d3 | 23 | Worker extends IPoolWorker, |
b2b1d84e JB |
24 | Data = unknown, |
25 | Response = unknown | |
bdaf31cd | 26 | > { |
b529c323 | 27 | private readonly workerChoiceStrategies: Map< |
95c83464 | 28 | WorkerChoiceStrategy, |
17393ac8 | 29 | IWorkerChoiceStrategy |
b529c323 | 30 | > |
bdaf31cd JB |
31 | |
32 | /** | |
33 | * Worker choice strategy context constructor. | |
34 | * | |
38e795c1 | 35 | * @param pool - The pool instance. |
a22cdf86 | 36 | * @param workerChoiceStrategyType - The worker choice strategy. |
bdaf31cd JB |
37 | */ |
38 | public constructor ( | |
3300e7bc | 39 | pool: IPoolInternal<Worker, Data, Response>, |
b2b1d84e | 40 | private workerChoiceStrategyType: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN |
bdaf31cd | 41 | ) { |
1086026a | 42 | this.execute.bind(this) |
b529c323 JB |
43 | this.workerChoiceStrategies = new Map< |
44 | WorkerChoiceStrategy, | |
17393ac8 | 45 | IWorkerChoiceStrategy |
b529c323 JB |
46 | >([ |
47 | [ | |
48 | WorkerChoiceStrategies.ROUND_ROBIN, | |
49 | new RoundRobinWorkerChoiceStrategy<Worker, Data, Response>(pool) | |
50 | ], | |
51 | [ | |
52 | WorkerChoiceStrategies.LESS_USED, | |
53 | new LessUsedWorkerChoiceStrategy<Worker, Data, Response>(pool) | |
54 | ], | |
55 | [ | |
56 | WorkerChoiceStrategies.LESS_BUSY, | |
57 | new LessBusyWorkerChoiceStrategy<Worker, Data, Response>(pool) | |
58 | ], | |
59 | [ | |
60 | WorkerChoiceStrategies.FAIR_SHARE, | |
61 | new FairShareWorkerChoiceStrategy<Worker, Data, Response>(pool) | |
62 | ], | |
63 | [ | |
64 | WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN, | |
65 | new WeightedRoundRobinWorkerChoiceStrategy<Worker, Data, Response>(pool) | |
66 | ] | |
67 | ]) | |
bdaf31cd JB |
68 | } |
69 | ||
97a2abc3 | 70 | /** |
51fe3d3c | 71 | * Gets the worker choice strategy in the context required statistics. |
97a2abc3 JB |
72 | * |
73 | * @returns The required statistics. | |
74 | */ | |
75 | public getRequiredStatistics (): RequiredStatistics { | |
95c83464 JB |
76 | return ( |
77 | this.workerChoiceStrategies.get( | |
78 | this.workerChoiceStrategyType | |
17393ac8 | 79 | ) as IWorkerChoiceStrategy |
95c83464 | 80 | ).requiredStatistics |
97a2abc3 JB |
81 | } |
82 | ||
bdaf31cd | 83 | /** |
bdede008 | 84 | * Sets the worker choice strategy to use in the context. |
bdaf31cd | 85 | * |
38e795c1 | 86 | * @param workerChoiceStrategy - The worker choice strategy to set. |
bdaf31cd JB |
87 | */ |
88 | public setWorkerChoiceStrategy ( | |
89 | workerChoiceStrategy: WorkerChoiceStrategy | |
90 | ): void { | |
aee46736 | 91 | if (this.workerChoiceStrategyType !== workerChoiceStrategy) { |
b2b1d84e | 92 | this.workerChoiceStrategyType = workerChoiceStrategy |
b2b1d84e | 93 | } |
aee46736 | 94 | this.workerChoiceStrategies.get(this.workerChoiceStrategyType)?.reset() |
bdaf31cd JB |
95 | } |
96 | ||
97 | /** | |
51fe3d3c | 98 | * Executes the worker choice strategy algorithm in the context. |
bdaf31cd | 99 | * |
c923ce56 | 100 | * @returns The key of the chosen one. |
bdaf31cd | 101 | */ |
c923ce56 | 102 | public execute (): number { |
17393ac8 JB |
103 | return ( |
104 | this.workerChoiceStrategies.get( | |
105 | this.workerChoiceStrategyType | |
106 | ) as IWorkerChoiceStrategy | |
107 | ).choose() | |
bdaf31cd | 108 | } |
97a2abc3 JB |
109 | |
110 | /** | |
51fe3d3c | 111 | * Removes a worker from the worker choice strategy in the context. |
97a2abc3 JB |
112 | * |
113 | * @param workerKey - The key of the worker to remove. | |
114 | * @returns `true` if the removal is successful, `false` otherwise. | |
115 | */ | |
116 | public remove (workerKey: number): boolean { | |
95c83464 JB |
117 | return ( |
118 | this.workerChoiceStrategies.get( | |
119 | this.workerChoiceStrategyType | |
17393ac8 | 120 | ) as IWorkerChoiceStrategy |
95c83464 JB |
121 | ).remove(workerKey) |
122 | } | |
bdaf31cd | 123 | } |