fix: fix worker choice strategy retries mechanism on some edge cases
[poolifier.git] / src / pools / selection-strategies / round-robin-worker-choice-strategy.ts
index 55fa8ad73d4c7b7713a490cf07265e3634803779..10bdf8714b8bbd23eaf1fab96051ed11e7094bdd 100644 (file)
@@ -24,7 +24,8 @@ export class RoundRobinWorkerChoiceStrategy<
   implements IWorkerChoiceStrategy {
   /** @inheritDoc */
   public readonly strategyPolicy: StrategyPolicy = {
-    useDynamicWorker: true
+    dynamicWorkerUsage: true,
+    dynamicWorkerReady: true
   }
 
   /** @inheritDoc */
@@ -48,11 +49,12 @@ export class RoundRobinWorkerChoiceStrategy<
   }
 
   /** @inheritDoc */
-  public choose (): number {
+  public choose (): number | undefined {
     const chosenWorkerNodeKey = this.nextWorkerNodeKey
-    do {
-      this.roundRobinNextWorkerNodeKey()
-    } while (!this.isWorkerNodeEligible(this.nextWorkerNodeKey))
+    this.roundRobinNextWorkerNodeKey()
+    if (!this.isWorkerNodeEligible(this.nextWorkerNodeKey as number)) {
+      this.nextWorkerNodeKey = undefined
+    }
     return chosenWorkerNodeKey
   }
 
@@ -68,11 +70,11 @@ export class RoundRobinWorkerChoiceStrategy<
     return true
   }
 
-  private roundRobinNextWorkerNodeKey (): number {
+  private roundRobinNextWorkerNodeKey (): number | undefined {
     this.nextWorkerNodeKey =
       this.nextWorkerNodeKey === this.pool.workerNodes.length - 1
         ? 0
-        : this.nextWorkerNodeKey + 1
+        : (this.nextWorkerNodeKey ?? 0) + 1
     return this.nextWorkerNodeKey
   }
 }