chore(wirecodec): pin the node encoder's retained-memory budget

Two of the three options that bound the encoder had no test. With the
worker cap removed, or zstd's low-memory mode dropped, every test stayed
green while an encoder built for 64 CPUs retained 154 MiB or 13.7 MiB
instead of 9.1 MiB. Only the 1 MiB window was pinned, through the frame
header.

Build the encoder in newEncoder(cpus) so a test can size it for a
many-core host whatever machine runs it, warm every pooled workspace and
fail above 12 MiB.
This commit is contained in:
Sanaei
2026-11-30 23:17:21 +03:30
parent bd8648de73
commit 267379456a
2 changed files with 36 additions and 7 deletions
+13 -6
View File
@@ -49,18 +49,25 @@ const (
// EncodeAll/DecodeAll on these shared instances are safe for concurrent use.
var (
encoderWorkers = min(maxEncoderWorkers, runtime.GOMAXPROCS(0))
encoderSlots = make(chan struct{}, encoderWorkers)
zstdEncoder, _ = zstd.NewWriter(nil, zstd.WithWindowSize(encoderWindow),
zstd.WithEncoderConcurrency(encoderWorkers), zstd.WithLowerEncoderMem(true))
decoderWorkers = runtime.GOMAXPROCS(0)
zstdDecoder, _ = zstd.NewReader(nil, zstd.WithDecoderMaxMemory(maxDecodeBytes),
zstdEncoder, encoderWorkers = newEncoder(runtime.GOMAXPROCS(0))
encoderSlots = make(chan struct{}, encoderWorkers)
decoderWorkers = runtime.GOMAXPROCS(0)
zstdDecoder, _ = zstd.NewReader(nil, zstd.WithDecoderMaxMemory(maxDecodeBytes),
zstd.WithDecodeAllCapLimit(true), zstd.WithDecoderConcurrency(decoderWorkers))
// decodeSlots keeps callers waiting for a decoder worker from each holding an output buffer.
decodeSlots = make(chan struct{}, decoderWorkers)
errDecodedTooLarge = errors.New("wirecodec: decompressed body exceeds limit")
)
// newEncoder builds the envelope encoder for a host with cpus CPUs and reports
// how many workspaces it pools; they are never freed, hence the cap (#7274).
func newEncoder(cpus int) (*zstd.Encoder, int) {
workers := min(maxEncoderWorkers, cpus)
enc, _ := zstd.NewWriter(nil, zstd.WithWindowSize(encoderWindow),
zstd.WithEncoderConcurrency(workers), zstd.WithLowerEncoderMem(true))
return enc, workers
}
// Compress zstd-compresses b.
func Compress(b []byte) []byte {
out, _ := CompressContext(context.Background(), b)
+23 -1
View File
@@ -72,6 +72,28 @@ func TestDecompressOfManyFramesAllocatesTheOutputOnce(t *testing.T) {
}
}
// The encoder keeps every workspace it has used for the life of the process, so
// their total must not grow with the master's CPU count (#7274).
func TestEncoderWorkspacesStayWithinBudgetOnAManyCoreHost(t *testing.T) {
const cpus, budget = 64, 12 << 20
payload := bytes.Repeat([]byte("synthetic client traffic row "), 20_000)
enc, _ := newEncoder(cpus)
defer enc.Close()
var before, after runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&before)
// EncodeAll hands the workspaces out in rotation, so sequential calls warm each one.
for range 2 * cpus {
enc.EncodeAll(payload, nil)
}
runtime.GC()
runtime.ReadMemStats(&after)
if retained := int64(after.HeapAlloc) - int64(before.HeapAlloc); retained > budget {
t.Fatalf("encoder for %d CPUs retains %.1f MiB after use, want at most %d MiB",
cpus, float64(retained)/(1<<20), budget>>20)
}
}
func TestCompressBoundsFrameWindow(t *testing.T) {
orig := bytes.Repeat([]byte("synthetic node configuration"), 150_000)
packed := Compress(orig)
@@ -97,7 +119,7 @@ func TestCompressConcurrentFramesAreCompatible(t *testing.T) {
var callers sync.WaitGroup
for i := range 16 {
callers.Go(func() {
raw := bytes.Repeat([]byte(fmt.Sprintf("client-%d", i)), 40_000)
raw := bytes.Repeat(fmt.Appendf(nil, "client-%d", i), 40_000)
packed, err := CompressContext(t.Context(), raw)
if err != nil {
t.Errorf("encode %d: %v", i, err)