1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-04-23 11:07:43 +02:00
go-micro/cmd/dashboard/util/goroutine.go

23 lines
343 B
Go
Raw Normal View History

2021-11-24 11:27:23 +08:00
package util
import (
"runtime/debug"
"go-micro.dev/v4/logger"
)
// GoSafe will run func in goroutine safely, avoid crash from unexpected panic
func GoSafe(fn func()) {
if fn == nil {
return
}
go func() {
defer func() {
if e := recover(); e != nil {
logger.Errorf("[panic]%v\n%s", e, debug.Stack())
}
}()
fn()
}()
}