0904
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package shared
|
||||
|
||||
import (
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// SafeGo 在独立 goroutine 中执行 fn 并捕获 panic,防止任一 goroutine
|
||||
// panic 未恢复导致整个进程崩溃(Go 默认行为:未恢复的 panic 终止进程)。
|
||||
//
|
||||
// 用法:shared.SafeGo(func() { ... })
|
||||
func SafeGo(fn func()) {
|
||||
go func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
logrus.WithField("panic", r).Error("goroutine panic recovered")
|
||||
}
|
||||
}()
|
||||
fn()
|
||||
}()
|
||||
}
|
||||
Reference in New Issue
Block a user