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 { LessBusyWorkerChoiceStrategy
} from
'./less-busy-worker-choice-strategy'
7 import { LessUsedWorkerChoiceStrategy
} from
'./less-used-worker-choice-strategy'
8 import { RoundRobinWorkerChoiceStrategy
} from
'./round-robin-worker-choice-strategy'
10 IWorkerChoiceStrategy
,
13 WorkerChoiceStrategyOptions
14 } from
'./selection-strategies-types'
15 import { WorkerChoiceStrategies
} from
'./selection-strategies-types'
16 import { WeightedRoundRobinWorkerChoiceStrategy
} from
'./weighted-round-robin-worker-choice-strategy'
19 * The worker choice strategy context.
21 * @typeParam Worker - Type of worker.
22 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
23 * @typeParam Response - Type of execution response. This can only be serializable data.
25 export class WorkerChoiceStrategyContext
<
26 Worker
extends IWorker
,
30 private readonly workerChoiceStrategies
: Map
<
36 * Worker choice strategy context constructor.
38 * @param pool - The pool instance.
39 * @param workerChoiceStrategy - The worker choice strategy.
40 * @param opts - The worker choice strategy options.
43 pool
: IPool
<Worker
, Data
, Response
>,
44 private workerChoiceStrategy
: WorkerChoiceStrategy
= WorkerChoiceStrategies
.ROUND_ROBIN
,
45 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
47 this.execute
= this.execute
.bind(this)
48 this.workerChoiceStrategies
= new Map
<
53 WorkerChoiceStrategies
.ROUND_ROBIN
,
54 new (RoundRobinWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
60 WorkerChoiceStrategies
.LESS_USED
,
61 new (LessUsedWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
67 WorkerChoiceStrategies
.LESS_BUSY
,
68 new (LessBusyWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
74 WorkerChoiceStrategies
.FAIR_SHARE
,
75 new (FairShareWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
81 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
,
82 new (WeightedRoundRobinWorkerChoiceStrategy
.bind(this))<
89 WorkerChoiceStrategies
.INTERLEAVED_WEIGHTED_ROUND_ROBIN
,
90 new (InterleavedWeightedRoundRobinWorkerChoiceStrategy
.bind(this))<
100 * Gets the worker choice strategy in the context required statistics.
102 * @returns The required statistics.
104 public getRequiredStatistics (): RequiredStatistics
{
106 this.workerChoiceStrategies
.get(
107 this.workerChoiceStrategy
108 ) as IWorkerChoiceStrategy
113 * Sets the worker choice strategy to use in the context.
115 * @param workerChoiceStrategy - The worker choice strategy to set.
117 public setWorkerChoiceStrategy (
118 workerChoiceStrategy
: WorkerChoiceStrategy
120 if (this.workerChoiceStrategy
!== workerChoiceStrategy
) {
121 this.workerChoiceStrategy
= workerChoiceStrategy
123 this.workerChoiceStrategies
.get(this.workerChoiceStrategy
)?.reset()
127 * Updates the worker node key in the worker choice strategy internals in the context.
129 * @returns `true` if the update is successful, `false` otherwise.
131 public update (workerNodeKey
: number): boolean {
133 this.workerChoiceStrategies
.get(
134 this.workerChoiceStrategy
135 ) as IWorkerChoiceStrategy
136 ).update(workerNodeKey
)
140 * Executes the worker choice strategy algorithm in the context.
142 * @returns The key of the worker node.
143 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the worker node key is null or undefined.
145 public execute (): number {
146 const workerNodeKey
= (
147 this.workerChoiceStrategies
.get(
148 this.workerChoiceStrategy
149 ) as IWorkerChoiceStrategy
151 if (workerNodeKey
== null) {
152 throw new Error('Worker node key chosen is null or undefined')
158 * Removes the worker node key from the worker choice strategy in the context.
160 * @param workerNodeKey - The key of the worker node.
161 * @returns `true` if the removal is successful, `false` otherwise.
163 public remove (workerNodeKey
: number): boolean {
165 this.workerChoiceStrategies
.get(
166 this.workerChoiceStrategy
167 ) as IWorkerChoiceStrategy
168 ).remove(workerNodeKey
)
172 * Sets the worker choice strategies in the context options.
174 * @param opts - The worker choice strategy options.
176 public setOptions (opts
: WorkerChoiceStrategyOptions
): void {
177 this.workerChoiceStrategies
.forEach(workerChoiceStrategy
=> {
178 workerChoiceStrategy
.setOptions(opts
)