go-zero threading包
里面主要两个方法:
- GoSafe(用协程运行方法)
- RunSafe(带panic捕捉)
先上两个方法
- 开启go协程运行这个函数
// GoSafe runs the given fn using another goroutine, recovers if fn panics.
func GoSafe(fn func()) {
go RunSafe(fn)
}
- 增加捕捉panic方法
// RunSafe runs the given fn, recovers if fn panics.
func RunSafe(fn func()) {
defer rescue.Recover()
fn()
}
// Recover is used with defer to do cleanup on panics.
// Use it like:
// defer Recover(func() {})
func Recover(cleanups ...func()) {
for _, cleanup := range cleanups {// 运行一些自己定义的方法
cleanup()
}
if p := recover(); p != nil {
logx.ErrorStack(p)// 把错误信息写入日志里
}
}