Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: simplify drain #29081

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -653,9 +660,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 @@ -667,6 +671,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 @@ -578,7 +579,10 @@ Object.defineProperty(OutgoingMessage.prototype, 'headersSent', {

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 @@ -778,8 +782,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
mcollina marked this conversation as resolved.
Show resolved Hide resolved
} 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 @@ -360,8 +359,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 @@ -456,6 +453,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 @@ -582,7 +585,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);
socket.removeListener('timeout', socketOnTimeout);
unconsume(parser, socket);
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