Reputation: 745
Code motto: To check the redis pub/sub performance, I am trying to make http request in loop in node.js.
Problem is:Say my loop ranges from 0 to 10000,when I run the node, it will send post request in loop. It works fine till 120 seconds as expected. After that node throws error as Socket hang up. When checking for the solution,I found that the node has default socket timeout as 2 minutes.
Questions are: 1. Will the issue will be solved by changing the default timeout? 2. If so how can we change the default value.? 3. Is there any other way can be used to solve this issues other than changing socket timeout value?
Code is:
var clientNo = 100000,a=0;
var express = require('express'),
app = express(),
http = require('http'),
server = http.createServer(app);
http.globalAgent.maxSockets = Infinity;
app.enable('trust proxy');
app.disable( 'x-powered-by' );
app.use(express.bodyParser());
app.use(app.router);
app.set('port', 6002);
server.listen(app.get('port'));
var postint= setInterval(function(){
makeSubscription(a,function(query){
var options = {
path:query,
port: '6001',
method: 'POST'
};
var req = http.request(options, function(response){
response.on('end', function () {
console.log("Ends");
});
});
req.on('error', function (e) {
console.log(e);
});
req.end();
});
a++;
if(a == clientNo)
{
clearInterval(postint);
}
},1);
Note:I am sending another post request to same port on different path with 1 second setTimeout.
Upvotes: 1
Views: 1088
Reputation: 745
The code can be written simple to do the same job.
var a=0,clientNo = 10;
var http=require('http');
var sub = setInterval(function(){
var temp = '/?channel=channel'+a;
var options = {
host:'127.0.0.1',
port:'6001',
path:temp,
method:'POST'
};
a++;
var req = http.request(options,function(res){
res.on('data', function(chunk) {
console.log('data');
});
res.on('error', function(err) {
console.log(err);
});
res.on('end', function() {
console.log('end');
});
});
req.on('error',function(err){
console.log(Date());
});
req.end();
if(a== clientNo)
{
clearInterval(sub);
}
},1);
Can send more requests too in same way.
Upvotes: 1
Reputation: 882
Are you sending a valid response from server? The error "socket hangup" looks like a timeout waiting for response. Please check your server code or try to post from rest client like Postman and ensure that you are getting a response.
Upvotes: 0
Reputation: 123423
The problem may not strictly be with the timeout, but that the response
s are simply sitting idle throughout the duration.
Note that the
end
event will not fire unless the data is completely consumed. This can be done by switching into flowing mode, or by callingread()
repeatedly until you get to the end.
This is because Streams in Node 0.10 and later don't automatically stream anything.
A Readable stream will not start emitting data until you indicate that you are ready to receive it.
There are 3 options for getting it to start:
Setup flowing mode with 'data'
:
response.on('data', function (chunk) {
console.log(data.toString());
});
response.on('end', function () {
console.log('End');
});
Setup non-flowing mode with 'readable'
and .read()
:
response.on('readable', function () {
var chunk;
while (chunk = response.read()) {
console.log(chunk.toString());
}
});
response.on('end', function () {
console.log('End');
});
Explicitly tell it to .resume()
anyways:
response.resume();
response.on('end', function () {
console.log('End');
});
Upvotes: 0