docs: refine code comments
[poolifier.git] / src / pools / selection-strategies / worker-choice-strategy-context.ts
1 import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
2 import type { IPool } from '../pool'
3 import type { IWorker } from '../worker'
4 import { FairShareWorkerChoiceStrategy } from './fair-share-worker-choice-strategy'
5 import { LessBusyWorkerChoiceStrategy } from './less-busy-worker-choice-strategy'
6 import { LessUsedWorkerChoiceStrategy } from './less-used-worker-choice-strategy'
7 import { RoundRobinWorkerChoiceStrategy } from './round-robin-worker-choice-strategy'
8 import type {
9 IWorkerChoiceStrategy,
10 RequiredStatistics,
11 WorkerChoiceStrategy,
12 WorkerChoiceStrategyOptions
13 } from './selection-strategies-types'
14 import { WorkerChoiceStrategies } from './selection-strategies-types'
15 import { WeightedRoundRobinWorkerChoiceStrategy } from './weighted-round-robin-worker-choice-strategy'
16
17 /**
18 * The worker choice strategy context.
19 *
20 * @typeParam Worker - Type of worker.
21 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
22 * @typeParam Response - Type of execution response. This can only be serializable data.
23 */
24 export class WorkerChoiceStrategyContext<
25 Worker extends IWorker,
26 Data = unknown,
27 Response = unknown
28 > {
29 private readonly workerChoiceStrategies: Map<
30 WorkerChoiceStrategy,
31 IWorkerChoiceStrategy
32 >
33
34 /**
35 * Worker choice strategy context constructor.
36 *
37 * @param pool - The pool instance.
38 * @param workerChoiceStrategy - The worker choice strategy.
39 * @param opts - The worker choice strategy options.
40 */
41 public constructor (
42 pool: IPool<Worker, Data, Response>,
43 private workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN,
44 opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
45 ) {
46 this.execute = this.execute.bind(this)
47 this.workerChoiceStrategies = new Map<
48 WorkerChoiceStrategy,
49 IWorkerChoiceStrategy
50 >([
51 [
52 WorkerChoiceStrategies.ROUND_ROBIN,
53 new (RoundRobinWorkerChoiceStrategy.bind(this))<Worker, Data, Response>(
54 pool,
55 opts
56 )
57 ],
58 [
59 WorkerChoiceStrategies.LESS_USED,
60 new (LessUsedWorkerChoiceStrategy.bind(this))<Worker, Data, Response>(
61 pool,
62 opts
63 )
64 ],
65 [
66 WorkerChoiceStrategies.LESS_BUSY,
67 new (LessBusyWorkerChoiceStrategy.bind(this))<Worker, Data, Response>(
68 pool,
69 opts
70 )
71 ],
72 [
73 WorkerChoiceStrategies.FAIR_SHARE,
74 new (FairShareWorkerChoiceStrategy.bind(this))<Worker, Data, Response>(
75 pool,
76 opts
77 )
78 ],
79 [
80 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN,
81 new (WeightedRoundRobinWorkerChoiceStrategy.bind(this))<
82 Worker,
83 Data,
84 Response
85 >(pool, opts)
86 ]
87 ])
88 }
89
90 /**
91 * Gets the worker choice strategy in the context required statistics.
92 *
93 * @returns The required statistics.
94 */
95 public getRequiredStatistics (): RequiredStatistics {
96 return (
97 this.workerChoiceStrategies.get(
98 this.workerChoiceStrategy
99 ) as IWorkerChoiceStrategy
100 ).requiredStatistics
101 }
102
103 /**
104 * Sets the worker choice strategy to use in the context.
105 *
106 * @param workerChoiceStrategy - The worker choice strategy to set.
107 */
108 public setWorkerChoiceStrategy (
109 workerChoiceStrategy: WorkerChoiceStrategy
110 ): void {
111 if (this.workerChoiceStrategy !== workerChoiceStrategy) {
112 this.workerChoiceStrategy = workerChoiceStrategy
113 }
114 this.workerChoiceStrategies.get(this.workerChoiceStrategy)?.reset()
115 }
116
117 /**
118 * Updates the worker node key in the worker choice strategy internals in the context.
119 *
120 * @returns `true` if the update is successful, `false` otherwise.
121 */
122 public update (workerNodeKey: number): boolean {
123 return (
124 this.workerChoiceStrategies.get(
125 this.workerChoiceStrategy
126 ) as IWorkerChoiceStrategy
127 ).update(workerNodeKey)
128 }
129
130 /**
131 * Executes the worker choice strategy algorithm in the context.
132 *
133 * @returns The key of the worker node.
134 */
135 public execute (): number {
136 return (
137 this.workerChoiceStrategies.get(
138 this.workerChoiceStrategy
139 ) as IWorkerChoiceStrategy
140 ).choose()
141 }
142
143 /**
144 * Removes the worker node key from the worker choice strategy in the context.
145 *
146 * @param workerNodeKey - The key of the worker node.
147 * @returns `true` if the removal is successful, `false` otherwise.
148 */
149 public remove (workerNodeKey: number): boolean {
150 return (
151 this.workerChoiceStrategies.get(
152 this.workerChoiceStrategy
153 ) as IWorkerChoiceStrategy
154 ).remove(workerNodeKey)
155 }
156
157 /**
158 * Sets the worker choice strategies in the context options.
159 *
160 * @param opts - The worker choice strategy options.
161 */
162 public setOptions (opts: WorkerChoiceStrategyOptions): void {
163 this.workerChoiceStrategies.forEach(workerChoiceStrategy => {
164 workerChoiceStrategy.setOptions(opts)
165 })
166 }
167 }