Undici Connection Pooling With Custom Agent

Create an undici Agent with per-origin connection limits and pipelining depth, then dispatch pooled HTTP requests with explicit abort timeouts.

JavascriptNodeData Fetching

Code

snippet
import { Agent, fetch } from 'undici';

const agent = new Agent({
  connections: 128,
  pipelining: 10,
  keepAliveTimeout: 60_000,
  keepAliveMaxTimeout: 300_000,
});

const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), 5000);

const res = await fetch('https://api.example.com/users', {
  dispatcher: agent,
  signal: controller.signal,
});
const users = await res.json();
clearTimeout(timer);

agent.close();