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