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 { LessBusyWorkerChoiceStrategy
} from
'./less-busy-worker-choice-strategy'
6 import { LessUsedWorkerChoiceStrategy
} from
'./less-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
.LESS_USED
,
60 new (LessUsedWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
66 WorkerChoiceStrategies
.LESS_BUSY
,
67 new (LessBusyWorkerChoiceStrategy
.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.
135 public execute (): number {
137 this.workerChoiceStrategies
.get(
138 this.workerChoiceStrategy
139 ) as IWorkerChoiceStrategy
144 * Removes the worker node key from the worker choice strategy in the context.
146 * @param workerNodeKey - The key of the worker node.
147 * @returns `true` if the removal is successful, `false` otherwise.
149 public remove (workerNodeKey
: number): boolean {
151 this.workerChoiceStrategies
.get(
152 this.workerChoiceStrategy
153 ) as IWorkerChoiceStrategy
154 ).remove(workerNodeKey
)
158 * Sets the worker choice strategies in the context options.
160 * @param opts - The worker choice strategy options.
162 public setOptions (opts
: WorkerChoiceStrategyOptions
): void {
163 this.workerChoiceStrategies
.forEach(workerChoiceStrategy
=> {
164 workerChoiceStrategy
.setOptions(opts
)