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)