http2: add diagnostics channel 'http2.server.stream.close' · nodejs/node@b1f60d2 · GitHub
Skip to content

Commit b1f60d2

Browse files
RaisinTentargos
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 94e53d4 commit b1f60d2

4 files changed

Lines changed: 129 additions & 3 deletions

File tree

doc/api/diagnostics_channel.md

Lines changed: 7 additions & 0 deletions

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

0 commit comments

Comments
 (0)