Skip to content

Commit d0c7d93

Browse files
kpdeckertjfontaine
authored andcommitted
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.
1 parent 4867578 commit d0c7d93

File tree

2 files changed

+118
-6
lines changed

2 files changed

+118
-6
lines changed

lib/_http_client.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ util.inherits(ClientRequest, OutgoingMessage);
174174

175175
exports.ClientRequest = ClientRequest;
176176

177+
ClientRequest.prototype.aborted = undefined;
178+
177179
ClientRequest.prototype._finish = function() {
178180
DTRACE_HTTP_CLIENT_REQUEST(this, this.connection);
179181
COUNTER_HTTP_CLIENT_REQUEST();
@@ -186,6 +188,11 @@ ClientRequest.prototype._implicitHeader = function() {
186188
};
187189

188190
ClientRequest.prototype.abort = function() {
191+
// Mark as aborting so we can avoid sending queued request data
192+
// This is used as a truthy flag elsewhere. The use of Date.now is for debugging
193+
// purposes only.
194+
this.aborted = Date.now();
195+
189196
// If we're aborting, we don't care about any more response data.
190197
if (this.res)
191198
this.res._dump();
@@ -194,14 +201,11 @@ ClientRequest.prototype.abort = function() {
194201
res._dump();
195202
});
196203

204+
// In the event that we don't have a socket, we will pop out of
205+
// the request queue through handling in onSocket.
197206
if (this.socket) {
198207
// in-progress
199208
this.socket.destroy();
200-
} else {
201-
// haven't been assigned a socket yet.
202-
// this could be more efficient, it could
203-
// remove itself from the pending requests
204-
this._deferToConnect('destroy', []);
205209
}
206210
};
207211

@@ -485,7 +489,12 @@ ClientRequest.prototype.onSocket = function(socket) {
485489
var req = this;
486490

487491
process.nextTick(function() {
488-
tickOnSocket(req, socket);
492+
if (req.aborted) {
493+
// If we were aborted while waiting for a socket, skip the whole thing.
494+
socket.emit('free');
495+
} else {
496+
tickOnSocket(req, socket);
497+
}
489498
});
490499
};
491500

test/simple/test-http-abort-queued.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var assert = require('assert'),
23+
http = require('http');
24+
25+
var complete;
26+
27+
var server = http.createServer(function (req, res) {
28+
// We should not see the queued /thatotherone request within the server
29+
// as it should be aborted before it is sent.
30+
assert.equal(req.url, '/');
31+
32+
res.writeHead(200);
33+
res.write('foo');
34+
35+
complete = complete || function () {
36+
res.end();
37+
};
38+
});
39+
40+
41+
server.listen(0, function () {
42+
console.log('listen', server.address().port);
43+
44+
var agent = new http.Agent({maxSockets: 1});
45+
assert.equal(Object.keys(agent.sockets).length, 0);
46+
47+
var options = {
48+
hostname: 'localhost',
49+
port: server.address().port,
50+
method: 'GET',
51+
path: '/',
52+
agent: agent
53+
};
54+
55+
var req1 = http.request(options);
56+
req1.on('response', function(res1) {
57+
assert.equal(Object.keys(agent.sockets).length, 1);
58+
assert.equal(Object.keys(agent.requests).length, 0);
59+
60+
var req2 = http.request({
61+
method: 'GET',
62+
host: 'localhost',
63+
port: server.address().port,
64+
path: '/thatotherone',
65+
agent: agent
66+
});
67+
assert.equal(Object.keys(agent.sockets).length, 1);
68+
assert.equal(Object.keys(agent.requests).length, 1);
69+
70+
req2.on('error', function(err) {
71+
// This is expected in response to our explicit abort call
72+
assert.equal(err.code, 'ECONNRESET');
73+
});
74+
75+
req2.end();
76+
req2.abort();
77+
78+
assert.equal(Object.keys(agent.sockets).length, 1);
79+
assert.equal(Object.keys(agent.requests).length, 1);
80+
81+
console.log('Got res: ' + res1.statusCode);
82+
console.dir(res1.headers);
83+
84+
res1.on('data', function(chunk) {
85+
console.log('Read ' + chunk.length + ' bytes');
86+
console.log(' chunk=%j', chunk.toString());
87+
complete();
88+
});
89+
90+
res1.on('end', function() {
91+
console.log('Response ended.');
92+
93+
setTimeout(function() {
94+
assert.equal(Object.keys(agent.sockets).length, 0);
95+
assert.equal(Object.keys(agent.requests).length, 0);
96+
97+
server.close();
98+
}, 100);
99+
});
100+
});
101+
102+
req1.end();
103+
});

0 commit comments

Comments
 (0)