fix(fetch): reuse keep-alive HTTP Agents across requests - #13182
Open
SebTardif wants to merge 1 commit into
Open
fix(fetch): reuse keep-alive HTTP Agents across requests#13182SebTardif wants to merge 1 commit into
SebTardif wants to merge 1 commit into
Conversation
Creating a new Agent({ keepAlive: true }) on every fetch left idle
sockets on abandoned Agents. Cache Agents by protocol, proxy, TLS,
timeout, and client cert options, including in-flight first requests.
Contributor
|
All contributors have signed the CLA ✍️ ✅ |
Author
|
I have read the CLA Document and I hereby sign the CLA |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Reuse keep-alive HTTP Agents in
@continuedev/fetchinstead of allocating a newAgent({ keepAlive: true })on every request.fetchwithRequestOptionsis on the path for LLM and other HTTP calls. Each call created a new follow-redirects / proxy Agent withkeepAlive: trueand a 2 hour timeout, then dropped the reference. Idle sockets stayed on those abandoned Agents and could exhaust file descriptors under chat load.keepAlivestays enabled. #8378 already droppedkeepAliveMsecsso TCP keep-alive pings work. This change makes that keep-alive pool actually get reused.Agents are keyed by protocol, proxy (or bypass), timeout,
verifySsl, CA bundle path, and client certificate fields. Concurrent first requests for the same key share one in-flight create so a startup burst does not leak extra Agents.Checklist
Tests
packages/fetch/src/httpAgentCache.test.ts: same options reuse one Agent;verifySsl, timeout, protocol, and proxy isolate the cache; bypassed proxy matches no proxy;keepAliveis true;clearHttpAgentCachedrops the instance; concurrent first requests share one Agent.packages/fetchvitest: 105 passed, including existing e2e. e2eafterEachnow destroys cached sockets so the next test does not reuse a closed server on the same port.Related: #8378, #8099. Agent-per-request dates to #1750;
keepAlive: truewas extracted in #5715.