A BalancedPool distributes requests across a set of <Pool> instances, each
connected to a different upstream. It uses a weighted round-robin algorithm:
every upstream starts with the same weight, and the weight is adjusted up on
successful connections and down on connection errors, so that healthy upstreams
receive a larger share of traffic.
Requests are not guaranteed to be dispatched in the order they are issued.
import { BalancedPool } from 'undici'
const pool = new BalancedPool([
'http://localhost:3000',
'http://localhost:3001',
'http://localhost:3002',
])
const { statusCode, body } = await pool.request({
path: '/',
method: 'GET',
})
console.log('response received', statusCode)
for await (const chunk of body) {
console.log('data', chunk.toString())
}class BalancedPool extends DispatcherA pool of <Pool> instances connected to multiple upstreams.
new BalancedPool(upstreams?, options?): void<string>
[] One or more upstream origins. Each
value should only include the
protocol, hostname, and port
.
Default:
[]
.<BalancedPoolOptions>The options passed to the constructor are forwarded to every <Pool> instance
that is created for an upstream.
Extends: <PoolOptions>
In addition to all <PoolOptions>, the following options are supported:
<Function><Pool>
instance for each
upstream.
Default:
(origin, opts) => new Pool(origin, opts)
.<Object><Dispatcher><number>100
.<number>15
.balancedPool.addUpstream(upstream): BalancedPool<BalancedPool>BalancedPool
instance, for chaining.Adds an upstream. If an open, non-destroyed upstream with the same origin is already present, the call is a no-op.
balancedPool.removeUpstream(upstream): BalancedPool<BalancedPool>BalancedPool
instance, for chaining.Removes an upstream that was previously added. If no matching upstream is found, the call is a no-op.
balancedPool.getUpstream(upstream): Pool | undefined<Pool>
|
<undefined><Pool>
instance managing the given upstream, or
undefined
if it is not present.Returns the <Pool> instance for the given upstream origin, if it is open and not
destroyed.
<string><boolean>true
after
balancedPool.close()
has been called.Implements [client.closed][].
<boolean>true
after
balancedPool.destroy()
has been called, or after
balancedPool.close()
has been called and the shutdown has completed.Implements [client.destroyed][].
<PoolStats>Returns a <PoolStats> instance aggregating the statistics of the underlying
pools.
balancedPool.close(callback?): voidImplements [Dispatcher.close([callback])][].
balancedPool.destroy(error?, callback?): voidImplements [Dispatcher.destroy([error, callback])][].
balancedPool.connect(options, callback?): voidSee [Dispatcher.connect(options[, callback])][].
balancedPool.dispatch(options, handlers): voidImplements [Dispatcher.dispatch(options, handler)][].
balancedPool.pipeline(options, handler): voidSee [Dispatcher.pipeline(options, handler)][].
balancedPool.request(options, callback?): voidSee [Dispatcher.request(options[, callback])][].
balancedPool.stream(options, factory, callback?): voidSee [Dispatcher.stream(options, factory[, callback])][].
balancedPool.upgrade(options, callback?): voidSee [Dispatcher.upgrade(options[, callback])][].
See Dispatcher Event: 'connect'.
See Dispatcher Event: 'disconnect'.
See Dispatcher Event: 'drain'.
[Dispatcher.close([callback])]: Dispatcher.md#dispatcherclosecallback-promise
[Dispatcher.connect(options[, callback])]: Dispatcher.md#dispatcherconnectoptions-callback
[Dispatcher.destroy([error, callback])]: Dispatcher.md#dispatcherdestroyerror-callback-promise
[Dispatcher.dispatch(options, handler)]: Dispatcher.md#dispatcherdispatchoptions-handler
[Dispatcher.pipeline(options, handler)]: Dispatcher.md#dispatcherpipelineoptions-handler
[Dispatcher.request(options[, callback])]: Dispatcher.md#dispatcherrequestoptions-callback
[Dispatcher.stream(options, factory[, callback])]: Dispatcher.md#dispatcherstreamoptions-factory-callback
[Dispatcher.upgrade(options[, callback])]: Dispatcher.md#dispatcherupgradeoptions-callback
[client.closed]: Client.md#clientclosed
[client.destroyed]: Client.md#clientdestroyed