Commit | Line | Data |
---|---|---|
bdaf31cd | 1 | import type { IPoolInternal } from '../pool-internal' |
ea7a90d3 | 2 | import type { IPoolWorker } from '../pool-worker' |
bdaf31cd JB |
3 | import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' |
4 | import type { | |
5 | IWorkerChoiceStrategy, | |
6 | WorkerChoiceStrategy | |
7 | } from './selection-strategies-types' | |
8 | import { WorkerChoiceStrategies } from './selection-strategies-types' | |
78cea37e | 9 | import { getWorkerChoiceStrategy } from './selection-strategies-utils' |
bdaf31cd JB |
10 | |
11 | /** | |
bb3d5b74 | 12 | * Selects the next worker for dynamic pool. |
bdaf31cd | 13 | * |
38e795c1 JB |
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. | |
bdaf31cd JB |
17 | */ |
18 | export class DynamicPoolWorkerChoiceStrategy< | |
bf90656c JB |
19 | Worker extends IPoolWorker, |
20 | Data, | |
21 | Response | |
22 | > | |
23 | extends AbstractWorkerChoiceStrategy<Worker, Data, Response> | |
24 | implements IWorkerChoiceStrategy { | |
c923ce56 | 25 | private readonly workerChoiceStrategy: IWorkerChoiceStrategy |
bdaf31cd JB |
26 | |
27 | /** | |
bb3d5b74 | 28 | * Constructs a worker choice strategy for dynamic pool. |
bdaf31cd | 29 | * |
38e795c1 | 30 | * @param pool - The pool instance. |
c923ce56 JB |
31 | * @param createWorkerCallback - The worker creation callback for dynamic pool. |
32 | * @param workerChoiceStrategy - The worker choice strategy when the pool is busy. | |
bdaf31cd JB |
33 | */ |
34 | public constructor ( | |
35 | pool: IPoolInternal<Worker, Data, Response>, | |
c923ce56 | 36 | private readonly createWorkerCallback: () => number, |
bdaf31cd JB |
37 | workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN |
38 | ) { | |
39 | super(pool) | |
78cea37e | 40 | this.workerChoiceStrategy = getWorkerChoiceStrategy( |
bdaf31cd JB |
41 | this.pool, |
42 | workerChoiceStrategy | |
43 | ) | |
10fcfaf4 | 44 | this.requiredStatistics = this.workerChoiceStrategy.requiredStatistics |
bdaf31cd JB |
45 | } |
46 | ||
38e795c1 | 47 | /** {@inheritDoc} */ |
a6f7f1b4 JB |
48 | public reset (): boolean { |
49 | return this.workerChoiceStrategy.reset() | |
ea7a90d3 JB |
50 | } |
51 | ||
38e795c1 | 52 | /** {@inheritDoc} */ |
c923ce56 | 53 | public choose (): number { |
78cea37e | 54 | if (this.pool.busy) { |
bdaf31cd JB |
55 | return this.workerChoiceStrategy.choose() |
56 | } | |
965415bb JB |
57 | const freeWorkerKey = this.pool.findFreeWorkerKey() |
58 | if (freeWorkerKey === -1) { | |
59 | return this.createWorkerCallback() | |
60 | } | |
61 | return this.workerChoiceStrategy.choose() | |
bdaf31cd | 62 | } |
97a2abc3 JB |
63 | |
64 | /** {@inheritDoc} */ | |
65 | public remove (workerKey: number): boolean { | |
66 | return this.workerChoiceStrategy.remove(workerKey) | |
67 | } | |
bdaf31cd | 68 | } |