perf: bind to this some methods in the tasks execution code path
[poolifier.git] / src / pools / selection-strategies / worker-choice-strategy-context.ts
CommitLineData
bdaf31cd
JB
1import type { IPoolInternal } from '../pool-internal'
2import { PoolType } from '../pool-internal'
ea7a90d3 3import type { IPoolWorker } from '../pool-worker'
bdaf31cd
JB
4import { DynamicPoolWorkerChoiceStrategy } from './dynamic-pool-worker-choice-strategy'
5import type {
6 IWorkerChoiceStrategy,
97a2abc3 7 RequiredStatistics,
bdaf31cd
JB
8 WorkerChoiceStrategy
9} from './selection-strategies-types'
10import { WorkerChoiceStrategies } from './selection-strategies-types'
78cea37e 11import { getWorkerChoiceStrategy } from './selection-strategies-utils'
bdaf31cd
JB
12
13/**
14 * The worker choice strategy context.
15 *
38e795c1
JB
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.
bdaf31cd
JB
19 */
20export class WorkerChoiceStrategyContext<
ea7a90d3 21 Worker extends IPoolWorker,
bdaf31cd
JB
22 Data,
23 Response
24> {
c923ce56 25 private workerChoiceStrategy!: IWorkerChoiceStrategy
bdaf31cd
JB
26
27 /**
28 * Worker choice strategy context constructor.
29 *
38e795c1 30 * @param pool - The pool instance.
c923ce56 31 * @param createWorkerCallback - The worker creation callback for dynamic pool.
38e795c1 32 * @param workerChoiceStrategy - The worker choice strategy.
bdaf31cd
JB
33 */
34 public constructor (
35 private readonly pool: IPoolInternal<Worker, Data, Response>,
c923ce56 36 private readonly createWorkerCallback: () => number,
bdaf31cd
JB
37 workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
38 ) {
1086026a 39 this.execute.bind(this)
bdaf31cd
JB
40 this.setWorkerChoiceStrategy(workerChoiceStrategy)
41 }
42
43 /**
bdede008 44 * Gets the worker choice strategy instance specific to the pool type.
bdaf31cd 45 *
38e795c1 46 * @param workerChoiceStrategy - The worker choice strategy.
bdaf31cd
JB
47 * @returns The worker choice strategy instance for the pool type.
48 */
49 private getPoolWorkerChoiceStrategy (
50 workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
c923ce56 51 ): IWorkerChoiceStrategy {
bdaf31cd
JB
52 if (this.pool.type === PoolType.DYNAMIC) {
53 return new DynamicPoolWorkerChoiceStrategy(
54 this.pool,
c923ce56 55 this.createWorkerCallback,
bdaf31cd
JB
56 workerChoiceStrategy
57 )
58 }
78cea37e 59 return getWorkerChoiceStrategy(this.pool, workerChoiceStrategy)
bdaf31cd
JB
60 }
61
97a2abc3
JB
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
bdaf31cd 71 /**
bdede008 72 * Sets the worker choice strategy to use in the context.
bdaf31cd 73 *
38e795c1 74 * @param workerChoiceStrategy - The worker choice strategy to set.
bdaf31cd
JB
75 */
76 public setWorkerChoiceStrategy (
77 workerChoiceStrategy: WorkerChoiceStrategy
78 ): void {
a6f7f1b4 79 this.workerChoiceStrategy?.reset()
78cea37e
JB
80 this.workerChoiceStrategy =
81 this.getPoolWorkerChoiceStrategy(workerChoiceStrategy)
bdaf31cd
JB
82 }
83
84 /**
bdede008 85 * Chooses a worker with the underlying selection strategy.
bdaf31cd 86 *
c923ce56 87 * @returns The key of the chosen one.
bdaf31cd 88 */
c923ce56 89 public execute (): number {
bdaf31cd
JB
90 return this.workerChoiceStrategy.choose()
91 }
97a2abc3
JB
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 }
bdaf31cd 102}