From 6546b6a8ba008ea2be1a3ecc129054f4b84417c2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 26 Aug 2024 15:25:54 +0200 Subject: [PATCH] refactor: cleanup worker node back pressure detection implementation MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/pools/abstract-pool.ts | 8 +++++--- src/pools/worker-node.ts | 13 ++++++++----- src/pools/worker.ts | 5 ----- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 6bee0367..d572bc0f 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -1192,9 +1192,11 @@ export abstract class AbstractPool< private hasBackPressure (): boolean { return ( this.opts.enableTasksQueue === true && - this.workerNodes.findIndex( - workerNode => !workerNode.hasBackPressure() - ) === -1 + this.workerNodes.reduce( + (accumulator, workerNode) => + workerNode.info.backPressure ? accumulator + 1 : accumulator, + 0 + ) === this.workerNodes.length ) } diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index b29601b3..867640f4 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -87,6 +87,14 @@ export class WorkerNode } } + /** + * Whether the worker node has back pressure (i.e. its tasks queue is full). + * @returns `true` if the worker node has back pressure, `false` otherwise. + */ + private hasBackPressure (): boolean { + return this.tasksQueue.size >= this.tasksQueueBackPressureSize + } + private initTaskFunctionWorkerUsage (name: string): WorkerUsage { const getTaskFunctionQueueSize = (): number => { let taskFunctionQueueSize = 0 @@ -254,11 +262,6 @@ export class WorkerNode return this.taskFunctionsUsage.get(name) } - /** @inheritdoc */ - public hasBackPressure (): boolean { - return this.tasksQueue.size >= this.tasksQueueBackPressureSize - } - /** @inheritdoc */ public registerOnceWorkerEventHandler ( event: string, diff --git a/src/pools/worker.ts b/src/pools/worker.ts index f67ec2f6..3c9c6580 100644 --- a/src/pools/worker.ts +++ b/src/pools/worker.ts @@ -323,11 +323,6 @@ export interface IWorkerNode * @returns The task function worker usage statistics if the task function worker usage statistics are initialized, `undefined` otherwise. */ readonly getTaskFunctionWorkerUsage: (name: string) => undefined | WorkerUsage - /** - * Whether the worker node has back pressure (i.e. its tasks queue is full). - * @returns `true` if the worker node has back pressure, `false` otherwise. - */ - readonly hasBackPressure: () => boolean /** * Worker info. */ -- 2.34.1