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