feat: add a socketFactory connection option #548
All Vouch review confidence checks passed
Residual risk 0.19 = Change Risk (0.48) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.48 · 503 lines diff across 6 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.372
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #548"
general
weight: 0.35
ShogunPanda
Fixes #547.
Adds a
socketFactoryconnection option. When it is set, the application opens the socket to each broker, and the client uses that socket instead of callingnet.connect()/tls.connect()itself. It is called for every broker connection, bootstrap and advertised alike, so an application can check broker addresses before connecting, go through a proxy, or reach brokers through a tunnel.Behaviour
net.Socket, either connected or still connecting. Withtlsset, the client negotiates TLS on it itself (tls.connect({ socket, host, ... })). So SNI inference,tlsServerName, verifying the certificate againsthostand the "TLS handshake failed" message all work as they do now, and the factory never handles TLS. This answers the open question in #547; I went with the client layering TLS because it keeps those rules in one place.keepAlive,setNoDelay, SASL,connectionsMaxIdleand back pressure apply to the factory's socket as they do to the client's own.NetworkErrorwhosecauseis the factory's error. The usual retry rules apply, so acanRetry: falseanywhere in the chain stops retries.connectTimeoutbounds the whole attempt: the factory, the TCP connection of a socket it returns still connecting, and the TLS handshake. A timeout fails every waiter, not only the one whose timer fired, and is reported once. Atimeoutthe application sets on the socket is in addition to it.close()is called first,signalis aborted and a socket returned later is destroyed; a close also ends the connect's diagnostics. The signal is never aborted once the connection is up, so it can be passed tonet.connect().net.Socket, a socket that is destroyed or was never connected, and aTLSSocketwhiletlsis set are destroyed, and fail the connection with aUserError.Compatibility
The option is unset by default. Without it,
connect()runs as before, except that an error the socket emits after a connect timeout is now ignored instead of being reported a second time; the socket set-up moved into a localattachSockethelper so both paths share it. The other changes are?.on#socketin the two error paths, which can now run before a socket exists, and an early return inclose()and inready()'s timeout for an attempt still waiting on the factory. Existing tests are unchanged.Changes
src/network/connection.ts: the option, plusSocketFactory/SocketFactoryOptions(exported throughsrc/index.ts).src/clients/base/options.ts:socketFactory: { function: true }, andsrc/clients/base/validators.tsregenerated withnpm run generate:validators.docs/base.md: a constructor table row and a "Custom socket factory" section with an example.-
test/network/connection.test.ts, 30 new: plaintext, a socket still connecting, TLS with SNI for a DNS name / an IP /tlsServerName, keep-alive and no-delay, the factory throwing or rejecting, a non-socket, never-connected or destroyed socket, aTLSSocketwithtlsset, timeout andclose()while pending (signal aborted, late socket destroyed), two waiters on a factory that never answers, a TLS handshake that never completes, reconnect after a failure, a timeout reported once (sockettimeout, abortedsignal, with and without TLS), reconnect after a timeout, late and refused sockets that emit an error, diagnostic channels on failure and on close;-
test/network/connection-pool.test.ts: one factory call per broker;-
test/clients/base/base.test.ts: option validation.npm run lint,npm run buildandpnpm testpass locally (Node 22,docker compose up -d --wait).🤖 Generated with Claude Code