1 import type { IPoolInternal
} from
'../pool-internal'
2 import type { IPoolWorker
} from
'../pool-worker'
3 import { FairShareWorkerChoiceStrategy
} from
'./fair-share-worker-choice-strategy'
4 import { LessBusyWorkerChoiceStrategy
} from
'./less-busy-worker-choice-strategy'
5 import { LessUsedWorkerChoiceStrategy
} from
'./less-used-worker-choice-strategy'
6 import { RoundRobinWorkerChoiceStrategy
} from
'./round-robin-worker-choice-strategy'
11 } from
'./selection-strategies-types'
12 import { WorkerChoiceStrategies
} from
'./selection-strategies-types'
13 import { WeightedRoundRobinWorkerChoiceStrategy
} from
'./weighted-round-robin-worker-choice-strategy'
16 * The worker choice strategy context.
18 * @typeParam Worker - Type of worker.
19 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
20 * @typeParam Response - Type of response of execution. This can only be serializable data.
22 export class WorkerChoiceStrategyContext
<
23 Worker
extends IPoolWorker
,
27 private readonly workerChoiceStrategies
: Map
<
33 * Worker choice strategy context constructor.
35 * @param pool - The pool instance.
36 * @param workerChoiceStrategyType - The worker choice strategy.
39 pool
: IPoolInternal
<Worker
, Data
, Response
>,
40 private workerChoiceStrategyType
: WorkerChoiceStrategy
= WorkerChoiceStrategies
.ROUND_ROBIN
42 this.execute
.bind(this)
43 this.workerChoiceStrategies
= new Map
<
48 WorkerChoiceStrategies
.ROUND_ROBIN
,
49 new RoundRobinWorkerChoiceStrategy
<Worker
, Data
, Response
>(pool
)
52 WorkerChoiceStrategies
.LESS_USED
,
53 new LessUsedWorkerChoiceStrategy
<Worker
, Data
, Response
>(pool
)
56 WorkerChoiceStrategies
.LESS_BUSY
,
57 new LessBusyWorkerChoiceStrategy
<Worker
, Data
, Response
>(pool
)
60 WorkerChoiceStrategies
.FAIR_SHARE
,
61 new FairShareWorkerChoiceStrategy
<Worker
, Data
, Response
>(pool
)
64 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
,
65 new WeightedRoundRobinWorkerChoiceStrategy
<Worker
, Data
, Response
>(pool
)
71 * Gets the worker choice strategy in the context required statistics.
73 * @returns The required statistics.
75 public getRequiredStatistics (): RequiredStatistics
{
77 this.workerChoiceStrategies
.get(
78 this.workerChoiceStrategyType
79 ) as IWorkerChoiceStrategy
84 * Sets the worker choice strategy to use in the context.
86 * @param workerChoiceStrategy - The worker choice strategy to set.
88 public setWorkerChoiceStrategy (
89 workerChoiceStrategy
: WorkerChoiceStrategy
91 if (this.workerChoiceStrategyType
!== workerChoiceStrategy
) {
92 this.workerChoiceStrategyType
= workerChoiceStrategy
94 this.workerChoiceStrategies
.get(this.workerChoiceStrategyType
)?.reset()
98 * Executes the worker choice strategy algorithm in the context.
100 * @returns The key of the chosen one.
102 public execute (): number {
104 this.workerChoiceStrategies
.get(
105 this.workerChoiceStrategyType
106 ) as IWorkerChoiceStrategy
111 * Removes a worker from the worker choice strategy in the context.
113 * @param workerKey - The key of the worker to remove.
114 * @returns `true` if the removal is successful, `false` otherwise.
116 public remove (workerKey
: number): boolean {
118 this.workerChoiceStrategies
.get(
119 this.workerChoiceStrategyType
120 ) as IWorkerChoiceStrategy