http2: emit timeout on compat request and response

v8.x Backport Note: The timeout has been increased to 10ms.

Fixes: https://github.com/nodejs/node/issues/20079

Backport-PR-URL: https://github.com/nodejs/node/pull/22850
PR-URL: https://github.com/nodejs/node/pull/22252
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
James M Snell
2018-08-10 13:33:46 -07:00
committed by Beth Griggs
parent cc561cc5a7
commit 348cde07fd
3 changed files with 15 additions and 2 deletions

View File

@@ -229,6 +229,13 @@ function onStreamCloseRequest() {
req.emit('close');
}
function onStreamTimeout(kind) {
return function onStreamTimeout() {
const obj = this[kind];
obj.emit('timeout');
};
}
class Http2ServerRequest extends Readable {
constructor(stream, headers, options, rawHeaders) {
super(options);
@@ -251,6 +258,7 @@ class Http2ServerRequest extends Readable {
stream.on('error', onStreamError);
stream.on('aborted', onStreamAbortedRequest);
stream.on('close', onStreamCloseRequest);
stream.on('timeout', onStreamTimeout(kRequest));
this.on('pause', onRequestPause);
this.on('resume', onRequestResume);
}
@@ -403,6 +411,7 @@ class Http2ServerResponse extends Stream {
stream.on('aborted', onStreamAbortedResponse);
stream.on('close', onStreamCloseResponse);
stream.on('wantTrailers', onStreamTrailersReady);
stream.on('timeout', onStreamTimeout(kResponse));
}
// User land modules such as finalhandler just check truthiness of this

View File

@@ -6,13 +6,15 @@ if (!common.hasCrypto)
const assert = require('assert');
const http2 = require('http2');
const msecs = common.platformTimeout(1);
// Set the timeout to 10ms since ending the response stream resets the timer.
const msecs = common.platformTimeout(10);
const server = http2.createServer();
server.on('request', (req, res) => {
req.setTimeout(msecs, common.mustCall(() => {
res.end();
}));
res.on('timeout', common.mustCall());
res.on('finish', common.mustCall(() => {
req.setTimeout(msecs, common.mustNotCall());
process.nextTick(() => {

View File

@@ -6,13 +6,15 @@ if (!common.hasCrypto)
const assert = require('assert');
const http2 = require('http2');
const msecs = common.platformTimeout(1);
// Set the timeout to 10ms since ending the response stream resets the timer.
const msecs = common.platformTimeout(10);
const server = http2.createServer();
server.on('request', (req, res) => {
res.setTimeout(msecs, common.mustCall(() => {
res.end();
}));
res.on('timeout', common.mustCall());
res.on('finish', common.mustCall(() => {
res.setTimeout(msecs, common.mustNotCall());
process.nextTick(() => {