Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: connect ETIMEDOUT at TCPConnectWrap.afterConnect [as oncomplete]

Tags:

rest

node.js

api

I'm trying to call REST API asynchronously in a for loop using XMLHttpRequest module. I'm making 400 requests in 1 loop, with a wait time of 1 sec after every 100 requests. This works fine on clientside JavaScript. However, when I run on NodeJS using the same module (it's not natively available, I had to download from npmjs) , I'm getting this error after about 230 requests. Any idea if there is another module that I can use to better handle this bulk API requests?

Error: connect ETIMEDOUT at TCPConnectWrap.afterConnect [as oncomplete]

like image 647
Pumpkin Pie Avatar asked Nov 16 '18 14:11

Pumpkin Pie


1 Answers

I finally found the solution to my problem.

When using request module for calling REST APIs , you need to specify pool variable in the options. This serves as maxsockets, which is a declaration for concurrent request processing.

Sample code is posted below for those who run unto this issue:

For more info, check out below post: How to use Request js (Node js Module) pools

var separateReqPool = {maxSockets: 20};
var request = require('request');
var url_array = ['url1','url2','url3'];//Array of all the urls to call


async.map(url_array, function(item, callback){
      request({url: item, pool: separateReqPool}, function (error, response, body) {
          //Do Something with the response
          });
        }, function(err, results){
          console.log(results);
        });
     }).
     catch((err) => {
        console.log(err);
 }); 
like image 163
Pumpkin Pie Avatar answered Sep 18 '22 01:09

Pumpkin Pie