1 import type { IPool
} from
'../pool.js'
2 import type { IWorker
} from
'../worker.js'
6 TaskStatisticsRequirements
,
8 WorkerChoiceStrategyOptions
9 } from
'./selection-strategies-types.js'
10 import { WorkerChoiceStrategies
} from
'./selection-strategies-types.js'
12 buildWorkerChoiceStrategiesPolicy
,
13 buildWorkerChoiceStrategiesTaskStatisticsRequirements
,
14 getWorkerChoiceStrategiesRetries
,
15 getWorkerChoiceStrategy
16 } from
'./selection-strategies-utils.js'
19 * The worker choice strategies context.
21 * @typeParam Worker - Type of worker.
22 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
23 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
25 export class WorkerChoiceStrategiesContext
<
26 Worker
extends IWorker
,
31 * The number of worker choice strategies execution retries.
33 public retriesCount
: number
36 * The default worker choice strategy in the context.
38 private defaultWorkerChoiceStrategy
: WorkerChoiceStrategy
41 * The worker choice strategies registered in the context.
43 private readonly workerChoiceStrategies
: Map
<
49 * The active worker choice strategies in the context policy.
51 private workerChoiceStrategiesPolicy
: StrategyPolicy
54 * The active worker choice strategies in the context task statistics requirements.
56 private workerChoiceStrategiesTaskStatisticsRequirements
: TaskStatisticsRequirements
59 * The maximum number of worker choice strategies execution retries.
61 private readonly retries
: number
64 * Worker choice strategies context constructor.
66 * @param pool - The pool instance.
67 * @param workerChoiceStrategies - The worker choice strategies. @defaultValue [WorkerChoiceStrategies.ROUND_ROBIN]
68 * @param opts - The worker choice strategy options.
71 private readonly pool
: IPool
<Worker
, Data
, Response
>,
72 workerChoiceStrategies
: WorkerChoiceStrategy
[] = [
73 WorkerChoiceStrategies
.ROUND_ROBIN
75 opts
?: WorkerChoiceStrategyOptions
77 this.execute
= this.execute
.bind(this)
78 this.defaultWorkerChoiceStrategy
= workerChoiceStrategies
[0]
79 this.workerChoiceStrategies
= new Map
<
83 for (const workerChoiceStrategy
of workerChoiceStrategies
) {
84 this.addWorkerChoiceStrategy(workerChoiceStrategy
, this.pool
, opts
)
86 this.workerChoiceStrategiesPolicy
= buildWorkerChoiceStrategiesPolicy(
87 this.workerChoiceStrategies
89 this.workerChoiceStrategiesTaskStatisticsRequirements
=
90 buildWorkerChoiceStrategiesTaskStatisticsRequirements(
91 this.workerChoiceStrategies
94 this.retries
= getWorkerChoiceStrategiesRetries
<Worker
, Data
, Response
>(
101 * Gets the active worker choice strategies in the context policy.
103 * @returns The strategies policy.
105 public getPolicy (): StrategyPolicy
{
106 return this.workerChoiceStrategiesPolicy
110 * Gets the active worker choice strategies in the context task statistics requirements.
112 * @returns The strategies task statistics requirements.
114 public getTaskStatisticsRequirements (): TaskStatisticsRequirements
{
115 return this.workerChoiceStrategiesTaskStatisticsRequirements
119 * Sets the default worker choice strategy to use in the context.
121 * @param workerChoiceStrategy - The default worker choice strategy to set.
122 * @param opts - The worker choice strategy options.
124 public setDefaultWorkerChoiceStrategy (
125 workerChoiceStrategy
: WorkerChoiceStrategy
,
126 opts
?: WorkerChoiceStrategyOptions
128 if (workerChoiceStrategy
!== this.defaultWorkerChoiceStrategy
) {
129 this.defaultWorkerChoiceStrategy
= workerChoiceStrategy
130 this.addWorkerChoiceStrategy(workerChoiceStrategy
, this.pool
, opts
)
135 * Updates the worker node key in the active worker choice strategies in the context internals.
137 * @returns `true` if the update is successful, `false` otherwise.
139 public update (workerNodeKey
: number): boolean {
141 this.workerChoiceStrategies
,
142 ([_
, workerChoiceStrategy
]) => workerChoiceStrategy
.update(workerNodeKey
)
147 * Executes the given worker choice strategy in the context algorithm.
149 * @param workerChoiceStrategy - The worker choice strategy algorithm to execute. @defaultValue this.defaultWorkerChoiceStrategy
150 * @returns The key of the worker node.
151 * @throws {@link https://nodejs.org/api/errors.html#class-error} If after computed retries the worker node key is null or undefined.
154 workerChoiceStrategy
: WorkerChoiceStrategy
= this
155 .defaultWorkerChoiceStrategy
157 return this.executeStrategy(
158 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
159 this.workerChoiceStrategies
.get(workerChoiceStrategy
)!
164 * Executes the given worker choice strategy.
166 * @param workerChoiceStrategy - The worker choice strategy.
167 * @returns The key of the worker node.
168 * @throws {@link https://nodejs.org/api/errors.html#class-error} If after computed retries the worker node key is null or undefined.
170 private executeStrategy (workerChoiceStrategy
: IWorkerChoiceStrategy
): number {
171 let workerNodeKey
: number | undefined
175 workerNodeKey
= workerChoiceStrategy
.choose()
176 if (workerNodeKey
== null && chooseCount
> 0) {
181 } while (workerNodeKey
== null && retriesCount
< this.retries
)
182 if (workerNodeKey
== null) {
184 `Worker node key chosen is null or undefined after ${retriesCount} retries`
191 * Removes the worker node key from the active worker choice strategies in the context.
193 * @param workerNodeKey - The worker node key.
194 * @returns `true` if the removal is successful, `false` otherwise.
196 public remove (workerNodeKey
: number): boolean {
198 this.workerChoiceStrategies
,
199 ([_
, workerChoiceStrategy
]) => workerChoiceStrategy
.remove(workerNodeKey
)
204 * Sets the active worker choice strategies in the context options.
206 * @param opts - The worker choice strategy options.
208 public setOptions (opts
: WorkerChoiceStrategyOptions
| undefined): void {
209 for (const workerChoiceStrategy
of this.workerChoiceStrategies
.values()) {
210 workerChoiceStrategy
.setOptions(opts
)
215 * Synchronizes the active worker choice strategies in the context with the given worker choice strategies.
217 * @param workerChoiceStrategies - The worker choice strategies to synchronize.
218 * @param opts - The worker choice strategy options.
220 public syncWorkerChoiceStrategies (
221 workerChoiceStrategies
: Set
<WorkerChoiceStrategy
>,
222 opts
?: WorkerChoiceStrategyOptions
224 for (const workerChoiceStrategy
of this.workerChoiceStrategies
.keys()) {
225 if (!workerChoiceStrategies
.has(workerChoiceStrategy
)) {
226 this.removeWorkerChoiceStrategy(workerChoiceStrategy
)
229 for (const workerChoiceStrategy
of workerChoiceStrategies
) {
230 if (!this.workerChoiceStrategies
.has(workerChoiceStrategy
)) {
231 this.addWorkerChoiceStrategy(workerChoiceStrategy
, this.pool
, opts
)
234 this.workerChoiceStrategiesPolicy
= buildWorkerChoiceStrategiesPolicy(
235 this.workerChoiceStrategies
237 this.workerChoiceStrategiesTaskStatisticsRequirements
=
238 buildWorkerChoiceStrategiesTaskStatisticsRequirements(
239 this.workerChoiceStrategies
244 * Adds a worker choice strategy to the context.
246 * @param workerChoiceStrategy - The worker choice strategy to add.
247 * @param opts - The worker choice strategy options.
248 * @returns The worker choice strategies.
250 private addWorkerChoiceStrategy (
251 workerChoiceStrategy
: WorkerChoiceStrategy
,
252 pool
: IPool
<Worker
, Data
, Response
>,
253 opts
?: WorkerChoiceStrategyOptions
254 ): Map
<WorkerChoiceStrategy
, IWorkerChoiceStrategy
> {
255 if (!this.workerChoiceStrategies
.has(workerChoiceStrategy
)) {
256 return this.workerChoiceStrategies
.set(
257 workerChoiceStrategy
,
258 getWorkerChoiceStrategy
<Worker
, Data
, Response
>(
259 workerChoiceStrategy
,
266 return this.workerChoiceStrategies
270 * Removes a worker choice strategy from the context.
272 * @param workerChoiceStrategy - The worker choice strategy to remove.
273 * @returns `true` if the worker choice strategy is removed, `false` otherwise.
275 private removeWorkerChoiceStrategy (
276 workerChoiceStrategy
: WorkerChoiceStrategy
278 return this.workerChoiceStrategies
.delete(workerChoiceStrategy
)