← Back to Pull requests

feat: add a socketFactory connection option #548

Closed
unknown closed this pull request without merging on Dec 01, 2026 09:08 UTC
View on GitHub
unknown unknown commented on Nov 28, 2026 15:26 UTC
Author

Fixes #547.

Adds a socketFactory connection option. When it is set, the application opens the socket to each broker, and the client uses that socket instead of calling net.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.

socketFactory?: (options: { host: string; port: number; timeout: number; signal: AbortSignal }) => Socket | Promise<Socket>

Behaviour

  • The factory returns a plain net.Socket, either connected or still connecting. With tls set, the client negotiates TLS on it itself (tls.connect({ socket, host, ... })). So SNI inference, tlsServerName, verifying the certificate against host and 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, connectionsMaxIdle and back pressure apply to the factory's socket as they do to the client's own.

  • A factory that throws, or a promise it rejects, fails the connection with a NetworkError whose cause is the factory's error. The usual retry rules apply, so a canRetry: false anywhere in the chain stops retries.

  • connectTimeout bounds 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. A timeout the application sets on the socket is in addition to it.

  • When the attempt times out, or close() is called first, signal is 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 to net.connect().

  • A value that isn't a net.Socket, a socket that is destroyed or was never connected, and a TLSSocket while tls is set are destroyed, and fail the connection with a UserError.
  • 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 local attachSocket helper so both paths share it. The other changes are ?. on #socket in the two error paths, which can now run before a socket exists, and an early return in close() and in ready()'s timeout for an attempt still waiting on the factory. Existing tests are unchanged.

    Changes

  • src/network/connection.ts: the option, plus SocketFactory / SocketFactoryOptions (exported through src/index.ts).

  • src/clients/base/options.ts: socketFactory: { function: true }, and src/clients/base/validators.ts regenerated with npm run generate:validators.

  • docs/base.md: a constructor table row and a "Custom socket factory" section with an example.

  • Tests:

  • - 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, a TLSSocket with tls set, timeout and close() 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 (socket timeout, aborted signal, 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 build and pnpm test pass locally (Node 22, docker compose up -d --wait).

    🤖 Generated with Claude Code

    Diff: 503 lines (+377 / -126) across 6 files Reviewer: ShogunPanda Review duration: 3942m
    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
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 47.5% (MEDIUM) · Higher = larger blast radius
    47.5%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 18.8% · Safe to merge
    18.8%