refactor: sensible defaults for worker choice strategy policy
[poolifier.git] / src / pools / selection-strategies / round-robin-worker-choice-strategy.ts
index 10bdf8714b8bbd23eaf1fab96051ed11e7094bdd..7a50db7b6387d5426dd4c4847482c5314f611b74 100644 (file)
@@ -4,7 +4,6 @@ import type { IWorker } from '../worker'
 import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
 import type {
   IWorkerChoiceStrategy,
-  StrategyPolicy,
   WorkerChoiceStrategyOptions
 } from './selection-strategies-types'
 
@@ -22,12 +21,6 @@ export class RoundRobinWorkerChoiceStrategy<
   >
   extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
   implements IWorkerChoiceStrategy {
-  /** @inheritDoc */
-  public readonly strategyPolicy: StrategyPolicy = {
-    dynamicWorkerUsage: true,
-    dynamicWorkerReady: true
-  }
-
   /** @inheritDoc */
   public constructor (
     pool: IPool<Worker, Data, Response>,
@@ -54,6 +47,8 @@ export class RoundRobinWorkerChoiceStrategy<
     this.roundRobinNextWorkerNodeKey()
     if (!this.isWorkerNodeEligible(this.nextWorkerNodeKey as number)) {
       this.nextWorkerNodeKey = undefined
+      this.previousWorkerNodeKey =
+        chosenWorkerNodeKey ?? this.previousWorkerNodeKey
     }
     return chosenWorkerNodeKey
   }
@@ -74,7 +69,7 @@ export class RoundRobinWorkerChoiceStrategy<
     this.nextWorkerNodeKey =
       this.nextWorkerNodeKey === this.pool.workerNodes.length - 1
         ? 0
-        : (this.nextWorkerNodeKey ?? 0) + 1
+        : (this.nextWorkerNodeKey ?? this.previousWorkerNodeKey) + 1
     return this.nextWorkerNodeKey
   }
 }