Commit | Line | Data |
---|---|---|
bdaf31cd | 1 | import type { IPoolInternal } from '../pool-internal' |
ea7a90d3 | 2 | import type { IPoolWorker } from '../pool-worker' |
bdaf31cd JB |
3 | import type { |
4 | IWorkerChoiceStrategy, | |
97a2abc3 | 5 | RequiredStatistics, |
bdaf31cd JB |
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 | /** | |
12 | * The worker choice strategy context. | |
13 | * | |
38e795c1 JB |
14 | * @typeParam Worker - Type of worker. |
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 WorkerChoiceStrategyContext< | |
ea7a90d3 | 19 | Worker extends IPoolWorker, |
bdaf31cd JB |
20 | Data, |
21 | Response | |
22 | > { | |
c923ce56 | 23 | private workerChoiceStrategy!: IWorkerChoiceStrategy |
bdaf31cd JB |
24 | |
25 | /** | |
26 | * Worker choice strategy context constructor. | |
27 | * | |
38e795c1 | 28 | * @param pool - The pool instance. |
c923ce56 | 29 | * @param createWorkerCallback - The worker creation callback for dynamic pool. |
38e795c1 | 30 | * @param workerChoiceStrategy - The worker choice strategy. |
bdaf31cd JB |
31 | */ |
32 | public constructor ( | |
33 | private readonly pool: IPoolInternal<Worker, Data, Response>, | |
c923ce56 | 34 | private readonly createWorkerCallback: () => number, |
bdaf31cd JB |
35 | workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN |
36 | ) { | |
1086026a | 37 | this.execute.bind(this) |
bdaf31cd JB |
38 | this.setWorkerChoiceStrategy(workerChoiceStrategy) |
39 | } | |
40 | ||
97a2abc3 JB |
41 | /** |
42 | * Gets the worker choice strategy required statistics. | |
43 | * | |
44 | * @returns The required statistics. | |
45 | */ | |
46 | public getRequiredStatistics (): RequiredStatistics { | |
47 | return this.workerChoiceStrategy.requiredStatistics | |
48 | } | |
49 | ||
bdaf31cd | 50 | /** |
bdede008 | 51 | * Sets the worker choice strategy to use in the context. |
bdaf31cd | 52 | * |
38e795c1 | 53 | * @param workerChoiceStrategy - The worker choice strategy to set. |
bdaf31cd JB |
54 | */ |
55 | public setWorkerChoiceStrategy ( | |
56 | workerChoiceStrategy: WorkerChoiceStrategy | |
57 | ): void { | |
a6f7f1b4 | 58 | this.workerChoiceStrategy?.reset() |
d59df138 | 59 | this.workerChoiceStrategy = getWorkerChoiceStrategy<Worker, Data, Response>( |
9cd39dd4 JB |
60 | this.pool, |
61 | workerChoiceStrategy | |
62 | ) | |
bdaf31cd JB |
63 | } |
64 | ||
65 | /** | |
9cd39dd4 | 66 | * Chooses a worker with the worker choice strategy. |
bdaf31cd | 67 | * |
c923ce56 | 68 | * @returns The key of the chosen one. |
bdaf31cd | 69 | */ |
c923ce56 | 70 | public execute (): number { |
9cd39dd4 | 71 | if ( |
ccd49f03 | 72 | this.workerChoiceStrategy.isDynamicPool && |
9cd39dd4 JB |
73 | !this.pool.full && |
74 | this.pool.findFreeWorkerKey() === -1 | |
75 | ) { | |
76 | return this.createWorkerCallback() | |
77 | } | |
bdaf31cd JB |
78 | return this.workerChoiceStrategy.choose() |
79 | } | |
97a2abc3 JB |
80 | |
81 | /** | |
9cd39dd4 | 82 | * Removes a worker in the worker choice strategy internals. |
97a2abc3 JB |
83 | * |
84 | * @param workerKey - The key of the worker to remove. | |
85 | * @returns `true` if the removal is successful, `false` otherwise. | |
86 | */ | |
87 | public remove (workerKey: number): boolean { | |
88 | return this.workerChoiceStrategy.remove(workerKey) | |
89 | } | |
bdaf31cd | 90 | } |