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

@@ -304,6 +304,15 @@ func isTemplate(tmplStr string) bool {
return strings.Contains(tmplStr, "{{")
}
type templateWithLen struct {
*template.Template
len int
}
func (t *templateWithLen) Len() int {
return t.len
}
func validateTemplate(tmplStr string, newline bool) (templateOrStr, gperr.Error) {
if newline && !strings.HasSuffix(tmplStr, "\n") {
tmplStr += "\n"
@@ -317,7 +326,7 @@ func validateTemplate(tmplStr string, newline bool) (templateOrStr, gperr.Error)
if err != nil {
return nil, ErrInvalidArguments.With(err)
}
return tmpl, nil
return &templateWithLen{tmpl, len(tmplStr)}, nil
}
func validateLevel(level string) (zerolog.Level, gperr.Error) {