X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fworker-choice-strategy-context.ts;h=f9ddb3a1c54dd5798c94a30fb3ae58437932fe42;hb=27a8a097ad47621d1a914240d777921f129cff61;hp=d3e7f03e6ba520ab69c2996dd97d0dbe9302ab48;hpb=39618ede0e08d380c1ac82005bcc2ab1f3c227b6;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 d3e7f03e..f9ddb3a1 100644 --- a/src/pools/selection-strategies/worker-choice-strategy-context.ts +++ b/src/pools/selection-strategies/worker-choice-strategy-context.ts @@ -1,11 +1,11 @@ import type { IPool } from '../pool.js' +import { getWorkerChoiceStrategyRetries } from '../utils.js' import type { IWorker } from '../worker.js' -import { getWorkerChoiceStrategyRetries } from '../../utils.js' import { FairShareWorkerChoiceStrategy } from './fair-share-worker-choice-strategy.js' import { InterleavedWeightedRoundRobinWorkerChoiceStrategy } from './interleaved-weighted-round-robin-worker-choice-strategy.js' import { LeastBusyWorkerChoiceStrategy } from './least-busy-worker-choice-strategy.js' -import { LeastUsedWorkerChoiceStrategy } from './least-used-worker-choice-strategy.js' import { LeastEluWorkerChoiceStrategy } from './least-elu-worker-choice-strategy.js' +import { LeastUsedWorkerChoiceStrategy } from './least-used-worker-choice-strategy.js' import { RoundRobinWorkerChoiceStrategy } from './round-robin-worker-choice-strategy.js' import type { IWorkerChoiceStrategy, @@ -30,7 +30,12 @@ export class WorkerChoiceStrategyContext< Response = unknown > { /** - * The worker choice strategies registered in the context. + * The number of worker choice strategy execution retries. + */ + public retriesCount: number + + /** + * The worker choice strategy instances registered in the context. */ private readonly workerChoiceStrategies: Map< WorkerChoiceStrategy, @@ -38,7 +43,7 @@ export class WorkerChoiceStrategyContext< > /** - * The number of worker choice strategy execution retries. + * The maximum number of worker choice strategy execution retries. */ private readonly retries: number @@ -111,6 +116,7 @@ export class WorkerChoiceStrategyContext< >(pool, opts) ] ]) + this.retriesCount = 0 this.retries = getWorkerChoiceStrategyRetries(pool, opts) } @@ -166,17 +172,13 @@ export class WorkerChoiceStrategyContext< * Executes the worker choice strategy in the context algorithm. * * @returns The key of the worker node. - * @throws {@link https://nodejs.org/api/errors.html#class-error} If after configured retries the worker node key is null or undefined. + * @throws {@link https://nodejs.org/api/errors.html#class-error} If after computed retries the worker node key is null or undefined. */ public execute (): number { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const workerChoiceStrategy = this.workerChoiceStrategies.get( - this.workerChoiceStrategy - )! - if (!workerChoiceStrategy.hasPoolWorkerNodesReady()) { - return this.execute() - } - return this.executeStrategy(workerChoiceStrategy) + return this.executeStrategy( + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.workerChoiceStrategies.get(this.workerChoiceStrategy)! + ) } /** @@ -184,7 +186,7 @@ export class WorkerChoiceStrategyContext< * * @param workerChoiceStrategy - The worker choice strategy. * @returns The key of the worker node. - * @throws {@link https://nodejs.org/api/errors.html#class-error} If after configured retries the worker node key is null or undefined. + * @throws {@link https://nodejs.org/api/errors.html#class-error} If after computed retries the worker node key is null or undefined. */ private executeStrategy (workerChoiceStrategy: IWorkerChoiceStrategy): number { let workerNodeKey: number | undefined @@ -193,9 +195,10 @@ export class WorkerChoiceStrategyContext< do { workerNodeKey = workerChoiceStrategy.choose() if (workerNodeKey == null && chooseCount > 0) { - retriesCount++ + ++retriesCount + ++this.retriesCount } - chooseCount++ + ++chooseCount } while (workerNodeKey == null && retriesCount < this.retries) if (workerNodeKey == null) { throw new Error(