fix: fix worker node event emitter memory leak
[poolifier.git] / src / pools / utils.ts
index 4aac5cda47d361fd723198a53ad44f254235b56c..eb800c4ab50f9d39e2b6fc98ebb4cf39bfc134b5 100644 (file)
@@ -1,19 +1,14 @@
 import { existsSync } from 'node:fs'
-import { isPlainObject } from '../utils'
+import { average, isPlainObject, max, median, min } from '../utils'
 import {
+  type MeasurementStatisticsRequirements,
   WorkerChoiceStrategies,
   type WorkerChoiceStrategy
 } from './selection-strategies/selection-strategies-types'
 import type { TasksQueueOptions } from './pool'
+import type { IWorker, MeasurementStatistics } from './worker'
 
 export const checkFilePath = (filePath: string): void => {
-  if (
-    filePath == null ||
-    typeof filePath !== 'string' ||
-    (typeof filePath === 'string' && filePath.trim().length === 0)
-  ) {
-    throw new Error('Please specify a file with a worker implementation')
-  }
   if (!existsSync(filePath)) {
     throw new Error(`Cannot find the worker file '${filePath}'`)
   }
@@ -90,3 +85,71 @@ export const checkValidTasksQueueOptions = (
     )
   }
 }
+
+export const checkWorkerNodeArguments = <Worker extends IWorker>(
+  worker: Worker,
+  tasksQueueBackPressureSize: number
+): void => {
+  if (worker == null) {
+    throw new TypeError('Cannot construct a worker node without a worker')
+  }
+  if (tasksQueueBackPressureSize == null) {
+    throw new TypeError(
+      'Cannot construct a worker node without a tasks queue back pressure size'
+    )
+  }
+  if (!Number.isSafeInteger(tasksQueueBackPressureSize)) {
+    throw new TypeError(
+      'Cannot construct a worker node with a tasks queue back pressure size that is not an integer'
+    )
+  }
+  if (tasksQueueBackPressureSize <= 0) {
+    throw new RangeError(
+      'Cannot construct a worker node with a tasks queue back pressure size that is not a positive integer'
+    )
+  }
+}
+
+/**
+ * Updates the given measurement statistics.
+ *
+ * @param measurementStatistics - The measurement statistics to update.
+ * @param measurementRequirements - The measurement statistics requirements.
+ * @param measurementValue - The measurement value.
+ * @param numberOfMeasurements - The number of measurements.
+ * @internal
+ */
+export const updateMeasurementStatistics = (
+  measurementStatistics: MeasurementStatistics,
+  measurementRequirements: MeasurementStatisticsRequirements,
+  measurementValue: number
+): void => {
+  if (measurementRequirements.aggregate) {
+    measurementStatistics.aggregate =
+      (measurementStatistics.aggregate ?? 0) + measurementValue
+    measurementStatistics.minimum = min(
+      measurementValue,
+      measurementStatistics.minimum ?? Infinity
+    )
+    measurementStatistics.maximum = max(
+      measurementValue,
+      measurementStatistics.maximum ?? -Infinity
+    )
+    if (
+      (measurementRequirements.average || measurementRequirements.median) &&
+      measurementValue != null
+    ) {
+      measurementStatistics.history.push(measurementValue)
+      if (measurementRequirements.average) {
+        measurementStatistics.average = average(measurementStatistics.history)
+      } else if (measurementStatistics.average != null) {
+        delete measurementStatistics.average
+      }
+      if (measurementRequirements.median) {
+        measurementStatistics.median = median(measurementStatistics.history)
+      } else if (measurementStatistics.median != null) {
+        delete measurementStatistics.median
+      }
+    }
+  }
+}