fix: ensure worker key can't be negative in worker choice strategies
[poolifier.git] / src / pools / selection-strategies / round-robin-worker-choice-strategy.ts
index 9b49d7643581d8a4991bc1b25239b2053e0265d2..91ceb5bc9ade9e8d8df8be244c54fd105b76771a 100644 (file)
@@ -40,10 +40,14 @@ export class RoundRobinWorkerChoiceStrategy<
   /** {@inheritDoc} */
   public remove (workerKey: number): boolean {
     if (this.nextWorkerId === workerKey) {
-      this.nextWorkerId =
-        this.nextWorkerId > this.pool.workers.length - 1
-          ? this.pool.workers.length - 1
-          : this.nextWorkerId
+      if (this.pool.workers.length === 0) {
+        this.nextWorkerId = 0
+      } else {
+        this.nextWorkerId =
+          this.nextWorkerId > this.pool.workers.length - 1
+            ? this.pool.workers.length - 1
+            : this.nextWorkerId
+      }
     }
     return true
   }