Codex sends every turn's body zstd. decodedBody made a new zstd decoder
for each one, with the package's default concurrency: it started a
stream goroutine and allocated its window and block buffers on every
request. In the procmem profile (a sandbox `magpie web` on main, three
Codex-style sessions sending zstd /v1/responses every 2-4s plus Chat and
Messages traffic, ~1,000 requests in 10 minutes) that was 1.74 GB of the
9.8 GB allocated, all under zstd.(*Decoder).startStreamDecoder.
Now a sync.Pool keeps decoders (concurrency 1, the same 512 MB memory
bound), as sessions' packed reads do since 21c2ab6fa. A decoder decodes
in step with the body's reader, which reads it through to its end
anyway, and goes back to the pool at Close; a read after Close gets
net.ErrClosed. A body that isn't zstd fails at Reset with the same 400
as before.
Measured with TestZstdBodyDecoderKept (a 304 KB body, 20 reads): 1719 KB
allocated a read before (fails), 678 KB after, which is io.ReadAll
growing to the body's size. The test is skipped under -race, where
sync.Pool drops a quarter of what is put in it (raceOn, as in sessions).
Run: go test -tags nogui -run 'Zstd|Encod|Codex.*Backend|BodyLimit|
Title|Bound|Gzip' ./internal/gateway (ok), the same with -race (ok),
gofmt -l clean, GOOS=linux and GOOS=windows go vet -tags nogui
./internal/gateway pass. The full suite is run before the push of this
batch.
51 lines
1.6 KiB
Go
51 lines
1.6 KiB
Go
package gateway
|
|
|
|
import (
|
|
"bytes"
|
|
"net/http/httptest"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/klauspost/compress/zstd"
|
|
)
|
|
|
|
// A zstd body is read through a decoder kept between requests: one made
|
|
// for each body allocated its window and block buffers every time, 1.7 GB
|
|
// of 9.8 GB in 10 minutes of three Codex sessions (procmem heap profile).
|
|
// What a read allocates past the decoded body itself stays small.
|
|
func TestZstdBodyDecoderKept(t *testing.T) {
|
|
plain := []byte(`{"model":"fake/m1","input":"` + strings.Repeat("please look at file 12 and fix the bug ", 8000) + `"}`)
|
|
enc, _ := zstd.NewWriter(nil)
|
|
packed := enc.EncodeAll(plain, nil)
|
|
enc.Close()
|
|
read := func() {
|
|
t.Helper()
|
|
r := httptest.NewRequest("POST", "/v1/responses", bytes.NewReader(packed))
|
|
r.Header.Set("Content-Encoding", "zstd")
|
|
body, status, err := readBoundedRequestBody(httptest.NewRecorder(), r, requestLimits{})
|
|
if err != nil || status != 0 || !bytes.Equal(body, plain) {
|
|
t.Fatalf("read %d bytes, status %d, %v", len(body), status, err)
|
|
}
|
|
}
|
|
read() // the pool's first decoder
|
|
if raceOn {
|
|
t.Skip("-race: sync.Pool lets a quarter of what is put in it go")
|
|
}
|
|
const n = 20
|
|
var before, after runtime.MemStats
|
|
runtime.GC()
|
|
runtime.ReadMemStats(&before)
|
|
for range n {
|
|
read()
|
|
}
|
|
runtime.ReadMemStats(&after)
|
|
// io.ReadAll's growth to the body's size is ~2-3x it; a decoder made
|
|
// for each body added its window and buffers on top
|
|
per := (after.TotalAlloc - before.TotalAlloc) / n
|
|
if per > uint64(4*len(plain)) {
|
|
t.Fatalf("a %d KB body allocated %d KB to read", len(plain)>>10, per>>10)
|
|
}
|
|
t.Logf("a %d KB body: %d KB a read", len(plain)>>10, per>>10)
|
|
}
|