fix: only pickup a free worker with dynamic pool if the worker selection
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 4 Apr 2023 22:47:47 +0000 (00:47 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 4 Apr 2023 22:47:47 +0000 (00:47 +0200)
strategy says so

Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
CHANGELOG.md
benchmarks/benchmarks-utils.js
src/pools/selection-strategies/dynamic-pool-worker-choice-strategy.ts
src/pools/selection-strategies/less-busy-worker-choice-strategy.ts
src/pools/selection-strategies/less-used-worker-choice-strategy.ts

index 0f32625e7009ecfa14291d54c377599c7276105d..f1b026dc34431a93d0b3500b2a8ca92db59ea1f9 100644 (file)
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ## [Unreleased]
 
+### Fixed
+
+- Ensure dynamic pool does not alter worker choice strategy expected behavior.
+
 ## [2.4.0] - 2023-04-04
 
 ### Added
index c04162e1a617d704708b56496040640947e22171..e6abe96d5b1e065c07b709bd83c37095e9996935 100644 (file)
@@ -62,9 +62,8 @@ function fibonacci (n) {
 function factorial (n) {
   if (n === 0) {
     return 1
-  } else {
-    return factorial(n - 1) * n
   }
+  return factorial(n - 1) * n
 }
 
 function executeWorkerFunction (data) {
index 54c95329ff67d7e948b23df7272b70b039dfa884..c0622613d8701c02360a04c7ab91abe10aff33d3 100644 (file)
@@ -51,17 +51,14 @@ export class DynamicPoolWorkerChoiceStrategy<
 
   /** {@inheritDoc} */
   public choose (): number {
-    const freeWorkerKey = this.pool.findFreeWorkerKey()
-    if (freeWorkerKey !== -1) {
-      return freeWorkerKey
-    }
-
     if (this.pool.busy) {
       return this.workerChoiceStrategy.choose()
     }
-
-    // All workers are busy, create a new worker
-    return this.createWorkerCallback()
+    const freeWorkerKey = this.pool.findFreeWorkerKey()
+    if (freeWorkerKey === -1) {
+      return this.createWorkerCallback()
+    }
+    return this.workerChoiceStrategy.choose()
   }
 
   /** {@inheritDoc} */
index 87ee1ef88f16714d4271a5ba671686d122667f35..1d8bbaab5e8900af0187f0b938ab7c57b6d873b1 100644 (file)
@@ -33,7 +33,7 @@ export class LessBusyWorkerChoiceStrategy<
   /** {@inheritDoc} */
   public choose (): number {
     const freeWorkerKey = this.pool.findFreeWorkerKey()
-    if (!this.isDynamicPool && freeWorkerKey !== -1) {
+    if (freeWorkerKey !== -1) {
       return freeWorkerKey
     }
     let minRunTime = Infinity
index c83a012b313eaa80cee0999bb49c200cce0ad769..3eeb8329aebd14897508c745b3e21fa40499457c 100644 (file)
@@ -24,7 +24,7 @@ export class LessUsedWorkerChoiceStrategy<
   /** {@inheritDoc} */
   public choose (): number {
     const freeWorkerKey = this.pool.findFreeWorkerKey()
-    if (!this.isDynamicPool && freeWorkerKey !== -1) {
+    if (freeWorkerKey !== -1) {
       return freeWorkerKey
     }
     let minNumberOfTasks = Infinity