1 import type { IPoolInternal
} from
'../pool-internal'
2 import { PoolType
} from
'../pool-internal'
3 import type { IPoolWorker
} from
'../pool-worker'
4 import { DynamicPoolWorkerChoiceStrategy
} from
'./dynamic-pool-worker-choice-strategy'
9 } from
'./selection-strategies-types'
10 import { WorkerChoiceStrategies
} from
'./selection-strategies-types'
11 import { getWorkerChoiceStrategy
} from
'./selection-strategies-utils'
14 * The worker choice strategy context.
16 * @typeParam Worker - Type of worker.
17 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
18 * @typeParam Response - Type of response of execution. This can only be serializable data.
20 export class WorkerChoiceStrategyContext
<
21 Worker
extends IPoolWorker
,
25 private workerChoiceStrategy
!: IWorkerChoiceStrategy
28 * Worker choice strategy context constructor.
30 * @param pool - The pool instance.
31 * @param createWorkerCallback - The worker creation callback for dynamic pool.
32 * @param workerChoiceStrategy - The worker choice strategy.
35 private readonly pool
: IPoolInternal
<Worker
, Data
, Response
>,
36 private readonly createWorkerCallback
: () => number,
37 workerChoiceStrategy
: WorkerChoiceStrategy
= WorkerChoiceStrategies
.ROUND_ROBIN
39 this.setWorkerChoiceStrategy(workerChoiceStrategy
)
43 * Gets the worker choice strategy instance specific to the pool type.
45 * @param workerChoiceStrategy - The worker choice strategy.
46 * @returns The worker choice strategy instance for the pool type.
48 private getPoolWorkerChoiceStrategy (
49 workerChoiceStrategy
: WorkerChoiceStrategy
= WorkerChoiceStrategies
.ROUND_ROBIN
50 ): IWorkerChoiceStrategy
{
51 if (this.pool
.type === PoolType
.DYNAMIC
) {
52 return new DynamicPoolWorkerChoiceStrategy(
54 this.createWorkerCallback
,
58 return getWorkerChoiceStrategy(this.pool
, workerChoiceStrategy
)
62 * Gets the worker choice strategy used in the context.
64 * @returns The worker choice strategy.
65 * @deprecated Scheduled removal.
67 public getWorkerChoiceStrategy (): IWorkerChoiceStrategy
{
68 return this.workerChoiceStrategy
72 * Gets the worker choice strategy required statistics.
74 * @returns The required statistics.
76 public getRequiredStatistics (): RequiredStatistics
{
77 return this.workerChoiceStrategy
.requiredStatistics
81 * Sets the worker choice strategy to use in the context.
83 * @param workerChoiceStrategy - The worker choice strategy to set.
85 public setWorkerChoiceStrategy (
86 workerChoiceStrategy
: WorkerChoiceStrategy
88 this.workerChoiceStrategy
?.reset()
89 this.workerChoiceStrategy
=
90 this.getPoolWorkerChoiceStrategy(workerChoiceStrategy
)
94 * Chooses a worker with the underlying selection strategy.
96 * @returns The key of the chosen one.
98 public execute (): number {
99 return this.workerChoiceStrategy
.choose()
103 * Removes a worker in the underlying selection strategy internals.
105 * @param workerKey - The key of the worker to remove.
106 * @returns `true` if the removal is successful, `false` otherwise.
108 public remove (workerKey
: number): boolean {
109 return this.workerChoiceStrategy
.remove(workerKey
)