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 May 12, 2024
1 parent ca2f874 commit 7e3de7f
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const kBytesWritten = Symbol('kBytesWritten');
const kErrored = Symbol('errored');
const kHighWaterMark = Symbol('kHighWaterMark');
const kRejectNonStandardBodyWrites = Symbol('kRejectNonStandardBodyWrites');
const kSocket = Symbol('kSocket');

const nop = () => {};

Expand Down Expand Up @@ -211,7 +212,7 @@ ObjectDefineProperty(OutgoingMessage.prototype, 'writableHighWaterMark', {
ObjectDefineProperty(OutgoingMessage.prototype, 'writableCorked', {
__proto__: null,
get() {
return this[kCorked];
return this[kSocket] ? this[kSocket].writableCorked : this[kCorked];
},
});

Expand Down Expand Up @@ -260,6 +261,20 @@ ObjectDefineProperty(OutgoingMessage.prototype, 'socket', {
},
});

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 @@ -1000,9 +1015,9 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
}
}

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 @@ -1027,8 +1042,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 @@ -1143,9 +1158,9 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
// Fully uncork connection on end().
this[kSocket]._writableState.corked = 1;
this[kSocket].uncork();
} else {
this[kCorked] = 0;
}
this[kCorked] = 1;
this.uncork();

this.finished = true;

Expand Down

0 comments on commit 7e3de7f

Please sign in to comment.