fix: fix fair share algorithm implementation
[poolifier.git] / src / pools / selection-strategies / less-busy-worker-choice-strategy.ts
index 12e87ff2575649c6c1a9847d8dd24a9ad1e30f9a..40a3f649b2116eec3f25b4b07347a8c5134b2e77 100644 (file)
@@ -1,8 +1,11 @@
+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 {
   IWorkerChoiceStrategy,
-  RequiredStatistics
+  RequiredStatistics,
+  WorkerChoiceStrategyOptions
 } from './selection-strategies-types'
 
 /**
@@ -26,33 +29,47 @@ export class LessBusyWorkerChoiceStrategy<
     medRunTime: false
   }
 
+  /** @inheritDoc */
+  public constructor (
+    pool: IPool<Worker, Data, Response>,
+    opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
+  ) {
+    super(pool, opts)
+    this.checkOptions(this.opts)
+  }
+
   /** @inheritDoc */
   public reset (): boolean {
     return true
   }
 
+  /** @inheritDoc */
+  public update (): boolean {
+    return true
+  }
+
   /** @inheritDoc */
   public choose (): number {
-    const freeWorkerNodeKey = this.pool.findFreeWorkerNodeKey()
+    const freeWorkerNodeKey = this.findFreeWorkerNodeKey()
     if (freeWorkerNodeKey !== -1) {
       return freeWorkerNodeKey
     }
     let minRunTime = Infinity
     let lessBusyWorkerNodeKey!: number
-    for (const [index, workerNode] of this.pool.workerNodes.entries()) {
+    for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) {
       const workerRunTime = workerNode.tasksUsage.runTime
       if (workerRunTime === 0) {
-        return index
+        return workerNodeKey
       } else if (workerRunTime < minRunTime) {
         minRunTime = workerRunTime
-        lessBusyWorkerNodeKey = index
+        lessBusyWorkerNodeKey = workerNodeKey
       }
     }
     return lessBusyWorkerNodeKey
   }
 
   /** @inheritDoc */
-  public remove (workerNodeKey: number): boolean {
+  public remove (): boolean {
     return true
   }
 }