X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fworker.ts;h=6b387a96088d430d455ed0a4dbb3759c1cbbe86e;hb=0717df3dc168fc35b8268713ac8b7bde86f024a9;hp=1de3cf09b6932053605efd6cc4719bfbe8bcf14c;hpb=a5d152043363ea21ec667205c88e0b9f2a6ff04e;p=poolifier.git diff --git a/src/pools/worker.ts b/src/pools/worker.ts index 1de3cf09..6b387a96 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. */ @@ -141,10 +141,6 @@ export interface WorkerInfo { * Task function names. */ taskFunctions?: string[] - /** - * Message channel. - */ - messageChannel?: MessageChannel } /** @@ -215,10 +211,25 @@ export interface IWorkerNode { * Worker info. */ readonly info: WorkerInfo + /** + * 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 /** * Tasks queue size. * @@ -229,21 +240,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 +281,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 }