X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fworker-node.ts;h=d8044032014d02d36e6cbe70ffd10026f6a085ea;hb=f7426dd9c3d10e5b605b5f9d4417ccdabf1044d0;hp=09e4a7b5cd8831360365a7a88e338e3b921eaf91;hpb=1416660efeb6aabf366295ebc79067ccc4df9897;p=poolifier.git diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index 09e4a7b5..d8044032 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -1,8 +1,8 @@ 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 { DEFAULT_TASK_NAME, once } from '../utils' +import { Deque } from '../deque' import { type IWorker, type IWorkerNode, @@ -28,32 +28,41 @@ implements IWorkerNode { public messageChannel?: MessageChannel /** @inheritdoc */ public usage: WorkerUsage + /** @inheritdoc */ + public tasksQueueBackPressureSize: number + /** @inheritdoc */ + public onBackPressure?: (workerId: number) => void private readonly taskFunctionsUsage: Map - private readonly tasksQueue: Queue> - private readonly tasksQueueBackPressureSize: number + private readonly tasksQueue: Deque> /** * Constructs a new worker node. * * @param worker - The worker. * @param workerType - The worker type. - * @param poolMaxSize - The pool maximum size. + * @param tasksQueueBackPressureSize - The tasks queue back pressure size. */ - constructor (worker: Worker, workerType: WorkerType, poolMaxSize: number) { + constructor ( + worker: Worker, + workerType: WorkerType, + tasksQueueBackPressureSize: number + ) { if (worker == null) { - throw new Error('Cannot construct a worker node without a worker') + throw new TypeError('Cannot construct a worker node without a worker') } if (workerType == null) { - throw new Error('Cannot construct a worker node without a worker type') + throw new TypeError( + '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 (tasksQueueBackPressureSize == null) { + throw new TypeError( + 'Cannot construct a worker node without a tasks queue back pressure size' ) } - if (isNaN(poolMaxSize)) { - throw new Error( - 'Cannot construct a worker node with a NaN pool maximum 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 @@ -63,8 +72,8 @@ implements IWorkerNode { } this.usage = this.initWorkerUsage() this.taskFunctionsUsage = new Map() - this.tasksQueue = new Queue>() - this.tasksQueueBackPressureSize = Math.pow(poolMaxSize, 2) + this.tasksQueue = new Deque>() + this.tasksQueueBackPressureSize = tasksQueueBackPressureSize } /** @inheritdoc */ @@ -83,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)(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)(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 */ @@ -188,21 +215,25 @@ implements IWorkerNode { } private initTaskFunctionWorkerUsage (name: string): WorkerUsage { - const getTaskQueueSize = (): number => { - let taskQueueSize = 0 + 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() }, failed: 0 },