chore: v2.4.0-3
[poolifier.git] / src / pools / selection-strategies / dynamic-pool-worker-choice-strategy.ts
index f98ec5ef09e78eafe2b033eb54f13460a5917241..54c95329ff67d7e948b23df7272b70b039dfa884 100644 (file)
@@ -1,51 +1,59 @@
-import type { AbstractPoolWorker } from '../abstract-pool-worker'
 import type { IPoolInternal } from '../pool-internal'
+import type { IPoolWorker } from '../pool-worker'
 import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
 import type {
   IWorkerChoiceStrategy,
   WorkerChoiceStrategy
 } from './selection-strategies-types'
 import { WorkerChoiceStrategies } from './selection-strategies-types'
-import { SelectionStrategiesUtils } from './selection-strategies-utils'
+import { getWorkerChoiceStrategy } from './selection-strategies-utils'
 
 /**
- * Dynamically choose a worker.
+ * Selects the next worker for dynamic pool.
  *
- * @template Worker Type of worker which manages the strategy.
- * @template Data Type of data sent to the worker. This can only be serializable data.
- * @template Response Type of response of execution. This can only be serializable data.
+ * @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.
  */
 export class DynamicPoolWorkerChoiceStrategy<
-  Worker extends AbstractPoolWorker,
-  Data,
-  Response
-> extends AbstractWorkerChoiceStrategy<Worker, Data, Response> {
-  private workerChoiceStrategy: IWorkerChoiceStrategy<Worker>
+    Worker extends IPoolWorker,
+    Data,
+    Response
+  >
+  extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
+  implements IWorkerChoiceStrategy {
+  private readonly workerChoiceStrategy: IWorkerChoiceStrategy
 
   /**
-   * Constructs a worker choice strategy for dynamical pool.
+   * Constructs a worker choice strategy for dynamic pool.
    *
-   * @param pool The pool instance.
-   * @param createDynamicallyWorkerCallback The worker creation callback for dynamic pool.
-   * @param workerChoiceStrategy The worker choice strategy when the pull is busy.
+   * @param pool The pool instance.
+   * @param createWorkerCallback - The worker creation callback for dynamic pool.
+   * @param workerChoiceStrategy - The worker choice strategy when the pool is busy.
    */
   public constructor (
     pool: IPoolInternal<Worker, Data, Response>,
-    private createDynamicallyWorkerCallback: () => Worker,
+    private readonly createWorkerCallback: () => number,
     workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
   ) {
     super(pool)
-    this.workerChoiceStrategy = SelectionStrategiesUtils.getWorkerChoiceStrategy(
+    this.workerChoiceStrategy = getWorkerChoiceStrategy(
       this.pool,
       workerChoiceStrategy
     )
+    this.requiredStatistics = this.workerChoiceStrategy.requiredStatistics
   }
 
-  /** @inheritdoc */
-  public choose (): Worker {
-    const freeWorker = this.pool.findFreeWorker()
-    if (freeWorker) {
-      return freeWorker
+  /** {@inheritDoc} */
+  public reset (): boolean {
+    return this.workerChoiceStrategy.reset()
+  }
+
+  /** {@inheritDoc} */
+  public choose (): number {
+    const freeWorkerKey = this.pool.findFreeWorkerKey()
+    if (freeWorkerKey !== -1) {
+      return freeWorkerKey
     }
 
     if (this.pool.busy) {
@@ -53,6 +61,11 @@ export class DynamicPoolWorkerChoiceStrategy<
     }
 
     // All workers are busy, create a new worker
-    return this.createDynamicallyWorkerCallback()
+    return this.createWorkerCallback()
+  }
+
+  /** {@inheritDoc} */
+  public remove (workerKey: number): boolean {
+    return this.workerChoiceStrategy.remove(workerKey)
   }
 }