Skip to content

Commit 348cde0

Browse files
jasnellBethGriggs
authored andcommitted
http2: emit timeout on compat request and response
v8.x Backport Note: The timeout has been increased to 10ms. Fixes: #20079 Backport-PR-URL: #22850 PR-URL: #22252 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent cc561cc commit 348cde0

3 files changed

Lines changed: 15 additions & 2 deletions

File tree

‎lib/internal/http2/compat.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,13 @@ function onStreamCloseRequest() {
229229
req.emit('close');
230230
}
231231

232+
functiononStreamTimeout(kind){
233+
returnfunctiononStreamTimeout(){
234+
constobj=this[kind];
235+
obj.emit('timeout');
236+
};
237+
}
238+
232239
classHttp2ServerRequestextendsReadable{
233240
constructor(stream,headers,options,rawHeaders){
234241
super(options);
@@ -251,6 +258,7 @@ class Http2ServerRequest extends Readable {
251258
stream.on('error',onStreamError);
252259
stream.on('aborted',onStreamAbortedRequest);
253260
stream.on('close',onStreamCloseRequest);
261+
stream.on('timeout',onStreamTimeout(kRequest));
254262
this.on('pause',onRequestPause);
255263
this.on('resume',onRequestResume);
256264
}
@@ -403,6 +411,7 @@ class Http2ServerResponse extends Stream {
403411
stream.on('aborted',onStreamAbortedResponse);
404412
stream.on('close',onStreamCloseResponse);
405413
stream.on('wantTrailers',onStreamTrailersReady);
414+
stream.on('timeout',onStreamTimeout(kResponse));
406415
}
407416

408417
// User land modules such as finalhandler just check truthiness of this

‎test/parallel/test-http2-compat-serverrequest-settimeout.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ if (!common.hasCrypto)
66
constassert=require('assert');
77
consthttp2=require('http2');
88

9-
constmsecs=common.platformTimeout(1);
9+
// Set the timeout to 10ms since ending the response stream resets the timer.
10+
constmsecs=common.platformTimeout(10);
1011
constserver=http2.createServer();
1112

1213
server.on('request',(req,res)=>{
1314
req.setTimeout(msecs,common.mustCall(()=>{
1415
res.end();
1516
}));
17+
res.on('timeout',common.mustCall());
1618
res.on('finish',common.mustCall(()=>{
1719
req.setTimeout(msecs,common.mustNotCall());
1820
process.nextTick(()=>{

‎test/parallel/test-http2-compat-serverresponse-settimeout.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ if (!common.hasCrypto)
66
constassert=require('assert');
77
consthttp2=require('http2');
88

9-
constmsecs=common.platformTimeout(1);
9+
// Set the timeout to 10ms since ending the response stream resets the timer.
10+
constmsecs=common.platformTimeout(10);
1011
constserver=http2.createServer();
1112

1213
server.on('request',(req,res)=>{
1314
res.setTimeout(msecs,common.mustCall(()=>{
1415
res.end();
1516
}));
17+
res.on('timeout',common.mustCall());
1618
res.on('finish',common.mustCall(()=>{
1719
res.setTimeout(msecs,common.mustNotCall());
1820
process.nextTick(()=>{

0 commit comments

Comments
 (0)