http2: add diagnostics channel 'http2.client.stream.created'

Signed-off-by: Darshan Sen <raisinten@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/58246
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
This commit is contained in:
Darshan Sen 2025-05-11 14:08:15 +05:30 committed by GitHub
parent a822a1cbe7
commit 5f841fb2c8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 87 additions and 0 deletions

View File

@ -1201,6 +1201,15 @@ The event is emitted before the response is sent.
Emitted when server sends a response.
#### HTTP/2
`http2.client.stream.created`
* `stream` {ClientHttp2Stream}
* `headers` {HTTP/2 Headers Object}
Emitted when a stream is created on the client.
#### Modules
`module.require.start`

View File

@ -183,6 +183,10 @@ const { UV_EOF } = internalBinding('uv');
const { StreamPipe } = internalBinding('stream_pipe');
const { _connectionListener: httpConnectionListener } = http;
const dc = require('diagnostics_channel');
const onClientStreamCreatedChannel = dc.channel('http2.client.stream.created');
let debug = require('internal/util/debuglog').debuglog('http2', (fn) => {
debug = fn;
});
@ -371,6 +375,12 @@ function onSessionHeaders(handle, id, cat, flags, headers, sensitiveHeaders) {
} else {
// eslint-disable-next-line no-use-before-define
stream = new ClientHttp2Stream(session, handle, id, {});
if (onClientStreamCreatedChannel.hasSubscribers) {
onClientStreamCreatedChannel.publish({
stream,
headers: obj,
});
}
if (endOfStream) {
stream.push(null);
}
@ -1863,6 +1873,14 @@ class ClientHttp2Session extends Http2Session {
} else {
onConnect();
}
if (onClientStreamCreatedChannel.hasSubscribers) {
onClientStreamCreatedChannel.publish({
stream,
headers: headersParam,
});
}
return stream;
}
}

View File

@ -0,0 +1,60 @@
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
// This test ensures that the built-in HTTP/2 diagnostics channels are reporting
// the diagnostics messages for the 'http2.client.stream.created' channel when
// ClientHttp2Streams are created by both:
// - the client calling ClientHttp2Session#request()
// - in response to an incoming 'push' event from the server
const Countdown = require('../common/countdown');
const assert = require('assert');
const dc = require('diagnostics_channel');
const http2 = require('http2');
const { Duplex } = require('stream');
const clientHttp2StreamCreationCount = 2;
dc.subscribe('http2.client.stream.created', common.mustCall(({ stream, headers }) => {
// Since ClientHttp2Stream is not exported from any module, this just checks
// if the stream is an instance of Duplex and the constructor name is
// 'ClientHttp2Stream'.
assert.ok(stream instanceof Duplex);
assert.strictEqual(stream.constructor.name, 'ClientHttp2Stream');
assert.ok(headers && !Array.isArray(headers) && typeof headers === 'object');
}, clientHttp2StreamCreationCount));
const server = http2.createServer();
server.on('stream', common.mustCall((stream) => {
stream.respond();
stream.end();
stream.pushStream({}, common.mustSucceed((pushStream) => {
pushStream.respond();
pushStream.end();
}, 1));
}, 1));
server.listen(0, common.mustCall(() => {
const port = server.address().port;
const client = http2.connect(`http://localhost:${port}`);
const countdown = new Countdown(clientHttp2StreamCreationCount, () => {
client.close();
server.close();
});
const stream = client.request({});
stream.on('response', common.mustCall(() => {
countdown.dec();
}));
client.on('stream', common.mustCall((pushStream) => {
pushStream.on('push', common.mustCall(() => {
countdown.dec();
}, 1));
}, 1));
}, 1));