X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fworker-node.ts;h=64880fcb9820c098e37fb103bd27b8563073833a;hb=671d515455c745dc74f4c385fe23683975bfc3df;hp=2c39393b05eb416b16f7aba82cdba417ab5b4758;hpb=85aeb3f356d749b96361e74cf17d403a697e3dd7;p=poolifier.git diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index 2c39393b..64880fcb 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -2,6 +2,7 @@ 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, @@ -19,24 +20,35 @@ 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 tasksUsage: Map private readonly tasksQueue: Queue> + private readonly tasksQueueBackPressureMaxSize: number /** * 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) { + constructor (worker: Worker, workerType: WorkerType, poolMaxSize: number) { this.worker = worker this.info = this.initWorkerInfo(worker, workerType) + if (workerType === WorkerTypes.thread) { + this.messageChannel = new MessageChannel() + } this.usage = this.initWorkerUsage() this.tasksUsage = new Map() this.tasksQueue = new Queue>() + this.tasksQueueBackPressureMaxSize = Math.pow(poolMaxSize, 2) } /** @inheritdoc */ @@ -68,14 +80,42 @@ implements IWorkerNode { this.tasksQueue.clear() } + /** @inheritdoc */ + public hasBackPressure (): boolean { + return this.tasksQueueSize() >= this.tasksQueueBackPressureMaxSize + } + /** @inheritdoc */ public resetUsage (): void { this.usage = this.initWorkerUsage() this.tasksUsage.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 getTaskWorkerUsage (name: string): WorkerUsage | undefined { + if (!Array.isArray(this.info.taskFunctions)) { + throw new Error( + `Cannot get task worker usage for task function name '${name}' when task function names list is not yet defined` + ) + } + if ( + name === DEFAULT_TASK_NAME && + Array.isArray(this.info.taskFunctions) && + this.info.taskFunctions.length > 1 + ) { + name = this.info.taskFunctions[1] + } if (!this.tasksUsage.has(name)) { this.tasksUsage.set(name, this.initTaskWorkerUsage(name)) } @@ -87,10 +127,7 @@ implements IWorkerNode { id: this.getWorkerId(worker, workerType), type: workerType, dynamic: false, - ready: false, - ...(workerType === WorkerTypes.thread && { - messageChannel: new MessageChannel() - }) + ready: false } }