Skip to content

Commit

Permalink
http: cleanup cork logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Feb 15, 2023
1 parent 1b87cb6 commit f862c81
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ const kUniqueHeaders = Symbol('kUniqueHeaders');
const kBytesWritten = Symbol('kBytesWritten');
const kEndCalled = Symbol('kEndCalled');
const kErrored = Symbol('errored');
const kSocket = Symbol('kSocket');

const nop = () => {};

Expand Down Expand Up @@ -188,22 +189,21 @@ ObjectDefineProperty(OutgoingMessage.prototype, 'writableObjectMode', {
ObjectDefineProperty(OutgoingMessage.prototype, 'writableLength', {
__proto__: null,
get() {
return this.outputSize + (this.socket ? this.socket.writableLength : 0);
return this.outputSize + (this[kSocket] ? this[kSocket].writableLength : 0);
}
});

ObjectDefineProperty(OutgoingMessage.prototype, 'writableHighWaterMark', {
__proto__: null,
get() {
return this.socket ? this.socket.writableHighWaterMark : HIGH_WATER_MARK;
return this[kSocket] ? this[kSocket].writableHighWaterMark : HIGH_WATER_MARK;
}
});

ObjectDefineProperty(OutgoingMessage.prototype, 'writableCorked', {
__proto__: null,
get() {
const corked = this.socket ? this.socket.writableCorked : 0;
return corked + this[kCorked];
return this[kSocket] ? this[kSocket].writableCorked : this[kCorked];
}
});

Expand Down Expand Up @@ -238,6 +238,20 @@ ObjectDefineProperty(OutgoingMessage.prototype, 'connection', {
}
});

ObjectDefineProperty(OutgoingMessage.prototype, 'socket', {
__proto__: null,
get: function() {
return this[kSocket];
},
set: function(val) {
for (let n = 0; n < this[kCorked]; n++) {
val?.cork();
}
this[kCorked] = 0;
this[kSocket] = val;
}
});

ObjectDefineProperty(OutgoingMessage.prototype, '_headerNames', {
__proto__: null,
get: internalUtil.deprecate(function() {
Expand Down Expand Up @@ -915,9 +929,9 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
return true;
}

if (!fromEnd && msg.socket && !msg.socket.writableCorked) {
msg.socket.cork();
process.nextTick(connectionCorkNT, msg.socket);
if (!fromEnd && !msg.writableCorked) {
msg.cork();
process.nextTick(connectionUncorkNT, msg);
}

let ret;
Expand All @@ -935,8 +949,8 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
}


function connectionCorkNT(conn) {
conn.uncork();
function connectionUncorkNT(msg) {
msg.uncork();
}

OutgoingMessage.prototype.addTrailers = function addTrailers(headers) {
Expand Down Expand Up @@ -1049,8 +1063,9 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
// Fully uncork connection on end().
this.socket._writableState.corked = 1;
this.socket.uncork();
} else {
this[kCorked] = 0;
}
this[kCorked] = 0;

this.finished = true;

Expand Down Expand Up @@ -1112,11 +1127,6 @@ OutgoingMessage.prototype._flush = function _flush() {
};

OutgoingMessage.prototype._flushOutput = function _flushOutput(socket) {
while (this[kCorked]) {
this[kCorked]--;
socket.cork();
}

const outputLength = this.outputData.length;
if (outputLength <= 0)
return undefined;
Expand Down

0 comments on commit f862c81

Please sign in to comment.