Skip to content

Commit

Permalink
http: simplify drain()
Browse files Browse the repository at this point in the history
Simplify and slightly optimize draining outgoing http streams. Avoid
extra event listener and inline with rest of the drain logic.

PR-URL: #29081
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
ronag authored and targos committed Aug 20, 2019
1 parent 6bafd35 commit c5edeb9
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 27 deletions.
15 changes: 10 additions & 5 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const {
_checkIsHttpToken: checkIsHttpToken,
debug,
freeParser,
httpSocketSetup,
parsers,
HTTPParser,
prepareError,
Expand All @@ -40,7 +39,7 @@ const Agent = require('_http_agent');
const { Buffer } = require('buffer');
const { defaultTriggerAsyncIdScope } = require('internal/async_hooks');
const { URL, urlToOptions, searchParamsSymbol } = require('internal/url');
const { kOutHeaders, ondrain } = require('internal/http');
const { kOutHeaders, kNeedDrain } = require('internal/http');
const { connResetException, codes } = require('internal/errors');
const {
ERR_HTTP_HEADERS_SENT,
Expand Down Expand Up @@ -335,6 +334,14 @@ function emitAbortNT() {
this.emit('abort');
}

function ondrain() {
const msg = this._httpMessage;
if (msg && !msg.finished && msg[kNeedDrain]) {
msg[kNeedDrain] = false;
msg.emit('drain');
}
}

function socketCloseListener() {
const socket = this;
const req = socket._httpMessage;
Expand Down Expand Up @@ -649,9 +656,6 @@ function tickOnSocket(req, socket) {
socket.parser = parser;
socket._httpMessage = req;

// Setup "drain" propagation.
httpSocketSetup(socket);

// Propagate headers limit from request object to parser
if (typeof req.maxHeadersCount === 'number') {
parser.maxHeaderPairs = req.maxHeadersCount << 1;
Expand All @@ -663,6 +667,7 @@ function tickOnSocket(req, socket) {
socket.on('data', socketOnData);
socket.on('end', socketOnEnd);
socket.on('close', socketCloseListener);
socket.on('drain', ondrain);

if (
req.timeout !== undefined ||
Expand Down
8 changes: 0 additions & 8 deletions lib/_http_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const { methods, HTTPParser } =
internalBinding('http_parser') : internalBinding('http_parser_llhttp');

const FreeList = require('internal/freelist');
const { ondrain } = require('internal/http');
const incoming = require('_http_incoming');
const {
IncomingMessage,
Expand Down Expand Up @@ -201,12 +200,6 @@ function freeParser(parser, req, socket) {
}
}


function httpSocketSetup(socket) {
socket.removeListener('drain', ondrain);
socket.on('drain', ondrain);
}

const tokenRegExp = /^[\^_`a-zA-Z\-0-9!#$%&'*+.|~]+$/;
/**
* Verifies that the given val is a valid HTTP token
Expand Down Expand Up @@ -253,7 +246,6 @@ module.exports = {
CRLF: '\r\n',
debug,
freeParser,
httpSocketSetup,
methods,
parsers,
kIncomingMessage,
Expand Down
12 changes: 8 additions & 4 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const { getDefaultHighWaterMark } = require('internal/streams/state');
const assert = require('internal/assert');
const Stream = require('stream');
const internalUtil = require('internal/util');
const { kOutHeaders, utcDate } = require('internal/http');
const { kOutHeaders, utcDate, kNeedDrain } = require('internal/http');
const { Buffer } = require('buffer');
const common = require('_http_common');
const checkIsHttpToken = common._checkIsHttpToken;
Expand Down Expand Up @@ -96,6 +96,7 @@ function OutgoingMessage() {
this._contentLength = null;
this._hasBody = true;
this._trailer = '';
this[kNeedDrain] = false;

this.finished = false;
this._headerSent = false;
Expand Down Expand Up @@ -582,7 +583,10 @@ Object.defineProperty(OutgoingMessage.prototype, 'writableEnded', {

const crlf_buf = Buffer.from('\r\n');
OutgoingMessage.prototype.write = function write(chunk, encoding, callback) {
return write_(this, chunk, encoding, callback, false);
const ret = write_(this, chunk, encoding, callback, false);
if (!ret)
this[kNeedDrain] = true;
return ret;
};

function write_(msg, chunk, encoding, callback, fromEnd) {
Expand Down Expand Up @@ -782,8 +786,8 @@ OutgoingMessage.prototype._flush = function _flush() {
if (this.finished) {
// This is a queue to the server or client to bring in the next this.
this._finish();
} else if (ret) {
// This is necessary to prevent https from breaking
} else if (ret && this[kNeedDrain]) {
this[kNeedDrain] = false;
this.emit('drain');
}
}
Expand Down
12 changes: 7 additions & 5 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ const {
CRLF,
continueExpression,
chunkExpression,
httpSocketSetup,
kIncomingMessage,
HTTPParser,
_checkInvalidHeaderChar: checkInvalidHeaderChar,
Expand All @@ -41,7 +40,7 @@ const {
const { OutgoingMessage } = require('_http_outgoing');
const {
kOutHeaders,
ondrain,
kNeedDrain,
nowDate,
emitStatistics
} = require('internal/http');
Expand Down Expand Up @@ -363,8 +362,6 @@ function connectionListener(socket) {
function connectionListenerInternal(server, socket) {
debug('SERVER new http connection');

httpSocketSetup(socket);

// Ensure that the server property of the socket is correctly set.
// See https://github.com/nodejs/node/issues/13435
if (socket.server === null)
Expand Down Expand Up @@ -459,6 +456,12 @@ function socketOnDrain(socket, state) {
socket.parser.resume();
socket.resume();
}

const msg = socket._httpMessage;
if (msg && !msg.finished && msg[kNeedDrain]) {
msg[kNeedDrain] = false;
msg.emit('drain');
}
}

function socketOnTimeout() {
Expand Down Expand Up @@ -585,7 +588,6 @@ function onParserExecuteCommon(server, socket, parser, state, ret, d) {
socket.removeListener('end', state.onEnd);
socket.removeListener('close', state.onClose);
socket.removeListener('drain', state.onDrain);
socket.removeListener('drain', ondrain);
socket.removeListener('error', socketOnError);
unconsume(parser, socket);
parser.finish();
Expand Down
6 changes: 1 addition & 5 deletions lib/internal/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ function resetCache() {
utcCache = undefined;
}

function ondrain() {
if (this._httpMessage) this._httpMessage.emit('drain');
}

class HttpRequestTiming extends PerformanceEntry {
constructor(statistics) {
super();
Expand All @@ -50,7 +46,7 @@ function emitStatistics(statistics) {

module.exports = {
kOutHeaders: Symbol('kOutHeaders'),
ondrain,
kNeedDrain: Symbol('kNeedDrain'),
nowDate,
utcDate,
emitStatistics
Expand Down

0 comments on commit c5edeb9

Please sign in to comment.