GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — http2: add diagnostics channel 'http2.client.stream.close' · nodejs/node@0885546

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • doc/api
    • diagnostics_channel.md
  • lib/internal/http2
    • core.js
  • test/parallel
    • test-diagnostics-channel-http2-client-stream-close-error.js
    • test-diagnostics-channel-http2-client-stream-close.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎doc/api/diagnostics_channel.md‎

Copy file name to clipboardExpand all lines: doc/api/diagnostics_channel.md
+7Lines changed: 7 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,13 @@ Emitted when an error occurs during the processing of a stream on the client.
12321232

12331233
Emitted when a stream is received on the client.
12341234

1235+
`http2.client.stream.close`
1236+
1237+
* `stream` {ClientHttp2Stream}
1238+
1239+
Emitted when a stream is closed on the client. The HTTP/2 error code used when
1240+
closing the stream can be retrieved using the `stream.rstCode` property.
1241+
12351242
#### Modules
12361243

12371244
> Stability: 1 - Experimental
Collapse file

‎lib/internal/http2/core.js‎

Copy file name to clipboardExpand all lines: lib/internal/http2/core.js
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ const onClientStreamCreatedChannel = dc.channel('http2.client.stream.created');
189189
const onClientStreamStartChannel = dc.channel('http2.client.stream.start');
190190
const onClientStreamErrorChannel = dc.channel('http2.client.stream.error');
191191
const onClientStreamFinishChannel = dc.channel('http2.client.stream.finish');
192+
const onClientStreamCloseChannel = dc.channel('http2.client.stream.close');
192193

193194
let debug = require('internal/util/debuglog').debuglog('http2', (fn) => {
194195
debug = fn;
@@ -1979,6 +1980,7 @@ const kSubmitRstStream = 1;
19791980
const kForceRstStream = 2;
19801981

19811982
function closeStream(stream, code, rstStreamStatus = kSubmitRstStream) {
1983+
const type = stream[kSession][kType];
19821984
const state = stream[kState];
19831985
state.flags |= STREAM_FLAGS_CLOSED;
19841986
state.rstCode = code;
@@ -2009,6 +2011,11 @@ function closeStream(stream, code, rstStreamStatus = kSubmitRstStream) {
20092011
else
20102012
stream.once('finish', finishFn);
20112013
}
2014+
2015+
if (type === NGHTTP2_SESSION_CLIENT &&
2016+
onClientStreamCloseChannel.hasSubscribers) {
2017+
onClientStreamCloseChannel.publish({ stream });
2018+
}
20122019
}
20132020

20142021
function finishCloseStream(code) {
Collapse file

‎test/parallel/test-diagnostics-channel-http2-client-stream-close-error.js‎

Copy file name to clipboard
+44Lines 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+
}));
Collapse file

‎test/parallel/test-diagnostics-channel-http2-client-stream-close.js‎

Copy file name to clipboard
+63Lines 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 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.