Skip to content

Commit ec237dd

Browse files
committed
http2: add diagnostics channel 'http2.client.stream.close'
Signed-off-by: Darshan Sen <[email protected]>
1 parent b197355 commit ec237dd

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed

doc/api/diagnostics_channel.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,13 @@ Emitted when a stream is created on the client.
12171217

12181218
Emitted when a stream is started on the client.
12191219

1220+
`http2.client.stream.close`
1221+
1222+
* `stream` {ClientHttp2Stream}
1223+
1224+
Emitted when a stream is closed on the client. The HTTP/2 error code used when
1225+
closing the stream can be retrieved using the `stream.rstCode` property.
1226+
12201227
#### Modules
12211228

12221229
> Stability: 1 - Experimental

lib/internal/http2/core.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ const { _connectionListener: httpConnectionListener } = http;
187187
const dc = require('diagnostics_channel');
188188
const onClientStreamCreatedChannel = dc.channel('http2.client.stream.created');
189189
const onClientStreamStartChannel = dc.channel('http2.client.stream.start');
190+
const onClientStreamCloseChannel = dc.channel('http2.client.stream.close');
190191

191192
let debug = require('internal/util/debuglog').debuglog('http2', (fn) => {
192193
debug = fn;
@@ -1968,6 +1969,7 @@ const kSubmitRstStream = 1;
19681969
const kForceRstStream = 2;
19691970

19701971
function closeStream(stream, code, rstStreamStatus = kSubmitRstStream) {
1972+
const type = stream[kSession][kType];
19711973
const state = stream[kState];
19721974
state.flags |= STREAM_FLAGS_CLOSED;
19731975
state.rstCode = code;
@@ -1998,6 +2000,11 @@ function closeStream(stream, code, rstStreamStatus = kSubmitRstStream) {
19982000
else
19992001
stream.once('finish', finishFn);
20002002
}
2003+
2004+
if (type === NGHTTP2_SESSION_CLIENT &&
2005+
onClientStreamCloseChannel.hasSubscribers) {
2006+
onClientStreamCloseChannel.publish({ stream });
2007+
}
20012008
}
20022009

20032010
function finishCloseStream(code) {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.close' channel when
9+
// a ClientHttp2Stream is destroyed because of an error.
10+
11+
const assert = require('assert');
12+
const dc = require('diagnostics_channel');
13+
const http2 = require('http2');
14+
const { Duplex } = require('stream');
15+
16+
dc.subscribe('http2.client.stream.close', common.mustCall(({ stream }) => {
17+
// Since ClientHttp2Stream is not exported from any module, this just checks
18+
// if the stream is an instance of Duplex and the constructor name is
19+
// 'ClientHttp2Stream'.
20+
assert.ok(stream instanceof Duplex);
21+
assert.strictEqual(stream.constructor.name, 'ClientHttp2Stream');
22+
assert.strictEqual(stream.closed, true);
23+
assert.strictEqual(stream.destroyed, true);
24+
25+
assert.strictEqual(stream.rstCode, http2.constants.NGHTTP2_CANCEL);
26+
}));
27+
28+
const server = http2.createServer();
29+
server.listen(0, common.mustCall(() => {
30+
const port = server.address().port;
31+
const client = http2.connect(`http://localhost:${port}`);
32+
33+
const ac = new AbortController();
34+
const stream = client.request({}, { signal: ac.signal });
35+
ac.abort();
36+
37+
stream.on('error', common.mustCall((err) => {
38+
assert.strictEqual(err.code, 'ABORT_ERR');
39+
assert.strictEqual(err.name, 'AbortError');
40+
41+
client.close();
42+
server.close();
43+
}));
44+
}));
Lines changed: 63 additions & 0 deletions
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.close' channel when
9+
// ClientHttp2Streams created by these actions are closed:
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 clientHttp2StreamCloseCount = 2;
20+
21+
dc.subscribe('http2.client.stream.close', common.mustCall(({ stream }) => {
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.strictEqual(stream.closed, true);
28+
assert.strictEqual(stream.destroyed, false);
29+
30+
assert.strictEqual(stream.rstCode, http2.constants.NGHTTP2_NO_ERROR);
31+
}, clientHttp2StreamCloseCount));
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(clientHttp2StreamCloseCount, () => {
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)