feat: use O(1) queue implementation
[poolifier.git] / src / pools / selection-strategies / worker-choice-strategy-context.ts
index e763f7b2709c083afc81f7c7e53c074e9c59e7a8..2a487acbdfde0f2d07c2f9eae9233d26da387604 100644 (file)
@@ -1,4 +1,5 @@
-import type { IPoolInternal } from '../pool-internal'
+import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
+import type { IPool } from '../pool'
 import type { IWorker } from '../worker'
 import { FairShareWorkerChoiceStrategy } from './fair-share-worker-choice-strategy'
 import { LessBusyWorkerChoiceStrategy } from './less-busy-worker-choice-strategy'
@@ -18,7 +19,7 @@ import { WeightedRoundRobinWorkerChoiceStrategy } from './weighted-round-robin-w
  *
  * @typeParam Worker - Type of worker.
  * @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 WorkerChoiceStrategyContext<
   Worker extends IWorker,
@@ -34,41 +35,54 @@ export class WorkerChoiceStrategyContext<
    * Worker choice strategy context constructor.
    *
    * @param pool - The pool instance.
-   * @param workerChoiceStrategyType - The worker choice strategy.
+   * @param workerChoiceStrategy - The worker choice strategy.
    * @param opts - The worker choice strategy options.
    */
   public constructor (
-    pool: IPoolInternal<Worker, Data, Response>,
-    private workerChoiceStrategyType: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN,
-    opts: WorkerChoiceStrategyOptions = { medRunTime: false }
+    pool: IPool<Worker, Data, Response>,
+    private workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN,
+    opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
   ) {
-    this.execute.bind(this)
+    this.execute = this.execute.bind(this)
     this.workerChoiceStrategies = new Map<
     WorkerChoiceStrategy,
     IWorkerChoiceStrategy
     >([
       [
         WorkerChoiceStrategies.ROUND_ROBIN,
-        new RoundRobinWorkerChoiceStrategy<Worker, Data, Response>(pool, opts)
+        new (RoundRobinWorkerChoiceStrategy.bind(this))<Worker, Data, Response>(
+          pool,
+          opts
+        )
       ],
       [
         WorkerChoiceStrategies.LESS_USED,
-        new LessUsedWorkerChoiceStrategy<Worker, Data, Response>(pool, opts)
+        new (LessUsedWorkerChoiceStrategy.bind(this))<Worker, Data, Response>(
+          pool,
+          opts
+        )
       ],
       [
         WorkerChoiceStrategies.LESS_BUSY,
-        new LessBusyWorkerChoiceStrategy<Worker, Data, Response>(pool, opts)
+        new (LessBusyWorkerChoiceStrategy.bind(this))<Worker, Data, Response>(
+          pool,
+          opts
+        )
       ],
       [
         WorkerChoiceStrategies.FAIR_SHARE,
-        new FairShareWorkerChoiceStrategy<Worker, Data, Response>(pool, opts)
-      ],
-      [
-        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN,
-        new WeightedRoundRobinWorkerChoiceStrategy<Worker, Data, Response>(
+        new (FairShareWorkerChoiceStrategy.bind(this))<Worker, Data, Response>(
           pool,
           opts
         )
+      ],
+      [
+        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN,
+        new (WeightedRoundRobinWorkerChoiceStrategy.bind(this))<
+        Worker,
+        Data,
+        Response
+        >(pool, opts)
       ]
     ])
   }
@@ -81,7 +95,7 @@ export class WorkerChoiceStrategyContext<
   public getRequiredStatistics (): RequiredStatistics {
     return (
       this.workerChoiceStrategies.get(
-        this.workerChoiceStrategyType
+        this.workerChoiceStrategy
       ) as IWorkerChoiceStrategy
     ).requiredStatistics
   }
@@ -94,10 +108,10 @@ export class WorkerChoiceStrategyContext<
   public setWorkerChoiceStrategy (
     workerChoiceStrategy: WorkerChoiceStrategy
   ): void {
-    if (this.workerChoiceStrategyType !== workerChoiceStrategy) {
-      this.workerChoiceStrategyType = workerChoiceStrategy
+    if (this.workerChoiceStrategy !== workerChoiceStrategy) {
+      this.workerChoiceStrategy = workerChoiceStrategy
     }
-    this.workerChoiceStrategies.get(this.workerChoiceStrategyType)?.reset()
+    this.workerChoiceStrategies.get(this.workerChoiceStrategy)?.reset()
   }
 
   /**
@@ -108,7 +122,7 @@ export class WorkerChoiceStrategyContext<
   public execute (): number {
     return (
       this.workerChoiceStrategies.get(
-        this.workerChoiceStrategyType
+        this.workerChoiceStrategy
       ) as IWorkerChoiceStrategy
     ).choose()
   }
@@ -122,8 +136,19 @@ export class WorkerChoiceStrategyContext<
   public remove (workerNodeKey: number): boolean {
     return (
       this.workerChoiceStrategies.get(
-        this.workerChoiceStrategyType
+        this.workerChoiceStrategy
       ) as IWorkerChoiceStrategy
     ).remove(workerNodeKey)
   }
+
+  /**
+   * Sets the worker choice strategies in the context options.
+   *
+   * @param opts - The worker choice strategy options.
+   */
+  public setOptions (opts: WorkerChoiceStrategyOptions): void {
+    this.workerChoiceStrategies.forEach(workerChoiceStrategy => {
+      workerChoiceStrategy.setOptions(opts)
+    })
+  }
 }