From: Jérôme Benoit Date: Mon, 26 Aug 2024 13:25:54 +0000 (+0200) Subject: refactor: cleanup worker node back pressure detection implementation X-Git-Tag: v4.2.5~2 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=6546b6a8ba008ea2be1a3ecc129054f4b84417c2;p=poolifier.git refactor: cleanup worker node back pressure detection implementation Signed-off-by: Jérôme Benoit --- 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. */