refactor: remove unneeded encapsulation around tasks usage handling
[poolifier.git] / src / pools / selection-strategies / selection-strategies-utils.ts
CommitLineData
bdaf31cd 1import type { IPoolInternal } from '../pool-internal'
ea7a90d3 2import type { IPoolWorker } from '../pool-worker'
23ff945a 3import { FairShareWorkerChoiceStrategy } from './fair-share-worker-choice-strategy'
bdaf31cd
JB
4import { LessRecentlyUsedWorkerChoiceStrategy } from './less-recently-used-worker-choice-strategy'
5import { RoundRobinWorkerChoiceStrategy } from './round-robin-worker-choice-strategy'
6import type {
7 IWorkerChoiceStrategy,
8 WorkerChoiceStrategy
9} from './selection-strategies-types'
10import { WorkerChoiceStrategies } from './selection-strategies-types'
fa6f1296 11import { WeightedRoundRobinWorkerChoiceStrategy } from './weighted-round-robin-worker-choice-strategy'
bdaf31cd
JB
12
13/**
78cea37e
JB
14 * Gets the worker choice strategy instance.
15 *
38e795c1
JB
16 * @param pool - The pool instance.
17 * @param workerChoiceStrategy - The worker choice strategy.
78cea37e 18 * @returns The worker choice strategy instance.
bdaf31cd 19 */
78cea37e
JB
20export 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 )
bdaf31cd
JB
42 }
43}