X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fpools%2Fworker-node.ts;h=09e4a7b5cd8831360365a7a88e338e3b921eaf91;hb=0676e5d32c8f8ee5c5b7f5998f4973b0a276460f;hp=a493b65ded97c3651076a21a244fd41fe3ce8620;hpb=658b9aa08266ed9a8ae3c0fc947d237fa2674f09;p=poolifier.git diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index a493b65d..09e4a7b5 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -1,27 +1,70 @@ +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 } from '../utils' import { type IWorker, type IWorkerNode, - type Task, type WorkerInfo, type WorkerType, WorkerTypes, type WorkerUsage } from './worker' +/** + * Worker node. + * + * @typeParam Worker - Type of worker. + * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. + */ 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 taskFunctionsUsage: Map private readonly tasksQueue: Queue> + private readonly tasksQueueBackPressureSize: number - constructor (worker: Worker, workerType: WorkerType) { + /** + * Constructs a new worker node. + * + * @param worker - The worker. + * @param workerType - The worker type. + * @param poolMaxSize - The pool maximum size. + */ + constructor (worker: Worker, workerType: WorkerType, poolMaxSize: number) { + if (worker == null) { + throw new Error('Cannot construct a worker node without a worker') + } + if (workerType == null) { + throw new Error('Cannot construct a worker node without a worker type') + } + if (poolMaxSize == null) { + throw new Error( + 'Cannot construct a worker node without a pool maximum size' + ) + } + if (isNaN(poolMaxSize)) { + throw new Error( + 'Cannot construct a worker node with a NaN pool maximum size' + ) + } this.worker = worker this.info = this.initWorkerInfo(worker, workerType) + if (workerType === WorkerTypes.thread) { + this.messageChannel = new MessageChannel() + } this.usage = this.initWorkerUsage() + this.taskFunctionsUsage = new Map() this.tasksQueue = new Queue>() + this.tasksQueueBackPressureSize = Math.pow(poolMaxSize, 2) } /** @inheritdoc */ @@ -30,7 +73,7 @@ implements IWorkerNode { } /** - * Worker node tasks queue maximum size. + * Tasks queue maximum size. * * @returns The tasks queue maximum size. */ @@ -53,8 +96,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 { @@ -62,7 +147,7 @@ implements IWorkerNode { id: this.getWorkerId(worker, workerType), type: workerType, dynamic: false, - started: true + ready: false } } @@ -70,7 +155,7 @@ implements IWorkerNode { const getTasksQueueSize = (): number => { return this.tasksQueueSize() } - const getTasksMaxQueueSize = (): number => { + const getTasksQueueMaxSize = (): number => { return this.tasksQueueMaxSize() } return { @@ -81,7 +166,43 @@ implements IWorkerNode { return getTasksQueueSize() }, get maxQueued (): number { - return getTasksMaxQueueSize() + return getTasksQueueMaxSize() + }, + failed: 0 + }, + runTime: { + history: new CircularArray() + }, + waitTime: { + history: new CircularArray() + }, + elu: { + idle: { + history: new CircularArray() + }, + active: { + history: new CircularArray() + } + } + } + } + + private initTaskFunctionWorkerUsage (name: string): WorkerUsage { + const getTaskQueueSize = (): number => { + let taskQueueSize = 0 + for (const task of this.tasksQueue) { + if (task.name === name) { + ++taskQueueSize + } + } + return taskQueueSize + } + return { + tasks: { + executed: 0, + executing: 0, + get queued (): number { + return getTaskQueueSize() }, failed: 0 }, @@ -106,6 +227,7 @@ implements IWorkerNode { * Gets the worker id. * * @param worker - The worker. + * @param workerType - The worker type. * @returns The worker id. */ private getWorkerId (