mirror of
https://github.com/Jinnrry/PMail.git
synced 2026-04-23 00:17:16 +08:00
054336fe9e
1、新增垃圾邮件过滤插件 2、使用使用github.com/dlclark/regexp2替换go原生的正则包 3、修复空数据导致的邮件插入失败
72 lines
1.2 KiB
Go
72 lines
1.2 KiB
Go
package async
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/Jinnrry/pmail/utils/context"
|
|
log "github.com/sirupsen/logrus"
|
|
"github.com/spf13/cast"
|
|
"runtime/debug"
|
|
"sync"
|
|
)
|
|
|
|
type Callback func(params any)
|
|
|
|
type Async struct {
|
|
wg *sync.WaitGroup
|
|
lastError error
|
|
ctx *context.Context
|
|
}
|
|
|
|
func New(ctx *context.Context) *Async {
|
|
return &Async{
|
|
ctx: ctx,
|
|
}
|
|
}
|
|
|
|
func (as *Async) LastError() error {
|
|
return as.lastError
|
|
}
|
|
|
|
func (as *Async) WaitProcess(callback Callback, params any) {
|
|
if as.wg == nil {
|
|
as.wg = &sync.WaitGroup{}
|
|
}
|
|
as.wg.Add(1)
|
|
as.Process(func(params any) {
|
|
defer as.wg.Done()
|
|
callback(params)
|
|
}, params)
|
|
}
|
|
|
|
func (as *Async) Process(callback Callback, params any) {
|
|
go func() {
|
|
defer func() {
|
|
if err := recover(); err != nil {
|
|
as.lastError = as.HandleErrRecover(err)
|
|
}
|
|
}()
|
|
callback(params)
|
|
}()
|
|
}
|
|
|
|
func (as *Async) Wait() {
|
|
if as.wg == nil {
|
|
return
|
|
}
|
|
as.wg.Wait()
|
|
}
|
|
|
|
// HandleErrRecover panic恢复处理
|
|
func (as *Async) HandleErrRecover(err interface{}) (returnErr error) {
|
|
switch err.(type) {
|
|
case error:
|
|
returnErr = err.(error)
|
|
default:
|
|
returnErr = errors.New(cast.ToString(err))
|
|
}
|
|
|
|
log.WithContext(as.ctx).Errorf("goroutine panic:%s \n %s", err, string(debug.Stack()))
|
|
|
|
return
|
|
}
|