build(ci): use PAT for continuous benchmark results
[poolifier.git] / src / pools / worker.ts
index 5ac90fbf1783e9e04a7e820e37c961e8527385ff..29050455088c6efd49d8147aed07c41f2a5a004e 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.
  */
@@ -96,6 +96,10 @@ export interface TaskStatistics {
    * Maximum number of queued tasks.
    */
   readonly maxQueued?: number
+  /**
+   * Number of stolen tasks.
+   */
+  stolen: number
   /**
    * Number of failed tasks.
    */
@@ -128,7 +132,7 @@ export interface WorkerInfo {
   /**
    * Worker type.
    */
-  type: WorkerType
+  readonly type: WorkerType
   /**
    * Dynamic flag.
    */
@@ -141,10 +145,6 @@ export interface WorkerInfo {
    * Task function names.
    */
   taskFunctions?: string[]
-  /**
-   * Message channel.
-   */
-  messageChannel?: MessageChannel
 }
 
 /**
@@ -171,6 +171,15 @@ export interface WorkerUsage {
   readonly elu: EventLoopUtilizationMeasurementStatistics
 }
 
+/**
+ * Worker choice strategy data.
+ *
+ * @internal
+ */
+export interface StrategyData {
+  virtualTaskEndTimestamp?: number
+}
+
 /**
  * Worker interface.
  */
@@ -193,12 +202,20 @@ export interface IWorker {
   /**
    * Registers a listener to the exit event that will only be performed once.
    *
-   * @param event - `'exit'`.
+   * @param event - The `'exit'` event.
    * @param handler - The exit handler.
    */
   readonly once: (event: 'exit', handler: ExitHandler<this>) => void
 }
 
+/**
+ * Worker node event callback.
+ *
+ * @param workerId - The worker id.
+ * @internal
+ */
+export type WorkerNodeEventCallback = (workerId: number) => void
+
 /**
  * Worker node interface.
  *
@@ -218,7 +235,29 @@ export interface IWorkerNode<Worker extends IWorker, Data = unknown> {
   /**
    * Worker usage statistics.
    */
-  usage: WorkerUsage
+  readonly usage: WorkerUsage
+  /**
+   * Worker choice strategy data.
+   * This is used to store data that is specific to the worker choice strategy.
+   */
+  strategyData?: StrategyData
+  /**
+   * Message channel (worker_threads only).
+   */
+  readonly messageChannel?: MessageChannel
+  /**
+   * Tasks queue back pressure size.
+   * This is the number of tasks that can be enqueued before the worker node has back pressure.
+   */
+  tasksQueueBackPressureSize: number
+  /**
+   * Callback invoked when worker node tasks queue is back pressured.
+   */
+  onBackPressure?: WorkerNodeEventCallback
+  /**
+   * Callback invoked when worker node tasks queue is empty.
+   */
+  onEmptyQueue?: WorkerNodeEventCallback
   /**
    * Tasks queue size.
    *
@@ -229,29 +268,51 @@ export interface IWorkerNode<Worker extends IWorker, Data = unknown> {
    * Enqueue task.
    *
    * @param task - The task to queue.
-   * @returns The task queue size.
+   * @returns The tasks queue size.
    */
   readonly enqueueTask: (task: Task<Data>) => number
+  /**
+   * Prepends a task to the tasks queue.
+   *
+   * @param task - The task to prepend.
+   * @returns The tasks queue size.
+   */
+  readonly unshiftTask: (task: Task<Data>) => number
   /**
    * Dequeue task.
    *
    * @returns The dequeued task.
    */
   readonly dequeueTask: () => Task<Data> | undefined
+  /**
+   * Pops a task from the tasks queue.
+   *
+   * @returns The popped task.
+   */
+  readonly popTask: () => Task<Data> | undefined
   /**
    * Clears tasks queue.
    */
   readonly clearTasksQueue: () => void
   /**
-   * Resets usage statistics .
+   * 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.
    */
   readonly resetUsage: () => void
   /**
-   * Close communication channel.
+   * Closes communication channel.
    */
   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
+  readonly getTaskFunctionWorkerUsage: (name: string) => WorkerUsage | undefined
 }