X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fpools%2Fselection-strategies%2Fround-robin-worker-choice-strategy.ts;h=55fa8ad73d4c7b7713a490cf07265e3634803779;hb=a6d60641bc8ca8e6c6e6ce15bdf3035cf2a5d192;hp=835860930b0d76f54c37d547b73274d9bf7fb175;hpb=e102732c0e3966b81834b2c0bdd087eb051162ad;p=poolifier.git diff --git a/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts b/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts index 83586093..55fa8ad7 100644 --- a/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts @@ -38,7 +38,7 @@ export class RoundRobinWorkerChoiceStrategy< /** @inheritDoc */ public reset (): boolean { - this.nextWorkerNodeId = 0 + this.nextWorkerNodeKey = 0 return true } @@ -49,23 +49,30 @@ export class RoundRobinWorkerChoiceStrategy< /** @inheritDoc */ public choose (): number { - const chosenWorkerNodeKey = this.nextWorkerNodeId - this.nextWorkerNodeId = - this.nextWorkerNodeId === this.pool.workerNodes.length - 1 - ? 0 - : this.nextWorkerNodeId + 1 + const chosenWorkerNodeKey = this.nextWorkerNodeKey + do { + this.roundRobinNextWorkerNodeKey() + } while (!this.isWorkerNodeEligible(this.nextWorkerNodeKey)) return chosenWorkerNodeKey } /** @inheritDoc */ public remove (workerNodeKey: number): boolean { - if (this.nextWorkerNodeId === workerNodeKey) { + if (this.nextWorkerNodeKey === workerNodeKey) { if (this.pool.workerNodes.length === 0) { - this.nextWorkerNodeId = 0 - } else if (this.nextWorkerNodeId > this.pool.workerNodes.length - 1) { - this.nextWorkerNodeId = this.pool.workerNodes.length - 1 + this.nextWorkerNodeKey = 0 + } else if (this.nextWorkerNodeKey > this.pool.workerNodes.length - 1) { + this.nextWorkerNodeKey = this.pool.workerNodes.length - 1 } } return true } + + private roundRobinNextWorkerNodeKey (): number { + this.nextWorkerNodeKey = + this.nextWorkerNodeKey === this.pool.workerNodes.length - 1 + ? 0 + : this.nextWorkerNodeKey + 1 + return this.nextWorkerNodeKey + } }