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 createWorkerCallback - The worker creation callback for dynamic pool.
37 * @param workerChoiceStrategy - The worker choice strategy.
40 pool
: IPoolInternal
<Worker
, Data
, Response
>,
41 private workerChoiceStrategyType
: WorkerChoiceStrategy
= WorkerChoiceStrategies
.ROUND_ROBIN
43 this.execute
.bind(this)
44 this.workerChoiceStrategies
= new Map
<
49 WorkerChoiceStrategies
.ROUND_ROBIN
,
50 new RoundRobinWorkerChoiceStrategy
<Worker
, Data
, Response
>(pool
)
53 WorkerChoiceStrategies
.LESS_USED
,
54 new LessUsedWorkerChoiceStrategy
<Worker
, Data
, Response
>(pool
)
57 WorkerChoiceStrategies
.LESS_BUSY
,
58 new LessBusyWorkerChoiceStrategy
<Worker
, Data
, Response
>(pool
)
61 WorkerChoiceStrategies
.FAIR_SHARE
,
62 new FairShareWorkerChoiceStrategy
<Worker
, Data
, Response
>(pool
)
65 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
,
66 new WeightedRoundRobinWorkerChoiceStrategy
<Worker
, Data
, Response
>(pool
)
72 * Gets the worker choice strategy in the context required statistics.
74 * @returns The required statistics.
76 public getRequiredStatistics (): RequiredStatistics
{
78 this.workerChoiceStrategies
.get(
79 this.workerChoiceStrategyType
80 ) as IWorkerChoiceStrategy
85 * Sets the worker choice strategy to use in the context.
87 * @param workerChoiceStrategy - The worker choice strategy to set.
89 public setWorkerChoiceStrategy (
90 workerChoiceStrategy
: WorkerChoiceStrategy
92 if (this.workerChoiceStrategyType
=== workerChoiceStrategy
) {
93 this.workerChoiceStrategies
.get(workerChoiceStrategy
)?.reset()
95 this.workerChoiceStrategyType
= workerChoiceStrategy
100 * Executes the worker choice strategy algorithm in the context.
102 * @returns The key of the chosen one.
104 public execute (): number {
106 this.workerChoiceStrategies
.get(
107 this.workerChoiceStrategyType
108 ) as IWorkerChoiceStrategy
113 * Removes a worker from the worker choice strategy in the context.
115 * @param workerKey - The key of the worker to remove.
116 * @returns `true` if the removal is successful, `false` otherwise.
118 public remove (workerKey
: number): boolean {
120 this.workerChoiceStrategies
.get(
121 this.workerChoiceStrategyType
122 ) as IWorkerChoiceStrategy