X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fweighted-round-robin-worker-choice-strategy.ts;h=bda206a7d7e1521ac8a6056b0485ad18e7698375;hb=e34f0ebcd2536e0890242ab414467c0e68ed65e6;hp=ad20db423826aa46a649da5512a3358e9f119572;hpb=0d80593b9a7596645612087f687fc6f5cab3101a;p=poolifier.git diff --git a/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts b/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts index ad20db42..bda206a7 100644 --- a/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts @@ -50,7 +50,7 @@ export class WeightedRoundRobinWorkerChoiceStrategy< opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS ) { super(pool, opts) - this.checkOptions(this.opts) + this.setRequiredStatistics(this.opts) this.defaultWorkerWeight = this.computeDefaultWorkerWeight() } @@ -61,16 +61,21 @@ export class WeightedRoundRobinWorkerChoiceStrategy< return true } + /** @inheritDoc */ + public update (): boolean { + return true + } + /** @inheritDoc */ public choose (): number { const chosenWorkerNodeKey = this.currentWorkerNodeId - const workerVirtualTaskRunTime = this.workerVirtualTaskRunTime ?? 0 - const workerTaskWeight = + const workerVirtualTaskRunTime = this.workerVirtualTaskRunTime + const workerWeight = this.opts.weights?.[chosenWorkerNodeKey] ?? this.defaultWorkerWeight - if (workerVirtualTaskRunTime < workerTaskWeight) { + if (workerVirtualTaskRunTime < workerWeight) { this.workerVirtualTaskRunTime = workerVirtualTaskRunTime + - (this.getWorkerVirtualTaskRunTime(chosenWorkerNodeKey) ?? 0) + this.getWorkerVirtualTaskRunTime(chosenWorkerNodeKey) } else { this.currentWorkerNodeId = this.currentWorkerNodeId === this.pool.workerNodes.length - 1 @@ -86,11 +91,8 @@ export class WeightedRoundRobinWorkerChoiceStrategy< if (this.currentWorkerNodeId === workerNodeKey) { if (this.pool.workerNodes.length === 0) { this.currentWorkerNodeId = 0 - } else { - this.currentWorkerNodeId = - this.currentWorkerNodeId > this.pool.workerNodes.length - 1 - ? this.pool.workerNodes.length - 1 - : this.currentWorkerNodeId + } else if (this.currentWorkerNodeId > this.pool.workerNodes.length - 1) { + this.currentWorkerNodeId = this.pool.workerNodes.length - 1 } this.workerVirtualTaskRunTime = 0 }