From 12a79d95913562bf414dc452e6f92acc73b3cd7c Mon Sep 17 00:00:00 2001 From: jinnrry Date: Wed, 30 Aug 2023 17:48:47 +0800 Subject: [PATCH] =?UTF-8?q?v2.1.2=20=E4=BF=AE=E5=A4=8D=E5=8D=8F=E7=A8=8B?= =?UTF-8?q?=E8=B0=83=E5=BA=A6=E9=A1=BA=E5=BA=8F=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/config/config.go | 2 +- server/controllers/email/send.go | 19 +++++++++++-------- server/smtp_server/read_content.go | 16 ++++++++++------ server/smtp_server/send.go | 4 ++-- server/utils/async/async.go | 14 +++++++------- 5 files changed, 31 insertions(+), 24 deletions(-) diff --git a/server/config/config.go b/server/config/config.go index 273f5c3..df0f4cb 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -35,7 +35,7 @@ type Config struct { //go:embed tables/* var tableConfig embed.FS -const Version = "2.1.1" +const Version = "2.1.2" const DBTypeMySQL = "mysql" const DBTypeSQLite = "sqlite" diff --git a/server/controllers/email/send.go b/server/controllers/email/send.go index c78c9a9..085395f 100644 --- a/server/controllers/email/send.go +++ b/server/controllers/email/send.go @@ -128,14 +128,16 @@ func Send(ctx *dto.Context, w http.ResponseWriter, req *http.Request) { } + as := async.New(ctx) for _, hook := range hooks.HookList { if hook == nil { continue } - async.New(ctx).Process(func() { - hook.SendBefore(ctx, e) - }) + as.WaitProcess(func(hk any) { + hk.(hooks.EmailHook).SendBefore(ctx, e) + }, hook) } + as.Wait() // 邮件落库 sql := "INSERT INTO email (type,subject, reply_to, from_name, from_address, `to`, bcc, cc, text, html, sender, attachments,spf_check, dkim_check, create_time,send_user_id,error) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" @@ -166,17 +168,18 @@ func Send(ctx *dto.Context, w http.ResponseWriter, req *http.Request) { return } - async.New(ctx).Process(func() { + async.New(ctx).Process(func(p any) { errMsg := "" err, sendErr := smtp_server.Send(ctx, e) + as2 := async.New(ctx) for _, hook := range hooks.HookList { if hook == nil { continue } - async.New(ctx).Process(func() { - hook.SendAfter(ctx, e, sendErr) - }) + as2.WaitProcess(func(hk any) { + hk.(hooks.EmailHook).SendAfter(ctx, e, sendErr) + }, hook) } if err != nil { @@ -192,7 +195,7 @@ func Send(ctx *dto.Context, w http.ResponseWriter, req *http.Request) { } } - }) + }, nil) response.NewSuccessResponse(i18n.GetText(ctx.Lang, "succ")).FPrint(w) } diff --git a/server/smtp_server/read_content.go b/server/smtp_server/read_content.go index 1f042a5..722a3df 100644 --- a/server/smtp_server/read_content.go +++ b/server/smtp_server/read_content.go @@ -23,14 +23,16 @@ func (s *Session) Data(r io.Reader) error { return err } + as1 := async.New(nil) for _, hook := range hooks.HookList { if hook == nil { continue } - async.New(nil).Process(func() { - hook.ReceiveParseBefore(emailData) - }) + as1.WaitProcess(func(hk any) { + hk.(hooks.EmailHook).ReceiveParseBefore(emailData) + }, hook) } + as1.Wait() log.Infof("邮件原始内容: %s", emailData) @@ -55,14 +57,16 @@ func (s *Session) Data(r io.Reader) error { spfV = 1 } + as2 := async.New(nil) for _, hook := range hooks.HookList { if hook == nil { continue } - async.New(nil).Process(func() { - hook.ReceiveParseAfter(email) - }) + as2.WaitProcess(func(hk any) { + hk.(hooks.EmailHook).ReceiveParseAfter(email) + }, hook) } + as2.Wait() sql := "INSERT INTO email (send_date, subject, reply_to, from_name, from_address, `to`, bcc, cc, text, html, sender, attachments,spf_check, dkim_check, create_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" _, err = db.Instance.Exec(sql, diff --git a/server/smtp_server/send.go b/server/smtp_server/send.go index 7402f54..792c9da 100644 --- a/server/smtp_server/send.go +++ b/server/smtp_server/send.go @@ -61,7 +61,7 @@ func Send(ctx *dto.Context, e *parsemail.Email) (error, map[string]error) { for domain, tos := range toByDomain { domain := domain tos := tos - as.WaitProcess(func() { + as.WaitProcess(func(p any) { err := smtp.SendMail("", domain.mxHost+":25", nil, e.From.EmailAddress, buildAddress(tos), b) @@ -85,7 +85,7 @@ func Send(ctx *dto.Context, e *parsemail.Email) (error, map[string]error) { } } errMap[domain.domain] = err - }) + }, nil) } as.Wait() diff --git a/server/utils/async/async.go b/server/utils/async/async.go index e6dbf1e..2fcc284 100644 --- a/server/utils/async/async.go +++ b/server/utils/async/async.go @@ -9,7 +9,7 @@ import ( "sync" ) -type Callback func() +type Callback func(params any) type Async struct { wg *sync.WaitGroup @@ -27,25 +27,25 @@ func (as *Async) LastError() error { return as.lastError } -func (as *Async) WaitProcess(callback Callback) { +func (as *Async) WaitProcess(callback Callback, params any) { if as.wg == nil { as.wg = &sync.WaitGroup{} } as.wg.Add(1) - as.Process(func() { + as.Process(func(params any) { defer as.wg.Done() - callback() - }) + callback(params) + }, params) } -func (as *Async) Process(callback Callback) { +func (as *Async) Process(callback Callback, params any) { go func() { defer func() { if err := recover(); err != nil { as.lastError = as.HandleErrRecover(err) } }() - callback() + callback(params) }() }