Merge dependabot/npm_and_yarn/examples/typescript/websocket-server-pool/ws-hybrid...
[poolifier.git] / src / pools / abstract-pool.ts
index 0b56cbe42dcfab43ce802cbcaecc92992292c8f1..9dcc6eddd28fcc9b0e27e6eebbe55e9dc31b6458 100644 (file)
@@ -14,7 +14,9 @@ import {
   average,
   isKillBehavior,
   isPlainObject,
+  max,
   median,
+  min,
   round,
   updateMeasurementStatistics
 } from '../utils'
@@ -297,37 +299,37 @@ export abstract class AbstractPool<
       throw new TypeError('Invalid tasks queue options: must be a plain object')
     }
     if (
-      tasksQueueOptions.concurrency != null &&
-      !Number.isSafeInteger(tasksQueueOptions.concurrency)
+      tasksQueueOptions?.concurrency != null &&
+      !Number.isSafeInteger(tasksQueueOptions?.concurrency)
     ) {
       throw new TypeError(
         'Invalid worker node tasks concurrency: must be an integer'
       )
     }
     if (
-      tasksQueueOptions.concurrency != null &&
-      tasksQueueOptions.concurrency <= 0
+      tasksQueueOptions?.concurrency != null &&
+      tasksQueueOptions?.concurrency <= 0
     ) {
       throw new RangeError(
-        `Invalid worker node tasks concurrency: ${tasksQueueOptions.concurrency} is a negative integer or zero`
+        `Invalid worker node tasks concurrency: ${tasksQueueOptions?.concurrency} is a negative integer or zero`
       )
     }
