Files
magpie/internal/provider/sentbody.go
T
yetone 106a245502 gateway: a request's body goes once it is answered, not when its stream ends (#3383)
The transport holds a request until its reply's body is closed, a
stream's whole length, and with it the bytes.Reader body and the GetBody
NewRequest made over it. So every streaming request kept the body it
sent upstream alive to its end: for a Claude Code request translated to
Chat, the marshalled body (marshalRaw/appendHTMLEscaped in the report's
heap, 503 MB live).

provider.SetBody gives a request a body whose reader drops its bytes
once read and whose GetBody reads them again until provider.LetGo.
forwardOnce lets go once its retries (RetryOneAuth, RetryZhipuCoding)
are done; Codex's zstd turn, Copilot Auto's model swap and Gemini's Code
Assist envelope set their bodies the same way, so LetGo reaches them.

Run (worktree perf-stack, on the four commits before it, over
origin/main 137187a40):
- a heap probe, 8 Claude Code requests of 4.7 MB held mid-stream to a
  Chat upstream: 152 MB held before, 88 MB after; the rest is the body
  as it came (another member) and as read (a continuation). Anthropic
  to Anthropic holds the body once, 41 MB.
- TestStreamingRequestsLetTheirSentBodyGo (bar 3x the body each) and
  TestSetBodyLetsItsBytesGo: green; red with LetGo commented out of
  forwardOnce ("8 streaming requests of 4 MB hold 166 MB, 4.2 times the
  body each") and with LetGo storing nothing ("the request holds its
  body's bytes after it was answered").
- go test ./internal/provider/ ./internal/redact/ ok; ./internal/gateway
  -run 'Mask|Redact|Codex|Copilot|Gemini|Google|Antigravity|Zhipu|Auth|
  Retry|Stream|Continu' ok; GOOS=linux and windows vet -tags nogui
  clean, gofmt clean. The full suite runs at the pushed head.
Not tried: a real vendor; the bytes on the wire are the same.
2026-10-24 21:00:05 +08:00

76 lines
2.1 KiB
Go

package provider
import (
"errors"
"io"
"net/http"
"sync/atomic"
)
// A request's body is held by the transport that sends it until its
// reply's body is closed: for a stream, the whole reply long. A body
// from bytes.NewReader, with the GetBody NewRequest makes for it, keeps
// every byte of the request alive that long; dozens of agents' long
// prompts streaming at once held hundreds of MB (#3383). SetBody's
// reader lets the bytes go once they are read, and its GetBody once
// LetGo says the request was answered.
// errLetGo is GetBody's error once the request was answered.
var errLetGo = errors.New("the request's body was let go once it was answered")
// heldBody is the bytes a request's GetBody reads again, until LetGo.
type heldBody struct{ b atomic.Pointer[[]byte] }
// sentBody is one read of a request's body, which drops what it has
// read once it reaches the end. Close leaves it as it is: the transport
// may close it while another of its goroutines reads it.
type sentBody struct {
h *heldBody
b []byte
}
func (s *sentBody) Read(p []byte) (int, error) {
if len(s.b) == 0 {
s.b = nil
return 0, io.EOF
}
n := copy(p, s.b)
if s.b = s.b[n:]; len(s.b) == 0 {
s.b = nil
}
return n, nil
}
func (s *sentBody) Close() error { return nil }
// SetBody makes b req's body, as NewRequest would from a bytes.Reader,
// read again by GetBody for a retry or a redirect until LetGo.
func SetBody(req *http.Request, b []byte) {
req.ContentLength = int64(len(b))
if len(b) == 0 {
req.Body, req.GetBody = http.NoBody, func() (io.ReadCloser, error) { return http.NoBody, nil }
return
}
h := &heldBody{}
h.b.Store(&b)
req.Body = &sentBody{h: h, b: b}
req.GetBody = func() (io.ReadCloser, error) {
p := h.b.Load()
if p == nil {
return nil, errLetGo
}
return &sentBody{h: h, b: *p}, nil
}
}
// LetGo lets go of the bytes SetBody gave req once it was answered and
// nothing will ask it again; a body SetBody didn't give is left as it is.
func LetGo(req *http.Request) {
if req == nil {
return
}
if s, ok := req.Body.(*sentBody); ok {
s.h.b.Store(nil)
}
}