From 965415bb43f5c76329f0854ed680e22068e3affb Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 5 Apr 2023 00:47:47 +0200 Subject: [PATCH] fix: only pickup a free worker with dynamic pool if the worker selection strategy says so MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- CHANGELOG.md | 4 ++++ benchmarks/benchmarks-utils.js | 3 +-- .../dynamic-pool-worker-choice-strategy.ts | 13 +++++-------- .../less-busy-worker-choice-strategy.ts | 2 +- .../less-used-worker-choice-strategy.ts | 2 +- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f32625e..f1b026dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/benchmarks/benchmarks-utils.js b/benchmarks/benchmarks-utils.js index c04162e1..e6abe96d 100644 --- a/benchmarks/benchmarks-utils.js +++ b/benchmarks/benchmarks-utils.js @@ -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) { diff --git a/src/pools/selection-strategies/dynamic-pool-worker-choice-strategy.ts b/src/pools/selection-strategies/dynamic-pool-worker-choice-strategy.ts index 54c95329..c0622613 100644 --- a/src/pools/selection-strategies/dynamic-pool-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/dynamic-pool-worker-choice-strategy.ts @@ -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} */ diff --git a/src/pools/selection-strategies/less-busy-worker-choice-strategy.ts b/src/pools/selection-strategies/less-busy-worker-choice-strategy.ts index 87ee1ef8..1d8bbaab 100644 --- a/src/pools/selection-strategies/less-busy-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/less-busy-worker-choice-strategy.ts @@ -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 diff --git a/src/pools/selection-strategies/less-used-worker-choice-strategy.ts b/src/pools/selection-strategies/less-used-worker-choice-strategy.ts index c83a012b..3eeb8329 100644 --- a/src/pools/selection-strategies/less-used-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/less-used-worker-choice-strategy.ts @@ -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 -- 2.34.1