perf: stop counting every client to check for a connected one #6287
lgtm
Agent.Options.factory accepts any public Dispatcher (types/agent.d.ts:24), but only DispatcherBase supplies the new getter. A custom dispatcher exposing kConnected without inheriting that getter is now removed and closed on disconnect or connectionError despite another connection remaining open, if it has no busy or pending work. Fall back to dispatcher[kConnected] > 0 when kHasConnectedClient is absent, and add a regression test for this factory case.
MockPool reports kConnected === 1 until explicitly closed (lib/mock/mock-pool.js:43–44), but this inherited getter ignores that state. When an Agent factory returns mockAgent.get(origin), the last passthrough disconnect now closes and unregisters the pool, losing its configured interceptors. Override kHasConnectedClient in MockPool to respect its synthetic connectivity and add a regression test.
<!-- ccr-overview-v2 -->
🟡 Changes recommended
The new connectivity check can prematurely close and unregister mock or custom dispatchers, losing configured interceptors or disrupting connections that remain open.
<details open>
<summary><strong>2 open issues</strong></summary>
</details>
<details>
<summary><strong>What changed in this PR</strong></summary>
Optimizes Undici’s idle-pool checks by checking for any connected client instead of counting every connected client.
Changes:
Agent and Socks5ProxyAgent cleanup checks to use it.| 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. |
</details>
<sub>Review effort: Max</sub>
Codecov Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 91.78%. Comparing base (
2bcc4b8) to head (9395082).:warning: Report is 3 commits behind head on main.
<details><summary>Additional details and impacted files</summary>
@@ 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
</details>
:umbrella: View full report in Codecov by Harness.
:loudspeaker: Have feedback on the report? Share it here.
<details><summary> :rocket: New features to boost your workflow: </summary>
</details>
Approved these changes.
mcollina
Nabeel-akk
This relates to...
nodejs/undici#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
[cert]: https://github.com/nodejs/undici/blob/main/CONTRIBUTING.md#developers-certificate-of-origin
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.