1 import type { IPool
} from
'../pool.js'
2 import type { IWorker
} from
'../worker.js'
6 TaskStatisticsRequirements
,
8 WorkerChoiceStrategyOptions
,
9 } from
'./selection-strategies-types.js'
11 import { WorkerChoiceStrategies
} from
'./selection-strategies-types.js'
13 buildWorkerChoiceStrategiesPolicy
,
14 buildWorkerChoiceStrategiesTaskStatisticsRequirements
,
15 getWorkerChoiceStrategiesRetries
,
16 getWorkerChoiceStrategy
,
17 } from
'./selection-strategies-utils.js'
20 * 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.
26 export class WorkerChoiceStrategiesContext
<
27 Worker
extends IWorker
,
32 * The default worker choice strategy in the context.
34 private defaultWorkerChoiceStrategy
: WorkerChoiceStrategy
37 * The maximum number of worker choice strategies execution retries.
39 private readonly retries
: number
42 * The worker choice strategies registered in the context.
44 private readonly workerChoiceStrategies
: Map
<
50 * The active worker choice strategies in the context policy.
52 private workerChoiceStrategiesPolicy
: StrategyPolicy
55 * The active worker choice strategies in the context task statistics requirements.
57 private workerChoiceStrategiesTaskStatisticsRequirements
: TaskStatisticsRequirements
60 * The number of worker choice strategies execution retries.
62 public retriesCount
: number
65 * 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 * Adds a worker choice strategy to the context.
102 * @param workerChoiceStrategy - The worker choice strategy to add.
103 * @param pool - The pool instance.
104 * @param opts - The worker choice strategy options.
105 * @returns The worker choice strategies.
107 private addWorkerChoiceStrategy (
108 workerChoiceStrategy
: WorkerChoiceStrategy
,
109 pool
: IPool
<Worker
, Data
, Response
>,
110 opts
?: WorkerChoiceStrategyOptions
111 ): Map
<WorkerChoiceStrategy
, IWorkerChoiceStrategy
> {
112 if (!this.workerChoiceStrategies
.has(workerChoiceStrategy
)) {
113 return this.workerChoiceStrategies
.set(
114 workerChoiceStrategy
,
115 getWorkerChoiceStrategy
<Worker
, Data
, Response
>(
116 workerChoiceStrategy
,
123 return this.workerChoiceStrategies
127 * Executes the given worker choice strategy.
128 * @param workerChoiceStrategy - The worker choice strategy.
129 * @returns The key of the worker node.
130 * @throws {@link https://nodejs.org/api/errors.html#class-error} If after computed retries the worker node key is null or undefined.
132 private executeStrategy (workerChoiceStrategy
: IWorkerChoiceStrategy
): number {
133 let workerNodeKey
: number | undefined
137 workerNodeKey
= workerChoiceStrategy
.choose()
138 if (workerNodeKey
== null && chooseCount
> 0) {
143 } while (workerNodeKey
== null && retriesCount
< this.retries
)
144 if (workerNodeKey
== null) {
146 `Worker node key chosen is null or undefined after ${retriesCount.toString()} retries`
153 * Removes a worker choice strategy from the context.
154 * @param workerChoiceStrategy - The worker choice strategy to remove.
155 * @returns `true` if the worker choice strategy is removed, `false` otherwise.
157 private removeWorkerChoiceStrategy (
158 workerChoiceStrategy
: WorkerChoiceStrategy
160 return this.workerChoiceStrategies
.delete(workerChoiceStrategy
)
164 * Executes the given worker choice strategy in the context algorithm.
165 * @param workerChoiceStrategy - The worker choice strategy algorithm to execute. @defaultValue this.defaultWorkerChoiceStrategy
166 * @returns The key of the worker node.
167 * @throws {@link https://nodejs.org/api/errors.html#class-error} If after computed retries the worker node key is null or undefined.
170 workerChoiceStrategy
: WorkerChoiceStrategy
= this
171 .defaultWorkerChoiceStrategy
173 return this.executeStrategy(
174 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
175 this.workerChoiceStrategies
.get(workerChoiceStrategy
)!
180 * Gets the active worker choice strategies in the context policy.
181 * @returns The strategies policy.
183 public getPolicy (): StrategyPolicy
{
184 return this.workerChoiceStrategiesPolicy
188 * Gets the active worker choice strategies in the context task statistics requirements.
189 * @returns The strategies task statistics requirements.
191 public getTaskStatisticsRequirements (): TaskStatisticsRequirements
{
192 return this.workerChoiceStrategiesTaskStatisticsRequirements
196 * Removes the worker node key from the active worker choice strategies in the context.
197 * @param workerNodeKey - The worker node key.
198 * @returns `true` if the removal is successful, `false` otherwise.
200 public remove (workerNodeKey
: number): boolean {
202 this.workerChoiceStrategies
,
203 ([_
, workerChoiceStrategy
]) => workerChoiceStrategy
.remove(workerNodeKey
)
208 * Sets the default worker choice strategy to use in the context.
209 * @param workerChoiceStrategy - The default worker choice strategy to set.
210 * @param opts - The worker choice strategy options.
212 public setDefaultWorkerChoiceStrategy (
213 workerChoiceStrategy
: WorkerChoiceStrategy
,
214 opts
?: WorkerChoiceStrategyOptions
216 if (workerChoiceStrategy
!== this.defaultWorkerChoiceStrategy
) {
217 this.defaultWorkerChoiceStrategy
= workerChoiceStrategy
218 this.addWorkerChoiceStrategy(workerChoiceStrategy
, this.pool
, opts
)
223 * Sets the active worker choice strategies in the context options.
224 * @param opts - The worker choice strategy options.
226 public setOptions (opts
: undefined | WorkerChoiceStrategyOptions
): void {
227 for (const workerChoiceStrategy
of this.workerChoiceStrategies
.values()) {
228 workerChoiceStrategy
.setOptions(opts
)
233 * Synchronizes the active worker choice strategies in the context with the given worker choice strategies.
234 * @param workerChoiceStrategies - The worker choice strategies to synchronize.
235 * @param opts - The worker choice strategy options.
237 public syncWorkerChoiceStrategies (
238 workerChoiceStrategies
: Set
<WorkerChoiceStrategy
>,
239 opts
?: WorkerChoiceStrategyOptions
241 for (const workerChoiceStrategy
of this.workerChoiceStrategies
.keys()) {
242 if (!workerChoiceStrategies
.has(workerChoiceStrategy
)) {
243 this.removeWorkerChoiceStrategy(workerChoiceStrategy
)
246 for (const workerChoiceStrategy
of workerChoiceStrategies
) {
247 if (!this.workerChoiceStrategies
.has(workerChoiceStrategy
)) {
248 this.addWorkerChoiceStrategy(workerChoiceStrategy
, this.pool
, opts
)
251 this.workerChoiceStrategiesPolicy
= buildWorkerChoiceStrategiesPolicy(
252 this.workerChoiceStrategies
254 this.workerChoiceStrategiesTaskStatisticsRequirements
=
255 buildWorkerChoiceStrategiesTaskStatisticsRequirements(
256 this.workerChoiceStrategies
261 * Updates the worker node key in the active worker choice strategies in the context internals.
262 * @param workerNodeKey - The worker node key.
263 * @returns `true` if the update is successful, `false` otherwise.
265 public update (workerNodeKey
: number): boolean {
267 this.workerChoiceStrategies
,
268 ([_
, workerChoiceStrategy
]) => workerChoiceStrategy
.update(workerNodeKey
)