Skip to content

Commit b99d131

Browse files
RaisinTenaduh95
authored andcommitted
http2: add diagnostics channel 'http2.client.stream.created'
Signed-off-by: Darshan Sen <[email protected]> PR-URL: #58246 Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: Paolo Insogna <[email protected]> Reviewed-By: Santiago Gimeno <[email protected]>
1 parent cff62e3 commit b99d131

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

doc/api/diagnostics_channel.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,15 @@ The event is emitted before the response is sent.
12001200

12011201
Emitted when server sends a response.
12021202

1203+
#### HTTP/2
1204+
1205+
`http2.client.stream.created`
1206+
1207+
* `stream` {ClientHttp2Stream}
1208+
* `headers` {HTTP/2 Headers Object}
1209+
1210+
Emitted when a stream is created on the client.
1211+
12031212
#### Modules
12041213

12051214
`module.require.start`

lib/internal/http2/core.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ const { UV_EOF } = internalBinding('uv');
188188

189189
const { StreamPipe } = internalBinding('stream_pipe');
190190
const { _connectionListener: httpConnectionListener } = http;
191+
192+
const dc = require('diagnostics_channel');
193+
const onClientStreamCreatedChannel = dc.channel('http2.client.stream.created');
194+
191195
let debug = require('internal/util/debuglog').debuglog('http2', (fn) => {
192196
debug = fn;
193197
});
@@ -379,6 +383,12 @@ function onSessionHeaders(handle, id, cat, flags, headers, sensitiveHeaders) {
379383
} else {
380384
// eslint-disable-next-line no-use-before-define
381385
stream = new ClientHttp2Stream(session, handle, id, {});
386+
if (onClientStreamCreatedChannel.hasSubscribers) {
387+
onClientStreamCreatedChannel.publish({
388+
stream,
389+
headers: obj,
390+
});
391+
}
382392
if (endOfStream) {
383393
stream.push(null);
384394
}
@@ -1865,6 +1875,14 @@ class ClientHttp2Session extends Http2Session {
18651875
} else {
18661876
onConnect();
18671877
}
1878+
1879+
if (onClientStreamCreatedChannel.hasSubscribers) {
1880+
onClientStreamCreatedChannel.publish({
1881+
stream,
1882+
headers: headersParam,
1883+
});
1884+
}
1885+
18681886
return stream;
18691887
}
18701888
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
if (!common.hasCrypto)
5+
common.skip('missing crypto');
6+
7+
// This test ensures that the built-in HTTP/2 diagnostics channels are reporting
8+
// the diagnostics messages for the 'http2.client.stream.created' channel when
9+
// ClientHttp2Streams are created by both:
10+
// - the client calling ClientHttp2Session#request()
11+
// - in response to an incoming 'push' event from the server
12+
13+
const Countdown = require('../common/countdown');
14+
const assert = require('assert');
15+
const dc = require('diagnostics_channel');
16+
const http2 = require('http2');
17+
const { Duplex } = require('stream');
18+
19+
const clientHttp2StreamCreationCount = 2;
20+
21+
dc.subscribe('http2.client.stream.created', common.mustCall(({ stream, headers }) => {
22+
// Since ClientHttp2Stream is not exported from any module, this just checks
23+
// if the stream is an instance of Duplex and the constructor name is
24+
// 'ClientHttp2Stream'.
25+
assert.ok(stream instanceof Duplex);
26+
assert.strictEqual(stream.constructor.name, 'ClientHttp2Stream');
27+
assert.ok(headers && !Array.isArray(headers) && typeof headers === 'object');
28+
}, clientHttp2StreamCreationCount));
29+
30+
const server = http2.createServer();
31+
server.on('stream', common.mustCall((stream) => {
32+
stream.respond();
33+
stream.end();
34+
35+
stream.pushStream({}, common.mustSucceed((pushStream) => {
36+
pushStream.respond();
37+
pushStream.end();
38+
}, 1));
39+
}, 1));
40+
41+
server.listen(0, common.mustCall(() => {
42+
const port = server.address().port;
43+
const client = http2.connect(`http://localhost:${port}`);
44+
45+
const countdown = new Countdown(clientHttp2StreamCreationCount, () => {
46+
client.close();
47+
server.close();
48+
});
49+
50+
const stream = client.request({});
51+
stream.on('response', common.mustCall(() => {
52+
countdown.dec();
53+
}));
54+
55+
client.on('stream', common.mustCall((pushStream) => {
56+
pushStream.on('push', common.mustCall(() => {
57+
countdown.dec();
58+
}, 1));
59+
}, 1));
60+
}, 1));

0 commit comments

Comments
 (0)