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
<
29 IWorkerChoiceStrategy
<Worker
, Data
, Response
>
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 readonly createWorkerCallback
: () => number,
42 private workerChoiceStrategyType
: WorkerChoiceStrategy
= WorkerChoiceStrategies
.ROUND_ROBIN
44 this.execute
.bind(this)
45 this.workerChoiceStrategies
= new Map
<
47 IWorkerChoiceStrategy
<Worker
, Data
, Response
>
50 WorkerChoiceStrategies
.ROUND_ROBIN
,
51 new RoundRobinWorkerChoiceStrategy
<Worker
, Data
, Response
>(pool
)
54 WorkerChoiceStrategies
.LESS_USED
,
55 new LessUsedWorkerChoiceStrategy
<Worker
, Data
, Response
>(pool
)
58 WorkerChoiceStrategies
.LESS_BUSY
,
59 new LessBusyWorkerChoiceStrategy
<Worker
, Data
, Response
>(pool
)
62 WorkerChoiceStrategies
.FAIR_SHARE
,
63 new FairShareWorkerChoiceStrategy
<Worker
, Data
, Response
>(pool
)
66 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
,
67 new WeightedRoundRobinWorkerChoiceStrategy
<Worker
, Data
, Response
>(pool
)
73 * Gets the worker choice strategy in the context required statistics.
75 * @returns The required statistics.
77 public getRequiredStatistics (): RequiredStatistics
{
79 this.workerChoiceStrategies
.get(
80 this.workerChoiceStrategyType
81 ) as IWorkerChoiceStrategy
<Worker
, Data
, Response
>
86 * Sets the worker choice strategy to use in the context.
88 * @param workerChoiceStrategy - The worker choice strategy to set.
90 public setWorkerChoiceStrategy (
91 workerChoiceStrategy
: WorkerChoiceStrategy
93 if (this.workerChoiceStrategyType
=== workerChoiceStrategy
) {
94 this.workerChoiceStrategies
.get(workerChoiceStrategy
)?.reset()
96 this.workerChoiceStrategyType
= workerChoiceStrategy
101 * Executes the worker choice strategy algorithm in the context.
103 * @returns The key of the chosen one.
105 public execute (): number {
106 const workerChoiceStrategy
= this.workerChoiceStrategies
.get(
107 this.workerChoiceStrategyType
108 ) as IWorkerChoiceStrategy
<Worker
, Data
, Response
>
110 workerChoiceStrategy
.isDynamicPool
&&
111 !workerChoiceStrategy
.pool
.full
&&
112 workerChoiceStrategy
.pool
.findFreeWorkerKey() === -1
114 return this.createWorkerCallback()
116 return workerChoiceStrategy
.choose()
120 * Removes a worker from the worker choice strategy in the context.
122 * @param workerKey - The key of the worker to remove.
123 * @returns `true` if the removal is successful, `false` otherwise.
125 public remove (workerKey
: number): boolean {
127 this.workerChoiceStrategies
.get(
128 this.workerChoiceStrategyType
129 ) as IWorkerChoiceStrategy
<Worker
, Data
, Response
>