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. |
c923ce56 | 36 | * @param createWorkerCallback - The worker creation callback for dynamic pool. |
38e795c1 | 37 | * @param workerChoiceStrategy - The worker choice strategy. |
bdaf31cd JB |
38 | */ |
39 | public constructor ( | |
3300e7bc | 40 | pool: IPoolInternal<Worker, Data, Response>, |
b2b1d84e | 41 | private workerChoiceStrategyType: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN |
bdaf31cd | 42 | ) { |
1086026a | 43 | this.execute.bind(this) |
b529c323 JB |
44 | this.workerChoiceStrategies = new Map< |
45 | WorkerChoiceStrategy, | |
17393ac8 | 46 | IWorkerChoiceStrategy |
b529c323 JB |
47 | >([ |
48 | [ | |
49 | WorkerChoiceStrategies.ROUND_ROBIN, | |
50 | new RoundRobinWorkerChoiceStrategy<Worker, Data, Response>(pool) | |
51 | ], | |
52 | [ | |
53 | WorkerChoiceStrategies.LESS_USED, | |
54 | new LessUsedWorkerChoiceStrategy<Worker, Data, Response>(pool) | |
55 | ], | |
56 | [ | |
57 | WorkerChoiceStrategies.LESS_BUSY, | |
58 | new LessBusyWorkerChoiceStrategy<Worker, Data, Response>(pool) | |
59 | ], | |
60 | [ | |
61 | WorkerChoiceStrategies.FAIR_SHARE, | |
62 | new FairShareWorkerChoiceStrategy<Worker, Data, Response>(pool) | |
63 | ], | |
64 | [ | |
65 | WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN, | |
66 | new WeightedRoundRobinWorkerChoiceStrategy<Worker, Data, Response>(pool) | |
67 | ] | |
68 | ]) | |
bdaf31cd JB |
69 | } |
70 | ||
97a2abc3 | 71 | /** |
51fe3d3c | 72 | * Gets the worker choice strategy in the context required statistics. |
97a2abc3 JB |
73 | * |
74 | * @returns The required statistics. | |
75 | */ | |
76 | public getRequiredStatistics (): RequiredStatistics { | |
95c83464 JB |
77 | return ( |
78 | this.workerChoiceStrategies.get( | |
79 | this.workerChoiceStrategyType | |
17393ac8 | 80 | ) as IWorkerChoiceStrategy |
95c83464 | 81 | ).requiredStatistics |
97a2abc3 JB |
82 | } |
83 | ||
bdaf31cd | 84 | /** |
bdede008 | 85 | * Sets the worker choice strategy to use in the context. |
bdaf31cd | 86 | * |
38e795c1 | 87 | * @param workerChoiceStrategy - The worker choice strategy to set. |
bdaf31cd JB |
88 | */ |
89 | public setWorkerChoiceStrategy ( | |
90 | workerChoiceStrategy: WorkerChoiceStrategy | |
91 | ): void { | |
aee46736 | 92 | if (this.workerChoiceStrategyType !== workerChoiceStrategy) { |
b2b1d84e | 93 | this.workerChoiceStrategyType = workerChoiceStrategy |
b2b1d84e | 94 | } |
aee46736 | 95 | this.workerChoiceStrategies.get(this.workerChoiceStrategyType)?.reset() |
bdaf31cd JB |
96 | } |
97 | ||
98 | /** | |
51fe3d3c | 99 | * Executes the worker choice strategy algorithm in the context. |
bdaf31cd | 100 | * |
c923ce56 | 101 | * @returns The key of the chosen one. |
bdaf31cd | 102 | */ |
c923ce56 | 103 | public execute (): number { |
17393ac8 JB |
104 | return ( |
105 | this.workerChoiceStrategies.get( | |
106 | this.workerChoiceStrategyType | |
107 | ) as IWorkerChoiceStrategy | |
108 | ).choose() | |
bdaf31cd | 109 | } |
97a2abc3 JB |
110 | |
111 | /** | |
51fe3d3c | 112 | * Removes a worker from the worker choice strategy in the context. |
97a2abc3 JB |
113 | * |
114 | * @param workerKey - The key of the worker to remove. | |
115 | * @returns `true` if the removal is successful, `false` otherwise. | |
116 | */ | |
117 | public remove (workerKey: number): boolean { | |
95c83464 JB |
118 | return ( |
119 | this.workerChoiceStrategies.get( | |
120 | this.workerChoiceStrategyType | |
17393ac8 | 121 | ) as IWorkerChoiceStrategy |
95c83464 JB |
122 | ).remove(workerKey) |
123 | } | |
bdaf31cd | 124 | } |