Commit | Line | Data |
---|---|---|
bdaf31cd JB |
1 | import type { IPoolInternal } from '../pool-internal' |
2 | import { PoolType } from '../pool-internal' | |
ea7a90d3 | 3 | import type { IPoolWorker } from '../pool-worker' |
bdaf31cd JB |
4 | import { DynamicPoolWorkerChoiceStrategy } from './dynamic-pool-worker-choice-strategy' |
5 | import type { | |
6 | IWorkerChoiceStrategy, | |
97a2abc3 | 7 | RequiredStatistics, |
bdaf31cd JB |
8 | WorkerChoiceStrategy |
9 | } from './selection-strategies-types' | |
10 | import { WorkerChoiceStrategies } from './selection-strategies-types' | |
78cea37e | 11 | import { getWorkerChoiceStrategy } from './selection-strategies-utils' |
bdaf31cd JB |
12 | |
13 | /** | |
14 | * The worker choice strategy context. | |
15 | * | |
38e795c1 JB |
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. | |
bdaf31cd JB |
19 | */ |
20 | export class WorkerChoiceStrategyContext< | |
ea7a90d3 | 21 | Worker extends IPoolWorker, |
bdaf31cd JB |
22 | Data, |
23 | Response | |
24 | > { | |
c923ce56 | 25 | private workerChoiceStrategy!: IWorkerChoiceStrategy |
bdaf31cd JB |
26 | |
27 | /** | |
28 | * Worker choice strategy context constructor. | |
29 | * | |
38e795c1 | 30 | * @param pool - The pool instance. |
c923ce56 | 31 | * @param createWorkerCallback - The worker creation callback for dynamic pool. |
38e795c1 | 32 | * @param workerChoiceStrategy - The worker choice strategy. |
bdaf31cd JB |
33 | */ |
34 | public constructor ( | |
35 | private readonly pool: IPoolInternal<Worker, Data, Response>, | |
c923ce56 | 36 | private readonly createWorkerCallback: () => number, |
bdaf31cd JB |
37 | workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN |
38 | ) { | |
39 | this.setWorkerChoiceStrategy(workerChoiceStrategy) | |
40 | } | |
41 | ||
42 | /** | |
bdede008 | 43 | * Gets the worker choice strategy instance specific to the pool type. |
bdaf31cd | 44 | * |
38e795c1 | 45 | * @param workerChoiceStrategy - The worker choice strategy. |
bdaf31cd JB |
46 | * @returns The worker choice strategy instance for the pool type. |
47 | */ | |
48 | private getPoolWorkerChoiceStrategy ( | |
49 | workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN | |
c923ce56 | 50 | ): IWorkerChoiceStrategy { |
bdaf31cd JB |
51 | if (this.pool.type === PoolType.DYNAMIC) { |
52 | return new DynamicPoolWorkerChoiceStrategy( | |
53 | this.pool, | |
c923ce56 | 54 | this.createWorkerCallback, |
bdaf31cd JB |
55 | workerChoiceStrategy |
56 | ) | |
57 | } | |
78cea37e | 58 | return getWorkerChoiceStrategy(this.pool, workerChoiceStrategy) |
bdaf31cd JB |
59 | } |
60 | ||
10fcfaf4 | 61 | /** |
bdede008 | 62 | * Gets the worker choice strategy used in the context. |
10fcfaf4 JB |
63 | * |
64 | * @returns The worker choice strategy. | |
97a2abc3 | 65 | * @deprecated Scheduled removal. |
10fcfaf4 | 66 | */ |
c923ce56 | 67 | public getWorkerChoiceStrategy (): IWorkerChoiceStrategy { |
10fcfaf4 JB |
68 | return this.workerChoiceStrategy |
69 | } | |
70 | ||
97a2abc3 JB |
71 | /** |
72 | * Gets the worker choice strategy required statistics. | |
73 | * | |
74 | * @returns The required statistics. | |
75 | */ | |
76 | public getRequiredStatistics (): RequiredStatistics { | |
77 | return this.workerChoiceStrategy.requiredStatistics | |
78 | } | |
79 | ||
bdaf31cd | 80 | /** |
bdede008 | 81 | * Sets the worker choice strategy to use in the context. |
bdaf31cd | 82 | * |
38e795c1 | 83 | * @param workerChoiceStrategy - The worker choice strategy to set. |
bdaf31cd JB |
84 | */ |
85 | public setWorkerChoiceStrategy ( | |
86 | workerChoiceStrategy: WorkerChoiceStrategy | |
87 | ): void { | |
a6f7f1b4 | 88 | this.workerChoiceStrategy?.reset() |
78cea37e JB |
89 | this.workerChoiceStrategy = |
90 | this.getPoolWorkerChoiceStrategy(workerChoiceStrategy) | |
bdaf31cd JB |
91 | } |
92 | ||
93 | /** | |
bdede008 | 94 | * Chooses a worker with the underlying selection strategy. |
bdaf31cd | 95 | * |
c923ce56 | 96 | * @returns The key of the chosen one. |
bdaf31cd | 97 | */ |
c923ce56 | 98 | public execute (): number { |
bdaf31cd JB |
99 | return this.workerChoiceStrategy.choose() |
100 | } | |
97a2abc3 JB |
101 | |
102 | /** | |
103 | * Removes a worker in the underlying selection strategy internals. | |
104 | * | |
105 | * @param workerKey - The key of the worker to remove. | |
106 | * @returns `true` if the removal is successful, `false` otherwise. | |
107 | */ | |
108 | public remove (workerKey: number): boolean { | |
109 | return this.workerChoiceStrategy.remove(workerKey) | |
110 | } | |
bdaf31cd | 111 | } |