docs: refine README.md
[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
117 /**
51fe3d3c 118 * Executes the worker choice strategy algorithm in the context.
bdaf31cd 119 *
f06e48d8 120 * @returns The key of the worker node.
bdaf31cd 121 */
c923ce56 122 public execute (): number {
17393ac8
JB
123 return (
124 this.workerChoiceStrategies.get(
d710242d 125 this.workerChoiceStrategy
17393ac8
JB
126 ) as IWorkerChoiceStrategy
127 ).choose()
bdaf31cd 128 }
97a2abc3
JB
129
130 /**
f06e48d8 131 * Removes a worker node key from the worker choice strategy in the context.
97a2abc3 132 *
f06e48d8 133 * @param workerNodeKey - The key of the worker node.
97a2abc3
JB
134 * @returns `true` if the removal is successful, `false` otherwise.
135 */
f06e48d8 136 public remove (workerNodeKey: number): boolean {
95c83464
JB
137 return (
138 this.workerChoiceStrategies.get(
d710242d 139 this.workerChoiceStrategy
17393ac8 140 ) as IWorkerChoiceStrategy
f06e48d8 141 ).remove(workerNodeKey)
95c83464 142 }
a20f0ba5
JB
143
144 /**
145 * Sets the worker choice strategies in the context options.
146 *
147 * @param opts - The worker choice strategy options.
148 */
149 public setOptions (opts: WorkerChoiceStrategyOptions): void {
150 this.workerChoiceStrategies.forEach(workerChoiceStrategy => {
151 workerChoiceStrategy.setOptions(opts)
152 })
153 }
bdaf31cd 154}