From 0fd5208a03884fb04679800331edc451b5f0caaa Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Thu, 30 Apr 2026 20:40:11 -0700 Subject: [PATCH 1/3] perf: avoid redundant scans in BalancedPool dispatcher selection Assisted-by: openai:gpt-5.5 Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com> --- lib/dispatcher/balanced-pool.js | 35 +++++++++++++++------------------ 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/lib/dispatcher/balanced-pool.js b/lib/dispatcher/balanced-pool.js index 8bad487b3f2..f0796b03e28 100644 --- a/lib/dispatcher/balanced-pool.js +++ b/lib/dispatcher/balanced-pool.js @@ -164,32 +164,25 @@ class BalancedPool extends PoolBase { throw new BalancedPoolMissingUpstreamError() } - const dispatcher = this[kClients].find(dispatcher => ( - !dispatcher[kNeedDrain] && - dispatcher.closed !== true && - dispatcher.destroyed !== true - )) - - if (!dispatcher) { - return - } - - const allClientsBusy = this[kClients].map(pool => pool[kNeedDrain]).reduce((a, b) => a && b, true) - - if (allClientsBusy) { - return - } - let counter = 0 - let maxWeightIndex = this[kClients].findIndex(pool => !pool[kNeedDrain]) + let maxWeightIndex = -1 while (counter++ < this[kClients].length) { this[kIndex] = (this[kIndex] + 1) % this[kClients].length const pool = this[kClients][this[kIndex]] + const available = ( + !pool[kNeedDrain] && + pool.closed !== true && + pool.destroyed !== true + ) // find pool index with the largest weight - if (pool[kWeight] > this[kClients][maxWeightIndex][kWeight] && !pool[kNeedDrain]) { + if (available && ( + maxWeightIndex === -1 || + pool[kWeight] > this[kClients][maxWeightIndex][kWeight] || + (pool[kWeight] === this[kClients][maxWeightIndex][kWeight] && this[kIndex] < maxWeightIndex) + )) { maxWeightIndex = this[kIndex] } @@ -202,11 +195,15 @@ class BalancedPool extends PoolBase { this[kCurrentWeight] = this[kMaxWeightPerServer] } } - if (pool[kWeight] >= this[kCurrentWeight] && (!pool[kNeedDrain])) { + if (available && pool[kWeight] >= this[kCurrentWeight]) { return pool } } + if (maxWeightIndex === -1) { + return + } + this[kCurrentWeight] = this[kClients][maxWeightIndex][kWeight] this[kIndex] = maxWeightIndex return this[kClients][maxWeightIndex] From 91b75bc33eee03dec1102cb764231d1d8288a563 Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Thu, 30 Apr 2026 21:02:23 -0700 Subject: [PATCH 2/3] chore: drop unnecessary BalancedPool equal-weight tie-breaker --- lib/dispatcher/balanced-pool.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/dispatcher/balanced-pool.js b/lib/dispatcher/balanced-pool.js index f0796b03e28..c3d22fa3263 100644 --- a/lib/dispatcher/balanced-pool.js +++ b/lib/dispatcher/balanced-pool.js @@ -180,8 +180,7 @@ class BalancedPool extends PoolBase { // find pool index with the largest weight if (available && ( maxWeightIndex === -1 || - pool[kWeight] > this[kClients][maxWeightIndex][kWeight] || - (pool[kWeight] === this[kClients][maxWeightIndex][kWeight] && this[kIndex] < maxWeightIndex) + pool[kWeight] > this[kClients][maxWeightIndex][kWeight] )) { maxWeightIndex = this[kIndex] } From dca2afc24b706083be3c80866bffd91d2a4b8485 Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Thu, 30 Apr 2026 22:14:46 -0700 Subject: [PATCH 3/3] chore: simplify BalancedPool availability checks --- lib/dispatcher/balanced-pool.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/dispatcher/balanced-pool.js b/lib/dispatcher/balanced-pool.js index c3d22fa3263..ca14bf6ed1d 100644 --- a/lib/dispatcher/balanced-pool.js +++ b/lib/dispatcher/balanced-pool.js @@ -171,19 +171,6 @@ class BalancedPool extends PoolBase { while (counter++ < this[kClients].length) { this[kIndex] = (this[kIndex] + 1) % this[kClients].length const pool = this[kClients][this[kIndex]] - const available = ( - !pool[kNeedDrain] && - pool.closed !== true && - pool.destroyed !== true - ) - - // find pool index with the largest weight - if (available && ( - maxWeightIndex === -1 || - pool[kWeight] > this[kClients][maxWeightIndex][kWeight] - )) { - maxWeightIndex = this[kIndex] - } // decrease the current weight every `this[kClients].length`. if (this[kIndex] === 0) { @@ -194,7 +181,22 @@ class BalancedPool extends PoolBase { this[kCurrentWeight] = this[kMaxWeightPerServer] } } - if (available && pool[kWeight] >= this[kCurrentWeight]) { + + // Skip unavailable pools after updating the current weight for this cycle. + if ( + pool[kNeedDrain] || + pool.closed === true || + pool.destroyed === true + ) { + continue + } + + // Track the best fallback if no pool matches the current weight. + if (maxWeightIndex === -1 || pool[kWeight] > this[kClients][maxWeightIndex][kWeight]) { + maxWeightIndex = this[kIndex] + } + + if (pool[kWeight] >= this[kCurrentWeight]) { return pool } }