-
-
Notifications
You must be signed in to change notification settings - Fork 729
Description
Despite the outcome of #2440, it seems like DNS caching is already part of Undici, via https://undici.nodejs.org/#/docs/api/Dispatcher?id=dns:
The
dnsinterceptor enables you to cache DNS lookups for a given duration, per origin.
Assuming I understand this correctly, the example at https://undici.nodejs.org/#/examples/?id=cacheable-dns-lookup should probably be updated to use the built-in dns interceptor instead of cacheable-lookup.
Here is an example from my code; improvement suggestions welcome:
import { Agent, interceptors, setGlobalDispatcher } from 'undici'
// Interceptors to add response caching, DNS caching and retrying to the dispatcher
const { cache, dns, retry } = interceptors
const defaultDispatcher = new Agent({
connections: 100, // https://github.com/nodejs/undici/issues/3221
headersTimeout: 10_000,
bodyTimeout: 10_000,
}).compose(cache(), dns(), retry())
setGlobalDispatcher(defaultDispatcher)Perhaps the example in the docs should be updated to something similar to this? And maybe rather than showing only DNS caching, it might be a better example to show multiple interceptors for increasing robustness; or more generally, I would find it useful for the docs to include a recommendation for how to configure the default dispatcher for high-traffic production applications.