1 import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
} from
'../../utils'
2 import type { IPool
} from
'../pool'
3 import type { IWorker
} from
'../worker'
4 import { FairShareWorkerChoiceStrategy
} from
'./fair-share-worker-choice-strategy'
5 import { InterleavedWeightedRoundRobinWorkerChoiceStrategy
} from
'./interleaved-weighted-round-robin-worker-choice-strategy'
6 import { LeastBusyWorkerChoiceStrategy
} from
'./least-busy-worker-choice-strategy'
7 import { LeastUsedWorkerChoiceStrategy
} from
'./least-used-worker-choice-strategy'
8 import { LeastEluWorkerChoiceStrategy
} from
'./least-elu-worker-choice-strategy'
9 import { RoundRobinWorkerChoiceStrategy
} from
'./round-robin-worker-choice-strategy'
11 IWorkerChoiceStrategy
,
13 TaskStatisticsRequirements
,
15 WorkerChoiceStrategyOptions
16 } from
'./selection-strategies-types'
17 import { WorkerChoiceStrategies
} from
'./selection-strategies-types'
18 import { WeightedRoundRobinWorkerChoiceStrategy
} from
'./weighted-round-robin-worker-choice-strategy'
21 * The worker choice strategy context.
23 * @typeParam Worker - Type of worker.
24 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
25 * @typeParam Response - Type of execution response. This can only be serializable data.
27 export class WorkerChoiceStrategyContext
<
28 Worker
extends IWorker
,
32 private readonly workerChoiceStrategies
: Map
<
38 * Worker choice strategy context constructor.
40 * @param pool - The pool instance.
41 * @param workerChoiceStrategy - The worker choice strategy.
42 * @param opts - The worker choice strategy options.
45 pool
: IPool
<Worker
, Data
, Response
>,
46 private workerChoiceStrategy
: WorkerChoiceStrategy
= WorkerChoiceStrategies
.ROUND_ROBIN
,
47 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
49 this.execute
= this.execute
.bind(this)
50 this.workerChoiceStrategies
= new Map
<
55 WorkerChoiceStrategies
.ROUND_ROBIN
,
56 new (RoundRobinWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
62 WorkerChoiceStrategies
.LEAST_USED
,
63 new (LeastUsedWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
69 WorkerChoiceStrategies
.LEAST_BUSY
,
70 new (LeastBusyWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
76 WorkerChoiceStrategies
.LEAST_ELU
,
77 new (LeastEluWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
83 WorkerChoiceStrategies
.FAIR_SHARE
,
84 new (FairShareWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
90 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
,
91 new (WeightedRoundRobinWorkerChoiceStrategy
.bind(this))<
98 WorkerChoiceStrategies
.INTERLEAVED_WEIGHTED_ROUND_ROBIN
,
99 new (InterleavedWeightedRoundRobinWorkerChoiceStrategy
.bind(this))<
109 * Gets the strategy policy in the context.
111 * @returns The strategy policy.
113 public getStrategyPolicy (): StrategyPolicy
{
115 this.workerChoiceStrategies
.get(
116 this.workerChoiceStrategy
117 ) as IWorkerChoiceStrategy
122 * Gets the worker choice strategy task statistics requirements in the context.
124 * @returns The task statistics requirements.
126 public getTaskStatisticsRequirements (): TaskStatisticsRequirements
{
128 this.workerChoiceStrategies
.get(
129 this.workerChoiceStrategy
130 ) as IWorkerChoiceStrategy
131 ).taskStatisticsRequirements
135 * Sets the worker choice strategy to use in the context.
137 * @param workerChoiceStrategy - The worker choice strategy to set.
139 public setWorkerChoiceStrategy (
140 workerChoiceStrategy
: WorkerChoiceStrategy
142 if (this.workerChoiceStrategy
!== workerChoiceStrategy
) {
143 this.workerChoiceStrategy
= workerChoiceStrategy
145 this.workerChoiceStrategies
.get(this.workerChoiceStrategy
)?.reset()
149 * Updates the worker node key in the worker choice strategy internals in the context.
151 * @returns `true` if the update is successful, `false` otherwise.
153 public update (workerNodeKey
: number): boolean {
155 this.workerChoiceStrategies
.get(
156 this.workerChoiceStrategy
157 ) as IWorkerChoiceStrategy
158 ).update(workerNodeKey
)
162 * Executes the worker choice strategy algorithm in the context.
164 * @returns The key of the worker node.
165 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the worker node key is null or undefined.
167 public execute (): number {
168 const workerNodeKey
= (
169 this.workerChoiceStrategies
.get(
170 this.workerChoiceStrategy
171 ) as IWorkerChoiceStrategy
173 if (workerNodeKey
== null) {
174 throw new Error('Worker node key chosen is null or undefined')
180 * Removes the worker node key from the worker choice strategy in the context.
182 * @param workerNodeKey - The key of the worker node.
183 * @returns `true` if the removal is successful, `false` otherwise.
185 public remove (workerNodeKey
: number): boolean {
187 this.workerChoiceStrategies
.get(
188 this.workerChoiceStrategy
189 ) as IWorkerChoiceStrategy
190 ).remove(workerNodeKey
)
194 * Sets the worker choice strategies in the context options.
196 * @param opts - The worker choice strategy options.
198 public setOptions (opts
: WorkerChoiceStrategyOptions
): void {
199 for (const workerChoiceStrategy
of this.workerChoiceStrategies
.values()) {
200 workerChoiceStrategy
.setOptions(opts
)