feat: add pool runtime setters
[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 ) {
1086026a 46 this.execute.bind(this)
b529c323
JB
47 this.workerChoiceStrategies = new Map<
48 WorkerChoiceStrategy,
17393ac8 49 IWorkerChoiceStrategy
b529c323
JB
50 >([
51 [
52 WorkerChoiceStrategies.ROUND_ROBIN,
da309861 53 new RoundRobinWorkerChoiceStrategy<Worker, Data, Response>(pool, opts)
b529c323
JB
54 ],
55 [
56 WorkerChoiceStrategies.LESS_USED,
da309861 57 new LessUsedWorkerChoiceStrategy<Worker, Data, Response>(pool, opts)
b529c323
JB
58 ],
59 [
60 WorkerChoiceStrategies.LESS_BUSY,
da309861 61 new LessBusyWorkerChoiceStrategy<Worker, Data, Response>(pool, opts)
b529c323
JB
62 ],
63 [
64 WorkerChoiceStrategies.FAIR_SHARE,
da309861 65 new FairShareWorkerChoiceStrategy<Worker, Data, Response>(pool, opts)
b529c323
JB
66 ],
67 [
68 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN,
da309861
JB
69 new WeightedRoundRobinWorkerChoiceStrategy<Worker, Data, Response>(
70 pool,
71 opts
72 )
b529c323
JB
73 ]
74 ])
bdaf31cd
JB
75 }
76
97a2abc3 77 /**
51fe3d3c 78 * Gets the worker choice strategy in the context required statistics.
97a2abc3
JB
79 *
80 * @returns The required statistics.
81 */
82 public getRequiredStatistics (): RequiredStatistics {
95c83464
JB
83 return (
84 this.workerChoiceStrategies.get(
d710242d 85 this.workerChoiceStrategy
17393ac8 86 ) as IWorkerChoiceStrategy
95c83464 87 ).requiredStatistics
97a2abc3
JB
88 }
89
bdaf31cd 90 /**
bdede008 91 * Sets the worker choice strategy to use in the context.
bdaf31cd 92 *
38e795c1 93 * @param workerChoiceStrategy - The worker choice strategy to set.
bdaf31cd
JB
94 */
95 public setWorkerChoiceStrategy (
96 workerChoiceStrategy: WorkerChoiceStrategy
97 ): void {
d710242d
JB
98 if (this.workerChoiceStrategy !== workerChoiceStrategy) {
99 this.workerChoiceStrategy = workerChoiceStrategy
b2b1d84e 100 }
d710242d 101 this.workerChoiceStrategies.get(this.workerChoiceStrategy)?.reset()
bdaf31cd
JB
102 }
103
104 /**
51fe3d3c 105 * Executes the worker choice strategy algorithm in the context.
bdaf31cd 106 *
f06e48d8 107 * @returns The key of the worker node.
bdaf31cd 108 */
c923ce56 109 public execute (): number {
17393ac8
JB
110 return (
111 this.workerChoiceStrategies.get(
d710242d 112 this.workerChoiceStrategy
17393ac8
JB
113 ) as IWorkerChoiceStrategy
114 ).choose()
bdaf31cd 115 }
97a2abc3
JB
116
117 /**
f06e48d8 118 * Removes a worker node key from the worker choice strategy in the context.
97a2abc3 119 *
f06e48d8 120 * @param workerNodeKey - The key of the worker node.
97a2abc3
JB
121 * @returns `true` if the removal is successful, `false` otherwise.
122 */
f06e48d8 123 public remove (workerNodeKey: number): boolean {
95c83464
JB
124 return (
125 this.workerChoiceStrategies.get(
d710242d 126 this.workerChoiceStrategy
17393ac8 127 ) as IWorkerChoiceStrategy
f06e48d8 128 ).remove(workerNodeKey)
95c83464 129 }
a20f0ba5
JB
130
131 /**
132 * Sets the worker choice strategies in the context options.
133 *
134 * @param opts - The worker choice strategy options.
135 */
136 public setOptions (opts: WorkerChoiceStrategyOptions): void {
137 this.workerChoiceStrategies.forEach(workerChoiceStrategy => {
138 workerChoiceStrategy.setOptions(opts)
139 })
140 }
bdaf31cd 141}