-import type { MessageChannel, WorkerOptions } from 'node:worker_threads'
import type { EventEmitter } from 'node:events'
-import type { CircularArray } from '../circular-array.js'
-import type { Task } from '../utility-types.js'
+import type { MessageChannel, WorkerOptions } from 'node:worker_threads'
+
+import type { CircularBuffer } from '../circular-buffer.js'
+import type { Task, TaskFunctionProperties } from '../utility-types.js'
/**
* Callback invoked when the worker has started successfully.
- *
* @typeParam Worker - Type of worker.
*/
export type OnlineHandler<Worker extends IWorker> = (this: Worker) => void
/**
* Callback invoked if the worker has received a message.
- *
* @typeParam Worker - Type of worker.
*/
export type MessageHandler<Worker extends IWorker> = (
/**
* Callback invoked if the worker raised an error.
- *
* @typeParam Worker - Type of worker.
*/
export type ErrorHandler<Worker extends IWorker> = (
/**
* Callback invoked when the worker exits successfully.
- *
* @typeParam Worker - Type of worker.
*/
export type ExitHandler<Worker extends IWorker> = (
/**
* Worker event handler.
- *
* @typeParam Worker - Type of worker.
*/
export type EventHandler<Worker extends IWorker> =
| ErrorHandler<Worker>
| ExitHandler<Worker>
+/**
+ * Measurement history size.
+ */
+export const MeasurementHistorySize = 386
+
/**
* Measurement statistics.
- *
* @internal
*/
export interface MeasurementStatistics {
/**
* Measurement history.
*/
- readonly history: CircularArray<number>
+ readonly history: CircularBuffer
}
/**
* Event loop utilization measurement statistics.
- *
* @internal
*/
export interface EventLoopUtilizationMeasurementStatistics {
/**
* Task statistics.
- *
* @internal
*/
export interface TaskStatistics {
/**
* Enumeration of worker types.
*/
-export const WorkerTypes = Object.freeze({
- thread: 'thread',
- cluster: 'cluster'
-} as const)
+export const WorkerTypes: Readonly<{ thread: 'thread'; cluster: 'cluster' }> =
+ Object.freeze({
+ thread: 'thread',
+ cluster: 'cluster',
+ } as const)
/**
* Worker type.
/**
* Worker information.
- *
* @internal
*/
export interface WorkerInfo {
*/
stealing: boolean
/**
- * Task function names.
+ * Back pressure flag.
+ * This flag is set to `true` when worker node tasks queue has back pressure.
+ */
+ backPressure: boolean
+ /**
+ * Task functions properties.
*/
- taskFunctionNames?: string[]
+ taskFunctionsProperties?: TaskFunctionProperties[]
}
/**
* Worker usage statistics.
- *
* @internal
*/
export interface WorkerUsage {
/**
* Worker choice strategy data.
- *
* @internal
*/
export interface StrategyData {
/**
* Worker interface.
*/
-export interface IWorker {
+export interface IWorker extends EventEmitter {
/**
* Cluster worker id.
*/
readonly threadId?: number
/**
* Registers an event handler.
- *
* @param event - The event.
* @param handler - The event handler.
*/
- readonly on: (event: string, handler: EventHandler<this>) => void
+ readonly on: (event: string, handler: EventHandler<this>) => this
/**
* Registers once an event handler.
- *
* @param event - The event.
* @param handler - The event handler.
*/
- readonly once: (event: string, handler: EventHandler<this>) => void
+ readonly once: (event: string, handler: EventHandler<this>) => this
+ /**
+ * Calling `unref()` on a worker allows the thread to exit if this is the only
+ * active handle in the event system. If the worker is already `unref()`ed calling`unref()` again has no effect.
+ * @since v10.5.0
+ */
+ readonly unref?: () => void
/**
* Stop all JavaScript execution in the worker thread as soon as possible.
* Returns a Promise for the exit code that is fulfilled when the `'exit' event` is emitted.
/**
* Worker node options.
- *
* @internal
*/
export interface WorkerNodeOptions {
workerOptions?: WorkerOptions
env?: Record<string, unknown>
tasksQueueBackPressureSize: number | undefined
+ tasksQueueBucketSize: number | undefined
+ tasksQueuePriority: boolean | undefined
}
/**
* Worker node interface.
- *
* @typeParam Worker - Type of worker.
* @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
* @internal
* This is the number of tasks that can be enqueued before the worker node has back pressure.
*/
tasksQueueBackPressureSize: number
+ /**
+ * Sets tasks queue priority.
+ * @param enablePriority - Whether to enable tasks queue priority.
+ */
+ readonly setTasksQueuePriority: (enablePriority: boolean) => void
/**
* Tasks queue size.
- *
* @returns The tasks queue size.
*/
readonly tasksQueueSize: () => number
/**
* Enqueue task.
- *
* @param task - The task to queue.
* @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.
- *
+ * @param bucket - The prioritized bucket to dequeue from. @defaultValue 0
* @returns The dequeued task.
*/
- readonly dequeueTask: () => Task<Data> | undefined
+ readonly dequeueTask: (bucket?: number) => Task<Data> | undefined
/**
- * Pops a task from the tasks queue.
- *
- * @returns The popped task.
+ * Dequeue last prioritized task.
+ * @returns The dequeued task.
*/
- readonly popTask: () => Task<Data> | undefined
+ readonly dequeueLastPrioritizedTask: () => Task<Data> | undefined
/**
* 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.
- */
- readonly resetUsage: () => void
/**
* Terminates the worker node.
*/
readonly terminate: () => Promise<void>
/**
* Registers a worker event handler.
- *
* @param event - The event.
* @param handler - The event handler.
*/
) => void
/**
* Registers once a worker event handler.
- *
* @param event - The event.
* @param handler - The event handler.
*/
) => void
/**
* 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 getTaskFunctionWorkerUsage: (name: string) => WorkerUsage | undefined
/**
* Deletes task function worker usage statistics.
- *
* @param name - The task function name.
* @returns `true` if the task function worker usage statistics were deleted, `false` otherwise.
*/
/**
* Worker node event detail.
- *
* @internal
*/
export interface WorkerNodeEventDetail {
- workerId: number
+ workerId?: number
workerNodeKey?: number
}