fix: ensure worker key can't be negative in worker choice strategies
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 5 Apr 2023 22:14:49 +0000 (00:14 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 5 Apr 2023 22:14:49 +0000 (00:14 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
CHANGELOG.md
src/pools/selection-strategies/round-robin-worker-choice-strategy.ts
src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts

index 7db1df47546ab30eddcffc9cc79e575ecd6623b2..5a152e22eab6eb0559ecff3660509bd7ab1e94b8 100644 (file)
@@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 - Add `full` event to dynamic pool.
 - Keep worker choice strategy usage in memory for conditional reuse.
 
+### Fixed
+
+- Fix possible negative worker key at worker removal in worker choice strategies.
+
 ## [2.4.1] - 2023-04-05
 
 ### Changed
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
   }
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) {