refactor: untangle worker choosing code from worker creation code
[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 28 WorkerChoiceStrategy,
17393ac8 29 IWorkerChoiceStrategy
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>,
b2b1d84e 41 private workerChoiceStrategyType: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
bdaf31cd 42 ) {
1086026a 43 this.execute.bind(this)
b529c323
JB
44 this.workerChoiceStrategies = new Map<
45 WorkerChoiceStrategy,
17393ac8 46 IWorkerChoiceStrategy
b529c323
JB
47 >([
48 [
49 WorkerChoiceStrategies.ROUND_ROBIN,
50 new RoundRobinWorkerChoiceStrategy<Worker, Data, Response>(pool)
51 ],
52 [
53 WorkerChoiceStrategies.LESS_USED,
54 new LessUsedWorkerChoiceStrategy<Worker, Data, Response>(pool)
55 ],
56 [
57 WorkerChoiceStrategies.LESS_BUSY,
58 new LessBusyWorkerChoiceStrategy<Worker, Data, Response>(pool)
59 ],
60 [
61 WorkerChoiceStrategies.FAIR_SHARE,
62 new FairShareWorkerChoiceStrategy<Worker, Data, Response>(pool)
63 ],
64 [
65 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN,
66 new WeightedRoundRobinWorkerChoiceStrategy<Worker, Data, Response>(pool)
67 ]
68 ])
bdaf31cd
JB
69 }
70
97a2abc3 71 /**
51fe3d3c 72 * Gets the worker choice strategy in the context required statistics.
97a2abc3
JB
73 *
74 * @returns The required statistics.
75 */
76 public getRequiredStatistics (): RequiredStatistics {
95c83464
JB
77 return (
78 this.workerChoiceStrategies.get(
79 this.workerChoiceStrategyType
17393ac8 80 ) as IWorkerChoiceStrategy
95c83464 81 ).requiredStatistics
97a2abc3
JB
82 }
83
bdaf31cd 84 /**
bdede008 85 * Sets the worker choice strategy to use in the context.
bdaf31cd 86 *
38e795c1 87 * @param workerChoiceStrategy - The worker choice strategy to set.
bdaf31cd
JB
88 */
89 public setWorkerChoiceStrategy (
90 workerChoiceStrategy: WorkerChoiceStrategy
91 ): void {
b2b1d84e 92 if (this.workerChoiceStrategyType === workerChoiceStrategy) {
95c83464 93 this.workerChoiceStrategies.get(workerChoiceStrategy)?.reset()
b2b1d84e
JB
94 } else {
95 this.workerChoiceStrategyType = workerChoiceStrategy
b2b1d84e 96 }
bdaf31cd
JB
97 }
98
99 /**
51fe3d3c 100 * Executes the worker choice strategy algorithm in the context.
bdaf31cd 101 *
c923ce56 102 * @returns The key of the chosen one.
bdaf31cd 103 */
c923ce56 104 public execute (): number {
17393ac8
JB
105 return (
106 this.workerChoiceStrategies.get(
107 this.workerChoiceStrategyType
108 ) as IWorkerChoiceStrategy
109 ).choose()
bdaf31cd 110 }
97a2abc3
JB
111
112 /**
51fe3d3c 113 * Removes a worker from the worker choice strategy in the context.
97a2abc3
JB
114 *
115 * @param workerKey - The key of the worker to remove.
116 * @returns `true` if the removal is successful, `false` otherwise.
117 */
118 public remove (workerKey: number): boolean {
95c83464
JB
119 return (
120 this.workerChoiceStrategies.get(
121 this.workerChoiceStrategyType
17393ac8 122 ) as IWorkerChoiceStrategy
95c83464
JB
123 ).remove(workerKey)
124 }
bdaf31cd 125}