refactor: remove unneeded encapsulation around tasks usage handling
[poolifier.git] / src / pools / selection-strategies / selection-strategies-utils.ts
1 import type { IPoolInternal } from '../pool-internal'
2 import type { IPoolWorker } from '../pool-worker'
3 import { FairShareWorkerChoiceStrategy } from './fair-share-worker-choice-strategy'
4 import { LessRecentlyUsedWorkerChoiceStrategy } from './less-recently-used-worker-choice-strategy'
5 import { RoundRobinWorkerChoiceStrategy } from './round-robin-worker-choice-strategy'
6 import type {
7 IWorkerChoiceStrategy,
8 WorkerChoiceStrategy
9 } from './selection-strategies-types'
10 import { WorkerChoiceStrategies } from './selection-strategies-types'
11 import { WeightedRoundRobinWorkerChoiceStrategy } from './weighted-round-robin-worker-choice-strategy'
12
13 /**
14 * Gets the worker choice strategy instance.
15 *
16 * @param pool - The pool instance.
17 * @param workerChoiceStrategy - The worker choice strategy.
18 * @returns The worker choice strategy instance.
19 */
20 export function getWorkerChoiceStrategy<
21 Worker extends IPoolWorker,
22 Data,
23 Response
24 > (
25 pool: IPoolInternal<Worker, Data, Response>,
26 workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
27 ): IWorkerChoiceStrategy<Worker> {
28 switch (workerChoiceStrategy) {
29 case WorkerChoiceStrategies.ROUND_ROBIN:
30 return new RoundRobinWorkerChoiceStrategy(pool)
31 case WorkerChoiceStrategies.LESS_RECENTLY_USED:
32 return new LessRecentlyUsedWorkerChoiceStrategy(pool)
33 case WorkerChoiceStrategies.FAIR_SHARE:
34 return new FairShareWorkerChoiceStrategy(pool)
35 case WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN:
36 return new WeightedRoundRobinWorkerChoiceStrategy(pool)
37 default:
38 throw new Error(
39 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
40 `Worker choice strategy '${workerChoiceStrategy}' not found`
41 )
42 }
43 }