refactor: apply stricter strategy design pattern requirements on worker
[poolifier.git] / src / pools / selection-strategies / worker-choice-strategy-context.ts
1 import type { IPoolInternal } from '../pool-internal'
2 import type { IPoolWorker } from '../pool-worker'
3 import type {
4 IWorkerChoiceStrategy,
5 RequiredStatistics,
6 WorkerChoiceStrategy
7 } from './selection-strategies-types'
8 import { WorkerChoiceStrategies } from './selection-strategies-types'
9 import { getWorkerChoiceStrategy } from './selection-strategies-utils'
10
11 /**
12 * The worker choice strategy context.
13 *
14 * @typeParam Worker - Type of worker.
15 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
16 * @typeParam Response - Type of response of execution. This can only be serializable data.
17 */
18 export class WorkerChoiceStrategyContext<
19 Worker extends IPoolWorker,
20 Data,
21 Response
22 > {
23 private workerChoiceStrategy: IWorkerChoiceStrategy<Worker, Data, Response>
24
25 /**
26 * Worker choice strategy context constructor.
27 *
28 * @param pool - The pool instance.
29 * @param createWorkerCallback - The worker creation callback for dynamic pool.
30 * @param workerChoiceStrategy - The worker choice strategy.
31 */
32 public constructor (
33 pool: IPoolInternal<Worker, Data, Response>,
34 private readonly createWorkerCallback: () => number,
35 workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
36 ) {
37 this.execute.bind(this)
38 this.workerChoiceStrategy = getWorkerChoiceStrategy<Worker, Data, Response>(
39 pool,
40 workerChoiceStrategy
41 )
42 }
43
44 /**
45 * Gets the worker choice strategy required statistics.
46 *
47 * @returns The required statistics.
48 */
49 public getRequiredStatistics (): RequiredStatistics {
50 return this.workerChoiceStrategy.requiredStatistics
51 }
52
53 /**
54 * Sets the worker choice strategy to use in the context.
55 *
56 * @param workerChoiceStrategy - The worker choice strategy to set.
57 */
58 public setWorkerChoiceStrategy (
59 pool: IPoolInternal<Worker, Data, Response>,
60 workerChoiceStrategy: WorkerChoiceStrategy
61 ): void {
62 this.workerChoiceStrategy?.reset()
63 this.workerChoiceStrategy = getWorkerChoiceStrategy<Worker, Data, Response>(
64 pool,
65 workerChoiceStrategy
66 )
67 }
68
69 /**
70 * Chooses a worker with the worker choice strategy.
71 *
72 * @returns The key of the chosen one.
73 */
74 public execute (): number {
75 if (
76 this.workerChoiceStrategy.isDynamicPool &&
77 !this.workerChoiceStrategy.pool.full &&
78 this.workerChoiceStrategy.pool.findFreeWorkerKey() === -1
79 ) {
80 return this.createWorkerCallback()
81 }
82 return this.workerChoiceStrategy.choose()
83 }
84
85 /**
86 * Removes a worker in the worker choice strategy internals.
87 *
88 * @param workerKey - The key of the worker to remove.
89 * @returns `true` if the removal is successful, `false` otherwise.
90 */
91 public remove (workerKey: number): boolean {
92 return this.workerChoiceStrategy.remove(workerKey)
93 }
94 }