beads/internal/httpclient/dispatch.go
Julian Knutsen 048f5e5192
fix(httpclient): bound response-header waits and retry stalled reads on another connection (#7553)
* 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>
2026-10-14 05:04:19 +00:00

120 lines
4.5 KiB
Go

// Contributed by gascity from bd-enterprise (internal/enterprise/httpstore/dispatch.go@49d1df2f6)
// to OSS beads under the MIT license.
package httpclient
import (
"context"
"errors"
"fmt"
"time"
"github.com/steveyegge/beads/internal/httpclient/wire"
)
const (
// maxIdempotentRetries bounds how many times dispatch comes back to a busy
// server for one idempotent read. Three attempts total: a 503 is transient
// slot pressure, and a client that hammered past this would BE the contention
// it is waiting out.
maxIdempotentRetries = 2
// baseRetryBackoff is the wait before the first retry when the server named no
// Retry-After. It doubles each attempt, capped at maxRetryBackoff.
baseRetryBackoff = 200 * time.Millisecond
// maxRetryBackoff caps a COMPUTED backoff. A server's own Retry-After is
// already bounded by wire.Options.MaxRetryAfter and is honored as it is given.
maxRetryBackoff = 5 * time.Second
)
// dispatch is this store's ONE door to the wire, and the only place the
// two-speed policy of D6 is spelled.
//
// Every operation goes through Preflight first — the policy itself lives in the
// wire package, which knows that five baseline operations return immediately and
// that every other one forces the handshake and consults the advertised token.
// Putting the call here rather than at each role method is what makes "every
// dispatch site honors the two-speed policy" a property of the code rather than
// of a reviewer's memory: a role that wants to reach the server has no other
// route to it.
//
// The bounded 503 retry lives here too: the wire layer bounds Retry-After and
// hands it back but never sleeps on it. Whether a 503 is safe to come back to
// is the question wire.Request.Replayable answers, and only a read's answer
// is yes. (The wire does resend a replayable request once, at once, when its
// first attempt stalled before any headers or had its connection reset; that
// retry answers a transport-level failure, a 503 is the server's answer.)
//
// A store with no transport is a build-wiring fault (ErrNoTransport), not the
// user's: the workspace and the server may both be fine.
func (s *Store) dispatch(ctx context.Context, req wire.Request, out any) error {
if s.wire == nil {
return fmt.Errorf("%w: cannot dial %s", ErrNoTransport, s.target)
}
// Before the preflight, so the tripwire costs no handshake: `limit=0` is
// refused off loopback, and this client never sends it (unlimited_read.go).
if err := refuseUnlimitedOnWire(req); err != nil {
return err
}
if err := s.wire.Preflight(ctx, req.Op); err != nil {
return err
}
err := s.wire.Do(ctx, req, out)
// Only a replayable read is retried: a GET, or a read on POST that says so
// (the batch get). A 503 can interrupt a non-idempotent write whose first
// attempt already committed before the server ran out of slots, so
// resending a POST/PATCH/DELETE could double-apply it — DELETE included,
// since forgetMemory is a mutation the wire happens to spell as one.
if !req.Replayable() {
return err
}
for attempt := 0; attempt < maxIdempotentRetries; attempt++ {
wait, retry := retryBackoff(err, attempt)
if !retry {
return err
}
if sleepErr := sleepWithContext(ctx, wait); sleepErr != nil {
// The caller's deadline won out mid-backoff. Surface the server's busy
// answer rather than the context error: "bd serve is busy" is the
// actionable diagnosis, and the deadline is downstream of it.
return err
}
err = s.wire.Do(ctx, req, out)
}
return err
}
// retryBackoff reports whether err is a 503-class busy answer worth coming back
// to, and how long to wait first. It honors the server's already-bounded
// Retry-After when present, and otherwise backs off exponentially from
// baseRetryBackoff. A nil error, a non-ProblemError, or any non-retryable
// problem stops the loop.
func retryBackoff(err error, attempt int) (time.Duration, bool) {
var prob *wire.ProblemError
if !errors.As(err, &prob) || !prob.Retryable() {
return 0, false
}
if prob.RetryAfter > 0 {
return prob.RetryAfter, true
}
backoff := baseRetryBackoff << attempt
if backoff > maxRetryBackoff {
backoff = maxRetryBackoff
}
return backoff, true
}
// sleepWithContext waits for d or until ctx is done, whichever comes first. A
// non-positive d is not a sleep at all, so it returns at once.
func sleepWithContext(ctx context.Context, d time.Duration) error {
if d <= 0 {
return nil
}
timer := time.NewTimer(d)
defer timer.Stop()
select {
case <-ctx.Done():
return ctx.Err()
case <-timer.C:
return nil
}
}