refactor: silence rollup warning
[poolifier.git] / src / pools / worker-node.ts
index 4349ed72471b3614e227ab4d374df7f0aba17186..d8044032014d02d36e6cbe70ffd10026f6a085ea 100644 (file)
@@ -1,8 +1,8 @@
 import { MessageChannel } from 'node:worker_threads'
 import { CircularArray } from '../circular-array'
-import { Queue } from '../queue'
 import type { Task } from '../utility-types'
-import { DEFAULT_TASK_NAME } from '../utils'
+import { DEFAULT_TASK_NAME, once } from '../utils'
+import { Deque } from '../deque'
 import {
   type IWorker,
   type IWorkerNode,
@@ -20,24 +20,60 @@ import {
  */
 export class WorkerNode<Worker extends IWorker, Data = unknown>
 implements IWorkerNode<Worker, Data> {
+  /** @inheritdoc */
   public readonly worker: Worker
+  /** @inheritdoc */
   public readonly info: WorkerInfo
+  /** @inheritdoc */
+  public messageChannel?: MessageChannel
+  /** @inheritdoc */
   public usage: WorkerUsage
-  private readonly tasksUsage: Map<string, WorkerUsage>
-  private readonly tasksQueue: Queue<Task<Data>>
+  /** @inheritdoc */
+  public tasksQueueBackPressureSize: number
+  /** @inheritdoc */
+  public onBackPressure?: (workerId: number) => void
+  private readonly taskFunctionsUsage: Map<string, WorkerUsage>
+  private readonly tasksQueue: Deque<Task<Data>>
 
   /**
    * 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) {
+  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'
+      )
+    }
     this.worker = worker
     this.info = this.initWorkerInfo(worker, workerType)
+    if (workerType === WorkerTypes.thread) {
+      this.messageChannel = new MessageChannel()
+    }
     this.usage = this.initWorkerUsage()
-    this.tasksUsage = new Map<string, WorkerUsage>()
-    this.tasksQueue = new Queue<Task<Data>>()
+    this.taskFunctionsUsage = new Map<string, WorkerUsage>()
+    this.tasksQueue = new Deque<Task<Data>>()
+    this.tasksQueueBackPressureSize = tasksQueueBackPressureSize
   }
 
   /** @inheritdoc */
@@ -56,12 +92,30 @@ implements IWorkerNode<Worker, Data> {
 
   /** @inheritdoc */
   public enqueueTask (task: Task<Data>): number {
-    return this.tasksQueue.enqueue(task)
+    const tasksQueueSize = this.tasksQueue.push(task)
+    if (this.onBackPressure != null && this.hasBackPressure()) {
+      once(this.onBackPressure, this)(this.info.id as number)
+    }
+    return tasksQueueSize
+  }
+
+  /** @inheritdoc */
+  public unshiftTask (task: Task<Data>): number {
+    const tasksQueueSize = this.tasksQueue.unshift(task)
+    if (this.onBackPressure != null && this.hasBackPressure()) {
+      once(this.onBackPressure, this)(this.info.id as number)
+    }
+    return tasksQueueSize
   }
 
   /** @inheritdoc */
   public dequeueTask (): Task<Data> | undefined {
-    return this.tasksQueue.dequeue()
+    return this.tasksQueue.shift()
+  }
+
+  /** @inheritdoc */
+  public popTask (): Task<Data> | undefined {
+    return this.tasksQueue.pop()
   }
 
   /** @inheritdoc */
@@ -69,41 +123,50 @@ implements IWorkerNode<Worker, Data> {
     this.tasksQueue.clear()
   }
 
+  /** @inheritdoc */
+  public hasBackPressure (): boolean {
+    return this.tasksQueue.size >= this.tasksQueueBackPressureSize
+  }
+
   /** @inheritdoc */
   public resetUsage (): void {
     this.usage = this.initWorkerUsage()
-    this.tasksUsage.clear()
+    this.taskFunctionsUsage.clear()
   }
 
   /** @inheritdoc */
   public closeChannel (): void {
-    if (this.info.messageChannel != null) {
-      this.info.messageChannel?.port1.unref()
-      this.info.messageChannel?.port2.unref()
-      this.info.messageChannel?.port1.close()
-      this.info.messageChannel?.port2.close()
-      delete this.info.messageChannel
+    if (this.messageChannel != null) {
+      this.messageChannel?.port1.unref()
+      this.messageChannel?.port2.unref()
+      this.messageChannel?.port1.close()
+      this.messageChannel?.port2.close()
+      delete this.messageChannel
     }
   }
 
   /** @inheritdoc */
-  public getTaskWorkerUsage (name: string): WorkerUsage | undefined {
-    if (name === DEFAULT_TASK_NAME && !Array.isArray(this.info.taskFunctions)) {
+  public getTaskFunctionWorkerUsage (name: string): WorkerUsage | undefined {
+    if (!Array.isArray(this.info.taskFunctions)) {
       throw new Error(
-        'Cannot get task worker usage for default task function name when task function names list is not yet defined'
+        `Cannot get task function worker usage for task function name '${name}' when task function names list is not yet defined`
       )
     }
     if (
-      name === DEFAULT_TASK_NAME &&
       Array.isArray(this.info.taskFunctions) &&
-      this.info.taskFunctions.length > 1
+      this.info.taskFunctions.length < 3
     ) {
+      throw new Error(
+        `Cannot get task function worker usage for task function name '${name}' when task function names list has less than 3 elements`
+      )
+    }
+    if (name === DEFAULT_TASK_NAME) {
       name = this.info.taskFunctions[1]
     }
-    if (!this.tasksUsage.has(name)) {
-      this.tasksUsage.set(name, this.initTaskWorkerUsage(name))
+    if (!this.taskFunctionsUsage.has(name)) {
+      this.taskFunctionsUsage.set(name, this.initTaskFunctionWorkerUsage(name))
     }
-    return this.tasksUsage.get(name)
+    return this.taskFunctionsUsage.get(name)
   }
 
   private initWorkerInfo (worker: Worker, workerType: WorkerType): WorkerInfo {
@@ -111,10 +174,7 @@ implements IWorkerNode<Worker, Data> {
       id: this.getWorkerId(worker, workerType),
       type: workerType,
       dynamic: false,
-      ready: false,
-      ...(workerType === WorkerTypes.thread && {
-        messageChannel: new MessageChannel()
-      })
+      ready: false
     }
   }
 
@@ -154,22 +214,26 @@ implements IWorkerNode<Worker, Data> {
     }
   }
 
-  private initTaskWorkerUsage (name: string): WorkerUsage {
-    const getTaskQueueSize = (): number => {
-      let taskQueueSize = 0
+  private initTaskFunctionWorkerUsage (name: string): WorkerUsage {
+    const getTaskFunctionQueueSize = (): number => {
+      let taskFunctionQueueSize = 0
       for (const task of this.tasksQueue) {
-        if (task.name === name) {
-          ++taskQueueSize
+        if (
+          (task.name === DEFAULT_TASK_NAME &&
+            name === (this.info.taskFunctions as string[])[1]) ||
+          (task.name !== DEFAULT_TASK_NAME && name === task.name)
+        ) {
+          ++taskFunctionQueueSize
         }
       }
-      return taskQueueSize
+      return taskFunctionQueueSize
     }
     return {
       tasks: {
         executed: 0,
         executing: 0,
         get queued (): number {
-          return getTaskQueueSize()
+          return getTaskFunctionQueueSize()
         },
         failed: 0
       },