chore: v2.6.15
[poolifier.git] / src / pools / abstract-pool.ts
index a3b46cee0f52e0be63827488730e671c1f316a98..b43d44a59eea1cb1c7990e660b96cb4cd5483a03 100644 (file)
@@ -128,7 +128,13 @@ export abstract class AbstractPool<
 
     this.setupHook()
 
-    while (this.workerNodes.length < this.numberOfWorkers) {
+    while (
+      this.workerNodes.reduce(
+        (accumulator, workerNode) =>
+          !workerNode.info.dynamic ? accumulator + 1 : accumulator,
+        0
+      ) < this.numberOfWorkers
+    ) {
       this.createAndSetupWorker()
     }
 
@@ -172,9 +178,9 @@ export abstract class AbstractPool<
         throw new RangeError(
           'Cannot instantiate a dynamic pool with a maximum pool size inferior to the minimum pool size'
         )
-      } else if (min === 0 && max === 0) {
+      } else if (max === 0) {
         throw new RangeError(
-          'Cannot instantiate a dynamic pool with a minimum pool size and a maximum pool size equal to zero'
+          'Cannot instantiate a dynamic pool with a pool size equal to zero'
         )
       } else if (min === max) {
         throw new RangeError(
@@ -411,16 +417,24 @@ export abstract class AbstractPool<
   }
 
   private get starting (): boolean {
-    return this.workerNodes.length < this.minSize
+    return (
+      this.workerNodes.reduce(
+        (accumulator, workerNode) =>
+          !workerNode.info.dynamic ? accumulator + 1 : accumulator,
+        0
+      ) < this.minSize
+    )
   }
 
   private get ready (): boolean {
     return (
-      this.workerNodes.length >= this.minSize &&
-      this.workerNodes.every(
-        (workerNode, workerNodeKey) =>
-          workerNodeKey < this.minSize && workerNode.info.ready
-      )
+      this.workerNodes.reduce(
+        (accumulator, workerNode) =>
+          !workerNode.info.dynamic && workerNode.info.ready
+            ? accumulator + 1
+            : accumulator,
+        0
+      ) >= this.minSize
     )
   }
 
@@ -949,7 +963,7 @@ export abstract class AbstractPool<
       this.removeWorkerNode(worker)
     })
 
-    this.pushWorkerNode(worker)
+    this.addWorkerNode(worker)
 
     this.afterWorkerSetup(worker)
 
@@ -979,10 +993,12 @@ export abstract class AbstractPool<
       }
     })
     const workerInfo = this.getWorkerInfo(this.getWorkerNodeKey(worker))
-    workerInfo.ready = true
     workerInfo.dynamic = true
+    if (this.workerChoiceStrategyContext.getStrategyPolicy().useDynamicWorker) {
+      workerInfo.ready = true
+    }
     this.sendToWorker(worker, {
-      checkAlive: true,
+      checkActive: true,
       workerId: workerInfo.id as number
     })
     return worker
@@ -1062,8 +1078,8 @@ export abstract class AbstractPool<
     return message => {
       this.checkMessageWorkerId(message)
       if (message.ready != null) {
-        // Worker ready message received
-        this.handleWorkerReadyMessage(message)
+        // Worker ready response received
+        this.handleWorkerReadyResponse(message)
       } else if (message.id != null) {
         // Task execution response received
         this.handleTaskExecutionResponse(message)
@@ -1071,7 +1087,7 @@ export abstract class AbstractPool<
     }
   }
 
-  private handleWorkerReadyMessage (message: MessageValue<Response>): void {
+  private handleWorkerReadyResponse (message: MessageValue<Response>): void {
     const worker = this.getWorkerById(message.workerId)
     this.getWorkerInfo(this.getWorkerNodeKey(worker as Worker)).ready =
       message.ready as boolean
@@ -1128,14 +1144,14 @@ export abstract class AbstractPool<
   }
 
   /**
-   * Pushes the given worker in the pool worker nodes.
+   * Adds the given worker in the pool worker nodes.
    *
    * @param worker - The worker.
    * @returns The worker nodes length.
    */
-  private pushWorkerNode (worker: Worker): number {
+  private addWorkerNode (worker: Worker): number {
     const workerNode = new WorkerNode<Worker, Data>(worker, this.worker)
-    // Flag the worker as ready at pool startup.
+    // Flag the worker node as ready at pool startup.
     if (this.starting) {
       workerNode.info.ready = true
     }
@@ -1155,6 +1171,12 @@ export abstract class AbstractPool<
     }
   }
 
+  /**
+   * Executes the given task on the given worker.
+   *
+   * @param worker - The worker.
+   * @param task - The task to execute.
+   */
   private executeTask (workerNodeKey: number, task: Task<Data>): void {
     this.beforeTaskExecutionHook(workerNodeKey, task)
     this.sendToWorker(this.workerNodes[workerNodeKey].worker, task)