Repository navigation
perf: stop counting every client to check for a connected one - #6287
Merged
Merged
Conversation
Agent and Socks5ProxyAgent ask whether a pool has at least one connected client before closing it as unused. Pool answered by adding up kConnected over all its clients, so with one long-lived client per open response every closing connection visited the whole pool. Answer the yes/no question directly: DispatcherBase keeps the generic kConnected > 0, and PoolBase stops at the first connected client. Assisted-by: OpenCode (DeepSeek V4.1 Flash Free) Signed-off-by: Nabeel-akk <Account@lushvista.cc>
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The new connectivity check can prematurely close and unregister mock or custom dispatchers, losing configured interceptors or disrupting connections that remain open.
2 open issues
What changed in this PR
Optimizes Undici’s idle-pool checks by checking for any connected client instead of counting every connected client.
Changes:
- Adds an internal connectivity getter with a default implementation and an early-exit pool override.
- Updates
AgentandSocks5ProxyAgentcleanup checks to use it. - Adds a regression test for early exit while leaving connection counts unchanged.
| File | Description |
|---|---|
test/agent-connection-management.js |
Verifies the pool check stops at the first connected client. |
lib/dispatcher/socks5-proxy-agent.js |
Uses the new getter during pool cleanup. |
lib/dispatcher/pool-base.js |
Adds the early-exit connectivity getter. |
lib/dispatcher/dispatcher-base.js |
Provides a default getter based on the connection count. |
lib/dispatcher/agent.js |
Uses the new getter during idle-dispatcher cleanup. |
lib/core/symbols.js |
Defines the internal connectivity symbol. |
Review effort: Max
| // Closing it here lets the replacement Client finish those requests | ||
| // and then destroys that new connection with ClientDestroyedError. | ||
| if (dispatcher[kConnected] > 0 || dispatcher[kBusy]) { | ||
| if (dispatcher[kHasConnectedClient] || dispatcher[kBusy]) { |
| // connected client instead of counting all of them. | ||
| get [kHasConnectedClient] () { | ||
| for (let i = 0; i < this[kClients].length; i++) { | ||
| if (this[kClients][i][kConnected]) { |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #6287 +/- ##
==========================================
- Coverage 91.78% 91.78% -0.01%
==========================================
Files 113 113
Lines 44920 44941 +21
==========================================
+ Hits 41232 41251 +19
- Misses 3688 3690 +2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
metcoder95
approved these changes
Dec 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

This relates to...
#6279
Rationale
Agentchecks whether a pool still has a connected client every time one of that pool's connections closes, so it can drop the pool when it is idle.PoolBase[kConnected]answers that question by adding upkConnectedover every client, and each client readssocket.destroyed. A pool holds one client per open response, so with long-lived responses (SSE, long polling, streaming RPC) each closing connection walks the whole pool.Socks5ProxyAgenthas the same check.The check only needs a yes/no answer, so it does not have to visit clients after the first connected one.
Changes
kHasConnectedClientsymbol.DispatcherBaseanswers it generically withkConnected > 0, so any dispatcher keeps working.PoolBaseoverrides it and returns on the first connected client.AgentandSocks5ProxyAgentuse it instead ofkConnected > 0.test/agent-connection-management.js: a pool with 100 clients, only the first connected, answerstrueafter reading exactly one client'skConnected. It fails without thePoolBaseoverride.kConnecteditself is unchanged — stats andkFreestill need the count.Status
Tests
npm run lintpasses on the changed files.I did not benchmark it: on this Windows host the reproduction from the issue runs into ephemeral-port pressure before the numbers get interesting, and the results were not stable enough to be worth quoting. The added test pins the behaviour that matters (one client read instead of all of them).
AI assistance
Prepared with AI assistance (OpenCode, DeepSeek V4.1 Flash Free) under my direction; the regression test and the reasoning above were checked against the code.