fix: ensure worker key can't be negative in worker choice strategies
[poolifier.git] / src / pools / selection-strategies / weighted-round-robin-worker-choice-strategy.ts
index c700c42a857a94962c40e6311f580781cb94fc30..e1325865eaba7df03f0d36b277599bd828be5b34 100644 (file)
@@ -102,10 +102,14 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
   /** {@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
+      if (this.pool.workers.length === 0) {
+        this.currentWorkerId = 0
+      } else {
+        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) {