Skip to content

Commit 8c9adbf

Browse files
committed
http: reuse socket only when it is drained
Preserve 'prefinish' event
1 parent c81839b commit 8c9adbf

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

lib/_http_client.js

+1
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ ObjectSetPrototypeOf(ClientRequest.prototype, OutgoingMessage.prototype);
351351
ObjectSetPrototypeOf(ClientRequest, OutgoingMessage);
352352

353353
ClientRequest.prototype._finish = function _finish() {
354+
FunctionPrototypeCall(OutgoingMessage.prototype._finish, this);
354355
if (hasObserver('http')) {
355356
startPerf(this, kClientRequestStatistics, {
356357
type: 'http',

lib/_http_outgoing.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const {
4040
} = primordials;
4141

4242
const { getDefaultHighWaterMark } = require('internal/streams/state');
43+
const assert = require('internal/assert');
4344
const EE = require('events');
4445
const Stream = require('stream');
4546
const internalUtil = require('internal/util');
@@ -984,11 +985,12 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
984985
};
985986

986987

987-
// Subclasses HttpClientRequest and HttpServerResponse should
988-
// implement this function. This function is called once all user data
989-
// are flushed to the socket. Note that it has a chance that the socket
990-
// is not drained.
991-
OutgoingMessage.prototype._finish = function() {};
988+
// This function is called once all user data are flushed to the socket.
989+
// Note that it has a chance that the socket is not drained.
990+
OutgoingMessage.prototype._finish = function _finish() {
991+
assert(this.socket);
992+
this.emit('prefinish');
993+
};
992994

993995

994996
// This logic is probably a bit confusing. Let me explain a bit:

lib/_http_server.js

+1
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ ServerResponse.prototype._finish = function _finish() {
222222
},
223223
});
224224
}
225+
OutgoingMessage.prototype._finish.call(this);
225226
};
226227

227228

0 commit comments

Comments
 (0)