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
>,
44 * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN
46 private workerChoiceStrategy
: WorkerChoiceStrategy
= WorkerChoiceStrategies
.ROUND_ROBIN
,
47 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
49 this.execute
.bind(this)
50 this.workerChoiceStrategies
= new Map
<
55 WorkerChoiceStrategies
.ROUND_ROBIN
,
56 new RoundRobinWorkerChoiceStrategy
<Worker
, Data
, Response
>(pool
, opts
)
59 WorkerChoiceStrategies
.LESS_USED
,
60 new LessUsedWorkerChoiceStrategy
<Worker
, Data
, Response
>(pool
, opts
)
63 WorkerChoiceStrategies
.LESS_BUSY
,
64 new LessBusyWorkerChoiceStrategy
<Worker
, Data
, Response
>(pool
, opts
)
67 WorkerChoiceStrategies
.FAIR_SHARE
,
68 new FairShareWorkerChoiceStrategy
<Worker
, Data
, Response
>(pool
, opts
)
71 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
,
72 new WeightedRoundRobinWorkerChoiceStrategy
<Worker
, Data
, Response
>(
81 * Gets the worker choice strategy in the context required statistics.
83 * @returns The required statistics.
85 public getRequiredStatistics (): RequiredStatistics
{
87 this.workerChoiceStrategies
.get(
88 this.workerChoiceStrategy
89 ) as IWorkerChoiceStrategy
94 * Sets the worker choice strategy to use in the context.
96 * @param workerChoiceStrategy - The worker choice strategy to set.
98 public setWorkerChoiceStrategy (
99 workerChoiceStrategy
: WorkerChoiceStrategy
101 if (this.workerChoiceStrategy
!== workerChoiceStrategy
) {
102 this.workerChoiceStrategy
= workerChoiceStrategy
104 this.workerChoiceStrategies
.get(this.workerChoiceStrategy
)?.reset()
108 * Executes the worker choice strategy algorithm in the context.
110 * @returns The key of the worker node.
112 public execute (): number {
114 this.workerChoiceStrategies
.get(
115 this.workerChoiceStrategy
116 ) as IWorkerChoiceStrategy
121 * Removes a worker node key from the worker choice strategy in the context.
123 * @param workerNodeKey - The key of the worker node.
124 * @returns `true` if the removal is successful, `false` otherwise.
126 public remove (workerNodeKey
: number): boolean {
128 this.workerChoiceStrategies
.get(
129 this.workerChoiceStrategy
130 ) as IWorkerChoiceStrategy
131 ).remove(workerNodeKey
)
135 * Sets the worker choice strategies in the context options.
137 * @param opts - The worker choice strategy options.
139 public setOptions (opts
: WorkerChoiceStrategyOptions
): void {
140 this.workerChoiceStrategies
.forEach(workerChoiceStrategy
=> {
141 workerChoiceStrategy
.setOptions(opts
)