Merge branch 'master' into interleaved-weighted-round-robin-worker-choice-strategy
[poolifier.git] / src / pools / abstract-pool.ts
index 5114bead6d4670af440b5d66f5af22bfa5aa213f..4c82ef08a8cdad70602ebf20a64235b1cb3833f0 100644 (file)
@@ -146,6 +146,9 @@ export abstract class AbstractPool<
       this.opts.workerChoiceStrategyOptions =
         opts.workerChoiceStrategyOptions ??
         DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
+      this.checkValidWorkerChoiceStrategyOptions(
+        this.opts.workerChoiceStrategyOptions
+      )
       this.opts.enableEvents = opts.enableEvents ?? true
       this.opts.enableTasksQueue = opts.enableTasksQueue ?? false
       if (this.opts.enableTasksQueue) {
@@ -179,6 +182,14 @@ export abstract class AbstractPool<
         'Invalid worker choice strategy options: must be a plain object'
       )
     }
+    if (
+      workerChoiceStrategyOptions.weights != null &&
+      Object.keys(workerChoiceStrategyOptions.weights).length !== this.size
+    ) {
+      throw new Error(
+        'Invalid worker choice strategy options: must have a weight for each worker node'
+      )
+    }
   }
 
   private checkValidTasksQueueOptions (
@@ -329,7 +340,7 @@ export abstract class AbstractPool<
 
   /** @inheritDoc */
   public async execute (data?: Data, name?: string): Promise<Response> {
-    const [workerNodeKey, workerNode] = this.chooseWorkerNode()
+    const workerNodeKey = this.chooseWorkerNode()
     const submittedTask: Task<Data> = {
       name,
       // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
@@ -340,7 +351,7 @@ export abstract class AbstractPool<
       this.promiseResponseMap.set(submittedTask.id as string, {
         resolve,
         reject,
-        worker: workerNode.worker
+        worker: this.workerNodes[workerNodeKey].worker
       })
     })
     if (
@@ -429,8 +440,11 @@ export abstract class AbstractPool<
         workerTasksUsage.avgRunTime =
           workerTasksUsage.runTime / workerTasksUsage.run
       }
-      if (this.workerChoiceStrategyContext.getRequiredStatistics().medRunTime) {
-        workerTasksUsage.runTimeHistory.push(message.runTime ?? 0)
+      if (
+        this.workerChoiceStrategyContext.getRequiredStatistics().medRunTime &&
+        message.runTime != null
+      ) {
+        workerTasksUsage.runTimeHistory.push(message.runTime)
         workerTasksUsage.medRunTime = median(workerTasksUsage.runTimeHistory)
       }
     }
@@ -439,11 +453,11 @@ export abstract class AbstractPool<
   /**
    * Chooses a worker node for the next task.
    *
-   * The default uses a round robin algorithm to distribute the load.
+   * The default worker choice strategy uses a round robin algorithm to distribute the load.
    *
-   * @returns [worker node key, worker node].
+   * @returns The worker node key
    */
-  protected chooseWorkerNode (): [number, WorkerNode<Worker, Data>] {
+  protected chooseWorkerNode (): number {
     let workerNodeKey: number
     if (this.type === PoolType.DYNAMIC && !this.full && this.internalBusy()) {
       const workerCreated = this.createAndSetupWorker()
@@ -463,7 +477,7 @@ export abstract class AbstractPool<
     } else {
       workerNodeKey = this.workerChoiceStrategyContext.execute()
     }
-    return [workerNodeKey, this.workerNodes[workerNodeKey]]
+    return workerNodeKey
   }
 
   /**