refactor: refine autocannon parameters
[poolifier.git] / src / pools / worker-node.ts
index 079881b94cf165a16f4fe2e6be9c4223579a8346..387d05f964270545b5b92f0e841768dea20a23df 100644 (file)
@@ -5,6 +5,8 @@ import {
   DEFAULT_TASK_NAME,
   EMPTY_FUNCTION,
   exponentialDelay,
+  getWorkerId,
+  getWorkerType,
   sleep
 } from '../utils'
 import { Deque } from '../deque'
@@ -12,6 +14,7 @@ import {
   type IWorker,
   type IWorkerNode,
   type WorkerInfo,
+  type WorkerNodeEventCallback,
   type WorkerType,
   WorkerTypes,
   type WorkerUsage
@@ -30,59 +33,37 @@ implements IWorkerNode<Worker, Data> {
   /** @inheritdoc */
   public readonly info: WorkerInfo
   /** @inheritdoc */
-  public messageChannel?: MessageChannel
-  /** @inheritdoc */
   public usage: WorkerUsage
   /** @inheritdoc */
+  public messageChannel?: MessageChannel
+  /** @inheritdoc */
   public tasksQueueBackPressureSize: number
   /** @inheritdoc */
-  public onBackPressure?: (workerId: number) => void
+  public onBackPressure?: WorkerNodeEventCallback
   /** @inheritdoc */
-  public onEmptyQueue?: (workerId: number) => void
-  private readonly taskFunctionsUsage: Map<string, WorkerUsage>
+  public onEmptyQueue?: WorkerNodeEventCallback
   private readonly tasksQueue: Deque<Task<Data>>
   private onEmptyQueueCount: number
+  private readonly taskFunctionsUsage: Map<string, WorkerUsage>
 
   /**
    * Constructs a new worker node.
    *
    * @param worker - The worker.
-   * @param workerType - The worker type.
    * @param tasksQueueBackPressureSize - The tasks queue back pressure size.
    */
-  constructor (
-    worker: Worker,
-    workerType: WorkerType,
-    tasksQueueBackPressureSize: number
-  ) {
-    if (worker == null) {
-      throw new TypeError('Cannot construct a worker node without a worker')
-    }
-    if (workerType == null) {
-      throw new TypeError(
-        'Cannot construct a worker node without a worker type'
-      )
-    }
-    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'
-      )
-    }
+  constructor (worker: Worker, tasksQueueBackPressureSize: number) {
+    this.checkWorkerNodeArguments(worker, tasksQueueBackPressureSize)
     this.worker = worker
-    this.info = this.initWorkerInfo(worker, workerType)
-    if (workerType === WorkerTypes.thread) {
+    this.info = this.initWorkerInfo(worker)
+    this.usage = this.initWorkerUsage()
+    if (this.info.type === WorkerTypes.thread) {
       this.messageChannel = new MessageChannel()
     }
-    this.usage = this.initWorkerUsage()
-    this.taskFunctionsUsage = new Map<string, WorkerUsage>()
-    this.tasksQueue = new Deque<Task<Data>>()
     this.tasksQueueBackPressureSize = tasksQueueBackPressureSize
+    this.tasksQueue = new Deque<Task<Data>>()
     this.onEmptyQueueCount = 0
+    this.taskFunctionsUsage = new Map<string, WorkerUsage>()
   }
 
   /** @inheritdoc */
@@ -185,16 +166,16 @@ implements IWorkerNode<Worker, Data> {
       this.onEmptyQueueCount = 0
       return
     }
-    (this.onEmptyQueue as (workerId: number) => void)(this.info.id as number)
+    (this.onEmptyQueue as WorkerNodeEventCallback)(this.info.id as number)
     ++this.onEmptyQueueCount
     await sleep(exponentialDelay(this.onEmptyQueueCount))
     await this.startOnEmptyQueue()
   }
 
-  private initWorkerInfo (worker: Worker, workerType: WorkerType): WorkerInfo {
+  private initWorkerInfo (worker: Worker): WorkerInfo {
     return {
-      id: this.getWorkerId(worker, workerType),
-      type: workerType,
+      id: getWorkerId(worker),
+      type: getWorkerType(worker) as WorkerType,
       dynamic: false,
       ready: false
     }
@@ -278,21 +259,27 @@ implements IWorkerNode<Worker, Data> {
     }
   }
 
-  /**
-   * Gets the worker id.
-   *
-   * @param worker - The worker.
-   * @param workerType - The worker type.
-   * @returns The worker id.
-   */
-  private getWorkerId (
+  private checkWorkerNodeArguments (
     worker: Worker,
-    workerType: WorkerType
-  ): number | undefined {
-    if (workerType === WorkerTypes.thread) {
-      return worker.threadId
-    } else if (workerType === WorkerTypes.cluster) {
-      return worker.id
+    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'
+      )
     }
   }
 }