fix(relay): set Request.GetBody so the HTTP/2 transport can transparently retry after an upstream stream reset (#6249)
* fix(relay): set Request.GetBody so the HTTP/2 transport can transparently retry after an upstream stream reset
The outbound request body is a type-erased io.Reader over BodyStorage, so
net/http cannot derive Request.GetBody (it only does so for *bytes.Reader,
*bytes.Buffer and *strings.Reader). With GetBody nil, the HTTP/2 transport
cannot transparently retry a request once the body has been written and the
upstream resets the stream with a retryable error (REFUSED_STREAM, or a
connection-level GOAWAY); the relay request then fails with:
http2: Transport: cannot retry err [...] after Request.Body was written;
define Request.GetBody to avoid this error
This affects every relay path that goes through DoApiRequest (chat, claude,
gemini, responses, embedding, image, rerank).
BodyStorage (memory and disk) already implements io.Seeker, so replay support
only needed wiring:
- NewOutboundJSONBody additionally returns a getBody that rewinds the storage
and hands out a fresh non-closing reader. The transport only calls GetBody
after the previous attempt's body has been abandoned, so the rewind cannot
race an in-flight read.
- RelayInfo carries it in the new UpstreamRequestGetBody field, set alongside
UpstreamRequestBodySize by the handlers that build storage-backed bodies.
- applyUpstreamGetBody (symmetric with applyUpstreamContentLength) wires it
into DoApiRequest/DoFormRequest/DoTaskApiRequest, only when req.GetBody is
still nil.
Also remove the hand-rolled GetBody override in DoTaskApiRequest: it returned
the same already-consumed reader, so any transport-level replay would have
silently sent an empty body, and it clobbered the correct snapshot-based
GetBody that net/http derives from the *bytes.Reader bodies the task adaptors
pass in. For non-replayable bodies GetBody now stays nil, so a retry fails
loudly instead of corrupting the request.
Covered by unit tests plus an end-to-end raw-frame HTTP/2 test that resets
the first stream with REFUSED_STREAM after the body is written and asserts
the transport transparently retries with the complete body.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* fix(relay): hand out independent readers from GetBody (address review)
Per the http.Request.GetBody contract ("returns a new copy of Body"),
each call must yield a reader with its own cursor. The previous
implementation rewound and reused the shared BodyStorage, so two
consecutive GetBody readers would interfere with each other, and a
replay could disturb the primary body's offset under extreme transport
timing (e.g. attempt N's body write not yet fully abandoned when the
transport builds attempt N+1).
Instead of snapshotting the payload (an extra copy), add
BodyStorage.NewReader, which returns an independent zero-copy reader:
- memory mode: a fresh bytes.Reader over the same immutable backing
array;
- disk mode: a separate file descriptor over the cache file, so the
transport closing a replayed body only closes that descriptor.
NewOutboundJSONBody's getBody now simply hands out storage.NewReader,
and once the handler releases the storage, GetBody fails with
ErrStorageClosed instead of replaying stale data.
Tests: interleaved reads across two replay readers and the primary
body each observe exactly their own byte stream, for both the memory
and the disk-backed storage; the existing GetBody and HTTP/2 retry
suites still pass (h2 e2e tests flake-free with -count=20).
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* fix(relay): bind replayable metadata on pass-through requests
* fix(relay): reset upstream body metadata between channels
* test(relay): cover replay across retries and channel attempts
* fix(relay): stop following upstream redirects
---------
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -20,6 +20,13 @@ type BodyStorage interface {
|
||||
Size() int64
|
||||
// IsDisk 是否是磁盘存储
|
||||
IsDisk() bool
|
||||
// NewReader returns an independent reader positioned at the start of the
|
||||
// stored payload. Each call returns a reader with its own cursor, so
|
||||
// callers (e.g. http.Request.GetBody) can replay the body concurrently
|
||||
// with, or after, other readers without sharing seek state. Closing the
|
||||
// returned reader releases only that reader, never the storage itself;
|
||||
// after the storage has been closed, NewReader returns ErrStorageClosed.
|
||||
NewReader() (io.ReadCloser, error)
|
||||
}
|
||||
|
||||
// ErrStorageClosed 存储已关闭错误
|
||||
@@ -80,6 +87,18 @@ func (m *memoryStorage) Bytes() ([]byte, error) {
|
||||
return m.data, nil
|
||||
}
|
||||
|
||||
func (m *memoryStorage) NewReader() (io.ReadCloser, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
if atomic.LoadInt32(&m.closed) == 1 {
|
||||
return nil, ErrStorageClosed
|
||||
}
|
||||
// A fresh bytes.Reader over the shared immutable backing array: an
|
||||
// independent cursor at zero copy cost. NopCloser keeps Close a no-op, so
|
||||
// the storage lifecycle stays owned by whoever holds the storage itself.
|
||||
return io.NopCloser(bytes.NewReader(m.data)), nil
|
||||
}
|
||||
|
||||
func (m *memoryStorage) Size() int64 {
|
||||
return m.size
|
||||
}
|
||||
@@ -229,6 +248,24 @@ func (d *diskStorage) Bytes() ([]byte, error) {
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (d *diskStorage) NewReader() (io.ReadCloser, error) {
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
if atomic.LoadInt32(&d.closed) == 1 {
|
||||
return nil, ErrStorageClosed
|
||||
}
|
||||
// A separate file descriptor over the same cache file: an independent
|
||||
// cursor at zero copy cost. Closing the returned reader closes only that
|
||||
// descriptor; the storage keeps owning the primary descriptor and the
|
||||
// file's lifetime. Readers opened before Close stay usable even after the
|
||||
// file is unlinked, as the descriptor keeps the inode alive.
|
||||
file, err := os.Open(d.filePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open body cache file for replay: %w", err)
|
||||
}
|
||||
return file, nil
|
||||
}
|
||||
|
||||
func (d *diskStorage) Size() int64 {
|
||||
return d.size
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user