From: Jérôme Benoit Date: Thu, 11 May 2023 07:43:21 +0000 (+0200) Subject: refactor: simplify chooseWorkerNode() signature X-Git-Tag: v2.5.0~46 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=20dcad1a0f8c1c6fdc0623aed98a998e8a31f185;p=poolifier.git refactor: simplify chooseWorkerNode() signature Signed-off-by: Jérôme Benoit --- 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()