refactor: sensible defaults for worker choice strategy policy
[poolifier.git] / src / pools / selection-strategies / round-robin-worker-choice-strategy.ts
index e99383caf977bec57a28d158b7c147806ed41fcb..7a50db7b6387d5426dd4c4847482c5314f611b74 100644 (file)
@@ -11,8 +11,8 @@ import type {
  * Selects the next worker in a round robin fashion.
  *
  * @typeParam Worker - Type of worker which manages the strategy.
- * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
- * @typeParam Response - Type of execution response. This can only be serializable data.
+ * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
+ * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
  */
 export class RoundRobinWorkerChoiceStrategy<
     Worker extends IWorker,
@@ -21,23 +21,18 @@ export class RoundRobinWorkerChoiceStrategy<
   >
   extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
   implements IWorkerChoiceStrategy {
-  /**
-   * Id of the next worker node.
-   */
-  private nextWorkerNodeId: number = 0
-
   /** @inheritDoc */
   public constructor (
     pool: IPool<Worker, Data, Response>,
     opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
   ) {
     super(pool, opts)
-    this.checkOptions(this.opts)
+    this.setTaskStatisticsRequirements(this.opts)
   }
 
   /** @inheritDoc */
   public reset (): boolean {
-    this.nextWorkerNodeId = 0
+    this.nextWorkerNodeKey = 0
     return true
   }
 
@@ -47,24 +42,34 @@ export class RoundRobinWorkerChoiceStrategy<
   }
 
   /** @inheritDoc */
-  public choose (): number {
-    const chosenWorkerNodeKey = this.nextWorkerNodeId
-    this.nextWorkerNodeId =
-      this.nextWorkerNodeId === this.pool.workerNodes.length - 1
-        ? 0
-        : this.nextWorkerNodeId + 1
+  public choose (): number | undefined {
+    const chosenWorkerNodeKey = this.nextWorkerNodeKey
+    this.roundRobinNextWorkerNodeKey()
+    if (!this.isWorkerNodeEligible(this.nextWorkerNodeKey as number)) {
+      this.nextWorkerNodeKey = undefined
+      this.previousWorkerNodeKey =
+        chosenWorkerNodeKey ?? this.previousWorkerNodeKey
+    }
     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 | undefined {
+    this.nextWorkerNodeKey =
+      this.nextWorkerNodeKey === this.pool.workerNodes.length - 1
+        ? 0
+        : (this.nextWorkerNodeKey ?? this.previousWorkerNodeKey) + 1
+    return this.nextWorkerNodeKey
+  }
 }