Skip to content

Commit 0885546

Browse files
RaisinTentargos
authored andcommitted
http2: add diagnostics channel 'http2.client.stream.close'
Signed-off-by: Darshan Sen <raisinten@gmail.com> PR-URL: #58329 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Kohei Ueno <kohei.ueno119@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
1 parent 8f09a1f commit 0885546

4 files changed

Lines changed: 121 additions & 0 deletions

File tree

‎doc/api/diagnostics_channel.md‎

Lines changed: 7 additions & 0 deletions
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

‎lib/internal/http2/core.js‎

Lines 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
constonClientStreamStartChannel=dc.channel('http2.client.stream.start');
190190
constonClientStreamErrorChannel=dc.channel('http2.client.stream.error');
191191
constonClientStreamFinishChannel=dc.channel('http2.client.stream.finish');
192+
constonClientStreamCloseChannel=dc.channel('http2.client.stream.close');
192193

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

19811982
functioncloseStream(stream,code,rstStreamStatus=kSubmitRstStream){
1983+
consttype=stream[kSession][kType];
19821984
conststate=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
functionfinishCloseStream(code){
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
constcommon=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+
constassert=require('assert');
12+
constdc=require('diagnostics_channel');
13+
consthttp2=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(streaminstanceofDuplex);
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+
constserver=http2.createServer();
29+
server.listen(0,common.mustCall(()=>{
30+
constport=server.address().port;
31+
constclient=http2.connect(`http://localhost:${port}`);
32+
33+
constac=newAbortController();
34+
conststream=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+
constcommon=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+
constCountdown=require('../common/countdown');
14+
constassert=require('assert');
15+
constdc=require('diagnostics_channel');
16+
consthttp2=require('http2');
17+
const{ Duplex }=require('stream');
18+
19+
constclientHttp2StreamCloseCount=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(streaminstanceofDuplex);
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+
constserver=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+
constport=server.address().port;
46+
constclient=http2.connect(`http://localhost:${port}`);
47+
48+
constcountdown=newCountdown(clientHttp2StreamCloseCount,()=>{
49+
client.close();
50+
server.close();
51+
});
52+
53+
conststream=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)