This commit is contained in:
2025-03-12 09:00:45 +08:00
parent aee6dd9883
commit 0352da5616
6 changed files with 57 additions and 24 deletions

View File

@ -15,7 +15,7 @@ func SafeGo(fn func()) {
defer func() {
if r := recover(); r != nil {
// 记录 Goroutine ID、panic 信息和堆栈
logger.Error(fmt.Sprintf("Recovered from panic in Goroutine %s: %v\nStack Trace:\n%s", getGoroutineID(), r, string(debug.Stack())))
logger.Error(fmt.Sprintf("Recovered from panic in Goroutine %s: %v\nStack Trace:\n%s", GetGoroutineID(), r, string(debug.Stack())))
}
}()
fn()
@ -23,7 +23,7 @@ func SafeGo(fn func()) {
}
// 获取 Goroutine ID
func getGoroutineID() string {
func GetGoroutineID() string {
buf := make([]byte, 64)
n := runtime.Stack(buf, false)
stack := string(buf[:n])
@ -36,7 +36,7 @@ func SafeGoParam[T any](fn func(T), param T) {
go func() {
defer func() {
if r := recover(); r != nil {
logger.Error(fmt.Sprintf(" SafeGoParam Recovered from panic in Goroutine %s: %v\nStack Trace:\n%s", getGoroutineID(), r, string(debug.Stack())))
logger.Error(fmt.Sprintf(" SafeGoParam Recovered from panic in Goroutine %s: %v\nStack Trace:\n%s", GetGoroutineID(), r, string(debug.Stack())))
}
}()
fn(param) // 执行传入的函数