refactor: reorder benchmarks
[poolifier.git] / src / pools / abstract-pool.ts
index a3b46cee0f52e0be63827488730e671c1f316a98..87427ad719079d1b4462661bcba473f94786127e 100644 (file)
@@ -84,6 +84,10 @@ export abstract class AbstractPool<
   Response
   >
 
+  /**
+   * Whether the pool is starting or not.
+   */
+  private readonly starting: boolean
   /**
    * The start timestamp of the pool.
    */
@@ -128,9 +132,9 @@ export abstract class AbstractPool<
 
     this.setupHook()
 
-    while (this.workerNodes.length < this.numberOfWorkers) {
-      this.createAndSetupWorker()
-    }
+    this.starting = true
+    this.startPool()
+    this.starting = false
 
     this.startTimestamp = performance.now()
   }
@@ -172,9 +176,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(
@@ -273,6 +277,18 @@ export abstract class AbstractPool<
     }
   }
 
+  private startPool (): void {
+    while (
+      this.workerNodes.reduce(
+        (accumulator, workerNode) =>
+          !workerNode.info.dynamic ? accumulator + 1 : accumulator,
+        0
+      ) < this.numberOfWorkers
+    ) {
+      this.createAndSetupWorker()
+    }
+  }
+
   /** @inheritDoc */
   public get info (): PoolInfo {
     return {
@@ -410,17 +426,15 @@ export abstract class AbstractPool<
     }
   }
 
-  private get starting (): boolean {
-    return this.workerNodes.length < 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
     )
   }
 
@@ -929,9 +943,7 @@ export abstract class AbstractPool<
       const workerNodeKey = this.getWorkerNodeKey(worker)
       const workerInfo = this.getWorkerInfo(workerNodeKey)
       workerInfo.ready = false
-      if (this.emitter != null) {
-        this.emitter.emit(PoolEvents.error, error)
-      }
+      this.emitter?.emit(PoolEvents.error, error)
       if (this.opts.restartWorkerOnError === true && !this.starting) {
         if (workerInfo.dynamic) {
           this.createAndSetupDynamicWorker()
@@ -949,7 +961,7 @@ export abstract class AbstractPool<
       this.removeWorkerNode(worker)
     })
 
-    this.pushWorkerNode(worker)
+    this.addWorkerNode(worker)
 
     this.afterWorkerSetup(worker)
 
@@ -979,10 +991,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 +1076,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 +1085,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
@@ -1084,9 +1098,7 @@ export abstract class AbstractPool<
     const promiseResponse = this.promiseResponseMap.get(message.id as string)
     if (promiseResponse != null) {
       if (message.taskError != null) {
-        if (this.emitter != null) {
-          this.emitter.emit(PoolEvents.taskError, message.taskError)
-        }
+        this.emitter?.emit(PoolEvents.taskError, message.taskError)
         promiseResponse.reject(message.taskError.message)
       } else {
         promiseResponse.resolve(message.data as Response)
@@ -1128,14 +1140,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 +1167,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)