refactor: cleanup worker node back pressure API
[poolifier.git] / src / pools / worker.ts
index 09253242f7d0af88204ba9989fb37a7fa12cc287..d6c236591341c2fb73086d62cdb111cecd2a012e 100644 (file)
@@ -2,6 +2,11 @@ import type { MessageChannel } from 'node:worker_threads'
 import type { CircularArray } from '../circular-array'
 import type { Task } from '../utility-types'
 
+/**
+ * Callback invoked when the worker has started successfully.
+ */
+export type OnlineHandler<Worker extends IWorker> = (this: Worker) => void
+
 /**
  * Callback invoked if the worker has received a message.
  */
@@ -18,11 +23,6 @@ export type ErrorHandler<Worker extends IWorker> = (
   error: Error
 ) => void
 
-/**
- * Callback invoked when the worker has started successfully.
- */
-export type OnlineHandler<Worker extends IWorker> = (this: Worker) => void
-
 /**
  * Callback invoked when the worker exits successfully.
  */
@@ -242,6 +242,12 @@ export interface IWorkerNode<Worker extends IWorker, Data = unknown> {
    * Clears tasks queue.
    */
   readonly clearTasksQueue: () => void
+  /**
+   * Whether the worker node has back pressure (i.e. its tasks queue is full).
+   *
+   * @returns `true` if the worker node has back pressure, `false` otherwise.
+   */
+  readonly hasBackPressure: () => boolean
   /**
    * Resets usage statistics .
    */
@@ -251,7 +257,10 @@ export interface IWorkerNode<Worker extends IWorker, Data = unknown> {
    */
   readonly closeChannel: () => void
   /**
-   * Gets task worker usage statistics.
+   * Gets task function worker usage statistics.
+   *
+   * @param name - The task function name.
+   * @returns The task function worker usage statistics if the task function worker usage statistics are initialized, `undefined` otherwise.
    */
   readonly getTaskWorkerUsage: (name: string) => WorkerUsage | undefined
 }