Skip to content

Commit 59c4f9c

Browse files
committed
http: cleanup cork logic
1 parent 1b87cb6 commit 59c4f9c

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

lib/_http_outgoing.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ const kUniqueHeaders = Symbol('kUniqueHeaders');
8888
const kBytesWritten = Symbol('kBytesWritten');
8989
const kEndCalled = Symbol('kEndCalled');
9090
const kErrored = Symbol('errored');
91+
const kSocket = Symbol('kSocket');
9192

9293
const nop = () => {};
9394

@@ -188,22 +189,21 @@ ObjectDefineProperty(OutgoingMessage.prototype, 'writableObjectMode', {
188189
ObjectDefineProperty(OutgoingMessage.prototype, 'writableLength', {
189190
__proto__: null,
190191
get() {
191-
return this.outputSize + (this.socket ? this.socket.writableLength : 0);
192+
return this.outputSize + (this[kSocket] ? this[kSocket].writableLength : 0);
192193
}
193194
});
194195

195196
ObjectDefineProperty(OutgoingMessage.prototype, 'writableHighWaterMark', {
196197
__proto__: null,
197198
get() {
198-
return this.socket ? this.socket.writableHighWaterMark : HIGH_WATER_MARK;
199+
return this[kSocket] ? this[kSocket].writableHighWaterMark : HIGH_WATER_MARK;
199200
}
200201
});
201202

202203
ObjectDefineProperty(OutgoingMessage.prototype, 'writableCorked', {
203204
__proto__: null,
204205
get() {
205-
const corked = this.socket ? this.socket.writableCorked : 0;
206-
return corked + this[kCorked];
206+
return this[kSocket] ? this[kSocket].writableCorked : this[kCorked];
207207
}
208208
});
209209

@@ -238,6 +238,20 @@ ObjectDefineProperty(OutgoingMessage.prototype, 'connection', {
238238
}
239239
});
240240

241+
ObjectDefineProperty(OutgoingMessage.prototype, 'socket', {
242+
__proto__: null,
243+
get: function() {
244+
return this[kSocket];
245+
},
246+
set: function(val) {
247+
for (let n = 0; n < this[kCorked]; n++) {
248+
val?.cork();
249+
}
250+
this[kCorked] = 0;
251+
this[kSocket] = val;
252+
}
253+
});
254+
241255
ObjectDefineProperty(OutgoingMessage.prototype, '_headerNames', {
242256
__proto__: null,
243257
get: internalUtil.deprecate(function() {
@@ -916,8 +930,8 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
916930
}
917931

918932
if (!fromEnd && msg.socket && !msg.socket.writableCorked) {
919-
msg.socket.cork();
920-
process.nextTick(connectionCorkNT, msg.socket);
933+
msg.cork();
934+
process.nextTick(connectionUncorkNT, msg);
921935
}
922936

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

937951

938-
function connectionCorkNT(conn) {
939-
conn.uncork();
952+
function connectionUncorkNT(msg) {
953+
msg.uncork();
940954
}
941955

942956
OutgoingMessage.prototype.addTrailers = function addTrailers(headers) {
@@ -1049,8 +1063,9 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
10491063
// Fully uncork connection on end().
10501064
this.socket._writableState.corked = 1;
10511065
this.socket.uncork();
1066+
} else {
1067+
this[kCorked] = 0;
10521068
}
1053-
this[kCorked] = 0;
10541069

10551070
this.finished = true;
10561071

@@ -1112,11 +1127,6 @@ OutgoingMessage.prototype._flush = function _flush() {
11121127
};
11131128

11141129
OutgoingMessage.prototype._flushOutput = function _flushOutput(socket) {
1115-
while (this[kCorked]) {
1116-
this[kCorked]--;
1117-
socket.cork();
1118-
}
1119-
11201130
const outputLength = this.outputData.length;
11211131
if (outputLength <= 0)
11221132
return undefined;

0 commit comments

Comments
 (0)