1 import type { IPoolInternal
} from
'../pool-internal'
2 import type { IPoolWorker
} from
'../pool-worker'
3 import { AbstractWorkerChoiceStrategy
} from
'./abstract-worker-choice-strategy'
7 } from
'./selection-strategies-types'
8 import { WorkerChoiceStrategies
} from
'./selection-strategies-types'
9 import { getWorkerChoiceStrategy
} from
'./selection-strategies-utils'
12 * Selects the next worker for dynamic pool.
14 * @template Worker Type of worker which manages the strategy.
15 * @template Data Type of data sent to the worker. This can only be serializable data.
16 * @template Response Type of response of execution. This can only be serializable data.
18 export class DynamicPoolWorkerChoiceStrategy
<
19 Worker
extends IPoolWorker
,
22 > extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
> {
23 private readonly workerChoiceStrategy
: IWorkerChoiceStrategy
<Worker
>
26 * Constructs a worker choice strategy for dynamic pool.
28 * @param pool The pool instance.
29 * @param createDynamicallyWorkerCallback The worker creation callback for dynamic pool.
30 * @param workerChoiceStrategy The worker choice strategy when the pull is busy.
33 pool
: IPoolInternal
<Worker
, Data
, Response
>,
34 private readonly createDynamicallyWorkerCallback
: () => Worker
,
35 workerChoiceStrategy
: WorkerChoiceStrategy
= WorkerChoiceStrategies
.ROUND_ROBIN
38 this.workerChoiceStrategy
= getWorkerChoiceStrategy(
42 this.requiredStatistics
= this.workerChoiceStrategy
.requiredStatistics
46 public reset (): boolean {
47 return this.workerChoiceStrategy
.reset()
51 public choose (): Worker
{
52 const freeWorker
= this.pool
.findFreeWorker()
53 if (freeWorker
!== false) {
58 return this.workerChoiceStrategy
.choose()
61 // All workers are busy, create a new worker
62 return this.createDynamicallyWorkerCallback()