Hi Ted,
Thanks for the pointer. Yes, it is due to agent.maxSockets. If I bump
it it works.
And yes, the program is proxying to the same domain in the same
process. Why should that matter?
What I don't understand is why it hangs. agent.maxSockets should limit
the number of sockets open at one time. If you request more than the
max, the extra requests should be queued and they should be handled a
bit later, as the responses start coming back and sockets get returned
to the pool. They should not block the whole thing. This looks
abnormal.
I've converted the code to "classical", for those would find a hairy
send function with 4 callbacks easier to "parse" than the following
one-liner:
function send(_, url){ return
streams.httpRequest(url).end().response(_).readAll(_); }
Bruno
--------- begin bug.js ----------------
var http = require("http");
var port = 3001;
var count = 5;
//http.Agent.defaultMaxSockets = 6;
function send(path, callback){
var result = "";
http.request({
host: "localhost",
port: port,
path: path,
method: 'GET'
}, function(res){
res.setEncoding('utf8');
res.on('data', function(chunk){
result += chunk;
});
res.on('end', function(){
callback(null, result);
});
}).on('error', function(err){
callback(err);
}).end();
}
function reply(response, statusCode, str){
response.writeHead(statusCode, {
"content-type": "text/plain"
});
response.end(str);
}
var server = http.createServer(function(request, response){
if (request.url.indexOf("/proxy?url=") == 0) {
send(request.url.substring(11), function(err, result){
if (err)
reply(response, 500, err.message);
else
reply(response, 200, result);
})
}
else {
reply(response, 200, request.url);
}
});
server.listen(port, function(){
console.log("server ready, sending messages")
var received = 0;
for (var i = 0; i < count; i++) {
send("/proxy?url=/" + i, function(err, result){
if (err)
console.log("ERROR: " + err.message);
else
console.log("OK: " + result);
if (++received == count)
server.close();
});
}
});
--------- end bug.js ----------------