Skip to content

Commit b1f60d2

Browse files
RaisinTentargos
authored andcommitted
http2: add diagnostics channel 'http2.server.stream.close'
Signed-off-by: Darshan Sen <[email protected]> PR-URL: #58602 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Stephen Belanger <[email protected]>
1 parent 94e53d4 commit b1f60d2

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
@@ -1268,6 +1268,13 @@ Emitted when an error occurs during the processing of a stream on the server.
12681268

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

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

12731280
> Stability: 1 - Experimental

lib/internal/http2/core.js

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

200201
let debug = require('internal/util/debuglog').debuglog('http2', (fn) => {
201202
debug = fn;
@@ -2011,9 +2012,12 @@ function closeStream(stream, code, rstStreamStatus = kSubmitRstStream) {
20112012
stream.once('finish', finishFn);
20122013
}
20132014

2014-
if (type === NGHTTP2_SESSION_CLIENT &&
2015-
onClientStreamCloseChannel.hasSubscribers) {
2016-
onClientStreamCloseChannel.publish({ stream });
2015+
if (type === NGHTTP2_SESSION_CLIENT) {
2016+
if (onClientStreamCloseChannel.hasSubscribers) {
2017+
onClientStreamCloseChannel.publish({ stream });
2018+
}
2019+
} else if (onServerStreamCloseChannel.hasSubscribers) {
2020+
onServerStreamCloseChannel.publish({ stream });
20172021
}
20182022
}
20192023

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)