Skip to content

Commit 74e4bbf

Browse files
committed
http2: add diagnostics channel 'http2.client.stream.finish'
Signed-off-by: Darshan Sen <[email protected]>
1 parent 6e222ed commit 74e4bbf

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

doc/api/diagnostics_channel.md

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

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

1213+
`http2.client.stream.finish`
1214+
1215+
* `stream` {ClientHttp2Stream}
1216+
* `headers` {HTTP/2 Headers Object}
1217+
* `flags` {number}
1218+
1219+
Emitted when a stream is received on the client.
1220+
12131221
#### Modules
12141222

12151223
`module.require.start`

lib/internal/http2/core.js

+10
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 onClientStreamFinishChannel = dc.channel('http2.client.stream.finish');
189190

190191
let debug = require('internal/util/debuglog').debuglog('http2', (fn) => {
191192
debug = fn;
@@ -419,6 +420,15 @@ function onSessionHeaders(handle, id, cat, flags, headers, sensitiveHeaders) {
419420
reqAsync.runInAsyncScope(process.nextTick, null, emit, stream, event, obj, flags, headers);
420421
else
421422
process.nextTick(emit, stream, event, obj, flags, headers);
423+
if ((event === 'response' ||
424+
event === 'push') &&
425+
onClientStreamFinishChannel.hasSubscribers) {
426+
onClientStreamFinishChannel.publish({
427+
stream,
428+
headers: obj,
429+
flags: flags,
430+
});
431+
}
422432
}
423433
if (endOfStream) {
424434
stream.push(null);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.finish' channel when
9+
// ClientHttp2Streams are received by both:
10+
// - the 'response' event
11+
// - the 'push' event
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 clientHttp2StreamFinishCount = 2;
20+
21+
dc.subscribe('http2.client.stream.finish', common.mustCall(({ stream, headers, flags }) => {
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+
28+
assert.ok(headers && !Array.isArray(headers) && typeof headers === 'object');
29+
30+
assert.strictEqual(typeof flags, 'number');
31+
}, clientHttp2StreamFinishCount));
32+
33+
const server = http2.createServer();
34+
server.on('stream', common.mustCall((stream) => {
35+
stream.respond();
36+
stream.end();
37+
38+
stream.pushStream({}, common.mustSucceed((pushStream) => {
39+
pushStream.respond();
40+
pushStream.end();
41+
}));
42+
}));
43+
44+
server.listen(0, common.mustCall(() => {
45+
const port = server.address().port;
46+
const client = http2.connect(`http://localhost:${port}`);
47+
48+
const countdown = new Countdown(clientHttp2StreamFinishCount, () => {
49+
client.close();
50+
server.close();
51+
});
52+
53+
const stream = client.request({});
54+
stream.on('response', common.mustCall(() => {
55+
countdown.dec();
56+
}));
57+
58+
client.on('stream', common.mustCall((pushStream) => {
59+
pushStream.on('push', common.mustCall(() => {
60+
countdown.dec();
61+
}));
62+
}));
63+
}));

0 commit comments

Comments
 (0)