{
  "type": "module",
  "source": "doc/api/api-roundrobinpool.md",
  "modules": [
    {
      "textRaw": "RoundRobinPool",
      "name": "roundrobinpool",
      "introduced_in": "v7.17.0",
      "type": "module",
      "stability": 2,
      "stabilityText": "Stable",
      "desc": "<p>A pool of <a href=\"Client.html#class-client\"><code>Client</code></a> instances connected to the same upstream target that\nselects clients in a round-robin fashion.</p>\n<p>Unlike <a href=\"Pool.html#class-pool\"><code>Pool</code></a>, which always reuses the first available client,\n<code>RoundRobinPool</code> cycles through its clients so that requests are distributed\nevenly across every open connection. This is useful when the upstream target is\nfronted by a load balancer that distributes TCP connections across multiple\nbackend servers (for example, a Kubernetes Service): each connection is pinned\nto a backend by the load balancer, and <code>RoundRobinPool</code> spreads requests across\nthose connections so every backend receives a comparable share of traffic.</p>\n<p>Requests are not guaranteed to be dispatched in the order they were invoked.</p>\n<pre><code class=\"language-mjs\">import { RoundRobinPool } from 'undici'\n\nconst pool = new RoundRobinPool('http://localhost:3000', { connections: 10 })\n</code></pre>\n<p><code>RoundRobinPool</code> distributes HTTP requests evenly across TCP connections, not\nacross backend servers directly. Even backend distribution therefore depends on\nthe load balancer assigning different connections to different backends (for\nexample, round-robin, random, or least-connections without client affinity). If\nthe load balancer pins all connections from one source to the same backend (for\nexample, source-IP affinity or sticky sessions), consider <a href=\"BalancedPool.html#class-balancedpool\"><code>BalancedPool</code></a>\nwith the individual backend addresses instead.</p>",
      "classes": [
        {
          "textRaw": "Class: `RoundRobinPool`",
          "name": "RoundRobinPool",
          "type": "class",
          "meta": {
            "added": [
              "v7.17.0"
            ],
            "changes": []
          },
          "desc": "<ul>\n<li>Extends: <a href=\"Dispatcher.md#class-dispatcher\"><code>&#x3C;Dispatcher></code></a></li>\n</ul>",
          "signatures": [
            {
              "textRaw": "`new RoundRobinPool(url[, options])`",
              "name": "RoundRobinPool",
              "type": "ctor",
              "meta": {
                "added": [
                  "v7.17.0"
                ],
                "changes": []
              },
              "params": [
                {
                  "textRaw": "`url` {URL|string} The upstream target. It should only include the **protocol, hostname, and port**.",
                  "name": "url",
                  "type": "URL|string",
                  "desc": "The upstream target. It should only include the **protocol, hostname, and port**."
                },
                {
                  "textRaw": "`options` {RoundRobinPoolOptions} (optional)",
                  "name": "options",
                  "type": "RoundRobinPoolOptions",
                  "desc": "(optional)",
                  "optional": true
                }
              ],
              "modules": [
                {
                  "textRaw": "Parameter: `RoundRobinPoolOptions`",
                  "name": "parameter:_`roundrobinpooloptions`",
                  "type": "module",
                  "desc": "<p>Extends: <a href=\"Client.md#new-clienturl-options\"><code>&#x3C;ClientOptions></code></a></p>\n<ul>\n<li><code>factory</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function\"><code>&#x3C;Function></code></a> A function used to create the underlying <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a>\ninstances. <strong>Default:</strong> <code>(origin, opts) => new Client(origin, opts)</code>.\n<ul>\n<li><code>origin</code> <a href=\"https://developer.mozilla.org/docs/Web/API/URL\"><code>&#x3C;URL></code></a></li>\n<li><code>opts</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object\"><code>&#x3C;Object></code></a></li>\n<li>Returns: <a href=\"Dispatcher.md#class-dispatcher\"><code>&#x3C;Dispatcher></code></a></li>\n</ul>\n</li>\n<li><code>connections</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#number_type\"><code>&#x3C;number></code></a> | <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#null_type\"><code>&#x3C;null></code></a> (optional) The maximum number of <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a>\ninstances to create. When set to <code>null</code>, the <code>RoundRobinPool</code> instance creates\nan unlimited number of <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> instances. <strong>Default:</strong> <code>null</code>.</li>\n<li><code>clientTtl</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#number_type\"><code>&#x3C;number></code></a> | <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#null_type\"><code>&#x3C;null></code></a> (optional) The amount of time, in milliseconds,\nbefore a <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> instance is removed from the <code>RoundRobinPool</code> and closed.\nWhen set to <code>null</code>, <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> instances are not removed or closed based on age. <strong>Default:</strong> <code>null</code>.</li>\n</ul>\n<p><code>RoundRobinPool</code> inherits all <a href=\"Client.html#class-client\"><code>Client</code></a> options. A <a href=\"Client.html#class-client\"><code>Client</code></a> instance is\ncreated lazily on the first dispatch and additional instances are created on\ndemand, up to <code>connections</code>, when every existing client is busy.</p>",
                  "displayName": "Parameter: `RoundRobinPoolOptions`"
                }
              ]
            }
          ],
          "properties": [
            {
              "textRaw": "Type: {boolean}",
              "name": "closed",
              "type": "boolean",
              "meta": {
                "added": [
                  "v7.17.0"
                ],
                "changes": []
              },
              "desc": "<p><code>true</code> after <code>roundRobinPool.close()</code> has been called.</p>"
            },
            {
              "textRaw": "Type: {boolean}",
              "name": "destroyed",
              "type": "boolean",
              "meta": {
                "added": [
                  "v7.17.0"
                ],
                "changes": []
              },
              "desc": "<p><code>true</code> after <code>roundRobinPool.destroy()</code> has been called, or after\n<code>roundRobinPool.close()</code> has been called and the pool shutdown has completed.</p>"
            },
            {
              "textRaw": "Type: {PoolStats}",
              "name": "stats",
              "type": "PoolStats",
              "meta": {
                "added": [
                  "v7.17.0"
                ],
                "changes": []
              },
              "desc": "<p>Aggregate connection statistics for the pool. See <a href=\"PoolStats.html#class-poolstats\"><code>PoolStats</code></a>.</p>"
            }
          ],
          "methods": [
            {
              "textRaw": "`roundRobinPool.close([callback])`",
              "name": "close",
              "type": "method",
              "meta": {
                "added": [
                  "v7.17.0"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": [
                    {
                      "name": "callback",
                      "optional": true
                    }
                  ]
                }
              ],
              "desc": "<p>Closes the pool and gracefully waits for enqueued requests to complete before\nresolving. Implements [<code>dispatcher.close([callback])</code>][].</p>"
            },
            {
              "textRaw": "`roundRobinPool.destroy([error[, callback]])`",
              "name": "destroy",
              "type": "method",
              "meta": {
                "added": [
                  "v7.17.0"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": [
                    {
                      "name": "error",
                      "optional": true
                    },
                    {
                      "name": "callback",
                      "optional": true
                    }
                  ]
                }
              ],
              "desc": "<p>Destroys the pool abruptly. All pending and running requests are aborted with\nthe given <code>error</code>. Implements [<code>dispatcher.destroy([error[, callback]])</code>][].</p>"
            },
            {
              "textRaw": "`roundRobinPool.connect(options[, callback])`",
              "name": "connect",
              "type": "method",
              "meta": {
                "added": [
                  "v7.17.0"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": [
                    {
                      "name": "options"
                    },
                    {
                      "name": "callback",
                      "optional": true
                    }
                  ]
                }
              ],
              "desc": "<p>Starts two-way communications with the requested resource using\n<a href=\"https://developer.mozilla.org/docs/Web/HTTP/Methods/CONNECT\">HTTP CONNECT</a>. See [<code>dispatcher.connect(options[, callback])</code>][].</p>"
            },
            {
              "textRaw": "`roundRobinPool.dispatch(options, handler)`",
              "name": "dispatch",
              "type": "method",
              "meta": {
                "added": [
                  "v7.17.0"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": [
                    {
                      "name": "options"
                    },
                    {
                      "name": "handler"
                    }
                  ]
                }
              ],
              "desc": "<p>Dispatches a request through the next client selected in round-robin order.\nImplements [<code>dispatcher.dispatch(options, handler)</code>][].</p>"
            },
            {
              "textRaw": "`roundRobinPool.pipeline(options, handler)`",
              "name": "pipeline",
              "type": "method",
              "meta": {
                "added": [
                  "v7.17.0"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": [
                    {
                      "name": "options"
                    },
                    {
                      "name": "handler"
                    }
                  ]
                }
              ],
              "desc": "<p>For easy use with [<code>stream.pipeline</code>][]. See\n[<code>dispatcher.pipeline(options, handler)</code>][].</p>"
            },
            {
              "textRaw": "`roundRobinPool.request(options[, callback])`",
              "name": "request",
              "type": "method",
              "meta": {
                "added": [
                  "v7.17.0"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": [
                    {
                      "name": "options"
                    },
                    {
                      "name": "callback",
                      "optional": true
                    }
                  ]
                }
              ],
              "desc": "<p>Performs an HTTP request. See [<code>dispatcher.request(options[, callback])</code>][].</p>"
            },
            {
              "textRaw": "`roundRobinPool.stream(options, factory[, callback])`",
              "name": "stream",
              "type": "method",
              "meta": {
                "added": [
                  "v7.17.0"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": [
                    {
                      "name": "options"
                    },
                    {
                      "name": "factory"
                    },
                    {
                      "name": "callback",
                      "optional": true
                    }
                  ]
                }
              ],
              "desc": "<p>A faster version of [<code>roundRobinPool.request()</code>][]. See\n[<code>dispatcher.stream(options, factory[, callback])</code>][].</p>"
            },
            {
              "textRaw": "`roundRobinPool.upgrade(options[, callback])`",
              "name": "upgrade",
              "type": "method",
              "meta": {
                "added": [
                  "v7.17.0"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": [
                    {
                      "name": "options"
                    },
                    {
                      "name": "callback",
                      "optional": true
                    }
                  ]
                }
              ],
              "desc": "<p>Upgrades a connection to a different protocol. See\n[<code>dispatcher.upgrade(options[, callback])</code>][].</p>"
            }
          ],
          "events": [
            {
              "textRaw": "Event: `'connect'`",
              "name": "connect",
              "type": "event",
              "meta": {
                "added": [
                  "v7.17.0"
                ],
                "changes": []
              },
              "params": [],
              "desc": "<p>See [Dispatcher Event: <code>'connect'</code>][].</p>"
            },
            {
              "textRaw": "Event: `'disconnect'`",
              "name": "disconnect",
              "type": "event",
              "meta": {
                "added": [
                  "v7.17.0"
                ],
                "changes": []
              },
              "params": [],
              "desc": "<p>See [Dispatcher Event: <code>'disconnect'</code>][].</p>"
            },
            {
              "textRaw": "Event: `'drain'`",
              "name": "drain",
              "type": "event",
              "meta": {
                "added": [
                  "v7.17.0"
                ],
                "changes": []
              },
              "params": [],
              "desc": "<p>See [Dispatcher Event: <code>'drain'</code>][].</p>"
            }
          ]
        }
      ],
      "modules": [
        {
          "textRaw": "Example",
          "name": "example",
          "type": "module",
          "desc": "<pre><code class=\"language-mjs\">import { RoundRobinPool } from 'undici'\n\nconst pool = new RoundRobinPool('http://localhost:3000', {\n  connections: 10\n})\n\n// Requests are distributed evenly across all 10 connections.\nfor (let i = 0; i &#x3C; 100; i++) {\n  const { body } = await pool.request({\n    path: '/api/data',\n    method: 'GET'\n  })\n  console.log(await body.json())\n}\n\nawait pool.close()\n</code></pre>\n<p>[<code>dispatcher.close([callback])</code>]: Dispatcher.md#dispatcherclosecallback-promise\n[<code>dispatcher.connect(options[, callback])</code>]: Dispatcher.md#dispatcherconnectoptions-callback\n[<code>dispatcher.destroy([error[, callback]])</code>]: Dispatcher.md#dispatcherdestroyerror-callback-promise\n[<code>dispatcher.dispatch(options, handler)</code>]: Dispatcher.md#dispatcherdispatchoptions-handler\n[<code>dispatcher.pipeline(options, handler)</code>]: Dispatcher.md#dispatcherpipelineoptions-handler\n[<code>dispatcher.request(options[, callback])</code>]: Dispatcher.md#dispatcherrequestoptions-callback\n[<code>dispatcher.stream(options, factory[, callback])</code>]: Dispatcher.md#dispatcherstreamoptions-factory-callback\n[<code>dispatcher.upgrade(options[, callback])</code>]: Dispatcher.md#dispatcherupgradeoptions-callback\n[<code>roundRobinPool.request()</code>]: #roundrobinpoolrequestoptions-callback\n[<code>stream.pipeline</code>]: <a href=\"https://nodejs.org/api/stream.html#streampipelinesource-transforms-destination-callback\">https://nodejs.org/api/stream.html#streampipelinesource-transforms-destination-callback</a>\n[Dispatcher Event: <code>'connect'</code>]: Dispatcher.md#event-connect\n[Dispatcher Event: <code>'disconnect'</code>]: Dispatcher.md#event-disconnect\n[Dispatcher Event: <code>'drain'</code>]: Dispatcher.md#event-drain</p>",
          "displayName": "Example"
        }
      ]
    }
  ]
}