Skip to content

Instantly share code, notes, and snippets.

@anhhh11
Forked from murvinlai/test-simple-http-data.js
Created August 9, 2016 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anhhh11/4e6cbdd0a7fda49239df1d5ba0400687 to your computer and use it in GitHub Desktop.
Save anhhh11/4e6cbdd0a7fda49239df1d5ba0400687 to your computer and use it in GitHub Desktop.
Socket Hang up problem - A sample to generate the problem.
/*
* This is a simple HTTP data generator for testing.
*
*/
var http = require('http');
var counter = 0;
http.createServer(function (req, res) {
var start = new Date();
var myCounter = counter++;
var timeout = 50; // default timeout. Mimic how much time it takes to run a process.
// Controlled long response time causing timeout in client side.
if ( (myCounter % 20000) == 0) {
console.log('-------- reach 20000 ------------');
timeout = 600000; // 10 minutes
}
// give it some timeout
setTimeout(function() {
var output = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n';
output += '<myData>ABCDE'+myCounter+'</myData>\n';
console.log("output: " + myCounter);
res.writeHead(200, {'Content-Type': 'application/xml'});
res.write(output);
res.end();
}, timeout);
}).listen(3015);
console.log('Server running at port 3015');
var http = require('http');
// option1 is the from Google. GoogleAPI usually has less timeout and causing less issue.
//https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Paris%20Hilton&callback=foo&context=bar
var agent1 = http.getAgent('ajax.googleapis.com',80);
agent1.maxSockets = 2000;
var options1 = {
agent:agent1,
host: 'ajax.googleapis.com',
port: 80,
path: '/ajax/services/search/web?v=1.0&q=Paris%20Hilton&callback=foo&context=bar',
method: 'GET',
headers:{
"Connection":"keep-alive"
}
};
// option2 is your own server that feeds data.
var agent2 = http.getAgent('localhost', 80);
agent2.maxSockets = 2000;
var options2 = {
agent:agent2,
host:'localhost',
port:80,
path:'/',
method:'GET',
headers:{
"Connection":"keep-alive"
}
};
// option3 is my AWS micro instance. It has controlled timeout for ever 20000 request.
var agent3 = http.getAgent('50.19.237.122', 3015);
agent3.maxSockets = 2000;
var options3 = {
agent:agent3,
host:'50.19.237.122',
port:3015,
path:'/',
method:'GET',
headers:{
"Connection":"keep-alive"
}
};
// set which
var workingOption = options3;
var counter = 0;
var server = http.createServer(function (req, res) {
var myCounter = counter ++;
console.log("Start: counter: " + myCounter);
var start = new Date(); // just for timing.
var clientRequest = http.request(workingOption, function(response) {
var result = '';
response.setEncoding('utf8');
res.writeHead(response.statusCode, {'Content-Type': 'application/xml'});
response.on('data', function (chunk) {
result += chunk;
if (!res.write(chunk)) { // if write failed, the stream is choking
response.pause(); // tell the incoming stream to wait until output stream drained
}
}).on('end', function () {
var end = new Date();
console.log("End: counter: " + myCounter + ' Time: ' + (end-start) + " chunk: " + result.substring(0, 20));
res.end();
});
});
clientRequest.on('error', function(e) {
console.error("ERROR: " + JSON.stringify(e));
console.log("ERROR: " + JSON.stringify(e));
});
clientRequest.end();
});
server.listen(3204);
console.log('Server running at port 3204');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment