docs: refine code comments
[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
JB
4import { FairShareWorkerChoiceStrategy } from './fair-share-worker-choice-strategy'
5import { LessBusyWorkerChoiceStrategy } from './less-busy-worker-choice-strategy'
6import { LessUsedWorkerChoiceStrategy } from './less-used-worker-choice-strategy'
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 [
59 WorkerChoiceStrategies.LESS_USED,
7254e419
JB
60 new (LessUsedWorkerChoiceStrategy.bind(this))<Worker, Data, Response>(
61 pool,
62 opts
63 )
b529c323
JB
64 ],
65 [
66 WorkerChoiceStrategies.LESS_BUSY,
7254e419
JB
67 new (LessBusyWorkerChoiceStrategy.bind(this))<Worker, Data, Response>(
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.
bdaf31cd 134 */
c923ce56 135 public execute (): number {
17393ac8
JB
136 return (
137 this.workerChoiceStrategies.get(
d710242d 138 this.workerChoiceStrategy
17393ac8
JB
139 ) as IWorkerChoiceStrategy
140 ).choose()
bdaf31cd 141 }
97a2abc3
JB
142
143 /**
c7e196ba 144 * Removes the worker node key from the worker choice strategy in the context.
97a2abc3 145 *
f06e48d8 146 * @param workerNodeKey - The key of the worker node.
97a2abc3
JB
147 * @returns `true` if the removal is successful, `false` otherwise.
148 */
f06e48d8 149 public remove (workerNodeKey: number): boolean {
95c83464
JB
150 return (
151 this.workerChoiceStrategies.get(
d710242d 152 this.workerChoiceStrategy
17393ac8 153 ) as IWorkerChoiceStrategy
f06e48d8 154 ).remove(workerNodeKey)
95c83464 155 }
a20f0ba5
JB
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 }
bdaf31cd 167}