10

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]

norbitrial
  • 14,716
  • 7
  • 32
  • 59
Pumpkin Pie
  • 510
  • 1
  • 4
  • 15
  • https://github.com/Microsoft/BotBuilder/issues/4419 – Sayed Mohd Ali Nov 16 '18 at 14:36
  • I used the keepaliveavent mentioned in this blog but still get this error. Strange how this works on client side and not on server side. May be I should use different module instead of XMLHttprequest. – Pumpkin Pie Nov 16 '18 at 15:48
  • The module where its timing out is (net.js:1161:14). Complete error is shown below: Error: connect ETIMEDOUT 15.125.324.173:443 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1161:14) – Pumpkin Pie Nov 18 '18 at 09:45

1 Answers1

9

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);
 }); 
Pumpkin Pie
  • 510
  • 1
  • 4
  • 15
  • Getting error like { Error: connect ETIMEDOUT xxx.xxx.xx at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1082:14) errno: 'ETIMEDOUT', code: 'ETIMEDOUT', syscall: 'connect', address: xxx.xxx.xx, port: 3010 } – Ashwanth Madhav Dec 05 '19 at 06:44