refactor: add task function name to task performance
[poolifier.git] / src / pools / abstract-pool.ts
index 6c9a0fd185f7c560f4f7fa5ac597ab784f6d6dbd..c699b5282a7c0b862a4cc16b63ee5dd34637a643 100644 (file)
@@ -158,14 +158,20 @@ export abstract class AbstractPool<
   }
 
   protected checkDynamicPoolSize (min: number, max: number): void {
-    if (this.type === PoolTypes.dynamic && min > max) {
-      throw new RangeError(
-        'Cannot instantiate a dynamic pool with a maximum pool size inferior to the minimum pool size'
-      )
-    } else if (this.type === PoolTypes.dynamic && min === max) {
-      throw new RangeError(
-        'Cannot instantiate a dynamic pool with a minimum pool size equal to the maximum pool size. Use a fixed pool instead'
-      )
+    if (this.type === PoolTypes.dynamic) {
+      if (min > max) {
+        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) {
+        throw new RangeError(
+          'Cannot instantiate a dynamic pool with a minimum pool size and a maximum pool size equal to zero'
+        )
+      } else if (min === max) {
+        throw new RangeError(
+          'Cannot instantiate a dynamic pool with a minimum pool size equal to the maximum pool size. Use a fixed pool instead'
+        )
+      }
     }
   }
 
@@ -397,14 +403,16 @@ export abstract class AbstractPool<
 
   private get starting (): boolean {
     return (
-      !this.full ||
-      (this.full && this.workerNodes.some(workerNode => !workerNode.info.ready))
+      this.workerNodes.length < this.minSize ||
+      (this.workerNodes.length >= this.minSize &&
+        this.workerNodes.some(workerNode => !workerNode.info.ready))
     )
   }
 
   private get ready (): boolean {
     return (
-      this.full && this.workerNodes.every(workerNode => workerNode.info.ready)
+      this.workerNodes.length >= this.minSize &&
+      this.workerNodes.every(workerNode => workerNode.info.ready)
     )
   }
 
@@ -462,6 +470,17 @@ export abstract class AbstractPool<
       ?.worker
   }
 
+  private checkMessageWorkerId (message: MessageValue<Response>): void {
+    if (
+      message.workerId != null &&
+      this.getWorkerById(message.workerId) == null
+    ) {
+      throw new Error(
+        `Worker message received from unknown worker '${message.workerId}'`
+      )
+    }
+  }
+
   /**
    * Gets the given worker its worker node key.
    *
@@ -573,6 +592,7 @@ export abstract class AbstractPool<
       // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
       data: data ?? ({} as Data),
       timestamp,
+      workerId: this.getWorkerInfo(workerNodeKey).id as number,
       id: randomUUID()
     }
     const res = new Promise<Response>((resolve, reject) => {
@@ -894,7 +914,7 @@ export abstract class AbstractPool<
     // Send startup message to worker.
     this.sendToWorker(worker, {
       ready: false,
-      workerId: this.getWorkerInfo(this.getWorkerNodeKey(worker)).id
+      workerId: this.getWorkerInfo(this.getWorkerNodeKey(worker)).id as number
     })
     // Setup worker task statistics computation.
     this.setWorkerStatistics(worker)
@@ -988,8 +1008,12 @@ export abstract class AbstractPool<
         void (this.destroyWorker(worker) as Promise<void>)
       }
     })
-    this.getWorkerInfo(this.getWorkerNodeKey(worker)).dynamic = true
-    this.sendToWorker(worker, { checkAlive: true })
+    const workerInfo = this.getWorkerInfo(this.getWorkerNodeKey(worker))
+    workerInfo.dynamic = true
+    this.sendToWorker(worker, {
+      checkAlive: true,
+      workerId: workerInfo.id as number
+    })
     return worker
   }
 
@@ -1000,6 +1024,7 @@ export abstract class AbstractPool<
    */
   protected workerListener (): (message: MessageValue<Response>) => void {
     return message => {
+      this.checkMessageWorkerId(message)
       if (message.ready != null && message.workerId != null) {
         // Worker ready message received
         this.handleWorkerReadyMessage(message)
@@ -1011,17 +1036,9 @@ export abstract class AbstractPool<
   }
 
   private handleWorkerReadyMessage (message: MessageValue<Response>): void {
-    const worker = this.getWorkerById(message.workerId as number)
-    if (worker != null) {
-      this.getWorkerInfo(this.getWorkerNodeKey(worker)).ready =
-        message.ready as boolean
-    } else {
-      throw new Error(
-        `Worker ready message received from unknown worker '${
-          message.workerId as number
-        }'`
-      )
-    }
+    const worker = this.getWorkerById(message.workerId)
+    this.getWorkerInfo(this.getWorkerNodeKey(worker as Worker)).ready =
+      message.ready as boolean
     if (this.emitter != null && this.ready) {
       this.emitter.emit(PoolEvents.ready, this.info)
     }
@@ -1138,7 +1155,8 @@ export abstract class AbstractPool<
             .runTime.aggregate,
         elu: this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
           .elu.aggregate
-      }
+      },
+      workerId: this.getWorkerInfo(this.getWorkerNodeKey(worker)).id as number
     })
   }
 }