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

@@ -19,7 +19,6 @@ import (
gperr "github.com/yusing/goutils/errs"
httputils "github.com/yusing/goutils/http"
"github.com/yusing/goutils/http/reverseproxy"
"github.com/yusing/goutils/synk"
)
type (
@@ -431,10 +430,7 @@ var commands = map[string]struct {
to := []string{provider}
return OnResponseCommand(func(w http.ResponseWriter, r *http.Request) error {
buf := bufPool.Get()
defer bufPool.Put(buf)
respBuf := bytes.NewBuffer(buf)
respBuf := bytes.NewBuffer(make([]byte, 0, titleTmpl.Len()+bodyTmpl.Len()))
err := executeReqRespTemplateTo(titleTmpl, respBuf, w, r)
if err != nil {
@@ -446,10 +442,11 @@ var commands = map[string]struct {
return err
}
b := respBuf.Bytes()
notif.Notify(&notif.LogMessage{
Level: level,
Title: string(buf[:titleLen]),
Body: notif.MessageBodyBytes(buf[titleLen:]),
Title: string(b[:titleLen]),
Body: notif.MessageBodyBytes(b[titleLen:]),
To: to,
})
return nil
@@ -466,8 +463,6 @@ type reqResponseTemplateData struct {
}
}
var bufPool = synk.GetBytesPoolWithUniqueMemory()
type onLogArgs = Tuple3[zerolog.Level, io.WriteCloser, templateOrStr]
type onNotifyArgs = Tuple4[zerolog.Level, string, templateOrStr, templateOrStr]