Skip to content

Commit 67adf42

Browse files
Update DNS caching example to include other interceptors, production settings (#4127)
1 parent 73916b3 commit 67adf42

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

docs/examples/README.md

+12-8
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,21 @@ async function deleteRequest (port = 3001) {
126126
}
127127
```
128128

129-
## Cacheable DNS Lookup
129+
## Production configuration
130130

131-
### Using CacheableLookup to cache DNS lookups in undici
131+
### Using interceptors to add response caching, DNS lookup caching and connection retries
132132

133133
```js
134-
import { Agent } from 'undici'
135-
import CacheableLookup from 'cacheable-lookup';
134+
import { Agent, interceptors, setGlobalDispatcher } from 'undici'
136135

137-
const cacheable = new CacheableLookup(opts)
136+
// Interceptors to add response caching, DNS caching and retrying to the dispatcher
137+
const { cache, dns, retry } = interceptors
138138

139-
const agent = new Agent({
140-
connect: { lookup: cacheable.lookup }
141-
})
139+
const defaultDispatcher = new Agent({
140+
connections: 100, // Limit concurrent kept-alive connections to not run out of resources
141+
headersTimeout: 10_000, // 10 seconds; set as appropriate for the remote servers you plan to connect to
142+
bodyTimeout: 10_000,
143+
}).compose(cache(), dns(), retry())
144+
145+
setGlobalDispatcher(defaultDispatcher) // Add these interceptors to all `fetch` and Undici `request` calls
142146
```

0 commit comments

Comments
 (0)