X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fworker-node.ts;h=5a25f8d9bf233c4afdd205bfd504a6e43be23aab;hb=9a38f99e676160c0bc7d28fe88f27b01fa31b5a1;hp=ca275dedab806b9a4cc2c9c6aee5d3cf550b99f5;hpb=b7e141c40bccfd7a4ec0ff98b7829f7d296f048b;p=poolifier.git diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index ca275ded..5a25f8d9 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -20,6 +20,7 @@ import { WorkerTypes, type WorkerUsage } from './worker' +import { checkWorkerNodeArguments } from './utils' /** * Worker node. @@ -46,6 +47,7 @@ implements IWorkerNode { /** @inheritdoc */ public onEmptyQueue?: WorkerNodeEventCallback private readonly tasksQueue: Deque> + private onBackPressureStarted: boolean private onEmptyQueueCount: number private readonly taskFunctionsUsage: Map @@ -56,7 +58,7 @@ implements IWorkerNode { * @param tasksQueueBackPressureSize - The tasks queue back pressure size. */ constructor (worker: Worker, tasksQueueBackPressureSize: number) { - this.checkWorkerNodeArguments(worker, tasksQueueBackPressureSize) + checkWorkerNodeArguments(worker, tasksQueueBackPressureSize) this.worker = worker this.info = this.initWorkerInfo(worker) this.usage = this.initWorkerUsage() @@ -65,6 +67,7 @@ implements IWorkerNode { } this.tasksQueueBackPressureSize = tasksQueueBackPressureSize this.tasksQueue = new Deque>() + this.onBackPressureStarted = false this.onEmptyQueueCount = 0 this.taskFunctionsUsage = new Map() } @@ -77,8 +80,14 @@ implements IWorkerNode { /** @inheritdoc */ public enqueueTask (task: Task): number { const tasksQueueSize = this.tasksQueue.push(task) - if (this.onBackPressure != null && this.hasBackPressure()) { + if ( + this.onBackPressure != null && + this.hasBackPressure() && + !this.onBackPressureStarted + ) { + this.onBackPressureStarted = true this.onBackPressure(this.info.id as number) + this.onBackPressureStarted = false } return tasksQueueSize } @@ -86,8 +95,14 @@ implements IWorkerNode { /** @inheritdoc */ public unshiftTask (task: Task): number { const tasksQueueSize = this.tasksQueue.unshift(task) - if (this.onBackPressure != null && this.hasBackPressure()) { + if ( + this.onBackPressure != null && + this.hasBackPressure() && + !this.onBackPressureStarted + ) { + this.onBackPressureStarted = true this.onBackPressure(this.info.id as number) + this.onBackPressureStarted = false } return tasksQueueSize } @@ -95,7 +110,11 @@ implements IWorkerNode { /** @inheritdoc */ public dequeueTask (): Task | undefined { const task = this.tasksQueue.shift() - if (this.onEmptyQueue != null && this.tasksQueue.size === 0) { + if ( + this.onEmptyQueue != null && + this.tasksQueue.size === 0 && + this.onEmptyQueueCount === 0 + ) { this.startOnEmptyQueue().catch(EMPTY_FUNCTION) } return task @@ -104,7 +123,11 @@ implements IWorkerNode { /** @inheritdoc */ public popTask (): Task | undefined { const task = this.tasksQueue.pop() - if (this.onEmptyQueue != null && this.tasksQueue.size === 0) { + if ( + this.onEmptyQueue != null && + this.tasksQueue.size === 0 && + this.onEmptyQueueCount === 0 + ) { this.startOnEmptyQueue().catch(EMPTY_FUNCTION) } return task @@ -139,21 +162,21 @@ implements IWorkerNode { /** @inheritdoc */ public getTaskFunctionWorkerUsage (name: string): WorkerUsage | undefined { - if (!Array.isArray(this.info.taskFunctions)) { + if (!Array.isArray(this.info.taskFunctionNames)) { 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 + Array.isArray(this.info.taskFunctionNames) && + this.info.taskFunctionNames.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] + name = this.info.taskFunctionNames[1] } if (!this.taskFunctionsUsage.has(name)) { this.taskFunctionsUsage.set(name, this.initTaskFunctionWorkerUsage(name)) @@ -161,6 +184,11 @@ implements IWorkerNode { return this.taskFunctionsUsage.get(name) } + /** @inheritdoc */ + public deleteTaskFunctionWorkerUsage (name: string): boolean { + return this.taskFunctionsUsage.delete(name) + } + private async startOnEmptyQueue (): Promise { if ( this.onEmptyQueueCount > 0 && @@ -169,8 +197,8 @@ implements IWorkerNode { this.onEmptyQueueCount = 0 return } - (this.onEmptyQueue as WorkerNodeEventCallback)(this.info.id as number) ++this.onEmptyQueueCount + this.onEmptyQueue?.(this.info.id as number) await sleep(exponentialDelay(this.onEmptyQueueCount)) await this.startOnEmptyQueue() } @@ -227,7 +255,7 @@ implements IWorkerNode { for (const task of this.tasksQueue) { if ( (task.name === DEFAULT_TASK_NAME && - name === (this.info.taskFunctions as string[])[1]) || + name === (this.info.taskFunctionNames as string[])[1]) || (task.name !== DEFAULT_TASK_NAME && name === task.name) ) { ++taskFunctionQueueSize @@ -261,28 +289,4 @@ implements IWorkerNode { } } } - - private checkWorkerNodeArguments ( - worker: Worker, - tasksQueueBackPressureSize: number - ): void { - if (worker == null) { - throw new TypeError('Cannot construct a worker node without a worker') - } - 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' - ) - } - if (tasksQueueBackPressureSize <= 0) { - throw new RangeError( - 'Cannot construct a worker node with a tasks queue back pressure size that is not a positive integer' - ) - } - } }