1

I am calling function dorequest many times per request to node server. I have problem with request to webpage running on apache2.2.21. Almost of these request are done without any problems, but several request ending with error ECONNRESET and I don't know why. If I use apapche2.4 then everything going well.

var request = require('request');

function dorequest(set, callback){
    request.get(url, function optionalCallback(err, httpResponse, body){
        if (err){
            console.log(url);
            throw err;
        } else {
            //do some stuffs 
        }
    });
}
Alexander Mikhalchenko
  • 4,525
  • 3
  • 32
  • 56
Tomas Ji
  • 55
  • 4
  • Maybe your apache server simply drops your request because there are too many connections at the same time initiated by `dorequest`? – Alexander Mikhalchenko Feb 04 '16 at 12:09
  • I know that it should be a problem. How can I resolve this behavior? I've tried to set a lot of settings of apache, but without any progress. It is stable when I do request to apache2.4. – Tomas Ji Feb 04 '16 at 12:58
  • Can Help to do some queue for request with any delay? – Tomas Ji Feb 04 '16 at 13:00

2 Answers2

1

Probably your apache server simply drops your request because there are too many connections at the same time initiated by dorequest function.

You can execute those request consequently by calling one in the callback of another by calling the next request in the callback for the previous one, but since there are quite a lot of them and for estetic reasons I would recommend to use async library - it's awesome and really handy when dealing with things like that.

function dorequest(set, callback){
    request.get(url, function optionalCallback(err, httpResponse, body){
        if (err){
            callback(err);
        } else {
            //do some stuffs 
        }
        callback(err, res);
    });
}

var maxRequestAtATime = 30;

async.mapLimit(arrayOfOptions, maxRequestAtATime, dorequest, function(err, results){
    // results is now an array of stats for each request
});

If the options of a request depend on the options of the previous one, you should use async.waterfall.

Alexander Mikhalchenko
  • 4,525
  • 3
  • 32
  • 56
0

I updated script and use async.queue function for that and still have some err on apache.

    function dorequest(set, callback)
{
     console.log('add request');
    q.push({set: set, callback: callback}, function (err) { }); 
}

var q = async.queue(function (task, callback) {

    setTimeout(function () {
        console.log('hello ' + task.set.url, ' lenght: ',q.length());

        if (task.set.method=='get')
        {               
            myrequest.get(task.set.url, function optionalCallback(err, httpResponse, body) 
            {

                if (err)
                {
                    console.log(task.set.url);
                    throw err;
                }
                else
                {
                    //console.log(set.url,body);
                    if (typeof task.callback !='undefined') task.callback(body);
                    callback();
                }
            });
        }
        else
        {       
            if (!task.set.data) task.set.data={};       
            myrequest.post(task.set.url, function optionalCallback(err, httpResponse, body) 
            {

                if (err)
                {
                    console.log(task.set.url);
                    throw err;
                }
                else
                {
                    //console.log(set.url,body);
                    if (typeof task.callback !='undefined') task.callback(body);
                    callback();
                }
            }).form(task.set.data); 
        }       

    },500); 
},1);
Tomas Ji
  • 55
  • 4