summaryrefslogtreecommitdiff
path: root/lib/_http_client.js
diff options
context:
space:
mode:
authorKevin Decker <kpdecker@gmail.com>2014-04-30 09:20:25 -0400
committerTimothy J Fontaine <tjfontaine@gmail.com>2014-05-22 10:46:44 -0700
commitd0c7d93536d8bc7977ed2d3bd0b0246a558e5137 (patch)
treeae9364e4203213c66c82c4d4144e8f5d30649439 /lib/_http_client.js
parent48675784fe6e3d575818c359c9892c5f508c9cc3 (diff)
downloadnode-d0c7d93536d8bc7977ed2d3bd0b0246a558e5137.tar.gz
http: Optimize queued client aborts
Avoid sending unsent data and destroying otherwise legitimate sockets for requests that are aborted while still in the agent queue. This reduces stress on upstream systems who will likely respond to the request but client app already knows that it will be dropped on the floor and also helps avoid killing keep-alive connections.
Diffstat (limited to 'lib/_http_client.js')
-rw-r--r--lib/_http_client.js21
1 files changed, 15 insertions, 6 deletions
diff --git a/lib/_http_client.js b/lib/_http_client.js
index 50f10d168..1dcf7f027 100644
--- a/lib/_http_client.js
+++ b/lib/_http_client.js
@@ -174,6 +174,8 @@ util.inherits(ClientRequest, OutgoingMessage);
exports.ClientRequest = ClientRequest;
+ClientRequest.prototype.aborted = undefined;
+
ClientRequest.prototype._finish = function() {
DTRACE_HTTP_CLIENT_REQUEST(this, this.connection);
COUNTER_HTTP_CLIENT_REQUEST();
@@ -186,6 +188,11 @@ ClientRequest.prototype._implicitHeader = function() {
};
ClientRequest.prototype.abort = function() {
+ // Mark as aborting so we can avoid sending queued request data
+ // This is used as a truthy flag elsewhere. The use of Date.now is for debugging
+ // purposes only.
+ this.aborted = Date.now();
+
// If we're aborting, we don't care about any more response data.
if (this.res)
this.res._dump();
@@ -194,14 +201,11 @@ ClientRequest.prototype.abort = function() {
res._dump();
});
+ // In the event that we don't have a socket, we will pop out of
+ // the request queue through handling in onSocket.
if (this.socket) {
// in-progress
this.socket.destroy();
- } else {
- // haven't been assigned a socket yet.
- // this could be more efficient, it could
- // remove itself from the pending requests
- this._deferToConnect('destroy', []);
}
};
@@ -485,7 +489,12 @@ ClientRequest.prototype.onSocket = function(socket) {
var req = this;
process.nextTick(function() {
- tickOnSocket(req, socket);
+ if (req.aborted) {
+ // If we were aborted while waiting for a socket, skip the whole thing.
+ socket.emit('free');
+ } else {
+ tickOnSocket(req, socket);
+ }
});
};