T.Okahara
T.Okahara

Reputation: 1234

Node.js https get request ECONNRESET

My question is similar to this question: Node Js :is it possible to hit 1000 http get request to some api from a node js server but their solution of throttle did not work for me.

I am making calls to our api/website hosted on Google App Engine to trigger a task to update the memcache for specific product pages. This function updateCache is called from an async.eachSeries. The code is pretty straight forward: (this is a sanitized version)

function updateCache(object_name, callback) {
  var options = {
    host          : 'www.example.com',
    path          : '/update-cache/' + object_name,
    method        : 'GET',
    agent         : false,
  };
  var req = https.request(options, function(res) {
    res.on('data', function() {
      if (res.statusCode !== 200) {
        console.log('successful');
      }
    });
    res.on('end', function() {
      console.log('end');
      callback();
    });
  });
  req.on('error', function(e) {
    console.log('failed');
    console.error(e.stack);
    callback();
  });
  req.end();
}

It runs perfectly on my Mac machine but I need the script to run on a Windows PC using Windows 7 and that is were it gets this error:

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

Error: read ECONNRESET
  at exports._errnoException (util.js:870:11)
  at TCP.onread (net.js:552:26)

Upvotes: 6

Views: 17393

Answers (1)

T.Okahara
T.Okahara

Reputation: 1234

I followed this solution and it looked something like this:

var req = https.request(options, function(res) {
    res.on('data', function() {
        if (res.statusCode !== 200) {
            // HANDLE
        }
    });
    res.on('end', function() {
        if (res.statusCode !== 200 && app.retry_count < 10) {
            app.retry_count += 1;
            retriggerFunction(param, callback);
        } else {
            app.retry_count = 0;
            return callback();
        }
    });
});
req.on('error', function(e) {
    app.retry_count += 1;
    retriggerFunction(param, callback);
});
req.on('timeout', function(e) {
    app.retry_count += 1;
    retriggerFunction(param, callback);
    req.abort();
});
req.on('uncaughtException', function(e) {
    app.retry_count += 1;
    retriggerFunction(param, callback);
    req.abort();
});

retriggerFunction would just be the function that I wrapped this in. It may not be "correct" but it is currently working for me. If anyone has improvements on this please let me know

Upvotes: 7

Related Questions