Skip to content

Update DNS caching example? #4120

@GeoffreyBooth

Description

@GeoffreyBooth

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 dns interceptor 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocsChanges related to the documentationenhancementNew feature or requestgood first issueGood for newcomers

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions