Http Client Lifetime (ARCH3083)
Esta página aún no está disponible en tu idioma.
Key typescript.performance.http-client-lifetime · Severity error · Category performance
Creating a new HTTP client/agent for every request defeats connection pooling and piles up sockets in TIME_WAIT, causing socket exhaustion. The client should be a shared singleton.
Incorrect
Section titled “Incorrect”async function fetchUser(id: string): Promise<User> { const agent = new https.Agent({ keepAlive: true }); const res = await fetch(`/users/${id}`, { agent }); return res.json();}Correct
Section titled “Correct”const sharedAgent = new https.Agent({ keepAlive: true });
async function fetchUser(id: string): Promise<User> { const res = await fetch(`/users/${id}`, { agent: sharedAgent }); return res.json();}Create the HTTP client/agent once and reuse it as a shared singleton.