1 import type { MessageChannel
} from
'node:worker_threads'
2 import type { CircularArray
} from
'../circular-array'
3 import type { Task
} from
'../utility-types'
6 * Callback invoked when the worker has started successfully.
8 export type OnlineHandler
<Worker
extends IWorker
> = (this: Worker
) => void
11 * Callback invoked if the worker has received a message.
13 export type MessageHandler
<Worker
extends IWorker
> = (
19 * Callback invoked if the worker raised an error.
21 export type ErrorHandler
<Worker
extends IWorker
> = (
27 * Callback invoked when the worker exits successfully.
29 export type ExitHandler
<Worker
extends IWorker
> = (
35 * Measurement statistics.
39 export interface MeasurementStatistics
{
41 * Measurement aggregate.
45 * Measurement minimum.
49 * Measurement maximum.
53 * Measurement average.
61 * Measurement history.
63 readonly history
: CircularArray
<number>
67 * Event loop utilization measurement statistics.
71 export interface EventLoopUtilizationMeasurementStatistics
{
72 readonly idle
: MeasurementStatistics
73 readonly active
: MeasurementStatistics
82 export interface TaskStatistics
{
84 * Number of executed tasks.
88 * Number of executing tasks.
92 * Number of queued tasks.
94 readonly queued
: number
96 * Maximum number of queued tasks.
98 readonly maxQueued
?: number
100 * Number of stolen tasks.
104 * Number of failed tasks.
110 * Enumeration of worker types.
112 export const WorkerTypes
= Object.freeze({
120 export type WorkerType
= keyof
typeof WorkerTypes
123 * Worker information.
127 export interface WorkerInfo
{
131 readonly id
: number | undefined
135 readonly type: WorkerType
145 * Task function names.
147 taskFunctionNames
?: string[]
151 * Worker usage statistics.
155 export interface WorkerUsage
{
159 readonly tasks
: TaskStatistics
161 * Tasks runtime statistics.
163 readonly runTime
: MeasurementStatistics
165 * Tasks wait time statistics.
167 readonly waitTime
: MeasurementStatistics
169 * Tasks event loop utilization statistics.
171 readonly elu
: EventLoopUtilizationMeasurementStatistics
175 * Worker choice strategy data.
179 export interface StrategyData
{
180 virtualTaskEndTimestamp
?: number
186 export interface IWorker
{
191 readonly threadId
?: number
193 * Registers an event listener.
195 * @param event - The event.
196 * @param handler - The event handler.
198 readonly on
: ((event
: 'online', handler
: OnlineHandler
<this>) => void) &
199 ((event
: 'message', handler
: MessageHandler
<this>) => void) &
200 ((event
: 'error', handler
: ErrorHandler
<this>) => void) &
201 ((event
: 'exit', handler
: ExitHandler
<this>) => void)
203 * Registers a listener to the exit event that will only be performed once.
205 * @param event - The `'exit'` event.
206 * @param handler - The exit handler.
208 readonly once
: (event
: 'exit', handler
: ExitHandler
<this>) => void
212 * Worker node event callback.
214 * @param workerId - The worker id.
217 export type WorkerNodeEventCallback
= (workerId
: number) => void
220 * Worker node interface.
222 * @typeParam Worker - Type of worker.
223 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
226 export interface IWorkerNode
<Worker
extends IWorker
, Data
= unknown
> {
230 readonly worker
: Worker
234 readonly info
: WorkerInfo
236 * Worker usage statistics.
238 readonly usage
: WorkerUsage
240 * Worker choice strategy data.
241 * This is used to store data that is specific to the worker choice strategy.
243 strategyData
?: StrategyData
245 * Message channel (worker_threads only).
247 readonly messageChannel
?: MessageChannel
249 * Tasks queue back pressure size.
250 * This is the number of tasks that can be enqueued before the worker node has back pressure.
252 tasksQueueBackPressureSize
: number
254 * Callback invoked when worker node tasks queue is back pressured.
256 onBackPressure
?: WorkerNodeEventCallback
258 * Callback invoked when worker node tasks queue is empty.
260 onEmptyQueue
?: WorkerNodeEventCallback
264 * @returns The tasks queue size.
266 readonly tasksQueueSize
: () => number
270 * @param task - The task to queue.
271 * @returns The tasks queue size.
273 readonly enqueueTask
: (task
: Task
<Data
>) => number
275 * Prepends a task to the tasks queue.
277 * @param task - The task to prepend.
278 * @returns The tasks queue size.
280 readonly unshiftTask
: (task
: Task
<Data
>) => number
284 * @returns The dequeued task.
286 readonly dequeueTask
: () => Task
<Data
> | undefined
288 * Pops a task from the tasks queue.
290 * @returns The popped task.
292 readonly popTask
: () => Task
<Data
> | undefined
294 * Clears tasks queue.
296 readonly clearTasksQueue
: () => void
298 * Whether the worker node has back pressure (i.e. its tasks queue is full).
300 * @returns `true` if the worker node has back pressure, `false` otherwise.
302 readonly hasBackPressure
: () => boolean
304 * Resets usage statistics.
306 readonly resetUsage
: () => void
308 * Closes communication channel.
310 readonly closeChannel
: () => void
312 * Gets task function worker usage statistics.
314 * @param name - The task function name.
315 * @returns The task function worker usage statistics if the task function worker usage statistics are initialized, `undefined` otherwise.
317 readonly getTaskFunctionWorkerUsage
: (name
: string) => WorkerUsage
| undefined