X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fworker-node.ts;h=208acdab423bc3293aa603165c05ef4bb44da31a;hb=c3f0a07446e3751eed1df850525672f75562429f;hp=bbca79135b5d7cb3df575abccebd70c0719296f2;hpb=922a735041f409408620e171d7f91edad70a63cd;p=poolifier.git diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index bbca7913..208acdab 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -1,9 +1,11 @@ +import { MessageChannel } from 'node:worker_threads' import { CircularArray } from '../circular-array' -import { Queue } from '../queue' +import type { Task } from '../utility-types' +import { DEFAULT_TASK_NAME, once } from '../utils' +import { Deque } from '../deque' import { type IWorker, type IWorkerNode, - type Task, type WorkerInfo, type WorkerType, WorkerTypes, @@ -18,22 +20,60 @@ import { */ export class WorkerNode implements IWorkerNode { + /** @inheritdoc */ public readonly worker: Worker + /** @inheritdoc */ public readonly info: WorkerInfo + /** @inheritdoc */ + public messageChannel?: MessageChannel + /** @inheritdoc */ public usage: WorkerUsage - private readonly tasksQueue: Queue> + /** @inheritdoc */ + public tasksQueueBackPressureSize: number + /** @inheritdoc */ + public onBackPressure?: (workerId: number) => void + private readonly taskFunctionsUsage: Map + private readonly tasksQueue: Deque> /** * Constructs a new worker node. * * @param worker - The worker. * @param workerType - The worker type. + * @param tasksQueueBackPressureSize - The tasks queue back pressure size. */ - constructor (worker: Worker, workerType: WorkerType) { + constructor ( + worker: Worker, + workerType: WorkerType, + tasksQueueBackPressureSize: number + ) { + if (worker == null) { + throw new TypeError('Cannot construct a worker node without a worker') + } + if (workerType == null) { + throw new TypeError( + 'Cannot construct a worker node without a worker type' + ) + } + if (tasksQueueBackPressureSize == null) { + throw new TypeError( + 'Cannot construct a worker node without a tasks queue back pressure size' + ) + } + if (!Number.isSafeInteger(tasksQueueBackPressureSize)) { + throw new TypeError( + 'Cannot construct a worker node with a tasks queue back pressure size that is not an integer' + ) + } this.worker = worker this.info = this.initWorkerInfo(worker, workerType) + if (workerType === WorkerTypes.thread) { + this.messageChannel = new MessageChannel() + } this.usage = this.initWorkerUsage() - this.tasksQueue = new Queue>() + this.taskFunctionsUsage = new Map() + this.tasksQueue = new Deque>() + this.tasksQueueBackPressureSize = tasksQueueBackPressureSize } /** @inheritdoc */ @@ -42,7 +82,7 @@ implements IWorkerNode { } /** - * Worker node tasks queue maximum size. + * Tasks queue maximum size. * * @returns The tasks queue maximum size. */ @@ -52,12 +92,30 @@ implements IWorkerNode { /** @inheritdoc */ public enqueueTask (task: Task): number { - return this.tasksQueue.enqueue(task) + const tasksQueueSize = this.tasksQueue.push(task) + if (this.onBackPressure != null && this.hasBackPressure()) { + once(this.onBackPressure)(this.info.id as number) + } + return tasksQueueSize + } + + /** @inheritdoc */ + public unshiftTask (task: Task): number { + const tasksQueueSize = this.tasksQueue.unshift(task) + if (this.onBackPressure != null && this.hasBackPressure()) { + once(this.onBackPressure)(this.info.id as number) + } + return tasksQueueSize } /** @inheritdoc */ public dequeueTask (): Task | undefined { - return this.tasksQueue.dequeue() + return this.tasksQueue.shift() + } + + /** @inheritdoc */ + public popTask (): Task | undefined { + return this.tasksQueue.pop() } /** @inheritdoc */ @@ -65,8 +123,50 @@ implements IWorkerNode { this.tasksQueue.clear() } + /** @inheritdoc */ + public hasBackPressure (): boolean { + return this.tasksQueue.size >= this.tasksQueueBackPressureSize + } + + /** @inheritdoc */ public resetUsage (): void { this.usage = this.initWorkerUsage() + this.taskFunctionsUsage.clear() + } + + /** @inheritdoc */ + public closeChannel (): void { + if (this.messageChannel != null) { + this.messageChannel?.port1.unref() + this.messageChannel?.port2.unref() + this.messageChannel?.port1.close() + this.messageChannel?.port2.close() + delete this.messageChannel + } + } + + /** @inheritdoc */ + public getTaskFunctionWorkerUsage (name: string): WorkerUsage | undefined { + if (!Array.isArray(this.info.taskFunctions)) { + throw new Error( + `Cannot get task function worker usage for task function name '${name}' when task function names list is not yet defined` + ) + } + if ( + Array.isArray(this.info.taskFunctions) && + this.info.taskFunctions.length < 3 + ) { + throw new Error( + `Cannot get task function worker usage for task function name '${name}' when task function names list has less than 3 elements` + ) + } + if (name === DEFAULT_TASK_NAME) { + name = this.info.taskFunctions[1] + } + if (!this.taskFunctionsUsage.has(name)) { + this.taskFunctionsUsage.set(name, this.initTaskFunctionWorkerUsage(name)) + } + return this.taskFunctionsUsage.get(name) } private initWorkerInfo (worker: Worker, workerType: WorkerType): WorkerInfo { @@ -114,6 +214,46 @@ implements IWorkerNode { } } + private initTaskFunctionWorkerUsage (name: string): WorkerUsage { + const getTaskFunctionQueueSize = (): number => { + let taskFunctionQueueSize = 0 + for (const task of this.tasksQueue) { + if ( + (task.name === DEFAULT_TASK_NAME && + name === (this.info.taskFunctions as string[])[1]) || + (task.name !== DEFAULT_TASK_NAME && name === task.name) + ) { + ++taskFunctionQueueSize + } + } + return taskFunctionQueueSize + } + return { + tasks: { + executed: 0, + executing: 0, + get queued (): number { + return getTaskFunctionQueueSize() + }, + failed: 0 + }, + runTime: { + history: new CircularArray() + }, + waitTime: { + history: new CircularArray() + }, + elu: { + idle: { + history: new CircularArray() + }, + active: { + history: new CircularArray() + } + } + } + } + /** * Gets the worker id. *