mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-04 00:15:09 +03:00
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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user