refactor: simplify chooseWorkerNode() signature
authorJérôme Benoit <jerome.benoit@sap.com>
Thu, 11 May 2023 07:43:21 +0000 (09:43 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Thu, 11 May 2023 07:43:21 +0000 (09:43 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/pools/abstract-pool.ts
tests/pools/selection-strategies/selection-strategies.test.js

index b9a7cd398920fc77a754d3d4047414beae94ba46..4c82ef08a8cdad70602ebf20a64235b1cb3833f0 100644 (file)
@@ -340,7 +340,7 @@ export abstract class AbstractPool<
 
   /** @inheritDoc */
   public async execute (data?: Data, name?: string): Promise<Response> {
-    const [workerNodeKey, workerNode] = this.chooseWorkerNode()
+    const workerNodeKey = this.chooseWorkerNode()
     const submittedTask: Task<Data> = {
       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<Worker, Data>] {
+  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
   }
 
   /**
index 1ecd270fcce7915d6d0c75d325d35ed3ffd4dd96..11a9aab3caa2001b799d5a8e48526481cef7da19 100644 (file)
@@ -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()