X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fpools%2Fselection-strategies%2Fworker-choice-strategy-context.ts;h=8041e2f6577b7ca046c6e51048ed2a7d873923db;hb=ccd49f039d24ec1590b2b32ff1738c80fae743e5;hp=c149cc2e9b48a7a6dae62466aea312fd9054e6dc;hpb=c923ce5670eeae4194aa996d44a1071e88cb21ad;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 c149cc2e..8041e2f6 100644 --- a/src/pools/selection-strategies/worker-choice-strategy-context.ts +++ b/src/pools/selection-strategies/worker-choice-strategy-context.ts @@ -1,9 +1,8 @@ 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, WorkerChoiceStrategy } from './selection-strategies-types' import { WorkerChoiceStrategies } from './selection-strategies-types' @@ -35,35 +34,17 @@ export class WorkerChoiceStrategyContext< 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. + * Gets the worker choice strategy required statistics. * - * @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) - } - - /** - * Gets the worker choice strategy used in the context. - * - * @returns The worker choice strategy. + * @returns The required statistics. */ - public getWorkerChoiceStrategy (): IWorkerChoiceStrategy { - return this.workerChoiceStrategy + public getRequiredStatistics (): RequiredStatistics { + return this.workerChoiceStrategy.requiredStatistics } /** @@ -75,16 +56,35 @@ export class WorkerChoiceStrategyContext< workerChoiceStrategy: WorkerChoiceStrategy ): void { this.workerChoiceStrategy?.reset() - this.workerChoiceStrategy = - this.getPoolWorkerChoiceStrategy(workerChoiceStrategy) + this.workerChoiceStrategy = getWorkerChoiceStrategy( + this.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.pool.full && + this.pool.findFreeWorkerKey() === -1 + ) { + return this.createWorkerCallback() + } return this.workerChoiceStrategy.choose() } + + /** + * 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. + */ + public remove (workerKey: number): boolean { + return this.workerChoiceStrategy.remove(workerKey) + } }