60 lines
1.1 KiB
Go
60 lines
1.1 KiB
Go
package mediaService
|
|
|
|
import (
|
|
"91porn-server/common/log"
|
|
"fmt"
|
|
"runtime"
|
|
"strings"
|
|
"sync"
|
|
)
|
|
|
|
type worker struct {
|
|
num int // 最大
|
|
ch chan struct{}
|
|
wg sync.WaitGroup
|
|
}
|
|
|
|
var w *worker
|
|
|
|
func init() {
|
|
num := 30 //最大同时执行的执程,防止同时开启的协程过多,把其他服务弄挂
|
|
w = &worker{
|
|
num: num,
|
|
ch: make(chan struct{}, num),
|
|
}
|
|
}
|
|
|
|
func (w *worker) Wait() {
|
|
w.wg.Wait()
|
|
}
|
|
|
|
func (w *worker) Exec(f func()) {
|
|
// 超出则阻塞
|
|
w.ch <- struct{}{}
|
|
w.wg.Add(1)
|
|
go func() {
|
|
defer func() {
|
|
<-w.ch
|
|
w.wg.Done()
|
|
if r := recover(); r != nil {
|
|
dep := 0
|
|
t := make([]string, 0, 10)
|
|
for i := 1; i < 10; i++ {
|
|
_, file, line, ok := runtime.Caller(i)
|
|
if !ok {
|
|
break
|
|
}
|
|
if strings.Contains(file, "/runtime/") || strings.Contains(file, "/reflect/") {
|
|
continue
|
|
}
|
|
t = append(t, fmt.Sprintf("%s∟%s:%d", strings.Repeat(" ", dep), file, line))
|
|
dep++
|
|
}
|
|
exception := strings.Join(t, "\n")
|
|
log.Error("worker exec Panic: ", log.Any("Exception", exception))
|
|
}
|
|
}()
|
|
f()
|
|
}()
|
|
}
|