Skip to content

Commit 36e75b7

Browse files
committed
1 parent 9d4c5a1 commit 36e75b7

3 files changed

Lines changed: 91 additions & 2 deletions

File tree

lib/http.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,8 +1289,10 @@ Agent.prototype._establishNewConnection = function() {
12891289
// All that should be required for keep-alive is to not reconnect,
12901290
// but outgoingFlush instead.
12911291
if (!req.shouldKeepAlive) {
1292-
debug('AGENT socket.end()');
1293-
if (socket.writable) socket.end();
1292+
if (socket.writable) {
1293+
debug('AGENT socket.destroySoon()');
1294+
socket.destroySoon();
1295+
}
12941296
assert(!socket.writable);
12951297
} else {
12961298
debug('AGENT socket keep-alive');

test/fixtures/person.jpg

56.6 KB
Loading
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// We are demonstrating a problem with http.get when queueing up many
2+
// transfers. The server simply introduces some delay and sends a file.
3+
// Note this is demonstarted with connection: close.
4+
var common = require('../common');
5+
var assert = require('assert');
6+
var http = require('http');
7+
var fs = require('fs');
8+
9+
var image = fs.readFileSync(common.fixturesDir + '/person.jpg');
10+
11+
console.log("image.length = " + image.length);
12+
13+
var total = 100;
14+
var requests = 0, responses = 0;
15+
16+
var server = http.Server(function(req, res) {
17+
if (++requests == total) {
18+
server.close();
19+
}
20+
21+
setTimeout(function() {
22+
res.writeHead(200, {
23+
'content-type': 'image/jpeg',
24+
'connection': 'close',
25+
'content-length': image.length
26+
});
27+
res.end(image);
28+
}, 1);
29+
});
30+
31+
32+
server.listen(common.PORT, function() {
33+
for (var i = 0; i < total; i++) {
34+
(function() {
35+
var x = i;
36+
37+
var opts = {
38+
port: common.PORT,
39+
headers: { connection: 'close' }
40+
};
41+
42+
http.get(opts, function(res) {
43+
console.error("recv " + x);
44+
var s = fs.createWriteStream(common.tmpDir + '/' + x + ".jpg");
45+
res.pipe(s);
46+
47+
// TODO there should be a callback to pipe() that will allow
48+
// us to get a callback when the pipe is finished.
49+
res.on('end', function() {
50+
console.error("done " + x);
51+
if (++responses == total) {
52+
s.on('close', checkFiles);
53+
}
54+
});
55+
}).on('error', function(e) {
56+
console.error('error! ', e.message)
57+
throw e;
58+
});
59+
})();
60+
}
61+
});
62+
63+
64+
var checkedFiles = false;
65+
function checkFiles() {
66+
// Should see 1.jpg, 2.jpg, ..., 100.jpg in tmpDir
67+
var files = fs.readdirSync(common.tmpDir);
68+
assert.equal(total, files.length);
69+
70+
for (var i = 0; i < total; i++) {
71+
var fn = i + '.jpg';
72+
assert.ok(files.indexOf(fn) >= 0, "couldn't find '" + fn + "'");
73+
var stat = fs.statSync(common.tmpDir + '/' + fn);
74+
assert.equal(image.length, stat.size,
75+
"size doesn't match on '" + fn +
76+
"'. Got " + stat.size + " bytes");
77+
}
78+
79+
checkedFiles = true;
80+
}
81+
82+
83+
process.on('exit', function() {
84+
assert.equal(total, requests);
85+
assert.equal(total, responses);
86+
assert.ok(checkedFiles);
87+
});

0 commit comments

Comments
 (0)