An Agent dispatches requests against multiple different origins, creating and
reusing a per-origin Pool (or Client) on demand. It is the default
dispatcher used by request, stream, and fetch when no explicit
dispatcher is supplied.
Requests are not guaranteed to be dispatched in their order of invocation.
import { Agent, setGlobalDispatcher } from 'undici'
const agent = new Agent({ connections: 10 })
setGlobalDispatcher(agent)class Agent extends DispatcherAgent keeps an internal map of origins to dispatchers. The first request to a
given origin lazily creates a dispatcher through the configured factory, and
subsequent requests to the same origin reuse it. Idle dispatchers are closed
automatically once they have no open connections and are no longer busy.
Agent Constructor
History
Added the maxOrigins option to cap the number of origins.
new Agent(options?): void<AgentOptions><PoolOptions>
.<Function>connections: 1
, the default factory creates a
<Client>
; otherwise it creates a
<Pool>
.
Default:
(origin, opts) => new Pool(origin, opts)
.<Object><Dispatcher><number><MaxOriginsReachedError>
is thrown when
a request targets a new origin once the limit is reached. When set to
Infinity
, no limit is enforced. Must be a number greater than
0
.
Default:
Infinity
.Agent inherits all <PoolOptions> (and therefore all <ClientOptions>). The
per-origin <Pool> it creates uses the default unlimited connections, so
concurrent requests to the same origin are spread across separate <Client>
instances on separate sockets.
Because each concurrent request to an origin may use a different <Client>,
HTTP/2 multiplexing on a shared session does not apply unless connections is
set to a small value (for example connections: 1). See <PoolOptions> and
<ClientOptions> for the full set of inherited options such as allowH2
(default true) and maxConcurrentStreams (default 100).
<boolean>true after agent.close() has been called.
<boolean>true after agent.destroy() has been called, or after agent.close()
has been called and the shutdown has completed.
<Record>Aggregate statistics for the agent, keyed by origin. Each value is the
ClientStats or PoolStats object reported by the dispatcher currently
serving that origin. Origins whose dispatcher does not expose stats are
omitted.
import { Agent } from 'undici'
const agent = new Agent()
await agent.request({ origin: 'http://localhost:3000', path: '/', method: 'GET' })
console.log(agent.stats)
// { 'http://localhost:3000': PoolStats { ... } }agent.dispatch(options, handler): boolean<AgentDispatchOptions><DispatchOptions>
.<DispatchHandler><boolean>false
if the dispatcher is busy and the caller should
wait for the
'drain'
event before dispatching again.Routes the request to the dispatcher for options.origin, creating one through
the factory if needed. Throws an <InvalidArgumentError> when options.origin is not a non-empty string or <URL>, and a <MaxOriginsReachedError> when a new
origin would exceed maxOrigins. Implements Dispatcher.dispatch().
agent.close(callback?): Promise | void<Function><Promise>
is returned.<Promise>
|
<void>Gracefully closes the agent and every dispatcher it owns, waiting for in-flight
requests to complete. Implements Dispatcher.close().
agent.destroy(error?, callback?): Promise | void<Error><Function><Promise>
is returned.<Promise>
|
<void>Forcefully destroys the agent and every dispatcher it owns, aborting all pending
requests. Implements Dispatcher.destroy().
agent.connect(options, callback?): voidSee Dispatcher.connect(). The request is routed to the dispatcher for
options.origin.
agent.pipeline(options, handler): voidSee Dispatcher.pipeline(). The request is routed to the dispatcher for
options.origin.
agent.request(options, callback?): voidSee Dispatcher.request(). The request is routed to the dispatcher for
options.origin.
import { Agent } from 'undici'
const agent = new Agent()
const { statusCode, body } = await agent.request({
origin: 'http://localhost:3000',
path: '/',
method: 'GET',
})
console.log('response received', statusCode)
console.log('data', await body.text())agent.stream(options, factory, callback?): voidSee Dispatcher.stream(). The request is routed to the dispatcher for
options.origin.
agent.upgrade(options, callback?): voidSee Dispatcher.upgrade(). The request is routed to the dispatcher for
options.origin.
<URL>Agent
followed by the per-origin dispatcher chain.Emitted when a socket has been created for an origin and is ready to receive
requests. Forwarded from the underlying dispatcher's 'connect' event.
Emitted when a socket for an origin has disconnected. Forwarded from the
underlying dispatcher's 'disconnect' event.
Emitted when a connection attempt for an origin fails. Forwarded from the
underlying dispatcher's 'connectionError' event.
<URL>Agent
.Emitted when an origin's dispatcher is no longer busy and can accept more
requests. Forwarded from the underlying dispatcher's 'drain' event.