* fix(httpclient): bound response-header waits and retry stalled reads on a new connection The maintainer-city shadow run saw 22 requests sit out the full 60s client timeout, each on one silent HTTP/1.1 connection while other connections to the same serve kept answering. wire.Client now bounds an idempotent request's wait for response headers (15s default; BEADS_HTTP_RESPONSE_HEADER_TIMEOUT or DialOptions.ResponseHeaderTimeout; "off" disables), separately from the 60s overall timeout, and stops the timer once headers arrive so a streaming body is never cut. A read (GET/HEAD/OPTIONS, or a Request marked Idempotent: the POST batch get) that stalls or has its connection reset is retried once: the stalled attempt's cancellation closes its connection and idle connections are dropped, so the retry dials fresh. Every retry is counted (wire.StallRetryCount) and reported (OnStallRetry; the dialer's default logs one stderr line with the count). Writes are never retried and get no header timer, because bd serve answers a write only after it commits (a large batch apply can legitimately take tens of seconds). A write whose overall timeout fires before its headers fails with a typed *wire.ResponseHeaderTimeoutError (inside *ConnectError) naming the host and saying the outcome is unknown. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(httpclient): retry a stalled read within the overall timeout (bd-p1wzpi) Adoption review of #7553 found three defects in the header timer and its retry. This maintainer fixup sits on top of the contributor commit and supersedes three of its claims: that the timer bounds a read's every attempt, that idle connections are dropped before the retry, and that any write whose overall timeout fires before its headers is typed. A slow but healthy read is no longer cut short. The 15s timer cannot tell a silent connection from a serve still working on the read (serve writes a read's headers only after waiting up to 10s for a database slot and running the query), and the retry ran under a fresh 15s timer, so a read slower than 15s failed at about 30s after sending two requests where it used to succeed within 60s. The timer now bounds only the first attempt. The retry has no header timer and runs under a deadline of when the first attempt was sent plus the client's overall Timeout, the moment that Timeout itself counts from, so it finishes such a read and no read waits longer than the 60s it could before. The request is built and stamped once and each attempt sends a copy of it, so the credential source is consulted once, before any clock starts, and a cold token command costs the retry none of its budget. What is left: a read whose headers take longer than the timer, and whose retry then needs more than what remains of the 60s (about 45s) for its headers and body together, now fails at 60s where it used to succeed, and the server also gets the abandoned first request. A read whose first attempt already spent the whole Timeout (the timer off) is not retried at all and keeps its typed timeout. A request is typed *ResponseHeaderTimeoutError only once it was written. Any Timeout() error on a write used to be typed, including the baseline transport's dial and TLS handshake timeouts where nothing reached the server, and the message then said the write "may or may not have been applied", gave the overall timeout as the wait, dropped the transport's error, and Temporary() was true. The timer is now armed from httptrace's WroteRequest (where http.Transport.ResponseHeaderTimeout starts its own clock), which also records that the request went out. A timeout is typed only for a written request whose caller's context is still alive; a dial or TLS timeout stays a plain *ConnectError. After is the measured wait, Err is printed, Temporary() reports Replayable (false for a write), and a retried read says why its first attempt was abandoned (Reason). Arming at the write also stops a slow credential source (BEADS_HTTP_TOKEN_COMMAND may take 30s) from being reported, counted and retried as a server stall. No idle-connection flush. CloseIdleConnections ran on a transport shared by every no-CA dial in the process (baselineTransport), so one stalled read dropped idle keep-alives to every host. Canceling the stalled attempt already closes its connection (net/http never pools a canceled HTTP/1.1 connection), so the retry rides another one. Also: - wire.Request.Replayable (GET/HEAD/OPTIONS, or Idempotent) is now the one predicate behind both the wire's stall retry and dispatch's bounded 503 retry, so the batch get, a read on POST, is retried on a 503 too. dispatch's doc comment no longer says the wire never resends. - The docs, the error and the stall line say the retry goes out on "another connection", not a "new" one: the idle pool may hand it one already open. dialStream's doc says the events:watch connect gets neither the timer nor the retry. - BEADS_HTTP_RESPONSE_HEADER_TIMEOUT takes bare seconds ("30") as BEADS_GATE_WAIT_TIMEOUT does, and "off" in any case. "off" or 0 turns off only the timer: a reset connection is still retried and a written request that times out is still typed. - README says "each read" and spells out the above, bounding the retry by its headers and body together; it no longer says a streaming body is never cut off (the header timer never does, the 60s bound still does). The CHANGELOG entry moves from Added to Fixed and states the same bound. - CredentialProvider's doc, in wire and on the public bdhttp alias, says Authorize runs once per request, not once per attempt: the stall retry resends the stamped headers up to the overall timeout later, so a single-use or short-lived credential may draw a 401 there, which reaches Refresh like any other. Tests: a read queued behind a LimitListener backlog finishes once a slot frees; a read slower than the timer on every connection finishes on its retry; a slow credential source is not a stall, and one that takes the whole Timeout to resolve still leaves the retry its budget (starting the deadline before the stamp, or re-stamping the retry, fails it); a write whose dial times out is a plain *ConnectError; a stall leaves another server's idle connection on a shared transport alone; a retry after a reset names the reset; the stall-twice case is bounded by the overall timeout and pins After, Reason and the message; dispatch retries a read on POST on a 503, and GetMany's batch get is sent as one (dropping its Idempotent fails that); the env knob takes "30", " 1.5 " and "OFF" and refuses "-3". A read whose first attempt spent the whole Timeout is not retried and keeps its typed error (dropping the spent-budget check fails it; no other test did). A stall retry that draws a 401 refreshes once and authorizes only the re-issued request, so Authorize runs twice, not three times (re-stamping the retry fails it). Not covered, as before, and now said in README and the CHANGELOG: the events:watch stream's connect (dialStream) has no header timer or retry. Nor is a reset on Windows: stallReason matches syscall.ECONNRESET, which a Windows reset (WSAECONNRESET) is not, so there a reset connection is retried only when it surfaces as EOF. Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Eddie the Engineer <adopt-pr@gascity.com>
466 lines
21 KiB
Go
466 lines
21 KiB
Go
// Contributed by gascity from bd-enterprise (internal/enterprise/httpstore/dial.go@49d1df2f6)
|
|
// to OSS beads under the MIT license.
|
|
package httpclient
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/steveyegge/beads/internal/httpapi/apigen"
|
|
"github.com/steveyegge/beads/internal/httpclient/wire"
|
|
)
|
|
|
|
// WireUserAgentSuffix identifies the backend inside a stamped User-Agent, so a
|
|
// server log line says both which binary and which client spoke to it.
|
|
const WireUserAgentSuffix = wire.DefaultUserAgent
|
|
|
|
// DialOptions tunes the production transport.
|
|
type DialOptions struct {
|
|
// UserAgent identifies the build. wire's own default names the backend only,
|
|
// because the version stamp lives in package main and nothing under this
|
|
// tree may import it; the activation layer passes the stamped one down.
|
|
UserAgent string
|
|
// HTTPClient replaces the transport. Tests use it to reach an httptest
|
|
// server or to inject a round tripper; production leaves it nil.
|
|
//
|
|
// Leaving this nil is what lets DialWith apply a target's CA trust
|
|
// (BEADS_HTTP_CA_FILE, or the sidecar's ca_file) automatically, and raise
|
|
// MaxIdleConnsPerHost even when no CA is configured — see TransportFor and
|
|
// baselineTransport.
|
|
//
|
|
// A caller that supplies an HTTPClient whose Transport is nil is not
|
|
// silently skipped: DialWith injects the resolved CA transport (if any)
|
|
// on a COPY of that client, leaving the caller's own *http.Client
|
|
// untouched, exactly as if HTTPClient had been left nil except that the
|
|
// other fields (Timeout, CheckRedirect, Jar) are the caller's.
|
|
//
|
|
// A caller that supplies an HTTPClient with a NON-NIL Transport has taken
|
|
// the transport over completely. If the target has no CA configured, it
|
|
// is used verbatim — DialWith has nothing to add. If the target DOES have
|
|
// a CA configured, DialWith refuses rather than silently using a
|
|
// transport that may not trust it: the caller must either leave
|
|
// Transport nil (and let DialWith inject), or compose TransportFor(target)
|
|
// (or TransportForFile, for the embedder door) into its own Transport
|
|
// itself before calling.
|
|
HTTPClient *http.Client
|
|
|
|
// caOverride, when non-nil, replaces resolveCAFile's own env/sidecar
|
|
// resolution entirely: DialWith uses *caOverride verbatim (which may
|
|
// itself report "no CA configured" via its own configured() == false).
|
|
//
|
|
// It is set two ways, both of which need CAFileEnv taken completely out
|
|
// of the loop rather than merely out-prioritized:
|
|
//
|
|
// - DialOptionsForFile, which has already built HTTPClient's Transport
|
|
// from exactly one explicit file. Without this, DialWith's own
|
|
// env-scoped resolveCAFile would run anyway and could trip the
|
|
// caller-supplied-Transport refusal below the moment CAFileEnv
|
|
// happens to be set (and host-scoped-match) for the same target
|
|
// being verified.
|
|
// - DialOptionsForTarget, which backend/http's embedder door uses for
|
|
// EVERY dial: an embedder juggling several targets in one process
|
|
// must not have any of them silently renarrowed, or blocked, by an
|
|
// ambient CAFileEnv value a completely unrelated invocation
|
|
// exported — only Target.CAFile, passed explicitly, may ever apply
|
|
// on that door.
|
|
//
|
|
// Unexported: a caller outside this package can never set it directly, so
|
|
// an external HTTPClient always goes through one of the two constructors
|
|
// above, or the normal env-aware resolution.
|
|
caOverride *resolvedCA
|
|
|
|
// AllowInsecureCredential opts THIS dial into sending a credential over
|
|
// plain http to a non-loopback target (MED-4, S6 review); see
|
|
// guardInsecureCredential for every grant DialWith honors. connect.go sets
|
|
// this from --allow-plaintext so its own Handshake probe honors the same
|
|
// opt-in the CLI flag already granted, before the sidecar recording it
|
|
// (Target.AllowInsecureCredential) exists. Every other caller leaves it
|
|
// false: a later dial takes the grant from Target.AllowInsecureCredential,
|
|
// and an ambient-ladder dial also from the process-wide
|
|
// BEADS_HTTP_ALLOW_INSECURE=1 escape hatch, which the explicit-credential
|
|
// door (DialWithCredential) never reads.
|
|
AllowInsecureCredential bool
|
|
|
|
// explicitCredential is set by DialWithCredential alone: the caller
|
|
// handed this dial its own credential, so DialWith must consult no
|
|
// process environment for it — not BEADS_HTTP_ALLOW_INSECURE (the
|
|
// plaintext grant then comes only from AllowInsecureCredential above or
|
|
// Target.AllowInsecureCredential), and, because DialWithCredential also
|
|
// pins CA resolution through DialOptionsForTarget, not CAFileEnv either.
|
|
// Unexported so the only way to set it is the door that also refuses a
|
|
// nil credential.
|
|
explicitCredential bool
|
|
|
|
// ResponseHeaderTimeout bounds a read's first attempt's wait for response
|
|
// headers, separately from the client's overall timeout
|
|
// (wire.DefaultTimeout): see wire.Options.ResponseHeaderTimeout. Zero takes
|
|
// ResponseHeaderTimeoutEnv, else wire.DefaultResponseHeaderTimeout;
|
|
// negative disables it.
|
|
ResponseHeaderTimeout time.Duration
|
|
// OnStallRetry is told about every retry of a stalled or reset idempotent
|
|
// request. Nil logs one line per retry to stderr, so a stall is visible
|
|
// rather than silently absorbed.
|
|
OnStallRetry func(wire.StallRetry)
|
|
}
|
|
|
|
// ResponseHeaderTimeoutEnv overrides the header timeout for every dial that
|
|
// does not set DialOptions.ResponseHeaderTimeout: a Go duration ("15s",
|
|
// "2m") or a number of seconds ("30"), as BEADS_GATE_WAIT_TIMEOUT takes, or "0"/
|
|
// "off" to disable it. Disabling it turns off only the timer: a read whose
|
|
// connection is reset is still retried once, and a write that times out
|
|
// awaiting its headers is still reported as one. An unparseable value
|
|
// refuses the dial.
|
|
const ResponseHeaderTimeoutEnv = "BEADS_HTTP_RESPONSE_HEADER_TIMEOUT"
|
|
|
|
// responseHeaderTimeout resolves the header timeout for one dial: the
|
|
// explicit option, else ResponseHeaderTimeoutEnv, else 0 (wire's default).
|
|
func responseHeaderTimeout(opt time.Duration) (time.Duration, error) {
|
|
if opt != 0 {
|
|
return opt, nil
|
|
}
|
|
raw := strings.TrimSpace(os.Getenv(ResponseHeaderTimeoutEnv))
|
|
if raw == "" {
|
|
return 0, nil
|
|
}
|
|
if strings.EqualFold(raw, "off") {
|
|
return -1, nil
|
|
}
|
|
d, err := time.ParseDuration(raw)
|
|
if err != nil {
|
|
d, err = time.ParseDuration(raw + "s")
|
|
}
|
|
if err != nil || d < 0 {
|
|
return 0, fmt.Errorf("%s=%q: want a non-negative duration such as 15s or 30, or off", ResponseHeaderTimeoutEnv, raw)
|
|
}
|
|
if d == 0 {
|
|
return -1, nil
|
|
}
|
|
return d, nil
|
|
}
|
|
|
|
// logStallRetry is the default OnStallRetry: one stderr line per retry, with
|
|
// the retry number and the process-wide count.
|
|
func logStallRetry(e wire.StallRetry) {
|
|
fmt.Fprintln(os.Stderr, e.String())
|
|
}
|
|
|
|
// Conn is the store's view of one wire client.
|
|
//
|
|
// It exists to answer the WireClient seam's ServerContext with the GATED
|
|
// handshake rather than the raw fetch: api_version equality and, when the
|
|
// workspace recorded one, project identity are the two checks a snapshot must
|
|
// pass before anything is served from it (design D6). GetContext would return a
|
|
// body from any server that answered.
|
|
type Conn struct {
|
|
*wire.Client
|
|
}
|
|
|
|
func (c *Conn) ServerContext(ctx context.Context) (*apigen.ContextResponse, error) {
|
|
snap, err := c.Handshake(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
body := snap.Context
|
|
return &body, nil
|
|
}
|
|
|
|
// Dial builds the transport for one resolved target: the default credential
|
|
// ladder plus a wire client bound to it.
|
|
func Dial(target Target, opts DialOptions) (*Conn, error) {
|
|
return DialWith(target, NewBearerProvider(target.BaseURL), opts)
|
|
}
|
|
|
|
// DialWith is Dial with the credential source chosen by the caller — the other
|
|
// half of the D5 seam, for the layers that ride it: a gateway dialer outside
|
|
// this repo and the public registrant (backend/http), each of
|
|
// which binds a provider this package does not know about.
|
|
//
|
|
// It exists so the target-to-client mapping has ONE body. The gateway dialer
|
|
// used to build the wire client itself, and the registrant would have been the
|
|
// second copy of the same six lines; what a copy really duplicates is
|
|
// ExpectProjectID, the identity pin that travels on the Target and is applied
|
|
// through the wire Options — a caller that dropped it would dial a client whose
|
|
// handshake silently skips the wrong-server gate.
|
|
//
|
|
// creds may be nil, which is the tip OSS server's loopback-trust posture: no
|
|
// Authorization header is sent and a 401 is never retried.
|
|
//
|
|
// When opts.HTTPClient is nil, or has a nil Transport, DialWith resolves
|
|
// target's CA trust (the same env-scoped resolution TransportFor does) and
|
|
// dials with a transport scoped to it — see DialOptions.HTTPClient for the
|
|
// exact rules, including the refusal when a caller-supplied HTTPClient
|
|
// already has its own Transport and the target has a CA configured. This is
|
|
// what lets `bd connect --ca-file` verify a server during its own Handshake
|
|
// call, before anything is written, and what lets the default and gateway
|
|
// dialers honor BEADS_HTTP_CA_FILE with no further plumbing. Every dial also
|
|
// gets the raised MaxIdleConnsPerHost ceiling (baselineTransport), whether or
|
|
// not a CA is configured — the RTT cost of an under-pooled host is the same
|
|
// either way.
|
|
func DialWith(target Target, creds CredentialProvider, opts DialOptions) (*Conn, error) {
|
|
var resolved resolvedCA
|
|
if opts.caOverride != nil {
|
|
resolved = *opts.caOverride
|
|
} else {
|
|
var err error
|
|
resolved, err = resolveCAFile(target)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
switch {
|
|
case opts.HTTPClient == nil:
|
|
transport, terr := dialTransport(resolved)
|
|
if terr != nil {
|
|
return nil, terr
|
|
}
|
|
opts.HTTPClient = &http.Client{Transport: transport, Timeout: wire.DefaultTimeout}
|
|
|
|
case opts.HTTPClient.Transport == nil:
|
|
// The SAME transport the opts.HTTPClient == nil arm above builds
|
|
// through dialTransport — the CA-scoped one when resolved names a
|
|
// file, the shared raised-ceiling baseline otherwise — because the
|
|
// doc comment above promises that ceiling on EVERY dial "whether or
|
|
// not a CA is configured", and a caller-supplied client with a nil
|
|
// Transport is still a dial this package owns the Transport for.
|
|
// Leaving it nil here would let it default to
|
|
// http.DefaultTransport's own unraised ceiling at request time
|
|
// instead, silently exempting this one caller-supplied-client shape
|
|
// from that promise. Injected onto a COPY, so the caller's own
|
|
// *http.Client (Timeout, CheckRedirect, Jar) is left untouched.
|
|
transport, terr := dialTransport(resolved)
|
|
if terr != nil {
|
|
return nil, terr
|
|
}
|
|
clientCopy := *opts.HTTPClient
|
|
clientCopy.Transport = transport
|
|
opts.HTTPClient = &clientCopy
|
|
|
|
default:
|
|
if resolved.configured() {
|
|
matched, verifyErr := matchesCachedTransport(opts.HTTPClient.Transport, resolved.path)
|
|
if !matched {
|
|
if errors.Is(verifyErr, errCAFileChangedSinceTransportBuilt) {
|
|
return nil, fmt.Errorf(
|
|
"%s configures a CA for %s, and the supplied HTTPClient's Transport was built by this package for the same file, but that file has changed since: %w",
|
|
resolved.label, target, verifyErr)
|
|
}
|
|
if verifyErr != nil {
|
|
return nil, fmt.Errorf(
|
|
"%s configures a CA for %s, but verifying the supplied HTTPClient's Transport against the current file failed: %w",
|
|
resolved.label, target, verifyErr)
|
|
}
|
|
return nil, fmt.Errorf(
|
|
"%s configures a CA for %s, but the supplied HTTPClient's Transport is not the CA-scoped one this package builds for it; leave DialOptions.HTTPClient nil (or its Transport nil) to let DialWith inject it, or build the Transport from TransportFor(target) (or TransportForFile) so DialWith can recognize it as exactly that CA's transport — or, if you deliberately want a transport of your own choosing, clear the CA (Target.CAFile, and any %s pattern that matches this target) and own trust yourself",
|
|
resolved.label, target, CAFileEnv)
|
|
}
|
|
}
|
|
// Either no CA is configured for this target, or the caller's
|
|
// Transport IS the cached CA-scoped transport TransportFor(target)
|
|
// (or TransportForFile) builds for resolved.path — the documented
|
|
// embedder recipe of composing that transport into HTTPClient before
|
|
// calling. Either way there is nothing to inject and nothing to
|
|
// refuse.
|
|
}
|
|
|
|
// target.AllowInsecureCredential (bee-ghosttrack CHANGES_REQUESTED on
|
|
// #7288, should-fix 2) is `bd connect --allow-plaintext`'s grant,
|
|
// persisted to the sidecar and scoped to the target it names: it is
|
|
// loaded back from there on every ordinary command's dial, not only
|
|
// connect's own Handshake probe, so a workspace that connected with the
|
|
// flag does not also need BEADS_HTTP_ALLOW_INSECURE=1 set for every
|
|
// later `bd` invocation. opts.AllowInsecureCredential stays OR'd in
|
|
// alongside it: connect.go sets that field for its own probe from the
|
|
// SAME flag before the sidecar carrying it even exists yet.
|
|
creds = guardInsecureCredential(target, creds, opts.AllowInsecureCredential || target.AllowInsecureCredential, !opts.explicitCredential)
|
|
|
|
headerTimeout, err := responseHeaderTimeout(opts.ResponseHeaderTimeout)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
onStall := opts.OnStallRetry
|
|
if onStall == nil {
|
|
onStall = logStallRetry
|
|
}
|
|
client, err := wire.New(target.BaseURL, creds, wire.Options{
|
|
HTTPClient: opts.HTTPClient,
|
|
UserAgent: opts.UserAgent,
|
|
ExpectProjectID: target.ExpectProjectID,
|
|
ResponseHeaderTimeout: headerTimeout,
|
|
OnStallRetry: onStall,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &Conn{Client: client}, nil
|
|
}
|
|
|
|
// DialWithCredential is the explicit-credential door: creds is the ONLY
|
|
// credential this dial ever authorizes with, and the dial reads nothing from
|
|
// the process environment or the credentials file to decide anything about
|
|
// it.
|
|
//
|
|
// It exists for an embedder that opens several projects in one process — on
|
|
// one server or many, each with a credential of its own (gc's per-city and
|
|
// per-rig credentials). The ambient ladder Dial binds (NewBearerProvider) is
|
|
// keyed by host[:port] and read from process-global state, so two projects
|
|
// behind the same host would be handed the same token, and a token exported
|
|
// for one would be offered to the other. Here, instead:
|
|
//
|
|
// - no bearer ladder is built: BEADS_HTTP_TOKEN, BEADS_HTTP_TOKEN_COMMAND and
|
|
// the credentials file ([host:port], BEADS_CREDENTIALS_FILE) are never read;
|
|
// - CA trust is pinned to target.CAFile alone (DialOptionsForTarget), so
|
|
// BEADS_HTTP_CA_FILE is never read;
|
|
// - the plaintext-to-non-loopback grant comes only from
|
|
// opts.AllowInsecureCredential or target.AllowInsecureCredential, never
|
|
// from BEADS_HTTP_ALLOW_INSECURE.
|
|
//
|
|
// creds is held by the returned Conn and nowhere else: whatever it resolves
|
|
// or caches is scoped to that one Conn (one store, or one handshake), never
|
|
// to a host in a process-wide table. A nil creds is ErrCredentialRequired —
|
|
// "explicit" with nothing in it would otherwise read as an unauthenticated
|
|
// open the caller never asked for.
|
|
func DialWithCredential(target Target, creds CredentialProvider, opts DialOptions) (*Conn, error) {
|
|
if creds == nil {
|
|
return nil, fmt.Errorf("httpclient: the explicit-credential dial was handed a nil credential: %w", ErrCredentialRequired)
|
|
}
|
|
opts = DialOptionsForTarget(target, opts)
|
|
opts.explicitCredential = true
|
|
return DialWith(target, creds, opts)
|
|
}
|
|
|
|
// dialTransport builds the transport DialWith uses when the caller left
|
|
// HTTPClient nil entirely: the CA-scoped one when resolved names a file, or
|
|
// the shared baseline (system roots, raised MaxIdleConnsPerHost) otherwise.
|
|
func dialTransport(resolved resolvedCA) (http.RoundTripper, error) {
|
|
if resolved.configured() {
|
|
return transportForFile(resolved.path, resolved.label)
|
|
}
|
|
return baselineTransport(), nil
|
|
}
|
|
|
|
var (
|
|
baselineTransportOnce sync.Once
|
|
baselineTransportRT http.RoundTripper
|
|
)
|
|
|
|
// baselineTransport is the http.RoundTripper DialWith uses for a target with
|
|
// no CA configured: http.DefaultTransport's own system-roots TLS trust,
|
|
// completely unchanged, but with MaxIdleConnsPerHost raised the same way
|
|
// TransportFor raises it for a CA-scoped target (see maxIdleConnsPerHost in
|
|
// ca.go) — applied to every http-backend target, not only CA-configured ones,
|
|
// since the RTT cost of an under-pooled host is identical either way. Built
|
|
// once and reused for the life of the process: nothing about it depends on
|
|
// per-target state, so there is nothing to key a cache on and no reason to
|
|
// pay Clone() on every dial.
|
|
func baselineTransport() http.RoundTripper {
|
|
baselineTransportOnce.Do(func() {
|
|
transport := http.DefaultTransport.(*http.Transport).Clone() //nolint:errcheck // http.DefaultTransport is always *http.Transport
|
|
transport.MaxIdleConnsPerHost = maxIdleConnsPerHost
|
|
baselineTransportRT = transport
|
|
})
|
|
return baselineTransportRT
|
|
}
|
|
|
|
// DefaultDialer is the WireDialer the enterprise distribution registers.
|
|
//
|
|
// It performs no handshake: design D6 makes the context fetch lazy, on the first
|
|
// post-baseline dispatch, so opening a workspace to run `bd ready` costs one
|
|
// round trip rather than two. The store owns that fetch and caches it.
|
|
func DefaultDialer(opts DialOptions) WireDialer {
|
|
return func(_ context.Context, target Target) (WireClient, error) {
|
|
return Dial(target, opts)
|
|
}
|
|
}
|
|
|
|
// RegisterDefaultDialer installs the production transport. Init-time wiring
|
|
// only, the same rule as RegisterWireDialer itself.
|
|
func RegisterDefaultDialer(opts DialOptions) {
|
|
RegisterWireDialer(DefaultDialer(opts))
|
|
}
|
|
|
|
// Handshake dials target once and returns its gated startup snapshot.
|
|
//
|
|
// It is the connect path's probe: `bd connect` verifies a server before it
|
|
// writes anything, and it must do so without opening a store, because the
|
|
// workspace it is about to describe may not select this backend yet. A project
|
|
// mismatch comes back as *wire.ProjectMismatchError, which names both ids plus
|
|
// the server's own database and repo root.
|
|
func Handshake(ctx context.Context, target Target, opts DialOptions) (*apigen.ContextResponse, error) {
|
|
conn, err := Dial(target, opts)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return conn.ServerContext(ctx)
|
|
}
|
|
|
|
// HandshakeWithCredential is Handshake through DialWithCredential: the probe
|
|
// authorizes with creds alone and reads no ambient credential, CA or plaintext
|
|
// grant from the process environment. It is what an embedder verifying one
|
|
// tenant's server calls, so the probe can never succeed on a credential the
|
|
// tenant's store would not be using.
|
|
func HandshakeWithCredential(ctx context.Context, target Target, creds CredentialProvider, opts DialOptions) (*apigen.ContextResponse, error) {
|
|
conn, err := DialWithCredential(target, creds, opts)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return conn.ServerContext(ctx)
|
|
}
|
|
|
|
// DialOptionsForFile returns base with HTTPClient set to a transport scoped
|
|
// to EXACTLY caFile, bypassing CAFileEnv and any sidecar entirely.
|
|
//
|
|
// It exists for `bd connect --ca-file X`: X must be verified for itself,
|
|
// before it is written anywhere, regardless of what BEADS_HTTP_CA_FILE
|
|
// currently resolves to for the same host — otherwise a `bd connect` run with
|
|
// the env var set to a good CA and --ca-file pointed at a wrong one would
|
|
// verify against the env's CA while writing the flag's unverified one to the
|
|
// sidecar. Passing the returned DialOptions (with Target.CAFile left "" for
|
|
// this call) to Handshake or Dial makes the verification match exactly what
|
|
// the flag names.
|
|
func DialOptionsForFile(caFile string, base DialOptions) (DialOptions, error) {
|
|
transport, err := TransportForFile(caFile)
|
|
if err != nil {
|
|
return DialOptions{}, err
|
|
}
|
|
base.HTTPClient = &http.Client{Transport: transport, Timeout: wire.DefaultTimeout}
|
|
// This transport was just built from exactly caFile, deliberately
|
|
// bypassing CAFileEnv and any sidecar. DialWith must not re-resolve the
|
|
// CA from the ambient environment for this call, nor refuse the
|
|
// Transport just assigned above as an unrecognized caller-supplied one —
|
|
// an empty override reports "no CA configured" to DialWith's default
|
|
// branch, which is exactly right: this HTTPClient's Transport is already
|
|
// final. See DialOptions.caOverride.
|
|
empty := resolvedCA{}
|
|
base.caOverride = &empty
|
|
return base, nil
|
|
}
|
|
|
|
// DialOptionsForTarget returns base with CA resolution pinned to EXACTLY
|
|
// target.CAFile, bypassing CAFileEnv entirely — never consulting it, not even
|
|
// to prefer the sidecar over a non-matching pattern.
|
|
//
|
|
// It exists for backend/http's embedder door (newDialer), which must compute
|
|
// this FRESH for every target it dials: an embedder juggling several targets
|
|
// in one process must not have any of them silently renarrowed to a private
|
|
// CA, or blocked from a legitimate one, because an unrelated invocation
|
|
// exported CAFileEnv for a completely different target. Unlike
|
|
// DialOptionsForFile, this does not build a transport itself — base.HTTPClient
|
|
// is left exactly as the caller supplied it (nil, a client with a nil
|
|
// Transport for DialWith to inject into on a copy, or a client whose
|
|
// Transport the caller already built via TransportFor(target)), and DialWith
|
|
// resolves and injects, or recognizes and accepts, from this override alone.
|
|
func DialOptionsForTarget(target Target, base DialOptions) DialOptions {
|
|
resolved := resolvedCA{}
|
|
if caFile := strings.TrimSpace(target.CAFile); caFile != "" {
|
|
resolved = resolvedCA{path: caFile, label: "ca_file"}
|
|
}
|
|
base.caOverride = &resolved
|
|
return base
|
|
}
|