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