X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Finterleaved-weighted-round-robin-worker-choice-strategy.ts;h=4b59a55461e7e92596c5ead2f7fdaca6eabf9d71;hb=f6bc9f26d8a0246bbee14b2b03d0bcc41b8aeb52;hp=907bd911d6e607966bfddc2cd02087c11cdab44f;hpb=3d6dd312a7825521cce506ebb7443bae36a111e6;p=poolifier.git diff --git a/src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts b/src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts index 907bd911..4b59a554 100644 --- a/src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts @@ -4,7 +4,6 @@ import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils' import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' import type { IWorkerChoiceStrategy, - StrategyPolicy, WorkerChoiceStrategyOptions } from './selection-strategies-types' @@ -22,11 +21,6 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy< > extends AbstractWorkerChoiceStrategy implements IWorkerChoiceStrategy { - /** @inheritDoc */ - public readonly strategyPolicy: StrategyPolicy = { - useDynamicWorker: true - } - /** * Round id. * This is used to determine the current round weight. @@ -65,38 +59,46 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy< } /** @inheritDoc */ - public choose (): number { - let roundId: number | undefined + public choose (): number | undefined { + let roundId: number = this.roundId let workerNodeId: number | undefined for ( let roundIndex = this.roundId; roundIndex < this.roundWeights.length; roundIndex++ ) { + roundId = roundIndex for ( - let workerNodeKey = this.nextWorkerNodeKey; + let workerNodeKey = + this.nextWorkerNodeKey ?? this.previousWorkerNodeKey; workerNodeKey < this.pool.workerNodes.length; workerNodeKey++ ) { const workerWeight = this.opts.weights?.[workerNodeKey] ?? this.defaultWorkerWeight - // if (this.isWorkerNodeReady(workerNodeKey) && workerWeight >= this.roundWeights[roundIndex]) { - if (workerWeight >= this.roundWeights[roundIndex]) { - roundId = roundIndex + if ( + this.isWorkerNodeEligible(workerNodeKey) && + workerWeight >= this.roundWeights[roundIndex] + ) { workerNodeId = workerNodeKey break } } } - this.roundId = roundId ?? 0 - this.nextWorkerNodeKey = workerNodeId ?? 0 + this.roundId = roundId + if (workerNodeId == null) { + this.previousWorkerNodeKey = + this.nextWorkerNodeKey ?? this.previousWorkerNodeKey + } + this.nextWorkerNodeKey = workerNodeId const chosenWorkerNodeKey = this.nextWorkerNodeKey if (this.nextWorkerNodeKey === this.pool.workerNodes.length - 1) { this.nextWorkerNodeKey = 0 this.roundId = this.roundId === this.roundWeights.length - 1 ? 0 : this.roundId + 1 } else { - this.nextWorkerNodeKey = this.nextWorkerNodeKey + 1 + this.nextWorkerNodeKey = + (this.nextWorkerNodeKey ?? this.previousWorkerNodeKey) + 1 } return chosenWorkerNodeKey }