Skip to content

Commit b197355

Browse files
authored
http2: add diagnostics channel 'http2.client.stream.start'
Signed-off-by: Darshan Sen <[email protected]> PR-URL: #58292 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Santiago Gimeno <[email protected]>
1 parent c2f3d2a commit b197355

File tree

3 files changed

+82
-3
lines changed

3 files changed

+82
-3
lines changed

doc/api/diagnostics_channel.md

+7
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,13 @@ Emitted when server sends a response.
12101210

12111211
Emitted when a stream is created on the client.
12121212

1213+
`http2.client.stream.start`
1214+
1215+
* `stream` {ClientHttp2Stream}
1216+
* `headers` {HTTP/2 Headers Object}
1217+
1218+
Emitted when a stream is started on the client.
1219+
12131220
#### Modules
12141221

12151222
> Stability: 1 - Experimental

lib/internal/http2/core.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ const { _connectionListener: httpConnectionListener } = http;
186186

187187
const dc = require('diagnostics_channel');
188188
const onClientStreamCreatedChannel = dc.channel('http2.client.stream.created');
189+
const onClientStreamStartChannel = dc.channel('http2.client.stream.start');
189190

190191
let debug = require('internal/util/debuglog').debuglog('http2', (fn) => {
191192
debug = fn;
@@ -381,6 +382,12 @@ function onSessionHeaders(handle, id, cat, flags, headers, sensitiveHeaders) {
381382
headers: obj,
382383
});
383384
}
385+
if (onClientStreamStartChannel.hasSubscribers) {
386+
onClientStreamStartChannel.publish({
387+
stream,
388+
headers: obj,
389+
});
390+
}
384391
if (endOfStream) {
385392
stream.push(null);
386393
}
@@ -717,7 +724,7 @@ function onGoawayData(code, lastStreamID, buf) {
717724
// When a ClientHttp2Session is first created, the socket may not yet be
718725
// connected. If request() is called during this time, the actual request
719726
// will be deferred until the socket is ready to go.
720-
function requestOnConnect(headers, options) {
727+
function requestOnConnect(headersList, headersParam, options) {
721728
const session = this[kSession];
722729

723730
// At this point, the stream should have already been destroyed during
@@ -744,7 +751,7 @@ function requestOnConnect(headers, options) {
744751

745752
// `ret` will be either the reserved stream ID (if positive)
746753
// or an error code (if negative)
747-
const ret = session[kHandle].request(headers,
754+
const ret = session[kHandle].request(headersList,
748755
streamOptions,
749756
options.parent | 0,
750757
options.weight | 0,
@@ -776,6 +783,12 @@ function requestOnConnect(headers, options) {
776783
return;
777784
}
778785
this[kInit](ret.id(), ret);
786+
if (onClientStreamStartChannel.hasSubscribers) {
787+
onClientStreamStartChannel.publish({
788+
stream: this,
789+
headers: headersParam,
790+
});
791+
}
779792
}
780793

781794
// Validates that priority options are correct, specifically:
@@ -1859,7 +1872,7 @@ class ClientHttp2Session extends Http2Session {
18591872
}
18601873
}
18611874

1862-
const onConnect = reqAsync.bind(requestOnConnect.bind(stream, headersList, options));
1875+
const onConnect = reqAsync.bind(requestOnConnect.bind(stream, headersList, headersParam, options));
18631876
if (this.connecting) {
18641877
if (this[kPendingRequestCalls] !== null) {
18651878
this[kPendingRequestCalls].push(onConnect);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.start' channel when
9+
// ClientHttp2Streams are started 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 clientHttp2StreamStartCount = 2;
20+
21+
dc.subscribe('http2.client.stream.start', 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.
24+
assert.ok(stream instanceof Duplex);
25+
assert.strictEqual(stream.constructor.name, 'ClientHttp2Stream');
26+
assert.ok(headers && !Array.isArray(headers) && typeof headers === 'object');
27+
}, clientHttp2StreamStartCount));
28+
29+
const server = http2.createServer();
30+
server.on('stream', common.mustCall((stream) => {
31+
stream.respond();
32+
stream.end();
33+
34+
stream.pushStream({}, common.mustSucceed((pushStream) => {
35+
pushStream.respond();
36+
pushStream.end();
37+
}, 1));
38+
}, 1));
39+
40+
server.listen(0, common.mustCall(() => {
41+
const port = server.address().port;
42+
const client = http2.connect(`http://localhost:${port}`);
43+
44+
const countdown = new Countdown(clientHttp2StreamStartCount, () => {
45+
client.close();
46+
server.close();
47+
});
48+
49+
const stream = client.request({});
50+
stream.on('response', common.mustCall(() => {
51+
countdown.dec();
52+
}));
53+
54+
client.on('stream', common.mustCall((pushStream) => {
55+
pushStream.on('push', common.mustCall(() => {
56+
countdown.dec();
57+
}, 1));
58+
}, 1));
59+
}, 1));

0 commit comments

Comments
 (0)