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