Retrying Failed HTTP Requests in Node.js

nodejs

nodejsRelying on 3rd party services to handle some part of your business logic is a common practice. You might use SendGrid to send emails, Google Cloud Bucket Storage API to store some binary blob or Twilio for sending SMS to your users.

Although, the three I mentioned above are quite reliable, you might find yourself in a situation where any of these three or the external resource you are relying on rejects your request with the dreaded 503 Serice Unavailable message.

Assuming everything is good on your side, you might want to retry that HTTP API request.

This is how you do that.

const https = require('https');
const RETRY_THRESHOLD = 3;
let retryCount = 1;

const httpRequest = (options, callback) => {
  let buffer = new Buffer(0);

  const retryRequest = error => {
    console.log(`Rejection: ${retryCount}`, error);

    if (retryCount === RETRY_THRESHOLD) {
      return new Error(error);
    }

    retryCount++;

    const TWO_SECS = 2000;

    // Waiting 2 secs before sending another request
    setTimeout(() => {
      httpRequest(options);
    }, TWO_SECS);
  }

  const request = https.request(options, response => {
    const responseInstance = response
      .on('error', error => {
        responseInstance.abort();

        retryRequest(error);
      })
      .on('data', chunk => Buffer.concat([buffer, chunk]))
      .on('end', () => {
        // Only for response type = json
        responseBody = JSON.parse(responseBody);

        // You an also check for `responseBody.statusCode === 503`
        // to be more specific
        // for your retries
        if (!responseBody.ok) {
          // retry http request here

          retryRequest(error);
        }
      });

    // Avoids socket hangups
    responseInstance.end();
  });
}

You can call this function as the standard https request in node.js.

httpRequest({
  host: 'www.googleapis.com',
  protocol: 'https',
  port: 443, // For https
  path: '/storage/v1/b/my-bucket',
  method: 'GET',
});

The function will retry itself after failing three times before giving up.

Of course, this is just for your knowledge. In real life, you should probably use a well-known package for this kind of stuff. I’d recommend one of these:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.