X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fworker-choice-strategy-context.ts;h=41b515b816d00dc991c27d8143efe80ec7c6116a;hb=3300e7bcb155358c2cc1eed6e4ac11581457616f;hp=1564e0f0f73e1c41d6da2f9d157620439f3489f8;hpb=1086026a2542abdcd0c5569d9afbfb8c2e628276;p=poolifier.git diff --git a/src/pools/selection-strategies/worker-choice-strategy-context.ts b/src/pools/selection-strategies/worker-choice-strategy-context.ts index 1564e0f0..41b515b8 100644 --- a/src/pools/selection-strategies/worker-choice-strategy-context.ts +++ b/src/pools/selection-strategies/worker-choice-strategy-context.ts @@ -1,7 +1,5 @@ import type { IPoolInternal } from '../pool-internal' -import { PoolType } from '../pool-internal' import type { IPoolWorker } from '../pool-worker' -import { DynamicPoolWorkerChoiceStrategy } from './dynamic-pool-worker-choice-strategy' import type { IWorkerChoiceStrategy, RequiredStatistics, @@ -22,7 +20,7 @@ export class WorkerChoiceStrategyContext< Data, Response > { - private workerChoiceStrategy!: IWorkerChoiceStrategy + private workerChoiceStrategy: IWorkerChoiceStrategy /** * Worker choice strategy context constructor. @@ -32,31 +30,15 @@ export class WorkerChoiceStrategyContext< * @param workerChoiceStrategy - The worker choice strategy. */ public constructor ( - private readonly pool: IPoolInternal, + pool: IPoolInternal, private readonly createWorkerCallback: () => number, workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN ) { this.execute.bind(this) - this.setWorkerChoiceStrategy(workerChoiceStrategy) - } - - /** - * Gets the worker choice strategy instance specific to the pool type. - * - * @param workerChoiceStrategy - The worker choice strategy. - * @returns The worker choice strategy instance for the pool type. - */ - private getPoolWorkerChoiceStrategy ( - workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN - ): IWorkerChoiceStrategy { - if (this.pool.type === PoolType.DYNAMIC) { - return new DynamicPoolWorkerChoiceStrategy( - this.pool, - this.createWorkerCallback, - workerChoiceStrategy - ) - } - return getWorkerChoiceStrategy(this.pool, workerChoiceStrategy) + this.workerChoiceStrategy = getWorkerChoiceStrategy( + pool, + workerChoiceStrategy + ) } /** @@ -74,24 +56,34 @@ export class WorkerChoiceStrategyContext< * @param workerChoiceStrategy - The worker choice strategy to set. */ public setWorkerChoiceStrategy ( + pool: IPoolInternal, workerChoiceStrategy: WorkerChoiceStrategy ): void { this.workerChoiceStrategy?.reset() - this.workerChoiceStrategy = - this.getPoolWorkerChoiceStrategy(workerChoiceStrategy) + this.workerChoiceStrategy = getWorkerChoiceStrategy( + pool, + workerChoiceStrategy + ) } /** - * Chooses a worker with the underlying selection strategy. + * Chooses a worker with the worker choice strategy. * * @returns The key of the chosen one. */ public execute (): number { + if ( + this.workerChoiceStrategy.isDynamicPool && + !this.workerChoiceStrategy.pool.full && + this.workerChoiceStrategy.pool.findFreeWorkerKey() === -1 + ) { + return this.createWorkerCallback() + } return this.workerChoiceStrategy.choose() } /** - * Removes a worker in the underlying selection strategy internals. + * Removes a worker in the worker choice strategy internals. * * @param workerKey - The key of the worker to remove. * @returns `true` if the removal is successful, `false` otherwise.