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 { LeastBusyWorkerChoiceStrategy
} from
'./least-busy-worker-choice-strategy'
6 import { LeastUsedWorkerChoiceStrategy
} from
'./least-used-worker-choice-strategy'
7 import { RoundRobinWorkerChoiceStrategy
} from
'./round-robin-worker-choice-strategy'
12 WorkerChoiceStrategyOptions
13 } from
'./selection-strategies-types'
14 import { WorkerChoiceStrategies
} from
'./selection-strategies-types'
15 import { WeightedRoundRobinWorkerChoiceStrategy
} from
'./weighted-round-robin-worker-choice-strategy'
18 * The worker choice strategy context.
20 * @typeParam Worker - Type of worker.
21 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
22 * @typeParam Response - Type of execution response. This can only be serializable data.
24 export class WorkerChoiceStrategyContext
<
25 Worker
extends IWorker
,
29 private readonly workerChoiceStrategies
: Map
<
35 * Worker choice strategy context constructor.
37 * @param pool - The pool instance.
38 * @param workerChoiceStrategy - The worker choice strategy.
39 * @param opts - The worker choice strategy options.
42 pool
: IPool
<Worker
, Data
, Response
>,
43 private workerChoiceStrategy
: WorkerChoiceStrategy
= WorkerChoiceStrategies
.ROUND_ROBIN
,
44 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
46 this.execute
= this.execute
.bind(this)
47 this.workerChoiceStrategies
= new Map
<
52 WorkerChoiceStrategies
.ROUND_ROBIN
,
53 new (RoundRobinWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
59 WorkerChoiceStrategies
.LEAST_USED
,
60 new (LeastUsedWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
66 WorkerChoiceStrategies
.LEAST_BUSY
,
67 new (LeastBusyWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
73 WorkerChoiceStrategies
.FAIR_SHARE
,
74 new (FairShareWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
80 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
,
81 new (WeightedRoundRobinWorkerChoiceStrategy
.bind(this))<
91 * Gets the worker choice strategy in the context required statistics.
93 * @returns The required statistics.
95 public getRequiredStatistics (): RequiredStatistics
{
97 this.workerChoiceStrategies
.get(
98 this.workerChoiceStrategy
99 ) as IWorkerChoiceStrategy
104 * Sets the worker choice strategy to use in the context.
106 * @param workerChoiceStrategy - The worker choice strategy to set.
108 public setWorkerChoiceStrategy (
109 workerChoiceStrategy
: WorkerChoiceStrategy
111 if (this.workerChoiceStrategy
!== workerChoiceStrategy
) {
112 this.workerChoiceStrategy
= workerChoiceStrategy
114 this.workerChoiceStrategies
.get(this.workerChoiceStrategy
)?.reset()
118 * Updates the worker node key in the worker choice strategy internals in the context.
120 * @returns `true` if the update is successful, `false` otherwise.
122 public update (workerNodeKey
: number): boolean {
124 this.workerChoiceStrategies
.get(
125 this.workerChoiceStrategy
126 ) as IWorkerChoiceStrategy
127 ).update(workerNodeKey
)
131 * Executes the worker choice strategy algorithm in the context.
133 * @returns The key of the worker node.
134 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the worker node key is null or undefined.
136 public execute (): number {
137 const workerNodeKey
= (
138 this.workerChoiceStrategies
.get(
139 this.workerChoiceStrategy
140 ) as IWorkerChoiceStrategy
142 if (workerNodeKey
== null) {
143 throw new Error('Worker node key chosen is null or undefined')
149 * Removes the worker node key from the worker choice strategy in the context.
151 * @param workerNodeKey - The key of the worker node.
152 * @returns `true` if the removal is successful, `false` otherwise.
154 public remove (workerNodeKey
: number): boolean {
156 this.workerChoiceStrategies
.get(
157 this.workerChoiceStrategy
158 ) as IWorkerChoiceStrategy
159 ).remove(workerNodeKey
)
163 * Sets the worker choice strategies in the context options.
165 * @param opts - The worker choice strategy options.
167 public setOptions (opts
: WorkerChoiceStrategyOptions
): void {
168 for (const workerChoiceStrategy
of this.workerChoiceStrategies
.values()) {
169 workerChoiceStrategy
.setOptions(opts
)