Files
servicebase/pkg/tools/safe.go
2025-11-19 10:23:05 +08:00

28 lines
380 B
Go

package tools
import (
"runtime/debug"
"gitea.ddegame.cn/open/servicebase/pkg/log"
)
func Recover(cleanups ...func()) {
for _, cleanup := range cleanups {
cleanup()
}
if p := recover(); p != nil {
log.ErrorF("occur panic: [%+v], stack info [%s]", p, debug.Stack())
}
}
func RunSafe(fn func()) {
defer Recover()
fn()
}
func GoSafe(fn func()) {
go RunSafe(fn)
}