fix: ensure worker removal impact is propated to worker choice strategy
[poolifier.git] / src / pools / selection-strategies / weighted-round-robin-worker-choice-strategy.ts
index e3d2be3f3e1c20d158d70dc0e85943e59bfeac4c..24a25b4d30de7fa553f500615b5b5e8e06efd972 100644 (file)
@@ -93,6 +93,23 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
     return chosenWorkerKey
   }
 
+  /** {@inheritDoc} */
+  public remove (workerKey: number): boolean {
+    if (this.currentWorkerId === workerKey) {
+      this.currentWorkerId =
+        this.currentWorkerId > this.pool.workers.length - 1
+          ? this.pool.workers.length - 1
+          : this.currentWorkerId
+    }
+    const workerDeleted = this.workersTaskRunTime.delete(workerKey)
+    for (const [key, value] of this.workersTaskRunTime) {
+      if (key > workerKey) {
+        this.workersTaskRunTime.set(key - 1, value)
+      }
+    }
+    return workerDeleted
+  }
+
   private initWorkersTaskRunTime (): void {
     for (const [index] of this.pool.workers.entries()) {
       this.initWorkerTaskRunTime(index)