chore: generate documentation
[poolifier.git] / src / pools / selection-strategies / worker-choice-strategy-context.ts
CommitLineData
bdaf31cd 1import type { IPoolInternal } from '../pool-internal'
ea7a90d3 2import type { IPoolWorker } from '../pool-worker'
51fe3d3c
JB
3import { FairShareWorkerChoiceStrategy } from './fair-share-worker-choice-strategy'
4import { LessBusyWorkerChoiceStrategy } from './less-busy-worker-choice-strategy'
5import { LessUsedWorkerChoiceStrategy } from './less-used-worker-choice-strategy'
6import { RoundRobinWorkerChoiceStrategy } from './round-robin-worker-choice-strategy'
bdaf31cd
JB
7import type {
8 IWorkerChoiceStrategy,
97a2abc3 9 RequiredStatistics,
bdaf31cd
JB
10 WorkerChoiceStrategy
11} from './selection-strategies-types'
12import { WorkerChoiceStrategies } from './selection-strategies-types'
51fe3d3c 13import { WeightedRoundRobinWorkerChoiceStrategy } from './weighted-round-robin-worker-choice-strategy'
bdaf31cd
JB
14
15/**
16 * The worker choice strategy context.
17 *
38e795c1
JB
18 * @typeParam Worker - Type of worker.
19 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
20 * @typeParam Response - Type of response of execution. This can only be serializable data.
bdaf31cd
JB
21 */
22export class WorkerChoiceStrategyContext<
ea7a90d3 23 Worker extends IPoolWorker,
b2b1d84e
JB
24 Data = unknown,
25 Response = unknown
bdaf31cd 26> {
b529c323 27 private readonly workerChoiceStrategies: Map<
95c83464
JB
28 WorkerChoiceStrategy,
29 IWorkerChoiceStrategy<Worker, Data, Response>
b529c323 30 >
bdaf31cd
JB
31
32 /**
33 * Worker choice strategy context constructor.
34 *
38e795c1 35 * @param pool - The pool instance.
c923ce56 36 * @param createWorkerCallback - The worker creation callback for dynamic pool.
38e795c1 37 * @param workerChoiceStrategy - The worker choice strategy.
bdaf31cd
JB
38 */
39 public constructor (
3300e7bc 40 pool: IPoolInternal<Worker, Data, Response>,
c923ce56 41 private readonly createWorkerCallback: () => number,
b2b1d84e 42 private workerChoiceStrategyType: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
bdaf31cd 43 ) {
1086026a 44 this.execute.bind(this)
b529c323
JB
45 this.workerChoiceStrategies = new Map<
46 WorkerChoiceStrategy,
47 IWorkerChoiceStrategy<Worker, Data, Response>
48 >([
49 [
50 WorkerChoiceStrategies.ROUND_ROBIN,
51 new RoundRobinWorkerChoiceStrategy<Worker, Data, Response>(pool)
52 ],
53 [
54 WorkerChoiceStrategies.LESS_USED,
55 new LessUsedWorkerChoiceStrategy<Worker, Data, Response>(pool)
56 ],
57 [
58 WorkerChoiceStrategies.LESS_BUSY,
59 new LessBusyWorkerChoiceStrategy<Worker, Data, Response>(pool)
60 ],
61 [
62 WorkerChoiceStrategies.FAIR_SHARE,
63 new FairShareWorkerChoiceStrategy<Worker, Data, Response>(pool)
64 ],
65 [
66 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN,
67 new WeightedRoundRobinWorkerChoiceStrategy<Worker, Data, Response>(pool)
68 ]
69 ])
bdaf31cd
JB
70 }
71
97a2abc3 72 /**
51fe3d3c 73 * Gets the worker choice strategy in the context required statistics.
97a2abc3
JB
74 *
75 * @returns The required statistics.
76 */
77 public getRequiredStatistics (): RequiredStatistics {
95c83464
JB
78 return (
79 this.workerChoiceStrategies.get(
80 this.workerChoiceStrategyType
81 ) as IWorkerChoiceStrategy<Worker, Data, Response>
82 ).requiredStatistics
97a2abc3
JB
83 }
84
bdaf31cd 85 /**
bdede008 86 * Sets the worker choice strategy to use in the context.
bdaf31cd 87 *
38e795c1 88 * @param workerChoiceStrategy - The worker choice strategy to set.
bdaf31cd
JB
89 */
90 public setWorkerChoiceStrategy (
91 workerChoiceStrategy: WorkerChoiceStrategy
92 ): void {
b2b1d84e 93 if (this.workerChoiceStrategyType === workerChoiceStrategy) {
95c83464 94 this.workerChoiceStrategies.get(workerChoiceStrategy)?.reset()
b2b1d84e
JB
95 } else {
96 this.workerChoiceStrategyType = workerChoiceStrategy
b2b1d84e 97 }
bdaf31cd
JB
98 }
99
100 /**
51fe3d3c 101 * Executes the worker choice strategy algorithm in the context.
bdaf31cd 102 *
c923ce56 103 * @returns The key of the chosen one.
bdaf31cd 104 */
c923ce56 105 public execute (): number {
95c83464
JB
106 const workerChoiceStrategy = this.workerChoiceStrategies.get(
107 this.workerChoiceStrategyType
108 ) as IWorkerChoiceStrategy<Worker, Data, Response>
9cd39dd4 109 if (
95c83464
JB
110 workerChoiceStrategy.isDynamicPool &&
111 !workerChoiceStrategy.pool.full &&
112 workerChoiceStrategy.pool.findFreeWorkerKey() === -1
9cd39dd4
JB
113 ) {
114 return this.createWorkerCallback()
115 }
95c83464 116 return workerChoiceStrategy.choose()
bdaf31cd 117 }
97a2abc3
JB
118
119 /**
51fe3d3c 120 * Removes a worker from the worker choice strategy in the context.
97a2abc3
JB
121 *
122 * @param workerKey - The key of the worker to remove.
123 * @returns `true` if the removal is successful, `false` otherwise.
124 */
125 public remove (workerKey: number): boolean {
95c83464
JB
126 return (
127 this.workerChoiceStrategies.get(
128 this.workerChoiceStrategyType
129 ) as IWorkerChoiceStrategy<Worker, Data, Response>
130 ).remove(workerKey)
131 }
bdaf31cd 132}