Merge pull request #2164 from poolifier/dependabot/npm_and_yarn/tatami-ng-0.3.0
[poolifier.git] / src / pools / selection-strategies / least-used-worker-choice-strategy.ts
index 515b4e935db6e33fd5f3946c4f1b7cb0eb52a5f0..e45d57549580b883c1f6a185dc7701b861004336 100644 (file)
@@ -1,11 +1,10 @@
-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 type { IWorker } from '../worker.js'
+import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy.js'
 import type {
   IWorkerChoiceStrategy,
   WorkerChoiceStrategyOptions
-} from './selection-strategies-types'
+} from './selection-strategies-types.js'
 
 /**
  * Selects the least used worker.
@@ -24,10 +23,9 @@ export class LeastUsedWorkerChoiceStrategy<
   /** @inheritDoc */
   public constructor (
     pool: IPool<Worker, Data, Response>,
-    opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
+    opts?: WorkerChoiceStrategyOptions
   ) {
     super(pool, opts)
-    this.setTaskStatisticsRequirements(this.opts)
   }
 
   /** @inheritDoc */
@@ -42,8 +40,8 @@ export class LeastUsedWorkerChoiceStrategy<
 
   /** @inheritDoc */
   public choose (): number | undefined {
-    const chosenWorkerNodeKey = this.leastUsedNextWorkerNodeKey()
-    this.assignChosenWorkerNodeKey(chosenWorkerNodeKey)
+    this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey)
+    this.nextWorkerNodeKey = this.leastUsedNextWorkerNodeKey()
     return this.nextWorkerNodeKey
   }
 
@@ -53,25 +51,22 @@ export class LeastUsedWorkerChoiceStrategy<
   }
 
   private leastUsedNextWorkerNodeKey (): number | undefined {
-    let minNumberOfTasks = Infinity
-    let chosenWorkerNodeKey: number | undefined
-    for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) {
-      const workerTaskStatistics = workerNode.usage.tasks
-      const workerTasks =
-        workerTaskStatistics.executed +
-        workerTaskStatistics.executing +
-        workerTaskStatistics.queued
-      if (this.isWorkerNodeEligible(workerNodeKey) && workerTasks === 0) {
-        chosenWorkerNodeKey = workerNodeKey
-        break
-      } else if (
-        this.isWorkerNodeEligible(workerNodeKey) &&
-        workerTasks < minNumberOfTasks
-      ) {
-        minNumberOfTasks = workerTasks
-        chosenWorkerNodeKey = workerNodeKey
-      }
+    if (this.pool.workerNodes.length === 0) {
+      return undefined
     }
-    return chosenWorkerNodeKey
+    return this.pool.workerNodes.reduce(
+      (minWorkerNodeKey, workerNode, workerNodeKey, workerNodes) => {
+        return this.isWorkerNodeReady(workerNodeKey) &&
+          workerNode.usage.tasks.executed +
+            workerNode.usage.tasks.executing +
+            workerNode.usage.tasks.queued <
+            workerNodes[minWorkerNodeKey].usage.tasks.executed +
+              workerNodes[minWorkerNodeKey].usage.tasks.executing +
+              workerNodes[minWorkerNodeKey].usage.tasks.queued
+          ? workerNodeKey
+          : minWorkerNodeKey
+      },
+      0
+    )
   }
 }