Skip to content

Commit 5cd8145

Browse files
RaisinTenaduh95
authored andcommitted
http2: add diagnostics channel 'http2.server.stream.close'
Signed-off-by: Darshan Sen <raisinten@gmail.com> PR-URL: #58602 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent d6660ba commit 5cd8145

File tree

4 files changed

+129
-3
lines changed

4 files changed

+129
-3
lines changed

doc/api/diagnostics_channel.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,13 @@ Emitted when an error occurs during the processing of a stream on the server.
12671267

12681268
Emitted when a stream is sent on the server.
12691269

1270+
`http2.server.stream.close`
1271+
1272+
* `stream` {ServerHttp2Stream}
1273+
1274+
Emitted when a stream is closed on the server. The HTTP/2 error code used when
1275+
closing the stream can be retrieved using the `stream.rstCode` property.
1276+
12701277
#### Modules
12711278

12721279
> Stability: 1 - Experimental

lib/internal/http2/core.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ const onServerStreamCreatedChannel = dc.channel('http2.server.stream.created');
194194
const onServerStreamStartChannel = dc.channel('http2.server.stream.start');
195195
const onServerStreamErrorChannel = dc.channel('http2.server.stream.error');
196196
const onServerStreamFinishChannel = dc.channel('http2.server.stream.finish');
197+
const onServerStreamCloseChannel = dc.channel('http2.server.stream.close');
197198

198199
let debug = require('internal/util/debuglog').debuglog('http2', (fn) => {
199200
debug = fn;
@@ -2017,9 +2018,12 @@ function closeStream(stream, code, rstStreamStatus = kSubmitRstStream) {
20172018
stream.once('finish', finishFn);
20182019
}
20192020

2020-
if (type === NGHTTP2_SESSION_CLIENT &&
2021-
onClientStreamCloseChannel.hasSubscribers) {
2022-
onClientStreamCloseChannel.publish({ stream });
2021+
if (type === NGHTTP2_SESSION_CLIENT) {
2022+
if (onClientStreamCloseChannel.hasSubscribers) {
2023+
onClientStreamCloseChannel.publish({ stream });
2024+
}
2025+
} else if (onServerStreamCloseChannel.hasSubscribers) {
2026+
onServerStreamCloseChannel.publish({ stream });
20232027
}
20242028
}
20252029

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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.server.stream.close' channel when
9+
// a ServerHttp2Stream 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.server.stream.close', common.mustCall(({ stream }) => {
17+
// Since ServerHttp2Stream is not exported from any module, this just checks
18+
// if the stream is an instance of Duplex and the constructor name is
19+
// 'ServerHttp2Stream'.
20+
assert.ok(stream instanceof Duplex);
21+
assert.strictEqual(stream.constructor.name, 'ServerHttp2Stream');
22+
assert.strictEqual(stream.closed, true);
23+
assert.strictEqual(stream.destroyed, true);
24+
25+
assert.strictEqual(stream.rstCode, http2.constants.NGHTTP2_INTERNAL_ERROR);
26+
}));
27+
28+
const server = http2.createServer();
29+
30+
server.on('stream', common.mustCall((stream) => {
31+
let expectedError;
32+
33+
stream.on('error', common.mustCall((err) => {
34+
assert.strictEqual(err, expectedError);
35+
}));
36+
37+
expectedError = new Error('HTTP/2 server stream error');
38+
stream.destroy(expectedError);
39+
}));
40+
41+
server.listen(0, common.mustCall(() => {
42+
const port = server.address().port;
43+
const client = http2.connect(`http://localhost:${port}`);
44+
45+
const stream = client.request({});
46+
stream.on('error', common.mustCall((err) => {
47+
assert.strictEqual(err.code, 'ERR_HTTP2_STREAM_ERROR');
48+
49+
client.close();
50+
server.close();
51+
}));
52+
}));
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.server.stream.close' channel when
9+
// ServerHttp2Streams created by these actions are closed:
10+
// - in response to an incoming 'stream' event from the client
11+
// - the server calling ServerHttp2Stream#pushStream()
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 serverHttp2StreamCloseCount = 2;
20+
21+
dc.subscribe('http2.server.stream.close', common.mustCall(({ stream }) => {
22+
// Since ServerHttp2Stream is not exported from any module, this just checks
23+
// if the stream is an instance of Duplex and the constructor name is
24+
// 'ServerHttp2Stream'.
25+
assert.ok(stream instanceof Duplex);
26+
assert.strictEqual(stream.constructor.name, 'ServerHttp2Stream');
27+
assert.strictEqual(stream.closed, true);
28+
assert.strictEqual(stream.destroyed, false);
29+
30+
assert.strictEqual(stream.rstCode, http2.constants.NGHTTP2_NO_ERROR);
31+
}, serverHttp2StreamCloseCount));
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(serverHttp2StreamCloseCount, () => {
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)