fix: fix fair share algorithm implementation
[poolifier.git] / src / pools / selection-strategies / less-busy-worker-choice-strategy.ts
index 87ef804d50881f157e2810a715c2db4627315f33..40a3f649b2116eec3f25b4b07347a8c5134b2e77 100644 (file)
@@ -1,8 +1,11 @@
-import type { IPoolWorker } from '../pool-worker'
+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'
 
 /**
@@ -10,10 +13,10 @@ import type {
  *
  * @typeParam Worker - Type of worker which manages the strategy.
  * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
- * @typeParam Response - Type of response of execution. This can only be serializable data.
+ * @typeParam Response - Type of execution response. This can only be serializable data.
  */
 export class LessBusyWorkerChoiceStrategy<
-    Worker extends IPoolWorker,
+    Worker extends IWorker,
     Data = unknown,
     Response = unknown
   >
@@ -22,7 +25,17 @@ export class LessBusyWorkerChoiceStrategy<
   /** @inheritDoc */
   public readonly requiredStatistics: RequiredStatistics = {
     runTime: true,
-    avgRunTime: false
+    avgRunTime: false,
+    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 */
@@ -30,28 +43,33 @@ export class LessBusyWorkerChoiceStrategy<
     return true
   }
 
+  /** @inheritDoc */
+  public update (): boolean {
+    return true
+  }
+
   /** @inheritDoc */
   public choose (): number {
-    const freeWorkerKey = this.pool.findFreeWorkerKey()
-    if (freeWorkerKey !== -1) {
-      return freeWorkerKey
+    const freeWorkerNodeKey = this.findFreeWorkerNodeKey()
+    if (freeWorkerNodeKey !== -1) {
+      return freeWorkerNodeKey
     }
     let minRunTime = Infinity
-    let lessBusyWorkerKey!: number
-    for (const [index, workerItem] of this.pool.workers.entries()) {
-      const workerRunTime = workerItem.tasksUsage.runTime
+    let lessBusyWorkerNodeKey!: number
+    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
-        lessBusyWorkerKey = index
+        lessBusyWorkerNodeKey = workerNodeKey
       }
     }
-    return lessBusyWorkerKey
+    return lessBusyWorkerNodeKey
   }
 
   /** @inheritDoc */
-  public remove (workerKey: number): boolean {
+  public remove (): boolean {
     return true
   }
 }