feat(gateway): 优化gateway相关功能
Quality check / Web UI (push) Successful in 9m19s

This commit is contained in:
mei
2026-09-12 15:32:27 +08:00
parent 0d89b7fe8f
commit 9b656cebee
84 changed files with 2769 additions and 825 deletions
+12 -17
View File
@@ -3,38 +3,31 @@ package email
import (
"context"
"fmt"
"sort"
"strings"
"sync"
"github.com/ssdomei232/goodBaby/model"
"github.com/ssdomei232/goodBaby/internal/retry"
)
// SendMail 向规则配置里的所有地址发送邮件。
// SendMail 用给定 SMTP 账号向规则配置里的所有地址发送邮件。
//
// 每个地址单独重试,互不阻塞;全部失败/部分失败都会汇总成错误返回,
// 每个地址单独重试、互不阻塞;全部失败/部分失败都会汇总成错误返回,
// 由 runner 记录到执行日志中。
func SendMail(ctx context.Context, rule *model.Rule) error {
emailRule, err := GetEmailRuleFromRule(rule)
if err != nil {
return fmt.Errorf("获取邮件规则配置失败: %w", err)
}
accountConfig, err := GetEmailAccountFromRule(rule)
if err != nil {
return fmt.Errorf("获取邮件账户配置失败: %w", err)
}
func SendMail(ctx context.Context, account *EmailAccountConfig, config *EmailRule) error {
var (
wg sync.WaitGroup
mu sync.Mutex
fails []string
)
for _, destination := range emailRule.Destinations {
for _, destination := range config.Destinations {
wg.Add(1)
go func(address string) {
defer wg.Done()
if err := sendMailMsgWithRetry(ctx, accountConfig, emailRule, address); err != nil {
if err := retry.Do(ctx, func() error {
return sendMail(ctx, account, config, address)
}); err != nil {
mu.Lock()
fails = append(fails, fmt.Sprintf("%s: %v", address, err))
mu.Unlock()
@@ -44,8 +37,10 @@ func SendMail(ctx context.Context, rule *model.Rule) error {
wg.Wait()
if len(fails) > 0 {
// 并发发送的顺序不确定,排序后错误信息才是稳定的
sort.Strings(fails)
return fmt.Errorf("%d/%d 封邮件发送失败: %s",
len(fails), len(emailRule.Destinations), strings.Join(fails, "; "))
len(fails), len(config.Destinations), strings.Join(fails, "; "))
}
return nil
}