Skip to content

Commit

Permalink
http2: emit timeout on compat request and response
Browse files Browse the repository at this point in the history
Fixes: nodejs#20079

PR-URL: nodejs#22252
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
  • Loading branch information
jasnell authored and kjin committed Oct 2, 2018
1 parent 68e5ef9 commit c4e0a27
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/internal/http2/compat.js
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions test/parallel/test-http2-compat-serverrequest-settimeout.js
Expand Up @@ -13,6 +13,11 @@ server.on('request', (req, res) => {
req.setTimeout(msecs, common.mustCall(() => {
res.end();
}));
// Note: The timeout is reset in the call to res.end() above. The 'timeout'
// event listener might be called more than once if it takes more than 1ms
// to close, so use the `once` event listener here. (This is more likely to
// happen in Node 8 than 10.)
res.once('timeout', common.mustCall());
res.on('finish', common.mustCall(() => {
req.setTimeout(msecs, common.mustNotCall());
process.nextTick(() => {
Expand Down
5 changes: 5 additions & 0 deletions test/parallel/test-http2-compat-serverresponse-settimeout.js
Expand Up @@ -13,6 +13,11 @@ server.on('request', (req, res) => {
res.setTimeout(msecs, common.mustCall(() => {
res.end();
}));
// Note: The timeout is reset in the call to res.end() above. The 'timeout'
// event listener might be called more than once if it takes more than 1ms
// to close, so use the `once` event listener here. (This is more likely to
// happen in Node 8 than 10.)
res.once('timeout', common.mustCall());
res.on('finish', common.mustCall(() => {
res.setTimeout(msecs, common.mustNotCall());
process.nextTick(() => {
Expand Down

0 comments on commit c4e0a27

Please sign in to comment.