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.
64 lines
1.7 KiB
Go
64 lines
1.7 KiB
Go
package provider
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"runtime"
|
|
"testing"
|
|
"weak"
|
|
)
|
|
|
|
// A request's body is held by the transport until its reply's body is
|
|
// closed, a stream's whole length (#3383): SetBody's bytes can be let go
|
|
// once they are read and LetGo says the request was answered, while the
|
|
// request is still held, and until then GetBody reads them again whole.
|
|
func TestSetBodyLetsItsBytesGo(t *testing.T) {
|
|
b := make([]byte, 4<<20)
|
|
for i := range b {
|
|
b[i] = byte('a' + i%26)
|
|
}
|
|
alive := weak.Make(&b[0])
|
|
req, err := http.NewRequest(http.MethodPost, "http://example.invalid/v1", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
SetBody(req, b)
|
|
b = nil
|
|
if req.ContentLength != 4<<20 {
|
|
t.Fatalf("ContentLength %d", req.ContentLength)
|
|
}
|
|
if got, _ := io.ReadAll(req.Body); len(got) != 4<<20 || got[27] != 'b' {
|
|
t.Fatalf("read %d bytes", len(got))
|
|
}
|
|
again, err := req.GetBody()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, _ := io.ReadAll(again); len(got) != 4<<20 {
|
|
t.Fatalf("GetBody read %d bytes, want the whole body again", len(got))
|
|
}
|
|
runtime.GC()
|
|
if alive.Value() == nil {
|
|
t.Fatal("the body was let go before LetGo, with GetBody still to read it")
|
|
}
|
|
LetGo(req)
|
|
runtime.GC()
|
|
if alive.Value() != nil {
|
|
t.Error("the request holds its body's bytes after it was answered")
|
|
}
|
|
if _, err := req.GetBody(); err == nil {
|
|
t.Error("GetBody read a body that was let go")
|
|
}
|
|
runtime.KeepAlive(req)
|
|
}
|
|
|
|
// An empty body is http.NoBody, as NewRequest makes it.
|
|
func TestSetBodyEmpty(t *testing.T) {
|
|
req, _ := http.NewRequest(http.MethodPost, "http://example.invalid/v1", nil)
|
|
SetBody(req, nil)
|
|
if req.Body != http.NoBody || req.ContentLength != 0 {
|
|
t.Errorf("body %T, length %d", req.Body, req.ContentLength)
|
|
}
|
|
LetGo(req)
|
|
}
|