refactor: refine byte pools usage and fix memory leak in rules

This commit is contained in:
yusing
2025-10-15 23:53:26 +08:00
parent 2b4c39a79e
commit 44536139c1
7 changed files with 73 additions and 42 deletions

View File

@@ -1,7 +1,6 @@
package rules
import (
"bytes"
"io"
"net/http"
"net/url"
@@ -243,15 +242,14 @@ var modFields = map[string]struct {
r.Body = nil
}
buf := pool.Get()
b := bytes.NewBuffer(buf)
bufPool := GetInitResponseModifier(w).BufPool()
b := bufPool.GetBuffer()
err := executeRequestTemplateTo(tmpl, b, r)
if err != nil {
return err
}
r.Body = ioutils.NewHookReadCloser(io.NopCloser(b), func() {
pool.Put(buf)
bufPool.PutBuffer(b)
})
return nil
}),