Merge branch 'master' of github.com:jerome-benoit/poolifier
[poolifier.git] / src / pools / selection-strategies / weighted-round-robin-worker-choice-strategy.ts
index ed38aa1497176ba88751e83a850e1794b3aaa9fd..fecba414ae1f8b18c29f566192ba577eb60b2288 100644 (file)
@@ -77,7 +77,9 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
   /** @inheritDoc */
   public choose (): number {
     const chosenWorkerNodeKey = this.nextWorkerNodeKey
-    this.weightedRoundRobinNextWorkerNodeKey()
+    do {
+      this.weightedRoundRobinNextWorkerNodeKey()
+    } while (!this.isWorkerNodeReady(this.nextWorkerNodeKey))
     return chosenWorkerNodeKey
   }
 
@@ -94,7 +96,7 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
     return true
   }
 
-  private weightedRoundRobinNextWorkerNodeKey (): void {
+  private weightedRoundRobinNextWorkerNodeKey (): number {
     const workerVirtualTaskRunTime = this.workerVirtualTaskRunTime
     const workerWeight =
       this.opts.weights?.[this.nextWorkerNodeKey] ?? this.defaultWorkerWeight
@@ -109,5 +111,6 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
           : this.nextWorkerNodeKey + 1
       this.workerVirtualTaskRunTime = 0
     }
+    return this.nextWorkerNodeKey
   }
 }