refactor: remove unused method argument
[poolifier.git] / src / pools / selection-strategies / worker-choice-strategy-context.ts
1 import type { IPoolInternal } from '../pool-internal'
2 import type { IPoolWorker } from '../pool-worker'
3 import { FairShareWorkerChoiceStrategy } from './fair-share-worker-choice-strategy'
4 import { LessBusyWorkerChoiceStrategy } from './less-busy-worker-choice-strategy'
5 import { LessUsedWorkerChoiceStrategy } from './less-used-worker-choice-strategy'
6 import { RoundRobinWorkerChoiceStrategy } from './round-robin-worker-choice-strategy'
7 import type {
8 IWorkerChoiceStrategy,
9 RequiredStatistics,
10 WorkerChoiceStrategy
11 } from './selection-strategies-types'
12 import { WorkerChoiceStrategies } from './selection-strategies-types'
13 import { WeightedRoundRobinWorkerChoiceStrategy } from './weighted-round-robin-worker-choice-strategy'
14
15 /**
16 * The worker choice strategy context.
17 *
18 * @typeParam Worker - Type of worker.
19 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
20 * @typeParam Response - Type of response of execution. This can only be serializable data.
21 */
22 export class WorkerChoiceStrategyContext<
23 Worker extends IPoolWorker,
24 Data = unknown,
25 Response = unknown
26 > {
27 private readonly workerChoiceStrategies: Map<
28 WorkerChoiceStrategy,
29 IWorkerChoiceStrategy<Worker, Data, Response>
30 >
31
32 /**
33 * Worker choice strategy context constructor.
34 *
35 * @param pool - The pool instance.
36 * @param createWorkerCallback - The worker creation callback for dynamic pool.
37 * @param workerChoiceStrategy - The worker choice strategy.
38 */
39 public constructor (
40 pool: IPoolInternal<Worker, Data, Response>,
41 private readonly createWorkerCallback: () => number,
42 private workerChoiceStrategyType: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
43 ) {
44 this.execute.bind(this)
45 this.workerChoiceStrategies = new Map<
46 WorkerChoiceStrategy,
47 IWorkerChoiceStrategy<Worker, Data, Response>
48 >([
49 [
50 WorkerChoiceStrategies.ROUND_ROBIN,
51 new RoundRobinWorkerChoiceStrategy<Worker, Data, Response>(pool)
52 ],
53 [
54 WorkerChoiceStrategies.LESS_USED,
55 new LessUsedWorkerChoiceStrategy<Worker, Data, Response>(pool)
56 ],
57 [
58 WorkerChoiceStrategies.LESS_BUSY,
59 new LessBusyWorkerChoiceStrategy<Worker, Data, Response>(pool)
60 ],
61 [
62 WorkerChoiceStrategies.FAIR_SHARE,
63 new FairShareWorkerChoiceStrategy<Worker, Data, Response>(pool)
64 ],
65 [
66 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN,
67 new WeightedRoundRobinWorkerChoiceStrategy<Worker, Data, Response>(pool)
68 ]
69 ])
70 }
71
72 /**
73 * Gets the worker choice strategy in the context required statistics.
74 *
75 * @returns The required statistics.
76 */
77 public getRequiredStatistics (): RequiredStatistics {
78 return (
79 this.workerChoiceStrategies.get(
80 this.workerChoiceStrategyType
81 ) as IWorkerChoiceStrategy<Worker, Data, Response>
82 ).requiredStatistics
83 }
84
85 /**
86 * Sets the worker choice strategy to use in the context.
87 *
88 * @param workerChoiceStrategy - The worker choice strategy to set.
89 */
90 public setWorkerChoiceStrategy (
91 workerChoiceStrategy: WorkerChoiceStrategy
92 ): void {
93 if (this.workerChoiceStrategyType === workerChoiceStrategy) {
94 this.workerChoiceStrategies.get(workerChoiceStrategy)?.reset()
95 } else {
96 this.workerChoiceStrategyType = workerChoiceStrategy
97 }
98 }
99
100 /**
101 * Executes the worker choice strategy algorithm in the context.
102 *
103 * @returns The key of the chosen one.
104 */
105 public execute (): number {
106 const workerChoiceStrategy = this.workerChoiceStrategies.get(
107 this.workerChoiceStrategyType
108 ) as IWorkerChoiceStrategy<Worker, Data, Response>
109 if (
110 workerChoiceStrategy.isDynamicPool &&
111 !workerChoiceStrategy.pool.full &&
112 workerChoiceStrategy.pool.findFreeWorkerKey() === -1
113 ) {
114 return this.createWorkerCallback()
115 }
116 return workerChoiceStrategy.choose()
117 }
118
119 /**
120 * Removes a worker from the worker choice strategy in the context.
121 *
122 * @param workerKey - The key of the worker to remove.
123 * @returns `true` if the removal is successful, `false` otherwise.
124 */
125 public remove (workerKey: number): boolean {
126 return (
127 this.workerChoiceStrategies.get(
128 this.workerChoiceStrategyType
129 ) as IWorkerChoiceStrategy<Worker, Data, Response>
130 ).remove(workerKey)
131 }
132 }