X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fworker-choice-strategy-context.ts;h=90c98c6018a1c786fda04234fd48eb5557b0edec;hb=95c83464bb1fab234d1c41204f98defcd01c789f;hp=644adfea1f3e9a10dd156a03db31983402438185;hpb=51fe3d3cb0858b5e2ad96076b51a6b9daa8f852c;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 644adfea..90c98c60 100644 --- a/src/pools/selection-strategies/worker-choice-strategy-context.ts +++ b/src/pools/selection-strategies/worker-choice-strategy-context.ts @@ -24,7 +24,10 @@ export class WorkerChoiceStrategyContext< Data = unknown, Response = unknown > { - private workerChoiceStrategy: IWorkerChoiceStrategy + private readonly workerChoiceStrategies = new Map< + WorkerChoiceStrategy, + IWorkerChoiceStrategy + >() /** * Worker choice strategy context constructor. @@ -39,10 +42,7 @@ export class WorkerChoiceStrategyContext< private workerChoiceStrategyType: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN ) { this.execute.bind(this) - this.workerChoiceStrategy = this.getWorkerChoiceStrategy( - pool, - this.workerChoiceStrategyType - ) + this.registerWorkerChoiceStrategy(pool, workerChoiceStrategyType) } /** @@ -51,7 +51,11 @@ export class WorkerChoiceStrategyContext< * @returns The required statistics. */ public getRequiredStatistics (): RequiredStatistics { - return this.workerChoiceStrategy.requiredStatistics + return ( + this.workerChoiceStrategies.get( + this.workerChoiceStrategyType + ) as IWorkerChoiceStrategy + ).requiredStatistics } /** @@ -64,13 +68,10 @@ export class WorkerChoiceStrategyContext< workerChoiceStrategy: WorkerChoiceStrategy ): void { if (this.workerChoiceStrategyType === workerChoiceStrategy) { - this.workerChoiceStrategy?.reset() + this.workerChoiceStrategies.get(workerChoiceStrategy)?.reset() } else { this.workerChoiceStrategyType = workerChoiceStrategy - this.workerChoiceStrategy = this.getWorkerChoiceStrategy( - pool, - this.workerChoiceStrategyType - ) + this.registerWorkerChoiceStrategy(pool, workerChoiceStrategy) } } @@ -80,14 +81,17 @@ export class WorkerChoiceStrategyContext< * @returns The key of the chosen one. */ public execute (): number { + const workerChoiceStrategy = this.workerChoiceStrategies.get( + this.workerChoiceStrategyType + ) as IWorkerChoiceStrategy if ( - this.workerChoiceStrategy.isDynamicPool && - !this.workerChoiceStrategy.pool.full && - this.workerChoiceStrategy.pool.findFreeWorkerKey() === -1 + workerChoiceStrategy.isDynamicPool && + !workerChoiceStrategy.pool.full && + workerChoiceStrategy.pool.findFreeWorkerKey() === -1 ) { return this.createWorkerCallback() } - return this.workerChoiceStrategy.choose() + return workerChoiceStrategy.choose() } /** @@ -97,7 +101,23 @@ export class WorkerChoiceStrategyContext< * @returns `true` if the removal is successful, `false` otherwise. */ public remove (workerKey: number): boolean { - return this.workerChoiceStrategy.remove(workerKey) + return ( + this.workerChoiceStrategies.get( + this.workerChoiceStrategyType + ) as IWorkerChoiceStrategy + ).remove(workerKey) + } + + private registerWorkerChoiceStrategy ( + pool: IPoolInternal, + workerChoiceStrategy: WorkerChoiceStrategy + ): void { + if (!this.workerChoiceStrategies.has(workerChoiceStrategy)) { + this.workerChoiceStrategies.set( + workerChoiceStrategy, + this.getWorkerChoiceStrategy(pool, workerChoiceStrategy) + ) + } } /**