Skip to content

http: add diagnostics channel for outgoing body chunks - #66081

Open
mcollina wants to merge 1 commit into
nodejs:mainfrom
mcollina:http-outgoing-body-chunk-diagnostics
Open

mcollina wants to merge 1 commit into
nodejs:mainfrom
mcollina:http-outgoing-body-chunk-diagnostics

Conversation

@mcollina

@mcollina mcollina commented Sep 17, 2026

Copy link
Copy Markdown
Member

Adds two diagnostics channels, http.client.request.bodyChunkSent and http.server.response.bodyChunkSent, that expose the outgoing body chunk and
its encoding. Subscribed tools can compute the byte length of what is being
sent themselves from data and encoding (Buffer.byteLength(data, encoding),
or data.byteLength for buffers).

This avoids counting buffered outgoing data in bytes on the hot path: Node does not pay to measure the chunk unless someone is actually reading it. The channel (and its subscriber presence) is resolved once in the message
constructor and cached on the instance, so the per-chunk write path only reads
a cached reference.

Alternative to #66039

Refs: #66039
Refs: #57985
Refs: #46601
Refs: #46605

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/http
  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added http Issues and PRs related to the http subsystem. needs-ci PRs that need a full CI run. labels Sep 17, 2026
@mcollina
mcollina requested review from pimterry and ronag September 17, 2026 10:04
@mcollina

Copy link
Copy Markdown
Member Author

cc @nodejs/diagnostics

Instead of counting buffered outgoing data in bytes (see nodejs#66039, where
the cost of Buffer.byteLength on the hot path was raised), expose the
outgoing body chunk and its encoding on new diagnostics channels:

- http.client.request.bodyChunkSent
- http.server.response.bodyChunkSent

Subscribed tools can compute the byte length themselves from `data`
and `encoding` (`Buffer.byteLength(data, encoding)`), so Node does not
pay to measure bytes unless someone is actually reading them.

The channel is resolved once in the message constructor
(`hasSubscribers` is checked there) and cached on the instance, so the
per-chunk write path only reads a cached reference instead of querying
the channel on every chunk.

Refs: nodejs#66039
Refs: nodejs#57985
Refs: nodejs#46601
Refs: nodejs#46605

Signed-off-by: Matteo Collina <hello@matteocollina.com>
Assisted-by: pi
@mcollina
mcollina force-pushed the http-outgoing-body-chunk-diagnostics branch from ad13df4 to 0631fdd Compare September 17, 2026 10:12
@codecov

codecov Bot commented Sep 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.26%. Comparing base (7aaf9b4) to head (0631fdd).
⚠️ Report is 462 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #66081      +/-   ##
==========================================
+ Coverage   90.13%   90.26%   +0.13%     
==========================================
  Files         751      789      +38     
  Lines      253639   271661   +18022     
  Branches    47790    51852    +4062     
==========================================
+ Hits       228618   245224   +16606     
- Misses      16264    16905     +641     
- Partials     8757     9532     +775     
Files with missing lines Coverage Δ
lib/_http_client.js 97.66% <100.00%> (+0.02%) ⬆️
lib/_http_outgoing.js 97.94% <100.00%> (+0.17%) ⬆️
lib/_http_server.js 96.62% <100.00%> (+<0.01%) ⬆️

... and 293 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

http Issues and PRs related to the http subsystem. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants