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