perf: optimize worker choice strategies
[poolifier.git] / src / pools / selection-strategies / weighted-round-robin-worker-choice-strategy.ts
index 618b5065e01bd33a91fc9a170388ee063f2e8fcb..fc6f48cf2632a89e0fa1de1afe198ff37eb2cdb7 100644 (file)
@@ -48,10 +48,6 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
     }
   }
 
-  /**
-   * Worker node id where the current task will be submitted.
-   */
-  private currentWorkerNodeId: number = 0
   /**
    * Default worker weight.
    */
@@ -73,7 +69,7 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
 
   /** @inheritDoc */
   public reset (): boolean {
-    this.currentWorkerNodeId = 0
+    this.nextWorkerNodeId = 0
     this.workerVirtualTaskRunTime = 0
     return true
   }
@@ -85,7 +81,7 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
 
   /** @inheritDoc */
   public choose (): number {
-    const chosenWorkerNodeKey = this.currentWorkerNodeId
+    const chosenWorkerNodeKey = this.nextWorkerNodeId
     const workerVirtualTaskRunTime = this.workerVirtualTaskRunTime
     const workerWeight =
       this.opts.weights?.[chosenWorkerNodeKey] ?? this.defaultWorkerWeight
@@ -94,10 +90,10 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
         workerVirtualTaskRunTime +
         this.getWorkerTaskRunTime(chosenWorkerNodeKey)
     } else {
-      this.currentWorkerNodeId =
-        this.currentWorkerNodeId === this.pool.workerNodes.length - 1
+      this.nextWorkerNodeId =
+        this.nextWorkerNodeId === this.pool.workerNodes.length - 1
           ? 0
-          : this.currentWorkerNodeId + 1
+          : this.nextWorkerNodeId + 1
       this.workerVirtualTaskRunTime = 0
     }
     return chosenWorkerNodeKey
@@ -105,11 +101,11 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
 
   /** @inheritDoc */
   public remove (workerNodeKey: number): boolean {
-    if (this.currentWorkerNodeId === workerNodeKey) {
+    if (this.nextWorkerNodeId === workerNodeKey) {
       if (this.pool.workerNodes.length === 0) {
-        this.currentWorkerNodeId = 0
-      } else if (this.currentWorkerNodeId > this.pool.workerNodes.length - 1) {
-        this.currentWorkerNodeId = this.pool.workerNodes.length - 1
+        this.nextWorkerNodeId = 0
+      } else if (this.nextWorkerNodeId > this.pool.workerNodes.length - 1) {
+        this.nextWorkerNodeId = this.pool.workerNodes.length - 1
       }
       this.workerVirtualTaskRunTime = 0
     }