Skip to content

Commit 962c042

Browse files
committed
http: destroy clean incoming messages in one tick
When an incoming message is destroyed after being fully received with no error, complete the destroy synchronously instead of deferring the callback with process.nextTick(). The deferral only exists so that 'error' listeners attached right after destroy(err) still receive the error, which cannot matter when there is no error. 'close' is still emitted asynchronously by the stream machinery. Reduces the per-request nextTick count of a hello-world HTTP server from 6 to 5. Signed-off-by: Matteo Collina <hello@matteocollina.com>
1 parent 803bd87 commit 962c042

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

lib/_http_incoming.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,13 @@ IncomingMessage.prototype._destroy = function _destroy(err, cb) {
299299
cleanup();
300300
process.nextTick(onError, this, e || err, cb);
301301
});
302+
} else if (err == null && !this.aborted) {
303+
// The message was received completely and is being destroyed cleanly:
304+
// complete the destroy synchronously. 'close' is still emitted on a
305+
// later tick by the stream machinery. The deferral below only exists
306+
// so that 'error' listeners attached right after destroy(err) still
307+
// receive the error, which cannot matter when there is no error.
308+
cb();
302309
} else {
303310
process.nextTick(onError, this, err, cb);
304311
}

0 commit comments

Comments
 (0)