fix: fix dynamic pool busy semantic
[poolifier.git] / src / pools / abstract-pool.ts
index 172feda0555cac95e453860ebddda218e5a5755d..1e61071c42382e1ccb9d546cb0a035ac6196afe3 100644 (file)
@@ -73,6 +73,12 @@ export abstract class AbstractPool<
     this.checkNumberOfWorkers(this.numberOfWorkers)
     this.checkFilePath(this.filePath)
     this.checkPoolOptions(this.opts)
+
+    this.chooseWorker.bind(this)
+    this.internalExecute.bind(this)
+    this.checkAndEmitBusy.bind(this)
+    this.sendToWorker.bind(this)
+
     this.setupHook()
 
     for (let i = 1; i <= this.numberOfWorkers; i++) {
@@ -137,8 +143,10 @@ export abstract class AbstractPool<
   /** {@inheritDoc} */
   public abstract get type (): PoolType
 
-  /** {@inheritDoc} */
-  public get numberOfRunningTasks (): number {
+  /**
+   * Number of tasks concurrently running.
+   */
+  private get numberOfRunningTasks (): number {
     return this.promiseResponseMap.size
   }
 
@@ -146,7 +154,7 @@ export abstract class AbstractPool<
    * Gets the given worker key.
    *
    * @param worker - The worker.
-   * @returns The worker key.
+   * @returns The worker key if the worker is found in the pool, `-1` otherwise.
    */
   private getWorkerKey (worker: Worker): number {
     return this.workers.findIndex(workerItem => workerItem.worker === worker)
@@ -171,22 +179,24 @@ export abstract class AbstractPool<
     )
   }
 
+  /** {@inheritDoc} */
+  public abstract get full (): boolean
+
   /** {@inheritDoc} */
   public abstract get busy (): boolean
 
-  protected internalGetBusyStatus (): boolean {
+  protected internalBusy (): boolean {
     return (
       this.numberOfRunningTasks >= this.numberOfWorkers &&
-      this.findFreeWorkerKey() === false
+      this.findFreeWorkerKey() === -1
     )
   }
 
   /** {@inheritDoc} */
-  public findFreeWorkerKey (): number | false {
-    const freeWorkerKey = this.workers.findIndex(workerItem => {
+  public findFreeWorkerKey (): number {
+    return this.workers.findIndex(workerItem => {
       return workerItem.tasksUsage.running === 0
     })
-    return freeWorkerKey !== -1 ? freeWorkerKey : false
   }
 
   /** {@inheritDoc} */
@@ -260,12 +270,12 @@ export abstract class AbstractPool<
     if (message.error != null) {
       ++workerTasksUsage.error
     }
-    if (
-      this.workerChoiceStrategyContext.getWorkerChoiceStrategy()
-        .requiredStatistics.runTime
-    ) {
+    if (this.workerChoiceStrategyContext.getRequiredStatistics().runTime) {
       workerTasksUsage.runTime += message.taskRunTime ?? 0
-      if (workerTasksUsage.run !== 0) {
+      if (
+        this.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime &&
+        workerTasksUsage.run !== 0
+      ) {
         workerTasksUsage.avgRunTime =
           workerTasksUsage.runTime / workerTasksUsage.run
       }
@@ -278,7 +288,9 @@ export abstract class AbstractPool<
    * @param worker - The worker that will be removed.
    */
   protected removeWorker (worker: Worker): void {
-    this.workers.splice(this.getWorkerKey(worker), 1)
+    const workerKey = this.getWorkerKey(worker)
+    this.workers.splice(workerKey, 1)
+    this.workerChoiceStrategyContext.remove(workerKey)
   }
 
   /**