feat: add connectTimeout option for TCP connection timeout#50
feat: add connectTimeout option for TCP connection timeout#50pabloelisseo wants to merge 2 commits intosagemathinc:mainfrom
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f46c7df564
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| // Handle connection timeout - fires if TCP handshake doesn't complete in time | ||
| if (options.connectTimeout) { | ||
| const connectTimer = setTimeout(() => { | ||
| const err = new Error("ECONNECT_TIMEOUT") as NodeJS.ErrnoException; | ||
| err.code = "ECONNECT_TIMEOUT"; | ||
| socket.destroy(err); | ||
| }, options.connectTimeout); |
There was a problem hiding this comment.
Skip connect timeout on already-connected sockets
connectTimeout is armed on every proxyReq socket, but on keep-alive reuse the connect event does not fire. In that case the timer will expire after connectTimeout and call socket.destroy(err) while a valid, already-established connection is carrying the request, causing intermittent failures whenever connection pooling is enabled. This only happens when connectTimeout is set and the agent reuses sockets; consider checking socket.connecting (or clearing immediately when it’s false) so the timeout only applies to the TCP handshake.
Useful? React with 👍 / 👎.
Summary
Adds a new
connectTimeoutoption that sets a timeout for the TCP connection handshake phase,separate from the existing
proxyTimeout(which handles idle socket timeout after connection).Closes #48
Problem
When proxying to an unreachable target (firewall drops packets, non-existent host), the proxy
hangs for 75-130 seconds waiting for OS-level TCP timeout. The existing
proxyTimeoutonlyfires after connection is established.
Solution
Listen for the
socketevent on the proxy request and set a timer that destroys the socketif
connectdoesn't fire within the specified time.Testing
Added tests for:
Notes
connect.timeoutoption (documented in README)ECONNECT_TIMEOUTto distinguish fromETIMEDOUT(OS-level) andECONNRESET