{
  "type": "module",
  "source": "doc/api/api-retryhandler.md",
  "modules": [
    {
      "textRaw": "RetryHandler",
      "name": "retryhandler",
      "introduced_in": "v5.28.0",
      "type": "module",
      "stability": 2,
      "stabilityText": "Stable",
      "desc": "<p>A <a href=\"Dispatcher.html#class-dispatcher\"><code>Dispatcher</code></a> handler that automatically retries a request when it fails\nwith a recoverable network error or an eligible HTTP status code. It wraps an\ninner handler and re-dispatches the request, applying an exponential backoff and\nhonouring the <code>Retry-After</code> response header. When a response is partially\nconsumed before the failure, the handler resumes the download with a <code>Range</code>\nrequest guarded by the original <code>ETag</code>.</p>\n<p>The handler is most often used indirectly through <a href=\"RetryAgent.html#class-retryagent\"><code>RetryAgent</code></a>, but it can\nalso be supplied directly to <a href=\"Dispatcher.html#dispatcherdispatchoptions-handler\"><code>dispatcher.dispatch()</code></a> for fine-grained\ncontrol over the retry behaviour.</p>\n<pre><code class=\"language-mjs\">import { RetryHandler } from 'undici'\n</code></pre>",
      "classes": [
        {
          "textRaw": "Class: `RetryHandler`",
          "name": "RetryHandler",
          "type": "class",
          "meta": {
            "added": [
              "v5.28.0"
            ],
            "changes": []
          },
          "desc": "<ul>\n<li>Extends: <a href=\"Dispatcher.md#class-dispatcher\"><code>&#x3C;DispatchHandler></code></a></li>\n</ul>\n<p>Implements the <a href=\"Dispatcher.html#parameter-dispatchhandler\"><code>DispatchHandler</code></a> interface. An instance is consumed by a\nsingle dispatch call and forwards the dispatch lifecycle to the inner <code>handler</code>,\nre-issuing the request through the supplied <code>dispatch</code> function whenever a retry\nis warranted.</p>\n<blockquote>\n<p><strong>Note:</strong> The <code>RetryHandler</code> does not retry over stateful bodies (for example\nstreams or <code>AsyncIterable</code>), because once consumed they cannot be replayed. In\nthese situations the body is identified as stateful and the request is rejected\nwith the <code>UND_ERR_REQ_RETRY</code> error instead of being retried.</p>\n</blockquote>",
          "signatures": [
            {
              "textRaw": "`new RetryHandler(options, retryHandlers)`",
              "name": "RetryHandler",
              "type": "ctor",
              "meta": {
                "added": [
                  "v5.28.0"
                ],
                "changes": [
                  {
                    "version": "v7.0.0",
                    "pr-url": "https://github.com/nodejs/undici/pull/3883",
                    "description": "Reimplemented on top of the new dispatch lifecycle hooks."
                  }
                ]
              },
              "params": [
                {
                  "textRaw": "`options` {DispatchOptions} The dispatch options for the request, extended with an optional `retryOptions` field. Type is `DispatchOptions & { retryOptions?: RetryOptions }`.",
                  "name": "options",
                  "type": "DispatchOptions",
                  "desc": "The dispatch options for the request, extended with an optional `retryOptions` field. Type is `DispatchOptions & { retryOptions?: RetryOptions }`.",
                  "options": [
                    {
                      "textRaw": "`retryOptions` {RetryOptions} (optional) Configuration controlling when and how the request is retried. See `RetryOptions`.",
                      "name": "retryOptions",
                      "type": "RetryOptions",
                      "desc": "(optional) Configuration controlling when and how the request is retried. See `RetryOptions`."
                    }
                  ]
                },
                {
                  "textRaw": "`retryHandlers` {RetryHandlers} The handlers used to drive the retry loop. See `RetryHandlers`.",
                  "name": "retryHandlers",
                  "type": "RetryHandlers",
                  "desc": "The handlers used to drive the retry loop. See `RetryHandlers`."
                }
              ],
              "return": {
                "textRaw": "Returns: {RetryHandler}",
                "name": "return",
                "type": "RetryHandler"
              },
              "modules": [
                {
                  "textRaw": "`RetryOptions`",
                  "name": "`retryoptions`",
                  "type": "module",
                  "desc": "<ul>\n<li><code>throwOnError</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#boolean_type\"><code>&#x3C;boolean></code></a> When <code>true</code>, an error is thrown on the last retry\nattempt and propagated to the inner handler; when <code>false</code>, the failing response\nis passed through instead, which is useful when the error body is needed or a\ncustom error handler is in place. <strong>Default:</strong> <code>true</code>.</li>\n<li><code>retry</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function\"><code>&#x3C;Function></code></a> Callback invoked on every retry iteration to decide whether\nanother attempt should be made. It receives the error, the retry context, and a\ncallback. Call the callback with an <code>Error</code> to stop retrying, or with <code>null</code> to\nschedule another attempt. <strong>Default:</strong> the built-in retry strategy described\nbelow.\n<ul>\n<li><code>err</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error\"><code>&#x3C;Error></code></a> The error that triggered the retry.</li>\n<li><code>context</code> <a href=\"RetryHandler.md#retrycontext\"><code>&#x3C;RetryContext></code></a> The current retry context. See <a href=\"#retrycontext\"><code>RetryContext</code></a>.</li>\n<li><code>callback</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function\"><code>&#x3C;Function></code></a> Signals the outcome of this iteration.\n<ul>\n<li><code>result</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error\"><code>&#x3C;Error></code></a> | <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#null_type\"><code>&#x3C;null></code></a> (optional) An <code>Error</code> to abort retrying, or <code>null</code> to\nretry.</li>\n</ul>\n</li>\n</ul>\n</li>\n<li><code>maxRetries</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#number_type\"><code>&#x3C;number></code></a> Maximum number of retries allowed. <strong>Default:</strong> <code>5</code>.</li>\n<li><code>maxTimeout</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#number_type\"><code>&#x3C;number></code></a> Maximum number of milliseconds to wait between retries. <strong>Default:</strong> <code>30000</code> (30 seconds).</li>\n<li><code>minTimeout</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#number_type\"><code>&#x3C;number></code></a> Initial number of milliseconds to wait before the first\nretry. <strong>Default:</strong> <code>500</code> (half a second).</li>\n<li><code>timeoutFactor</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#number_type\"><code>&#x3C;number></code></a> Multiplier applied to the timeout between successive\nretries to produce an exponential backoff. <strong>Default:</strong> <code>2</code>.</li>\n<li><code>retryAfter</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#boolean_type\"><code>&#x3C;boolean></code></a> When <code>true</code>, the delay before the next retry is inferred\nfrom the <code>Retry-After</code> response header when present. <strong>Default:</strong> <code>true</code>.</li>\n<li><code>methods</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type\"><code>&#x3C;string></code></a>[] HTTP methods that are eligible for retrying. <strong>Default:</strong>\n<code>['GET', 'HEAD', 'OPTIONS', 'PUT', 'DELETE', 'TRACE']</code>.</li>\n<li><code>statusCodes</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#number_type\"><code>&#x3C;number></code></a>[] HTTP status codes that trigger a retry. <strong>Default:</strong>\n<code>[500, 502, 503, 504, 429]</code>.</li>\n<li><code>errorCodes</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type\"><code>&#x3C;string></code></a>[] Network error codes that trigger a retry. <strong>Default:</strong>\n<code>['ECONNRESET', 'ECONNREFUSED', 'ENOTFOUND', 'ENETDOWN', 'ENETUNREACH', 'EHOSTDOWN', 'EHOSTUNREACH', 'EPIPE', 'UND_ERR_SOCKET']</code>.</li>\n</ul>\n<p>The default <code>retry</code> strategy computes the delay before the next attempt as\n<code>minTimeout * timeoutFactor ** (counter - 1)</code>, capped at <code>maxTimeout</code>. When a\n<code>Retry-After</code> header is present it takes precedence (interpreted as seconds, or as\nan HTTP date), still capped at <code>maxTimeout</code>. The default strategy stops retrying\nonce <code>counter</code> exceeds <code>maxRetries</code>, when the error code is not in <code>errorCodes</code>,\nwhen the method is not in <code>methods</code>, or when the response status code is not in\n<code>statusCodes</code>.</p>",
                  "displayName": "`RetryOptions`"
                },
                {
                  "textRaw": "`RetryContext`",
                  "name": "`retrycontext`",
                  "type": "module",
                  "desc": "<ul>\n<li><code>state</code> <a href=\"RetryHandler.md#retrystate\"><code>&#x3C;RetryState></code></a> The current retry state. See <a href=\"#retrystate\"><code>RetryState</code></a>.</li>\n<li><code>opts</code> <a href=\"Dispatcher.md#class-dispatcher\"><code>&#x3C;DispatchOptions></code></a> The dispatch options passed to the handler, including\nthe resolved <code>retryOptions</code>. Type is\n<code>DispatchOptions &#x26; { retryOptions?: RetryOptions }</code>.</li>\n</ul>\n<p>The context object passed as the second argument to the <code>retry</code> callback.</p>",
                  "displayName": "`RetryContext`"
                },
                {
                  "textRaw": "`RetryState`",
                  "name": "`retrystate`",
                  "type": "module",
                  "desc": "<ul>\n<li><code>counter</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#number_type\"><code>&#x3C;number></code></a> The current retry attempt, starting at <code>1</code> for the first\nretry.</li>\n</ul>\n<p>Represents the retry state for a given request.</p>",
                  "displayName": "`RetryState`"
                },
                {
                  "textRaw": "Parameter: `RetryHandlers`",
                  "name": "parameter:_`retryhandlers`",
                  "type": "module",
                  "desc": "<ul>\n<li><code>dispatch</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function\"><code>&#x3C;Function></code></a> The dispatch function called to (re-)issue the request on\nevery attempt. Type is <code>(options, handler) => boolean</code>.</li>\n<li><code>handler</code> <a href=\"Dispatcher.md#class-dispatcher\"><code>&#x3C;DispatchHandler></code></a> The inner handler invoked once the request succeeds\nor the retries are exhausted.</li>\n</ul>",
                  "displayName": "Parameter: `RetryHandlers`"
                }
              ]
            }
          ],
          "modules": [
            {
              "textRaw": "Examples",
              "name": "examples",
              "type": "module",
              "desc": "<pre><code class=\"language-mjs\">import { Client, RetryHandler } from 'undici'\n\nconst client = new Client(`http://localhost:${server.address().port}`)\nconst chunks = []\n\nconst handler = new RetryHandler(\n  {\n    ...dispatchOptions,\n    retryOptions: {\n      // Custom retry decision function.\n      retry (err, { state, opts }, callback) {\n        if (err.code === 'UND_ERR_DESTROYED') {\n          callback(err)\n          return\n        }\n\n        if (err.statusCode === 206) {\n          callback(err)\n          return\n        }\n\n        setTimeout(() => callback(null), 1000)\n      }\n    }\n  },\n  {\n    dispatch (...args) {\n      return client.dispatch(...args)\n    },\n    handler: {\n      onRequestStart () {},\n      onResponseStart (controller, status, headers) {\n        // Do something with the response headers.\n      },\n      onResponseData (controller, chunk) {\n        chunks.push(chunk)\n      },\n      onResponseEnd () {},\n      onResponseError (controller, err) {\n        // Handle the error.\n      }\n    }\n  }\n)\n\nclient.dispatch(dispatchOptions, handler)\n</code></pre>\n<p>A minimal handler that relies entirely on the default retry options:</p>\n<pre><code class=\"language-mjs\">import { Client, RetryHandler } from 'undici'\n\nconst client = new Client(`http://localhost:${server.address().port}`)\n\nconst handler = new RetryHandler(dispatchOptions, {\n  dispatch: client.dispatch.bind(client),\n  handler: {\n    onRequestStart () {},\n    onResponseStart (controller, status, headers) {},\n    onResponseData (controller, chunk) {},\n    onResponseEnd () {},\n    onResponseError (controller, err) {}\n  }\n})\n\nclient.dispatch(dispatchOptions, handler)\n</code></pre>",
              "displayName": "Examples"
            }
          ]
        }
      ]
    }
  ]
}