简体   繁体   中英

Node.js : Unhandled error on parallel GET requests on a certain time

I'm sending about 100 parallel GET requests to an API using request/request module. The problem I have is randomly on about 80th to 100th request , node throws this exception :

events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: connect ETIMEDOUT 0.0.0.0:80 //a dummy ip
    at Object.exports._errnoException (util.js:860:11)
    at exports._exceptionWithHostPort (util.js:883:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)

Here is my request defaults :

var apiRequest = request.defaults({

    url : url, //response is in xml format
    headers : {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Language': 'en-US,en;q=0.8'
    },
    method : 'GET',
    gzip : true,
    encoding : 'utf8',
    pool: {maxSockets: Infinity},
    forever : true,
    timeout : 120000

});

Also I'm requesting using a loop over an array

Fixed my problem by requesting the failed urls on error event recursively till no timeout occurs:

function getAPI(url) {
    var apiRequest = request.defaults({

        url : url,
        headers : {
            'Accept': 'text/html,application/xhtml+xml,application/xml'
        },
        method : 'GET',
        gzip : true
    });

    apiRequest(function (error, response, body) {

    }).on('error', function(e){
        getAPI(this.uri.href);//this is important
    }).end();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM