fix: refine pool statuses handling
[poolifier.git] / src / pools / selection-strategies / interleaved-weighted-round-robin-worker-choice-strategy.ts
index 15a6c7f48eaac7f0a81e5a30bfd74762a426435d..b11f0a177aa44925d510b697e48b578fe14169b4 100644 (file)
@@ -1,14 +1,11 @@
 import type { IWorker } from '../worker'
 import type { IPool } from '../pool'
-import {
-  DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
-  DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
-} from '../../utils'
+import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } from '../../utils'
 import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
 import type {
   IWorkerChoiceStrategy,
-  TaskStatisticsRequirements,
-  WorkerChoiceStrategyOptions
+  InternalWorkerChoiceStrategyOptions,
+  TaskStatisticsRequirements
 } from './selection-strategies-types'
 
 /**
@@ -40,10 +37,6 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy<
    * Round id.
    */
   private roundId: number = 0
-  /**
-   * Default worker weight.
-   */
-  private readonly defaultWorkerWeight: number
   /**
    * Round weights.
    */
@@ -60,11 +53,10 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy<
   /** @inheritDoc */
   public constructor (
     pool: IPool<Worker, Data, Response>,
-    opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
+    opts: InternalWorkerChoiceStrategyOptions
   ) {
     super(pool, opts)
     this.setTaskStatisticsRequirements(this.opts)
-    this.defaultWorkerWeight = this.computeDefaultWorkerWeight()
     this.roundWeights = this.getRoundWeights()
   }
 
@@ -102,8 +94,7 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy<
         ) {
           this.workerNodeVirtualTaskRunTime = 0
         }
-        const workerWeight =
-          this.opts.weights?.[workerNodeKey] ?? this.defaultWorkerWeight
+        const workerWeight = this.opts.weights?.[workerNodeKey] as number
         if (
           this.isWorkerNodeReady(workerNodeKey) &&
           workerWeight >= this.roundWeights[roundIndex] &&
@@ -122,20 +113,18 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy<
   }
 
   private interleavedWeightedRoundRobinNextWorkerNodeId (): void {
-    do {
-      if (
-        this.roundId === this.roundWeights.length - 1 &&
-        this.workerNodeId === this.pool.workerNodes.length - 1
-      ) {
-        this.roundId = 0
-        this.workerNodeId = 0
-      } else if (this.workerNodeId === this.pool.workerNodes.length - 1) {
-        this.roundId = this.roundId + 1
-        this.workerNodeId = 0
-      } else {
-        this.workerNodeId = this.workerNodeId + 1
-      }
-    } while (!this.isWorkerNodeReady(this.workerNodeId))
+    if (
+      this.roundId === this.roundWeights.length - 1 &&
+      this.workerNodeId === this.pool.workerNodes.length - 1
+    ) {
+      this.roundId = 0
+      this.workerNodeId = 0
+    } else if (this.workerNodeId === this.pool.workerNodes.length - 1) {
+      this.roundId = this.roundId + 1
+      this.workerNodeId = 0
+    } else {
+      this.workerNodeId = this.workerNodeId + 1
+    }
   }
 
   /** @inheritDoc */
@@ -159,18 +148,15 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy<
   }
 
   /** @inheritDoc */
-  public setOptions (opts: WorkerChoiceStrategyOptions): void {
+  public setOptions (opts: InternalWorkerChoiceStrategyOptions): void {
     super.setOptions(opts)
     this.roundWeights = this.getRoundWeights()
   }
 
   private getRoundWeights (): number[] {
-    if (this.opts.weights == null) {
-      return [this.defaultWorkerWeight]
-    }
     return [
       ...new Set(
-        Object.values(this.opts.weights)
+        Object.values(this.opts.weights as Record<number, number>)
           .slice()
           .sort((a, b) => a - b)
       )