X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fworker-node.ts;h=09e4a7b5cd8831360365a7a88e338e3b921eaf91;hb=46e29227a97fe04f886969199f1c14338af50543;hp=923fe9b222a4ea9dc4279294d51d0755f67f1ba7;hpb=b558f6b5a5625753de41024325e40e1cbd03eda1;p=poolifier.git diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index 923fe9b2..09e4a7b5 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -20,25 +20,51 @@ import { */ export class WorkerNode implements IWorkerNode { + /** @inheritdoc */ public readonly worker: Worker + /** @inheritdoc */ public readonly info: WorkerInfo + /** @inheritdoc */ + public messageChannel?: MessageChannel + /** @inheritdoc */ public usage: WorkerUsage - public taskFunctions!: string[] - private readonly tasksUsage: Map + private readonly taskFunctionsUsage: Map private readonly tasksQueue: Queue> + private readonly tasksQueueBackPressureSize: 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) { + 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.tasksUsage = new Map() + this.taskFunctionsUsage = new Map() this.tasksQueue = new Queue>() + this.tasksQueueBackPressureSize = Math.pow(poolMaxSize, 2) } /** @inheritdoc */ @@ -70,36 +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.tasksUsage.clear() + this.taskFunctionsUsage.clear() } /** @inheritdoc */ public closeChannel (): void { - if (this.info.messageChannel != null) { - this.info.messageChannel?.port1.unref() - this.info.messageChannel?.port2.unref() - this.info.messageChannel?.port1.close() - this.info.messageChannel?.port2.close() - delete this.info.messageChannel + 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 { + 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 ( - name === DEFAULT_TASK_NAME && - Array.isArray(this.taskFunctions) && - this.taskFunctions.length > 1 + Array.isArray(this.info.taskFunctions) && + this.info.taskFunctions.length < 3 ) { - name = this.taskFunctions[1] + 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.tasksUsage.has(name)) { - this.tasksUsage.set(name, this.initTaskWorkerUsage(name)) + if (!this.taskFunctionsUsage.has(name)) { + this.taskFunctionsUsage.set(name, this.initTaskFunctionWorkerUsage(name)) } - return this.tasksUsage.get(name) + return this.taskFunctionsUsage.get(name) } private initWorkerInfo (worker: Worker, workerType: WorkerType): WorkerInfo { @@ -107,10 +147,7 @@ implements IWorkerNode { id: this.getWorkerId(worker, workerType), type: workerType, dynamic: false, - ready: false, - ...(workerType === WorkerTypes.thread && { - messageChannel: new MessageChannel() - }) + ready: false } } @@ -150,7 +187,7 @@ implements IWorkerNode { } } - private initTaskWorkerUsage (name: string): WorkerUsage { + private initTaskFunctionWorkerUsage (name: string): WorkerUsage { const getTaskQueueSize = (): number => { let taskQueueSize = 0 for (const task of this.tasksQueue) {