refactor: move back pressure event emission in the helper
[poolifier.git] / src / pools / selection-strategies / least-used-worker-choice-strategy.ts
index 910d10a3ac320db8f3feac42eb43293181a806d1..e72efda2eb7db69411cc324e2e003c64fa08f529 100644 (file)
@@ -42,8 +42,7 @@ export class LeastUsedWorkerChoiceStrategy<
 
   /** @inheritDoc */
   public choose (): number {
-    this.leastUsedNextWorkerNodeKey()
-    return this.nextWorkerNodeKey
+    return this.leastUsedNextWorkerNodeKey()
   }
 
   /** @inheritDoc */
@@ -51,7 +50,7 @@ export class LeastUsedWorkerChoiceStrategy<
     return true
   }
 
-  private leastUsedNextWorkerNodeKey (): void {
+  private leastUsedNextWorkerNodeKey (): number {
     let minNumberOfTasks = Infinity
     for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) {
       const workerTaskStatistics = workerNode.usage.tasks
@@ -59,16 +58,17 @@ export class LeastUsedWorkerChoiceStrategy<
         workerTaskStatistics.executed +
         workerTaskStatistics.executing +
         workerTaskStatistics.queued
-      if (this.isWorkerNodeReady(workerNodeKey) && workerTasks === 0) {
+      if (this.isWorkerNodeEligible(workerNodeKey) && workerTasks === 0) {
         this.nextWorkerNodeKey = workerNodeKey
         break
       } else if (
-        this.isWorkerNodeReady(workerNodeKey) &&
+        this.isWorkerNodeEligible(workerNodeKey) &&
         workerTasks < minNumberOfTasks
       ) {
         minNumberOfTasks = workerTasks
         this.nextWorkerNodeKey = workerNodeKey
       }
     }
+    return this.nextWorkerNodeKey
   }
 }