A Dispatcher that automatically retries failed requests by wrapping
another dispatcher with the retry logic of RetryHandler. Requests that fail
with a retryable status code or transport error are re-dispatched according to
the configured retry policy.
import { RetryAgent } from 'undici'class RetryAgent extends DispatcherWraps an existing dispatcher and applies retry behaviour to every request it
dispatches. Lifecycle methods such as retryAgent.close() and
retryAgent.destroy() are forwarded to the wrapped dispatcher.
new RetryAgent(dispatcher, options?): void<Dispatcher><RetryOptions><boolean>true
, an error is thrown on the final retry
attempt, preventing the following handlers from being called and destroying
the socket. Disable it when you need the response body for error responses
or when you provide a custom error handler.
Default:
true
.<Function><Error><RetryContext><Object>retryOptions
.<Function>Error
to stop retrying,
or
null
/
undefined
to perform the next retry.<number>5
.<number>30000
(30 seconds).<number>500
(half a second).<number>2
.<boolean>true
, the delay between retries is inferred from
the
Retry-After
response header when present.
Default:
true
.<string>['GET', 'HEAD', 'OPTIONS', 'PUT', 'DELETE', 'TRACE']
.<number>[500, 502, 503, 504, 429]
.<string>['ECONNRESET', 'ECONNREFUSED', 'ENOTFOUND', 'ENETDOWN', 'ENETUNREACH', 'EHOSTDOWN', 'EHOSTUNREACH', 'EPIPE', 'UND_ERR_SOCKET']
.Creates a new RetryAgent that wraps dispatcher. The options are stored and
used to construct a RetryHandler for each dispatched request.
import { Agent, RetryAgent } from 'undici'
const agent = new RetryAgent(new Agent(), {
maxRetries: 3,
minTimeout: 1000,
timeoutFactor: 2,
})
const res = await agent.request({
method: 'GET',
origin: 'http://example.com',
path: '/',
})
console.log(res.statusCode)
console.log(await res.body.text())retryAgent.dispatch(options, handler): boolean<DispatchOptions>dispatcher.dispatch()
.<DispatchHandler><boolean>Dispatches options through the wrapped dispatcher, installing a
RetryHandler that applies the configured retry policy before delegating to
handler. Returns false if the dispatcher is busy and the caller should wait
before dispatching again, otherwise true.
retryAgent.close(): Promise<Promise>Gracefully closes the wrapped dispatcher, waiting for in-flight requests to
complete. Equivalent to calling dispatcher.close() on the wrapped
dispatcher.
retryAgent.destroy(): Promise<Promise>Forcefully destroys the wrapped dispatcher, aborting all pending requests.
Equivalent to calling dispatcher.destroy() on the wrapped dispatcher.