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 * @typeParam Worker - Type of worker which manages the strategy.
15 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
16 * @typeParam Response - Type of response of execution. This can only be serializable data.
18 export class DynamicPoolWorkerChoiceStrategy
<
19 Worker
extends IPoolWorker
,
23 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
24 implements IWorkerChoiceStrategy
{
25 private readonly workerChoiceStrategy
: IWorkerChoiceStrategy
28 * Constructs a worker choice strategy for dynamic pool.
30 * @param pool - The pool instance.
31 * @param createWorkerCallback - The worker creation callback for dynamic pool.
32 * @param workerChoiceStrategy - The worker choice strategy when the pool is busy.
35 pool
: IPoolInternal
<Worker
, Data
, Response
>,
36 private readonly createWorkerCallback
: () => number,
37 workerChoiceStrategy
: WorkerChoiceStrategy
= WorkerChoiceStrategies
.ROUND_ROBIN
40 this.workerChoiceStrategy
= getWorkerChoiceStrategy(
44 this.requiredStatistics
= this.workerChoiceStrategy
.requiredStatistics
48 public reset (): boolean {
49 return this.workerChoiceStrategy
.reset()
53 public choose (): number {
55 return this.workerChoiceStrategy
.choose()
57 if (!this.pool
.full
&& this.pool
.findFreeWorkerKey() === -1) {
58 return this.createWorkerCallback()
60 return this.workerChoiceStrategy
.choose()
64 public remove (workerKey
: number): boolean {
65 return this.workerChoiceStrategy
.remove(workerKey
)