Skip to content

Commit

Permalink
http: reduce parts in chunked response when corking
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Oct 12, 2023
1 parent ac2a68c commit d88a454
Showing 1 changed file with 49 additions and 10 deletions.
59 changes: 49 additions & 10 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ let debug = require('internal/util/debuglog').debuglog('http', (fn) => {
});

const kCorked = Symbol('corked');
const kCorkedBuffer = Symbol('kCorkedBuffer');
const kCorkedLength = Symbol('kCorkedLength');
const kUniqueHeaders = Symbol('kUniqueHeaders');
const kBytesWritten = Symbol('kBytesWritten');
const kErrored = Symbol('errored');
Expand Down Expand Up @@ -140,6 +142,8 @@ function OutgoingMessage(options) {
this.finished = false;
this._headerSent = false;
this[kCorked] = 0;
this[kCorkedBuffer] = [];
this[kCorkedLength] = 0;
this._closed = false;

this.socket = null;
Expand Down Expand Up @@ -206,8 +210,7 @@ ObjectDefineProperty(OutgoingMessage.prototype, 'writableHighWaterMark', {
ObjectDefineProperty(OutgoingMessage.prototype, 'writableCorked', {
__proto__: null,
get() {
const corked = this.socket ? this.socket.writableCorked : 0;
return corked + this[kCorked];
return this[kCorked];
},
});

Expand Down Expand Up @@ -299,19 +302,49 @@ OutgoingMessage.prototype._renderHeaders = function _renderHeaders() {
};

OutgoingMessage.prototype.cork = function() {
this[kCorked]++;
if (this.socket) {
this.socket.cork();
} else {
this[kCorked]++;
}
};

OutgoingMessage.prototype.uncork = function() {
this[kCorked]--;
if (this.socket) {
this.socket.uncork();
} else if (this[kCorked]) {
this[kCorked]--;
}

if (this[kCorked] || this[kCorkedBuffer].length === 0) {
return;
}

const len = this[kCorkedLength];
const buf = this[kCorkedBuffer];

if (this.chunkedEncoding) {
let callbacks;
this._send(NumberPrototypeToString(len, 16), 'latin1', null);
this._send(crlf_buf, null, null);
for (let n = 0; n < buf.length; n += 3) {
this._send(buf[n + 0], buf[n + 1], null);
if (buf[n + 2]) {
callbacks ??= [];
callbacks.push(buf[n + 2]);
}
}
this._send(crlf_buf, null, callbacks.length ? (err) => {
for (const callback of callbacks) {
callback(err);
}
} : null);
} else {
for (let n = 0; n < buf.length; n += 3) {
this._send(buf[n + 0], buf[n + 1], buf[n + 2]);
}
}

this[kCorkedBuffer].length = 0;
this[kCorkedLength] = 0;
};

OutgoingMessage.prototype.setTimeout = function setTimeout(msecs, callback) {
Expand Down Expand Up @@ -936,14 +969,19 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
}

let ret;
if (msg.chunkedEncoding && chunk.length !== 0) {
if (msg[kCorked] && msg._headerSent) {
len ??= typeof chunk === 'string' ? Buffer.byteLength(chunk, encoding) : chunk.byteLength;
msg[kCorkedBuffer].push(chunk, encoding, callback);
msg[kCorkedLength] += len;
ret = msg[kCorkedLength] < msg[kHighWaterMark];
} else if (msg.chunkedEncoding && chunk.length !== 0) {
len ??= typeof chunk === 'string' ? Buffer.byteLength(chunk, encoding) : chunk.byteLength;
msg._send(NumberPrototypeToString(len, 16), 'latin1', null);
msg._send(crlf_buf, null, null);
msg._send(chunk, encoding, null, len);
msg._send(chunk, encoding, null);
ret = msg._send(crlf_buf, null, callback);
} else {
ret = msg._send(chunk, encoding, callback, len);
ret = msg._send(chunk, encoding, callback);
}

debug('write ret = ' + ret);
Expand Down Expand Up @@ -1068,7 +1106,8 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
this.socket._writableState.corked = 1;
this.socket.uncork();
}
this[kCorked] = 0;
this[kCorked] = 1;
this.uncork();

this.finished = true;

Expand Down

0 comments on commit d88a454

Please sign in to comment.