-    if (tasksQueueOptions.queueMaxSize != null) {
+    if (tasksQueueOptions?.queueMaxSize != null) {
       throw new Error(
         'Invalid tasks queue options: queueMaxSize is deprecated, please use size instead'
       )
     }
     if (
-      tasksQueueOptions.size != null &&
-      !Number.isSafeInteger(tasksQueueOptions.size)
+      tasksQueueOptions?.size != null &&
+      !Number.isSafeInteger(tasksQueueOptions?.size)
     ) {
       throw new TypeError(
         'Invalid worker node tasks queue size: must be an integer'
       )
     }
-    if (tasksQueueOptions.size != null && tasksQueueOptions.size <= 0) {
+    if (tasksQueueOptions?.size != null && tasksQueueOptions?.size <= 0) {
       throw new RangeError(
-        `Invalid worker node tasks queue size: ${tasksQueueOptions.size} is a negative integer or zero`
+        `Invalid worker node tasks queue size: ${tasksQueueOptions?.size} is a negative integer or zero`
       )
     }
   }
@@ -414,14 +416,14 @@ export abstract class AbstractPool<
         .runTime.aggregate && {
         runTime: {
           minimum: round(
-            Math.min(
+            min(
               ...this.workerNodes.map(
                 (workerNode) => workerNode.usage.runTime?.minimum ?? Infinity
               )
             )
           ),
           maximum: round(
-            Math.max(
+            max(
               ...this.workerNodes.map(
                 (workerNode) => workerNode.usage.runTime?.maximum ?? -Infinity
               )
@@ -457,14 +459,14 @@ export abstract class AbstractPool<
         .waitTime.aggregate && {
         waitTime: {
           minimum: round(
-            Math.min(
+            min(
               ...this.workerNodes.map(
                 (workerNode) => workerNode.usage.waitTime?.minimum ?? Infinity
               )
             )
           ),
           maximum: round(
-            Math.max(
+            max(
               ...this.workerNodes.map(
                 (workerNode) => workerNode.usage.waitTime?.maximum ?? -Infinity
               )
@@ -1199,31 +1201,25 @@ export abstract class AbstractPool<
 
   private redistributeQueuedTasks (workerNodeKey: number): void {
     while (this.tasksQueueSize(workerNodeKey) > 0) {
-      let destinationWorkerNodeKey!: number
-      let minQueuedTasks = Infinity
-      for (const [workerNodeId, workerNode] of this.workerNodes.entries()) {
-        if (workerNode.info.ready && workerNodeId !== workerNodeKey) {
-          if (workerNode.usage.tasks.queued === 0) {
-            destinationWorkerNodeKey = workerNodeId
-            break
-          }
-          if (workerNode.usage.tasks.queued < minQueuedTasks) {
-            minQueuedTasks = workerNode.usage.tasks.queued
-            destinationWorkerNodeKey = workerNodeId
-          }
-        }
+      const destinationWorkerNodeKey = this.workerNodes.reduce(
+        (minWorkerNodeKey, workerNode, workerNodeKey, workerNodes) => {
+          return workerNode.info.ready &&
+            workerNode.usage.tasks.queued <
+              workerNodes[minWorkerNodeKey].usage.tasks.queued
+            ? workerNodeKey
+            : minWorkerNodeKey
+        },
+        0
+      )
+      const destinationWorkerNode = this.workerNodes[destinationWorkerNodeKey]
+      const task = {
+        ...(this.dequeueTask(workerNodeKey) as Task<Data>),
+        workerId: destinationWorkerNode.info.id as number
       }
-      if (destinationWorkerNodeKey != null) {
-        const destinationWorkerNode = this.workerNodes[destinationWorkerNodeKey]
-        const task = {
-          ...(this.dequeueTask(workerNodeKey) as Task<Data>),
-          workerId: destinationWorkerNode.info.id as number
-        }
-        if (this.shallExecuteTask(destinationWorkerNodeKey)) {
-          this.executeTask(destinationWorkerNodeKey, task)
-        } else {
-          this.enqueueTask(destinationWorkerNodeKey, task)
-        }
+      if (this.shallExecuteTask(destinationWorkerNodeKey)) {
+        this.executeTask(destinationWorkerNodeKey, task)
+      } else {
+        this.enqueueTask(destinationWorkerNodeKey, task)
       }
     }
   }
@@ -1256,30 +1252,26 @@ export abstract class AbstractPool<
         (workerNodeA, workerNodeB) =>
           workerNodeB.usage.tasks.queued - workerNodeA.usage.tasks.queued
       )
-    for (const sourceWorkerNode of workerNodes) {
-      if (sourceWorkerNode.usage.tasks.queued === 0) {
-        break
+    const sourceWorkerNode = workerNodes.find(
+      (workerNode) =>
+        workerNode.info.ready &&
+        workerNode.info.id !== workerId &&
+        workerNode.usage.tasks.queued > 0
+    )
+    if (sourceWorkerNode != null) {
+      const task = {
+        ...(sourceWorkerNode.popTask() as Task<Data>),
+        workerId: destinationWorkerNode.info.id as number
       }
-      if (
-        sourceWorkerNode.info.ready &&
-        sourceWorkerNode.info.id !== workerId &&
-        sourceWorkerNode.usage.tasks.queued > 0
-      ) {
-        const task = {
-          ...(sourceWorkerNode.popTask() as Task<Data>),
-          workerId: destinationWorkerNode.info.id as number
-        }
-        if (this.shallExecuteTask(destinationWorkerNodeKey)) {
-          this.executeTask(destinationWorkerNodeKey, task)
-        } else {
-          this.enqueueTask(destinationWorkerNodeKey, task)
-        }
-        this.updateTaskStolenStatisticsWorkerUsage(
-          destinationWorkerNodeKey,
-          task.name as string
-        )
-        break
+      if (this.shallExecuteTask(destinationWorkerNodeKey)) {
+        this.executeTask(destinationWorkerNodeKey, task)
+      } else {
+        this.enqueueTask(destinationWorkerNodeKey, task)
       }
+      this.updateTaskStolenStatisticsWorkerUsage(
+        destinationWorkerNodeKey,
+        task.name as string
+      )
     }
   }