feat(h2): honor maxRequestsPerClient - #5802
Open
johnlyonms wants to merge 2 commits into
Open
Conversation
`maxRequestsPerClient` was only honored over HTTP/1.1, so a `Client` with `allowH2: true` (or an `H2CClient`) kept a single session alive forever no matter how the option was configured. Count every stream that `session.request()` successfully opened against the limit, and retire the session once it is reached. A retired session is marked busy, so the dispatch loop stops putting new streams on it and queues them for the next connection instead. Retirement happens synchronously, before the write that reached the limit returns, so a resume pass dispatching several requests at once cannot overshoot. Tearing the session down is deferred until it has drained. Doing it in the same tick as `session.request()` makes nghttp2 refuse the very stream that triggered retirement, and a locally initiated `session.close()` can leave the socket half-open indefinitely when the peer keeps its side open. Drainage is tracked with a dedicated counter driven by each stream's physical `close` event rather than by `kOpenStreams`, which is released when the response completes and would therefore truncate a request body the server had already responded to. Closes nodejs#5787 Signed-off-by: John Lyon <264892913+johnlyonms@users.noreply.github.com>
johnlyonms
marked this pull request as ready for review
September 11, 2026 00:37
johnlyonms
marked this pull request as draft
September 11, 2026 01:03
Use headersTimeout to reset retired HTTP/2 sessions whose accepted streams do not close, allowing queued requests to reconnect instead of stalling indefinitely. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: John Lyon <264892913+johnlyonms@users.noreply.github.com>
johnlyonms
marked this pull request as ready for review
September 11, 2026 04:17
metcoder95
reviewed
Sep 11, 2026
Comment on lines
+80
to
+85
| Over HTTP/1.1 a request is counted per message and the socket is reset once | ||
| the limit is reached. Over HTTP/2 a request is counted per successfully | ||
| opened stream: the session is retired once the limit is reached, meaning it | ||
| accepts no further streams. Streams it already accepted are allowed to | ||
| complete for up to `headersTimeout` before the connection is reset. Queued | ||
| and subsequent requests are then dispatched on a new session. |
Member
There was a problem hiding this comment.
This can be a bit confusing for users, especially given that we have the maxConcurrentStreams; let's mention how they differ and how they interact with each other
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #5802 +/- ##
==========================================
+ Coverage 93.52% 93.54% +0.01%
==========================================
Files 110 110
Lines 39415 39604 +189
==========================================
+ Hits 36864 37046 +182
- Misses 2551 2558 +7 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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...
Closes #5787
Rationale
maxRequestsPerClientwas only honored by the HTTP/1.1 dispatcher. AClientcreated withallowH2: true, or anH2CClient, ignored the option and kept a single HTTP/2 session alive indefinitely. The option is important for rotatingconnections so load balancers can rebalance and clients do not remain pinned to one backend.
HTTP/2 also requires bounded retirement. Once a client has reached its request limit and we mark it as such, we must wait for existing streams to close. Waiting indefinitely for accepted streams to close lets a long-lived SSE, CONNECT, WebSocket, long-polling, gRPC, or stalled upload would block all queued work if we didn't implement a bound.
Changes
maxRequestsPerClient.headersTimeout. If accepted streams remain open at the deadline, reset the retired session with an informational error so queued requests remain replayable on a fresh connection.0,null, andundefineddisabled behavior.Client,H2CClient, and TypeScript consumers.The implementation tracks physical stream closure separately from
kOpenStreams. A response may finish while its request body is still uploading, so response completion alone is not sufficient to safely retire the session.Tests
test/http2-max-requests-per-client.jscontains 17 tests covering:1and disabled limitsClient,H2CClient, andPoolheadersTimeoutwhile queued work reconnects successfullyValidation:
Commits
12ac5172 feat(h2): honor maxRequestsPerClient41ba964e fix(h2): bound retired session drainingBreaking Changes and Deprecations
None. HTTP/1.1 behavior is unchanged. HTTP/2 users who do not configure
maxRequestsPerClient, or set it to0, see no change. SettingheadersTimeout: 0explicitly disables the retirement drain deadline.Status