| 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 { LessBusyWorkerChoiceStrategy } from './less-busy-worker-choice-strategy' |
| 5 | import { LessUsedWorkerChoiceStrategy } from './less-used-worker-choice-strategy' |
| 6 | import { RoundRobinWorkerChoiceStrategy } from './round-robin-worker-choice-strategy' |
| 7 | import type { |
| 8 | IWorkerChoiceStrategy, |
| 9 | WorkerChoiceStrategy |
| 10 | } from './selection-strategies-types' |
| 11 | import { WorkerChoiceStrategies } from './selection-strategies-types' |
| 12 | import { WeightedRoundRobinWorkerChoiceStrategy } from './weighted-round-robin-worker-choice-strategy' |
| 13 | |
| 14 | /** |
| 15 | * Gets the worker choice strategy instance. |
| 16 | * |
| 17 | * @param pool - The pool instance. |
| 18 | * @param workerChoiceStrategy - The worker choice strategy. |
| 19 | * @returns The worker choice strategy instance. |
| 20 | */ |
| 21 | export function getWorkerChoiceStrategy< |
| 22 | Worker extends IPoolWorker, |
| 23 | Data, |
| 24 | Response |
| 25 | > ( |
| 26 | pool: IPoolInternal<Worker, Data, Response>, |
| 27 | workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN |
| 28 | ): IWorkerChoiceStrategy { |
| 29 | switch (workerChoiceStrategy) { |
| 30 | case WorkerChoiceStrategies.ROUND_ROBIN: |
| 31 | return new RoundRobinWorkerChoiceStrategy<Worker, Data, Response>(pool) |
| 32 | case WorkerChoiceStrategies.LESS_USED: |
| 33 | return new LessUsedWorkerChoiceStrategy<Worker, Data, Response>(pool) |
| 34 | case WorkerChoiceStrategies.LESS_BUSY: |
| 35 | return new LessBusyWorkerChoiceStrategy<Worker, Data, Response>(pool) |
| 36 | case WorkerChoiceStrategies.FAIR_SHARE: |
| 37 | return new FairShareWorkerChoiceStrategy<Worker, Data, Response>(pool) |
| 38 | case WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN: |
| 39 | return new WeightedRoundRobinWorkerChoiceStrategy<Worker, Data, Response>( |
| 40 | pool |
| 41 | ) |
| 42 | default: |
| 43 | throw new Error( |
| 44 | // eslint-disable-next-line @typescript-eslint/restrict-template-expressions |
| 45 | `Worker choice strategy '${workerChoiceStrategy}' not found` |
| 46 | ) |
| 47 | } |
| 48 | } |