build(deps-dev): bump typescript
[poolifier.git] / src / pools / selection-strategies / least-busy-worker-choice-strategy.ts
index 31fa281aa75dd0dcbf63fffe1d2508f0277d3f6a..8db32ac7f1421330e36d9bfb81bddf53392a666c 100644 (file)
@@ -1,12 +1,12 @@
-import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
-import type { IPool } from '../pool'
-import type { IWorker } from '../worker'
-import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
+import type { IPool } from '../pool.js'
+import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } from '../utils.js'
+import type { IWorker } from '../worker.js'
+import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy.js'
 import type {
   IWorkerChoiceStrategy,
   TaskStatisticsRequirements,
   WorkerChoiceStrategyOptions
-} from './selection-strategies-types'
+} from './selection-strategies-types.js'
 
 /**
  * Selects the least busy worker.
@@ -34,17 +34,13 @@ export class LeastBusyWorkerChoiceStrategy<
       average: false,
       median: false
     },
-    elu: {
-      aggregate: false,
-      average: false,
-      median: false
-    }
+    elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
   }
 
   /** @inheritDoc */
   public constructor (
     pool: IPool<Worker, Data, Response>,
-    opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
+    opts?: WorkerChoiceStrategyOptions
   ) {
     super(pool, opts)
     this.setTaskStatisticsRequirements(this.opts)
@@ -61,25 +57,32 @@ export class LeastBusyWorkerChoiceStrategy<
   }
 
   /** @inheritDoc */
-  public choose (): number {
-    let minTime = Infinity
-    for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) {
-      const workerTime =
-        workerNode.workerUsage.runTime.aggregate +
-        workerNode.workerUsage.waitTime.aggregate
-      if (workerTime === 0) {
-        this.nextWorkerNodeId = workerNodeKey
-        break
-      } else if (workerTime < minTime) {
-        minTime = workerTime
-        this.nextWorkerNodeId = workerNodeKey
-      }
-    }
-    return this.nextWorkerNodeId
+  public choose (): number | undefined {
+    this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey)
+    this.nextWorkerNodeKey = this.leastBusyNextWorkerNodeKey()
+    return this.nextWorkerNodeKey
   }
 
   /** @inheritDoc */
   public remove (): boolean {
     return true
   }
+
+  private leastBusyNextWorkerNodeKey (): number | undefined {
+    if (this.pool.workerNodes.length === 0) {
+      return undefined
+    }
+    return this.pool.workerNodes.reduce(
+      (minWorkerNodeKey, workerNode, workerNodeKey, workerNodes) => {
+        return this.isWorkerNodeReady(workerNodeKey) &&
+          (workerNode.usage.runTime.aggregate ?? 0) +
+            (workerNode.usage.waitTime.aggregate ?? 0) <
+            (workerNodes[minWorkerNodeKey].usage.runTime.aggregate ?? 0) +
+              (workerNodes[minWorkerNodeKey].usage.waitTime.aggregate ?? 0)
+          ? workerNodeKey
+          : minWorkerNodeKey
+      },
+      0
+    )
+  }
 }