تخطَّ إلى المحتوى

Http Client Lifetime (ARCH3083)

هذا المحتوى غير متوفر بلغتك بعد.

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.

async function fetchUser(id: string): Promise<User> {
const agent = new https.Agent({ keepAlive: true });
const res = await fetch(`/users/${id}`, { agent });
return res.json();
}
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.