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, | |
7 | WorkerChoiceStrategy | |
8 | } from './selection-strategies-types' | |
9 | import { WorkerChoiceStrategies } from './selection-strategies-types' | |
78cea37e | 10 | import { getWorkerChoiceStrategy } from './selection-strategies-utils' |
bdaf31cd JB |
11 | |
12 | /** | |
13 | * The worker choice strategy context. | |
14 | * | |
38e795c1 JB |
15 | * @typeParam Worker - Type of worker. |
16 | * @typeParam Data - Type of data sent to the worker. This can only be serializable data. | |
17 | * @typeParam Response - Type of response of execution. This can only be serializable data. | |
bdaf31cd JB |
18 | */ |
19 | export class WorkerChoiceStrategyContext< | |
ea7a90d3 | 20 | Worker extends IPoolWorker, |
bdaf31cd JB |
21 | Data, |
22 | Response | |
23 | > { | |
bdaf31cd JB |
24 | private workerChoiceStrategy!: IWorkerChoiceStrategy<Worker> |
25 | ||
26 | /** | |
27 | * Worker choice strategy context constructor. | |
28 | * | |
38e795c1 JB |
29 | * @param pool - The pool instance. |
30 | * @param createDynamicallyWorkerCallback - The worker creation callback for dynamic pool. | |
31 | * @param workerChoiceStrategy - The worker choice strategy. | |
bdaf31cd JB |
32 | */ |
33 | public constructor ( | |
34 | private readonly pool: IPoolInternal<Worker, Data, Response>, | |
78cea37e | 35 | private readonly createDynamicallyWorkerCallback: () => Worker, |
bdaf31cd JB |
36 | workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN |
37 | ) { | |
38 | this.setWorkerChoiceStrategy(workerChoiceStrategy) | |
39 | } | |
40 | ||
41 | /** | |
bdede008 | 42 | * Gets the worker choice strategy instance specific to the pool type. |
bdaf31cd | 43 | * |
38e795c1 | 44 | * @param workerChoiceStrategy - The worker choice strategy. |
bdaf31cd JB |
45 | * @returns The worker choice strategy instance for the pool type. |
46 | */ | |
47 | private getPoolWorkerChoiceStrategy ( | |
48 | workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN | |
49 | ): IWorkerChoiceStrategy<Worker> { | |
50 | if (this.pool.type === PoolType.DYNAMIC) { | |
51 | return new DynamicPoolWorkerChoiceStrategy( | |
52 | this.pool, | |
53 | this.createDynamicallyWorkerCallback, | |
54 | workerChoiceStrategy | |
55 | ) | |
56 | } | |
78cea37e | 57 | return getWorkerChoiceStrategy(this.pool, workerChoiceStrategy) |
bdaf31cd JB |
58 | } |
59 | ||
10fcfaf4 | 60 | /** |
bdede008 | 61 | * Gets the worker choice strategy used in the context. |
10fcfaf4 JB |
62 | * |
63 | * @returns The worker choice strategy. | |
64 | */ | |
65 | public getWorkerChoiceStrategy (): IWorkerChoiceStrategy<Worker> { | |
66 | return this.workerChoiceStrategy | |
67 | } | |
68 | ||
bdaf31cd | 69 | /** |
bdede008 | 70 | * Sets the worker choice strategy to use in the context. |
bdaf31cd | 71 | * |
38e795c1 | 72 | * @param workerChoiceStrategy - The worker choice strategy to set. |
bdaf31cd JB |
73 | */ |
74 | public setWorkerChoiceStrategy ( | |
75 | workerChoiceStrategy: WorkerChoiceStrategy | |
76 | ): void { | |
a6f7f1b4 | 77 | this.workerChoiceStrategy?.reset() |
78cea37e JB |
78 | this.workerChoiceStrategy = |
79 | this.getPoolWorkerChoiceStrategy(workerChoiceStrategy) | |
bdaf31cd JB |
80 | } |
81 | ||
82 | /** | |
bdede008 | 83 | * Chooses a worker with the underlying selection strategy. |
bdaf31cd JB |
84 | * |
85 | * @returns The chosen one. | |
86 | */ | |
87 | public execute (): Worker { | |
88 | return this.workerChoiceStrategy.choose() | |
89 | } | |
90 | } |