BillionMail gives you open-source MailServer, NewsLetter, Email Marketing — fully self-hosted, dev-friendly, and free from monthly fees. Join the discord: https://discord.gg/asfXzBUhZr
Refactor SpintaxParser to improve template parsing and rendering with caching
lotk1349413ff0df
diff --git a/core/internal/service/batch_mail/spintax.go b/core/internal/service/batch_mail/spintax.goindex 24b94cc9..5ac7d85d 100644--- a/core/internal/service/batch_mail/spintax.go+++ b/core/internal/service/batch_mail/spintax.go@@ -21,7 +21,7 @@ type TemplatePart struct {Options []string}-// PartType 片段类型+// PartTypetype PartType intconst (@@ -31,8 +31,9 @@ const (// SpintaxParser Spintax parser with cachingtype SpintaxParser struct {- random *rand.Rand- cache *TemplateCache+ random *rand.Rand+ cache *TemplateCache+ randPool *sync.Pool}type TemplateCache struct {@@ -46,6 +47,11 @@ func NewSpintaxParser() *SpintaxParser {return &SpintaxParser{random: rand.New(rand.NewSource(time.Now().UnixNano())),cache: NewTemplateCache(100),+ randPool: &sync.Pool{+ New: func() interface{} {+ return rand.New(rand.NewSource(time.Now().UnixNano()))+ },+ },}}@@ -56,33 +62,81 @@ func NewTemplateCache(maxSize int) *TemplateCache {}}-// ParseSpintax Parse Spintax syntax with caching optimization-func (p *SpintaxParser) ParseSpintax(content string) string {+func (p *SpintaxParser) ParseTemplate(content string) *SpintaxTemplate {+ if content == "" {+ return &SpintaxTemplate{Parts: []TemplatePart{}}+ }contentHash := p.calculateHash(content)template := p.cache.Get(contentHash)if template != nil {- // cache hit- return p.renderTemplate(template)+ return template}- if !strings.Contains(content, "{") {- return content+ if !strings.Contains(content, "{") || !strings.Contains(content, "|") || !p.hasValidSpintaxPattern(content) {++ template = &SpintaxTemplate{+ Parts: []TemplatePart{{+ Type: StaticPart,+ Content: content,+ }},+ Hash: contentHash,+ }+ } else {++ template = p.parseToTemplate(content, contentHash)}- if !strings.Contains(content, "|") {- return content+ p.cache.Set(contentHash, template)+ return template+}++func (p *SpintaxParser) RenderTemplate(template *SpintaxTemplate) string {+ if template == nil || len(template.Parts) == 0 {+ return ""}- if !p.hasValidSpintaxPattern(content) {- return content+ r := p.randPool.Get().(*rand.Rand)+ defer p.randPool.Put(r)++ var result strings.Builder++ estimatedSize := 0+ for _, part := range template.Parts {+ if part.Type == StaticPart {+ estimatedSize += len(part.Content)+ } else {+ avgLen := 0+ for _, option := range part.Options {+ avgLen += len(option)+ }+ if len(part.Options) > 0 {+ estimatedSize += avgLen / len(part.Options)+ }+ }}+ result.Grow(estimatedSize + 64)- template = p.parseToTemplate(content, contentHash)- p.cache.Set(contentHash, template)+ for _, part := range template.Parts {+ switch part.Type {+ case StaticPart:+ result.WriteString(part.Content)+ case SpintaxPart:+ if len(part.Options) > 0 {+ selectedIndex := r.Intn(len(part.Options))+ result.WriteString(part.Options[selectedIndex])+ }+ }+ }++ return result.String()+}- return p.renderTemplate(template)+// ParseSpintax Parse Spintax syntax with caching optimization (保持向后兼容)+func (p *SpintaxParser) ParseSpintax(content string) string {+ template := p.ParseTemplate(content)+ return p.RenderTemplate(template)}func (p *SpintaxParser) calculateHash(content string) string {@@ -205,41 +259,6 @@ func (p *SpintaxParser) parseSpintaxAtPosition(content string, pos int) *Spintax}}-func (p *SpintaxParser) renderTemplate(template *SpintaxTemplate) string {- var result strings.Builder-- estimatedSize := 0- for _, part := range template.Parts {- if part.Type == StaticPart {- estimatedSize += len(part.Content)- } else {-- avgLen := 0- for _, option := range part.Options {- avgLen += len(option)- }- if len(part.Options) > 0 {- estimatedSize += avgLen / len(part.Options)- }- }- }- result.Grow(estimatedSize + 64)-- for _, part := range template.Parts {- switch part.Type {- case StaticPart:- result.WriteString(part.Content)- case SpintaxPart:- if len(part.Options) > 0 {- selectedIndex := p.random.Intn(len(part.Options))- result.WriteString(part.Options[selectedIndex])- }- }- }-- return result.String()-}-func (c *TemplateCache) Get(hash string) *SpintaxTemplate {c.mutex.RLock()defer c.mutex.RUnlock()diff --git a/core/internal/service/batch_mail/task_executor.go b/core/internal/service/batch_mail/task_executor.goindex e47778d7..ce433475 100644--- a/core/internal/service/batch_mail/task_executor.go+++ b/core/internal/service/batch_mail/task_executor.go@@ -148,6 +148,8 @@ type TaskExecutor struct {taskConfig *entity.EmailTaskconfigLoaded time.Time+ spintaxTemplate *SpintaxTemplate+// rate controllerrateController *SimpleRateController@@ -1045,6 +1047,13 @@ func (e *TaskExecutor) processEmailContent(ctx context.Context, content string,content = strings.ReplaceAll(content, "__UNSUBSCRIBE_URL__", "{{ UnsubscribeURL . }}")}++ // Preparse the spintax template+ if e.spintaxTemplate == nil {+ spintaxParser := GetSpintaxParser()+ e.spintaxTemplate = spintaxParser.ParseTemplate(content)+ }+return content}diff --git a/core/internal/service/batch_mail/template_render.go b/core/internal/service/batch_mail/template_render.goindex e0a4f671..7585cb4f 100644--- a/core/internal/service/batch_mail/template_render.go+++ b/core/internal/service/batch_mail/template_render.go@@ -149,9 +149,42 @@ func (e *TemplateEngine) RenderEmailTemplateWithAPI(ctx context.Context, contentreturn "", err}- // Analyzing Spintax Grammar+ // Handle spintax renderingspintaxParser := GetSpintaxParser()- result = spintaxParser.ParseSpintax(result)++ if task != nil {++ executor := GetTaskExecutor(task.Id)+ if executor != nil && executor.spintaxTemplate != nil {++ hasSpintax := spintaxParser.HasSpintax(result)++ if hasSpintax {++ result = spintaxParser.ParseSpintax(result)+ } else {++ originalContentHash := spintaxParser.calculateHash(content)+ if executor.spintaxTemplate.Hash == originalContentHash {++ shouldUsePreTemplate := len(executor.spintaxTemplate.Parts) > 1 ||+ (len(executor.spintaxTemplate.Parts) == 1 && executor.spintaxTemplate.Parts[0].Type == SpintaxPart)++ if shouldUsePreTemplate {++ result = spintaxParser.RenderTemplate(executor.spintaxTemplate)+ }+ } else {++ result = spintaxParser.ParseSpintax(result)+ }+ }+ } else {+ result = spintaxParser.ParseSpintax(result)+ }+ } else {+ result = spintaxParser.ParseSpintax(result)+ }return result, nil}