X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fworker-node.ts;h=9ecfd012f9b5402c4676004f24f7c04ecc4bf020;hb=db746922cea3decbb34b5878d56a468d659d33f5;hp=2c39393b05eb416b16f7aba82cdba417ab5b4758;hpb=85aeb3f356d749b96361e74cf17d403a697e3dd7;p=poolifier.git diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index 2c39393b..9ecfd012 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -1,7 +1,13 @@ 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, + EMPTY_FUNCTION, + exponentialDelay, + sleep +} from '../utils' +import { Deque } from '../deque' import { type IWorker, type IWorkerNode, @@ -19,24 +25,64 @@ 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> + /** @inheritdoc */ + public tasksQueueBackPressureSize: number + /** @inheritdoc */ + public onBackPressure?: (workerId: number) => void + /** @inheritdoc */ + public onEmptyQueue?: (workerId: number) => void + private readonly taskFunctionsUsage: Map + private readonly tasksQueue: Deque> + private onEmptyQueueCount: number /** * 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.tasksUsage = new Map() - this.tasksQueue = new Queue>() + this.taskFunctionsUsage = new Map() + this.tasksQueue = new Deque>() + this.tasksQueueBackPressureSize = tasksQueueBackPressureSize + this.onEmptyQueueCount = 0 } /** @inheritdoc */ @@ -44,23 +90,40 @@ implements IWorkerNode { return this.tasksQueue.size } - /** - * Tasks queue maximum size. - * - * @returns The tasks queue maximum size. - */ - private tasksQueueMaxSize (): number { - return this.tasksQueue.maxSize + /** @inheritdoc */ + public enqueueTask (task: Task): number { + const tasksQueueSize = this.tasksQueue.push(task) + if (this.onBackPressure != null && this.hasBackPressure()) { + this.onBackPressure(this.info.id as number) + } + return tasksQueueSize } /** @inheritdoc */ - public enqueueTask (task: Task): number { - return this.tasksQueue.enqueue(task) + public unshiftTask (task: Task): number { + const tasksQueueSize = this.tasksQueue.unshift(task) + if (this.onBackPressure != null && this.hasBackPressure()) { + this.onBackPressure(this.info.id as number) + } + return tasksQueueSize } /** @inheritdoc */ public dequeueTask (): Task | undefined { - return this.tasksQueue.dequeue() + const task = this.tasksQueue.shift() + if (this.onEmptyQueue != null && this.tasksQueue.size === 0) { + this.startOnEmptyQueue().catch(EMPTY_FUNCTION) + } + return task + } + + /** @inheritdoc */ + public popTask (): Task | undefined { + const task = this.tasksQueue.pop() + if (this.onEmptyQueue != null && this.tasksQueue.size === 0) { + this.startOnEmptyQueue().catch(EMPTY_FUNCTION) + } + return task } /** @inheritdoc */ @@ -68,18 +131,61 @@ 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 getTaskWorkerUsage (name: string): WorkerUsage | undefined { - if (!this.tasksUsage.has(name)) { - this.tasksUsage.set(name, this.initTaskWorkerUsage(name)) + 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 async startOnEmptyQueue (): Promise { + if (this.tasksQueue.size > 0) { + this.onEmptyQueueCount = 0 + return } - return this.tasksUsage.get(name) + (this.onEmptyQueue as (workerId: number) => void)(this.info.id as number) + ++this.onEmptyQueueCount + await sleep(exponentialDelay(this.onEmptyQueueCount)) + await this.startOnEmptyQueue() } private initWorkerInfo (worker: Worker, workerType: WorkerType): WorkerInfo { @@ -87,19 +193,16 @@ implements IWorkerNode { id: this.getWorkerId(worker, workerType), type: workerType, dynamic: false, - ready: false, - ...(workerType === WorkerTypes.thread && { - messageChannel: new MessageChannel() - }) + ready: false } } private initWorkerUsage (): WorkerUsage { const getTasksQueueSize = (): number => { - return this.tasksQueueSize() + return this.tasksQueue.size } const getTasksQueueMaxSize = (): number => { - return this.tasksQueueMaxSize() + return this.tasksQueue.maxSize } return { tasks: { @@ -111,6 +214,7 @@ implements IWorkerNode { get maxQueued (): number { return getTasksQueueMaxSize() }, + stolen: 0, failed: 0 }, runTime: { @@ -130,23 +234,28 @@ implements IWorkerNode { } } - private initTaskWorkerUsage (name: string): WorkerUsage { - const getTaskQueueSize = (): number => { - let taskQueueSize = 0 + private initTaskFunctionWorkerUsage (name: string): WorkerUsage { + const getTaskFunctionQueueSize = (): number => { + let taskFunctionQueueSize = 0 for (const task of this.tasksQueue) { - if (task.name === name) { - ++taskQueueSize + if ( + (task.name === DEFAULT_TASK_NAME && + name === (this.info.taskFunctions as string[])[1]) || + (task.name !== DEFAULT_TASK_NAME && name === task.name) + ) { + ++taskFunctionQueueSize } } - return taskQueueSize + return taskFunctionQueueSize } return { tasks: { executed: 0, executing: 0, get queued (): number { - return getTaskQueueSize() + return getTaskFunctionQueueSize() }, + stolen: 0, failed: 0 }, runTime: {