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 <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent cc561cc commit 348cde0

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

lib/internal/http2/compat.js

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

232+
function onStreamTimeout(kind) {
233+
return function onStreamTimeout() {
234+
const obj = this[kind];
235+
obj.emit('timeout');
236+
};
237+
}
238+
232239
class Http2ServerRequest extends Readable {
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

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ if (!common.hasCrypto)
66
const assert = require('assert');
77
const http2 = require('http2');
88

9-
const msecs = common.platformTimeout(1);
9+
// Set the timeout to 10ms since ending the response stream resets the timer.
10+
const msecs = common.platformTimeout(10);
1011
const server = 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

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ if (!common.hasCrypto)
66
const assert = require('assert');
77
const http2 = require('http2');
88

9-
const msecs = common.platformTimeout(1);
9+
// Set the timeout to 10ms since ending the response stream resets the timer.
10+
const msecs = common.platformTimeout(10);
1011
const server = 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)