X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fworker.ts;h=52b56a2411f0f41f7c56452270b2c63be7ecd0a5;hb=68cbdc846878bc058323b757a68b4c83eedc6388;hp=09253242f7d0af88204ba9989fb37a7fa12cc287;hpb=7884d1837ee55026fe5204a56f4ebeca17e7e7dd;p=poolifier.git diff --git a/src/pools/worker.ts b/src/pools/worker.ts index 09253242..52b56a24 100644 --- a/src/pools/worker.ts +++ b/src/pools/worker.ts @@ -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 = (this: Worker) => void + /** * Callback invoked if the worker has received a message. */ @@ -18,11 +23,6 @@ export type ErrorHandler = ( error: Error ) => void -/** - * Callback invoked when the worker has started successfully. - */ -export type OnlineHandler = (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. */ @@ -189,7 +193,7 @@ 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) => void @@ -212,13 +216,30 @@ export interface IWorkerNode { */ readonly info: WorkerInfo /** - * Message channel. + * Message channel (worker_threads only). */ readonly messageChannel?: MessageChannel /** * Worker usage statistics. */ usage: WorkerUsage + /** + * 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. + * + * @param workerId - The worker id. + */ + onBackPressure?: (workerId: number) => void + /** + * Callback invoked when worker node tasks queue is empty. + * + * @param workerId - The worker id. + */ + onEmptyQueue?: (workerId: number) => void /** * Tasks queue size. * @@ -229,21 +250,40 @@ export interface IWorkerNode { * Enqueue task. * * @param task - The task to queue. - * @returns The task queue size. + * @returns The tasks queue size. */ readonly enqueueTask: (task: Task) => number + /** + * Prepends a task to the tasks queue. + * + * @param task - The task to prepend. + * @returns The tasks queue size. + */ + readonly unshiftTask: (task: Task) => number /** * Dequeue task. * * @returns The dequeued task. */ readonly dequeueTask: () => Task | undefined + /** + * Pops a task from the tasks queue. + * + * @returns The popped task. + */ + readonly popTask: () => Task | 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 /** @@ -251,7 +291,10 @@ export interface IWorkerNode { */ 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 }