From 20dcad1a0f8c1c6fdc0623aed98a998e8a31f185 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 11 May 2023 09:43:21 +0200 Subject: [PATCH] refactor: simplify chooseWorkerNode() signature MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/pools/abstract-pool.ts | 12 ++++++------ .../selection-strategies.test.js | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index b9a7cd39..4c82ef08 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -340,7 +340,7 @@ export abstract class AbstractPool< /** @inheritDoc */ public async execute (data?: Data, name?: string): Promise { - const [workerNodeKey, workerNode] = this.chooseWorkerNode() + const workerNodeKey = this.chooseWorkerNode() const submittedTask: Task = { name, // eslint-disable-next-line @typescript-eslint/consistent-type-assertions @@ -351,7 +351,7 @@ export abstract class AbstractPool< this.promiseResponseMap.set(submittedTask.id as string, { resolve, reject, - worker: workerNode.worker + worker: this.workerNodes[workerNodeKey].worker }) }) if ( @@ -453,11 +453,11 @@ export abstract class AbstractPool< /** * Chooses a worker node for the next task. * - * The default uses a round robin algorithm to distribute the load. + * The default worker choice strategy uses a round robin algorithm to distribute the load. * - * @returns [worker node key, worker node]. + * @returns The worker node key */ - protected chooseWorkerNode (): [number, WorkerNode] { + protected chooseWorkerNode (): number { let workerNodeKey: number if (this.type === PoolType.DYNAMIC && !this.full && this.internalBusy()) { const workerCreated = this.createAndSetupWorker() @@ -477,7 +477,7 @@ export abstract class AbstractPool< } else { workerNodeKey = this.workerChoiceStrategyContext.execute() } - return [workerNodeKey, this.workerNodes[workerNodeKey]] + return workerNodeKey } /** diff --git a/tests/pools/selection-strategies/selection-strategies.test.js b/tests/pools/selection-strategies/selection-strategies.test.js index 1ecd270f..11a9aab3 100644 --- a/tests/pools/selection-strategies/selection-strategies.test.js +++ b/tests/pools/selection-strategies/selection-strategies.test.js @@ -186,7 +186,7 @@ describe('Selection strategies test suite', () => { ) let results = new Set() for (let i = 0; i < max; i++) { - results.add(pool.chooseWorkerNode()[1].worker.id) + results.add(pool.workerNodes[pool.chooseWorkerNode()].worker.id) } expect(results.size).toBe(max) await pool.destroy() @@ -197,7 +197,7 @@ describe('Selection strategies test suite', () => { ) results = new Set() for (let i = 0; i < max; i++) { - results.add(pool.chooseWorkerNode()[1].worker.threadId) + results.add(pool.workerNodes[pool.chooseWorkerNode()].worker.threadId) } expect(results.size).toBe(max) await pool.destroy() -- 2.34.1