build: refine editorconfig configuration
[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'
bdaf31cd
JB
3import type {
4 IWorkerChoiceStrategy,
97a2abc3 5 RequiredStatistics,
bdaf31cd
JB
6 WorkerChoiceStrategy
7} from './selection-strategies-types'
8import { WorkerChoiceStrategies } from './selection-strategies-types'
78cea37e 9import { getWorkerChoiceStrategy } from './selection-strategies-utils'
bdaf31cd
JB
10
11/**
12 * The worker choice strategy context.
13 *
38e795c1
JB
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.
bdaf31cd
JB
17 */
18export class WorkerChoiceStrategyContext<
ea7a90d3 19 Worker extends IPoolWorker,
bdaf31cd
JB
20 Data,
21 Response
22> {
3300e7bc 23 private workerChoiceStrategy: IWorkerChoiceStrategy<Worker, Data, Response>
bdaf31cd
JB
24
25 /**
26 * Worker choice strategy context constructor.
27 *
38e795c1 28 * @param pool - The pool instance.
c923ce56 29 * @param createWorkerCallback - The worker creation callback for dynamic pool.
38e795c1 30 * @param workerChoiceStrategy - The worker choice strategy.
bdaf31cd
JB
31 */
32 public constructor (
3300e7bc 33 pool: IPoolInternal<Worker, Data, Response>,
c923ce56 34 private readonly createWorkerCallback: () => number,
bdaf31cd
JB
35 workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
36 ) {
1086026a 37 this.execute.bind(this)
3300e7bc
JB
38 this.workerChoiceStrategy = getWorkerChoiceStrategy<Worker, Data, Response>(
39 pool,
40 workerChoiceStrategy
41 )
bdaf31cd
JB
42 }
43
97a2abc3
JB
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
bdaf31cd 53 /**
bdede008 54 * Sets the worker choice strategy to use in the context.
bdaf31cd 55 *
38e795c1 56 * @param workerChoiceStrategy - The worker choice strategy to set.
bdaf31cd
JB
57 */
58 public setWorkerChoiceStrategy (
3300e7bc 59 pool: IPoolInternal<Worker, Data, Response>,
bdaf31cd
JB
60 workerChoiceStrategy: WorkerChoiceStrategy
61 ): void {
a6f7f1b4 62 this.workerChoiceStrategy?.reset()
d59df138 63 this.workerChoiceStrategy = getWorkerChoiceStrategy<Worker, Data, Response>(
3300e7bc 64 pool,
9cd39dd4
JB
65 workerChoiceStrategy
66 )
bdaf31cd
JB
67 }
68
69 /**
9cd39dd4 70 * Chooses a worker with the worker choice strategy.
bdaf31cd 71 *
c923ce56 72 * @returns The key of the chosen one.
bdaf31cd 73 */
c923ce56 74 public execute (): number {
9cd39dd4 75 if (
ccd49f03 76 this.workerChoiceStrategy.isDynamicPool &&
3300e7bc
JB
77 !this.workerChoiceStrategy.pool.full &&
78 this.workerChoiceStrategy.pool.findFreeWorkerKey() === -1
9cd39dd4
JB
79 ) {
80 return this.createWorkerCallback()
81 }
bdaf31cd
JB
82 return this.workerChoiceStrategy.choose()
83 }
97a2abc3
JB
84
85 /**
9cd39dd4 86 * Removes a worker in the worker choice strategy internals.
97a2abc3
JB
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 }
bdaf31cd 94}