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
,
14 WorkerChoiceStrategyOptions
15 } from
'./selection-strategies-types'
16 import { WorkerChoiceStrategies
} from
'./selection-strategies-types'
17 import { WeightedRoundRobinWorkerChoiceStrategy
} from
'./weighted-round-robin-worker-choice-strategy'
20 * The worker choice strategy context.
22 * @typeParam Worker - Type of worker.
23 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
24 * @typeParam Response - Type of execution response. This can only be serializable data.
26 export class WorkerChoiceStrategyContext
<
27 Worker
extends IWorker
,
31 private readonly workerChoiceStrategies
: Map
<
37 * Worker choice strategy context constructor.
39 * @param pool - The pool instance.
40 * @param workerChoiceStrategy - The worker choice strategy.
41 * @param opts - The worker choice strategy options.
44 pool
: IPool
<Worker
, Data
, Response
>,
45 private workerChoiceStrategy
: WorkerChoiceStrategy
= WorkerChoiceStrategies
.ROUND_ROBIN
,
46 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
48 this.execute
= this.execute
.bind(this)
49 this.workerChoiceStrategies
= new Map
<
54 WorkerChoiceStrategies
.ROUND_ROBIN
,
55 new (RoundRobinWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
61 WorkerChoiceStrategies
.LEAST_USED
,
62 new (LeastUsedWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
68 WorkerChoiceStrategies
.LEAST_BUSY
,
69 new (LeastBusyWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
75 WorkerChoiceStrategies
.LEAST_ELU
,
76 new (LeastEluWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
82 WorkerChoiceStrategies
.FAIR_SHARE
,
83 new (FairShareWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
89 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
,
90 new (WeightedRoundRobinWorkerChoiceStrategy
.bind(this))<
97 WorkerChoiceStrategies
.INTERLEAVED_WEIGHTED_ROUND_ROBIN
,
98 new (InterleavedWeightedRoundRobinWorkerChoiceStrategy
.bind(this))<
108 * Gets the worker choice strategy task statistics in the context.
110 * @returns The task statistics.
112 public getTaskStatistics (): TaskStatistics
{
114 this.workerChoiceStrategies
.get(
115 this.workerChoiceStrategy
116 ) as IWorkerChoiceStrategy
121 * Sets the worker choice strategy to use in the context.
123 * @param workerChoiceStrategy - The worker choice strategy to set.
125 public setWorkerChoiceStrategy (
126 workerChoiceStrategy
: WorkerChoiceStrategy
128 if (this.workerChoiceStrategy
!== workerChoiceStrategy
) {
129 this.workerChoiceStrategy
= workerChoiceStrategy
131 this.workerChoiceStrategies
.get(this.workerChoiceStrategy
)?.reset()
135 * Updates the worker node key in the worker choice strategy internals in the context.
137 * @returns `true` if the update is successful, `false` otherwise.
139 public update (workerNodeKey
: number): boolean {
141 this.workerChoiceStrategies
.get(
142 this.workerChoiceStrategy
143 ) as IWorkerChoiceStrategy
144 ).update(workerNodeKey
)
148 * Executes the worker choice strategy algorithm in the context.
150 * @returns The key of the worker node.
151 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the worker node key is null or undefined.
153 public execute (): number {
154 const workerNodeKey
= (
155 this.workerChoiceStrategies
.get(
156 this.workerChoiceStrategy
157 ) as IWorkerChoiceStrategy
159 if (workerNodeKey
== null) {
160 throw new Error('Worker node key chosen is null or undefined')
166 * Removes the worker node key from the worker choice strategy in the context.
168 * @param workerNodeKey - The key of the worker node.
169 * @returns `true` if the removal is successful, `false` otherwise.
171 public remove (workerNodeKey
: number): boolean {
173 this.workerChoiceStrategies
.get(
174 this.workerChoiceStrategy
175 ) as IWorkerChoiceStrategy
176 ).remove(workerNodeKey
)
180 * Sets the worker choice strategies in the context options.
182 * @param opts - The worker choice strategy options.
184 public setOptions (opts
: WorkerChoiceStrategyOptions
): void {
185 for (const workerChoiceStrategy
of this.workerChoiceStrategies
.values()) {
186 workerChoiceStrategy
.setOptions(opts
)