1 import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
} from
'../../utils'
2 import type { IPoolInternal
} from
'../pool-internal'
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 response of execution. 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 workerChoiceStrategyType - The worker choice strategy.
39 * @param opts - The worker choice strategy options.
42 pool
: IPoolInternal
<Worker
, Data
, Response
>,
43 private workerChoiceStrategyType
: WorkerChoiceStrategy
= WorkerChoiceStrategies
.ROUND_ROBIN
,
44 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
46 this.execute
.bind(this)
47 this.workerChoiceStrategies
= new Map
<
52 WorkerChoiceStrategies
.ROUND_ROBIN
,
53 new RoundRobinWorkerChoiceStrategy
<Worker
, Data
, Response
>(pool
, opts
)
56 WorkerChoiceStrategies
.LESS_USED
,
57 new LessUsedWorkerChoiceStrategy
<Worker
, Data
, Response
>(pool
, opts
)
60 WorkerChoiceStrategies
.LESS_BUSY
,
61 new LessBusyWorkerChoiceStrategy
<Worker
, Data
, Response
>(pool
, opts
)
64 WorkerChoiceStrategies
.FAIR_SHARE
,
65 new FairShareWorkerChoiceStrategy
<Worker
, Data
, Response
>(pool
, opts
)
68 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
,
69 new WeightedRoundRobinWorkerChoiceStrategy
<Worker
, Data
, Response
>(
78 * Gets the worker choice strategy in the context required statistics.
80 * @returns The required statistics.
82 public getRequiredStatistics (): RequiredStatistics
{
84 this.workerChoiceStrategies
.get(
85 this.workerChoiceStrategyType
86 ) as IWorkerChoiceStrategy
91 * Sets the worker choice strategy to use in the context.
93 * @param workerChoiceStrategy - The worker choice strategy to set.
95 public setWorkerChoiceStrategy (
96 workerChoiceStrategy
: WorkerChoiceStrategy
98 if (this.workerChoiceStrategyType
!== workerChoiceStrategy
) {
99 this.workerChoiceStrategyType
= workerChoiceStrategy
101 this.workerChoiceStrategies
.get(this.workerChoiceStrategyType
)?.reset()
105 * Executes the worker choice strategy algorithm in the context.
107 * @returns The key of the worker node.
109 public execute (): number {
111 this.workerChoiceStrategies
.get(
112 this.workerChoiceStrategyType
113 ) as IWorkerChoiceStrategy
118 * Removes a worker node key from the worker choice strategy in the context.
120 * @param workerNodeKey - The key of the worker node.
121 * @returns `true` if the removal is successful, `false` otherwise.
123 public remove (workerNodeKey
: number): boolean {
125 this.workerChoiceStrategies
.get(
126 this.workerChoiceStrategyType
127 ) as IWorkerChoiceStrategy
128 ).remove(workerNodeKey
)