Got HTTP Retry With Exponential Backoff

Configure the got client to retry transient failures with exponential backoff, status-code allowlists, and per-attempt timeouts for safer polling.

JavascriptNodeResilience

Code

snippet
import got from 'got';

const client = got.extend({
  retry: {
    limit: 4,
    methods: ['GET'],
    statusCodes: [408, 429, 500, 502, 503, 504],
    calculateDelay: ({ computedValue }) => Math.min(computedValue, 8000),
    errorCodes: ['ETIMEDOUT', 'ECONNRESET', 'ECONNREFUSED'],
  },
  timeout: { request: 10_000 },
  hooks: {
    beforeRetry: [
      ({ attemptCount, url }) => {
        console.log('retrying', url.href, 'attempt', attemptCount);
      },
    ],
  },
});

const body = await client('https://api.example.com/orders').json();