836a1cb5052c5f2219e9e872b3a979684b0336af
[poolifier.git] / src / pools / selection-strategies / worker-choice-strategy-context.ts
1 import type { IPoolInternal } from '../pool-internal'
2 import { PoolType } from '../pool-internal'
3 import type { IPoolWorker } from '../pool-worker'
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 IPoolWorker,
21 Data,
22 Response
23 > {
24 private workerChoiceStrategy!: IWorkerChoiceStrategy<Worker>
25
26 /**
27 * Worker choice strategy context constructor.
28 *
29 * @param pool The pool instance.
30 * @param createDynamicallyWorkerCallback The worker creation callback for dynamic pool.
31 * @param workerChoiceStrategy The worker choice strategy.
32 */
33 public constructor (
34 private readonly pool: IPoolInternal<Worker, Data, Response>,
35 private createDynamicallyWorkerCallback: () => Worker,
36 workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
37 ) {
38 this.setWorkerChoiceStrategy(workerChoiceStrategy)
39 }
40
41 /**
42 * Gets the worker choice strategy instance specific to the pool type.
43 *
44 * @param workerChoiceStrategy The worker choice strategy.
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 }
57 return SelectionStrategiesUtils.getWorkerChoiceStrategy(
58 this.pool,
59 workerChoiceStrategy
60 )
61 }
62
63 /**
64 * Gets the worker choice strategy used in the context.
65 *
66 * @returns The worker choice strategy.
67 */
68 public getWorkerChoiceStrategy (): IWorkerChoiceStrategy<Worker> {
69 return this.workerChoiceStrategy
70 }
71
72 /**
73 * Sets the worker choice strategy to use in the context.
74 *
75 * @param workerChoiceStrategy The worker choice strategy to set.
76 */
77 public setWorkerChoiceStrategy (
78 workerChoiceStrategy: WorkerChoiceStrategy
79 ): void {
80 this.workerChoiceStrategy?.reset()
81 this.workerChoiceStrategy = this.getPoolWorkerChoiceStrategy(
82 workerChoiceStrategy
83 )
84 }
85
86 /**
87 * Chooses a worker with the underlying selection strategy.
88 *
89 * @returns The chosen one.
90 */
91 public execute (): Worker {
92 return this.workerChoiceStrategy.choose()
93 }
94 }