package app import ( "context" "fmt" "github.com/panjf2000/ants/v2" ) type AntsLog struct { } func (AntsLog) Printf(format string, args ...interface{}) { Logger.Error(context.Background(), fmt.Sprintf(format, args...)) } // 队列消费者池 var QueueConsumerPool, _ = ants.NewMultiPool( 50, // 50个池 60, // 每个池60个任务 ants.RoundRobin, ants.WithPreAlloc(true), ants.WithPanicHandler(func(i interface{}) { Logger.Error(context.Background(), fmt.Sprintf("ants task panic: %v", i)) }), ants.WithLogger(AntsLog{}), ) func GoSafe(fn func(), msg ...any) { go func() { defer func() { if re := recover(); re != nil { //协程异常捕捉及重试 if len(msg) > 0 { Logger.Error(context.Background(), fmt.Sprint(fmt.Sprint(msg...), fmt.Sprintf("协程捕捉到异常:%v", re))) } else { Logger.Error(context.Background(), fmt.Sprintf("协程捕捉到异常:%v", re)) } } }() fn() }() } func GoSafeRetry(fn func(), retryFn func(), msg ...any) { go func() { defer func() { if re := recover(); re != nil { //协程异常捕捉及重试 if len(msg) > 0 { Logger.Error(context.Background(), fmt.Sprint(fmt.Sprint(msg...), fmt.Sprintf("协程捕捉到异常:%v", re))) } else { Logger.Error(context.Background(), fmt.Sprintf("协程捕捉到异常:%v", re)) } retryFn() } }() fn() }() }