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