1 import type { IPool
} from
'../pool.js'
2 import type { IWorker
} from
'../worker.js'
3 import { getWorkerChoiceStrategyRetries
} from
'../../utils.js'
4 import { FairShareWorkerChoiceStrategy
} from
'./fair-share-worker-choice-strategy.js'
5 import { InterleavedWeightedRoundRobinWorkerChoiceStrategy
} from
'./interleaved-weighted-round-robin-worker-choice-strategy.js'
6 import { LeastBusyWorkerChoiceStrategy
} from
'./least-busy-worker-choice-strategy.js'
7 import { LeastUsedWorkerChoiceStrategy
} from
'./least-used-worker-choice-strategy.js'
8 import { LeastEluWorkerChoiceStrategy
} from
'./least-elu-worker-choice-strategy.js'
9 import { RoundRobinWorkerChoiceStrategy
} from
'./round-robin-worker-choice-strategy.js'
11 IWorkerChoiceStrategy
,
13 TaskStatisticsRequirements
,
15 WorkerChoiceStrategyOptions
16 } from
'./selection-strategies-types.js'
17 import { WorkerChoiceStrategies
} from
'./selection-strategies-types.js'
18 import { WeightedRoundRobinWorkerChoiceStrategy
} from
'./weighted-round-robin-worker-choice-strategy.js'
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 structured-cloneable data.
25 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
27 export class WorkerChoiceStrategyContext
<
28 Worker
extends IWorker
,
33 * The number of worker choice strategy execution retries.
35 public retriesCount
: number
38 * The worker choice strategy instances registered in the context.
40 private readonly workerChoiceStrategies
: Map
<
46 * The maximum number of worker choice strategy execution retries.
48 private readonly retries
: number
51 * Worker choice strategy context constructor.
53 * @param pool - The pool instance.
54 * @param workerChoiceStrategy - The worker choice strategy.
55 * @param opts - The worker choice strategy options.
58 pool
: IPool
<Worker
, Data
, Response
>,
59 private workerChoiceStrategy
: WorkerChoiceStrategy
= WorkerChoiceStrategies
.ROUND_ROBIN
,
60 opts
?: WorkerChoiceStrategyOptions
62 this.execute
= this.execute
.bind(this)
63 this.workerChoiceStrategies
= new Map
<
68 WorkerChoiceStrategies
.ROUND_ROBIN
,
69 new (RoundRobinWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
75 WorkerChoiceStrategies
.LEAST_USED
,
76 new (LeastUsedWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
82 WorkerChoiceStrategies
.LEAST_BUSY
,
83 new (LeastBusyWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
89 WorkerChoiceStrategies
.LEAST_ELU
,
90 new (LeastEluWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
96 WorkerChoiceStrategies
.FAIR_SHARE
,
97 new (FairShareWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
103 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
,
104 new (WeightedRoundRobinWorkerChoiceStrategy
.bind(this))<
111 WorkerChoiceStrategies
.INTERLEAVED_WEIGHTED_ROUND_ROBIN
,
112 new (InterleavedWeightedRoundRobinWorkerChoiceStrategy
.bind(this))<
119 this.retriesCount
= 0
120 this.retries
= getWorkerChoiceStrategyRetries(pool
, opts
)
124 * Gets the strategy policy in the context.
126 * @returns The strategy policy.
128 public getStrategyPolicy (): StrategyPolicy
{
129 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
130 return this.workerChoiceStrategies
.get(this.workerChoiceStrategy
)!
135 * Gets the worker choice strategy in the context task statistics requirements.
137 * @returns The task statistics requirements.
139 public getTaskStatisticsRequirements (): TaskStatisticsRequirements
{
140 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
141 return this.workerChoiceStrategies
.get(this.workerChoiceStrategy
)!
142 .taskStatisticsRequirements
146 * Sets the worker choice strategy to use in the context.
148 * @param workerChoiceStrategy - The worker choice strategy to set.
150 public setWorkerChoiceStrategy (
151 workerChoiceStrategy
: WorkerChoiceStrategy
153 if (this.workerChoiceStrategy
!== workerChoiceStrategy
) {
154 this.workerChoiceStrategy
= workerChoiceStrategy
156 this.workerChoiceStrategies
.get(this.workerChoiceStrategy
)?.reset()
160 * Updates the worker node key in the worker choice strategy in the context internals.
162 * @returns `true` if the update is successful, `false` otherwise.
164 public update (workerNodeKey
: number): boolean {
165 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
166 return this.workerChoiceStrategies
167 .get(this.workerChoiceStrategy
)!
168 .update(workerNodeKey
)
172 * Executes the worker choice strategy in the context algorithm.
174 * @returns The key of the worker node.
175 * @throws {@link https://nodejs.org/api/errors.html#class-error} If after computed retries the worker node key is null or undefined.
177 public execute (): number {
178 return this.executeStrategy(
179 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
180 this.workerChoiceStrategies
.get(this.workerChoiceStrategy
)!
185 * Executes the given worker choice strategy.
187 * @param workerChoiceStrategy - The worker choice strategy.
188 * @returns The key of the worker node.
189 * @throws {@link https://nodejs.org/api/errors.html#class-error} If after computed retries the worker node key is null or undefined.
191 private executeStrategy (workerChoiceStrategy
: IWorkerChoiceStrategy
): number {
192 let workerNodeKey
: number | undefined
196 workerNodeKey
= workerChoiceStrategy
.choose()
197 if (workerNodeKey
== null && chooseCount
> 0) {
202 } while (workerNodeKey
== null && retriesCount
< this.retries
)
203 if (workerNodeKey
== null) {
205 `Worker node key chosen is null or undefined after ${retriesCount} retries`
212 * Removes the worker node key from the worker choice strategy in the context.
214 * @param workerNodeKey - The worker node key.
215 * @returns `true` if the removal is successful, `false` otherwise.
217 public remove (workerNodeKey
: number): boolean {
218 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
219 return this.workerChoiceStrategies
220 .get(this.workerChoiceStrategy
)!
221 .remove(workerNodeKey
)
225 * Sets the worker choice strategies in the context options.
227 * @param opts - The worker choice strategy options.
229 public setOptions (opts
: WorkerChoiceStrategyOptions
| undefined): void {
230 for (const workerChoiceStrategy
of this.workerChoiceStrategies
.values()) {
231 workerChoiceStrategy
.setOptions(opts
)