fix
This commit is contained in:
parent
22d00bd069
commit
60fa5df1de
|
@ -8,14 +8,17 @@ import (
|
|||
"sync"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
var EmailTaskResendTime = time.Second * 30
|
||||
var TimeLimit *check.TimeLimit[string]
|
||||
var EmailManager *EmailSender
|
||||
|
||||
func init() {
|
||||
|
||||
TimeLimit = check.NewTimelimit[string](time.Second * 25)
|
||||
TimeLimit = check.NewTimelimit[string](EmailTaskResendTime)
|
||||
|
||||
// Initialize the email manager
|
||||
EmailManager = &EmailSender{
|
||||
|
@ -28,8 +31,8 @@ func init() {
|
|||
),
|
||||
FromEmail: "support@fusenpack.com",
|
||||
emailSending: make(map[string]*EmailTask, 10),
|
||||
ResendTimeLimit: time.Minute * 1,
|
||||
semaphore: make(chan struct{}, 10), // Initialize semaphore with a capacity of 10
|
||||
ResendTimeLimit: EmailTaskResendTime,
|
||||
semaphore: make(chan struct{}, 100), // Initialize semaphore with a capacity of 10
|
||||
}
|
||||
|
||||
// Start processing email tasks
|
||||
|
@ -40,6 +43,7 @@ func init() {
|
|||
}
|
||||
|
||||
type EmailFormat struct {
|
||||
UniqueKey string // 用于处理唯一的任务,重发都会被利用到
|
||||
TargetEmail string // 发送的目标email
|
||||
CompanyName string // fs公司名
|
||||
ConfirmationLink string // fs确认连接
|
||||
|
@ -73,14 +77,19 @@ func (m *EmailSender) ProcessEmailTasks() {
|
|||
break
|
||||
}
|
||||
|
||||
if emailformat.UniqueKey == "" {
|
||||
logx.Error("email UniqueKey must be exists")
|
||||
continue
|
||||
}
|
||||
|
||||
m.lock.Lock()
|
||||
_, isSending := m.emailSending[emailformat.TargetEmail]
|
||||
_, isSending := m.emailSending[emailformat.UniqueKey]
|
||||
if isSending {
|
||||
m.lock.Unlock()
|
||||
continue
|
||||
}
|
||||
|
||||
m.emailSending[emailformat.TargetEmail] = &EmailTask{
|
||||
m.emailSending[emailformat.UniqueKey] = &EmailTask{
|
||||
Email: emailformat,
|
||||
SendTime: time.Now().UTC(),
|
||||
}
|
||||
|
@ -96,26 +105,26 @@ func (m *EmailSender) ProcessEmailTasks() {
|
|||
err := smtp.SendMail("smtp.gmail.com:587", m.Auth, m.FromEmail, []string{emailformat.TargetEmail}, content)
|
||||
if err != nil {
|
||||
log.Printf("Failed to send email to %s: %v\n", emailformat, err)
|
||||
m.Resend(emailformat.TargetEmail, content)
|
||||
m.Resend(emailformat.UniqueKey, content)
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
// Resend 重发邮件
|
||||
func (m *EmailSender) Resend(emailTarget string, content []byte) {
|
||||
func (m *EmailSender) Resend(uniqueKey string, content []byte) {
|
||||
time.Sleep(m.ResendTimeLimit)
|
||||
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
|
||||
// Check if the email task still exists and has not been sent successfully
|
||||
if task, ok := m.emailSending[emailTarget]; ok && task.SendTime.Add(m.ResendTimeLimit).After(time.Now().UTC()) {
|
||||
err := smtp.SendMail(emailTarget, m.Auth, m.FromEmail, []string{emailTarget}, content)
|
||||
if task, ok := m.emailSending[uniqueKey]; ok && task.SendTime.Add(m.ResendTimeLimit).After(time.Now().UTC()) {
|
||||
err := smtp.SendMail(task.Email.TargetEmail, m.Auth, m.FromEmail, []string{task.Email.TargetEmail}, content)
|
||||
if err != nil {
|
||||
log.Printf("Failed to resend email to %s: %v\n", emailTarget, err)
|
||||
log.Printf("Failed to resend email to %s: %v\n", task.Email.TargetEmail, err)
|
||||
} else {
|
||||
delete(m.emailSending, emailTarget)
|
||||
delete(m.emailSending, uniqueKey)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -74,6 +74,7 @@ func (l *UserEmailRegisterLogic) UserEmailRegister(req *types.RequestEmailRegist
|
|||
|
||||
// 进入发送邮箱的系统
|
||||
EmailManager.EmailTasks <- &EmailFormat{
|
||||
UniqueKey: "register-" + req.Email,
|
||||
TargetEmail: req.Email,
|
||||
CompanyName: "fusen",
|
||||
ConfirmationLink: clurl,
|
||||
|
|
|
@ -74,6 +74,7 @@ func (l *UserRegisterLogic) UserRegister(req *types.RequestUserRegister, userinf
|
|||
|
||||
// 进入发送邮箱的系统
|
||||
EmailManager.EmailTasks <- &EmailFormat{
|
||||
UniqueKey: "register-" + req.Email,
|
||||
TargetEmail: req.Email,
|
||||
CompanyName: "fusen",
|
||||
ConfirmationLink: clurl,
|
||||
|
|
Loading…
Reference in New Issue
Block a user