You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/core/threading/routines.go

34 lines
648 B
Go

4 years ago
package threading
import (
"bytes"
"runtime"
"strconv"
"github.com/tal-tech/go-zero/core/rescue"
4 years ago
)
// GoSafe runs the given fn using another goroutine, recovers if fn panics.
4 years ago
func GoSafe(fn func()) {
go RunSafe(fn)
}
// RoutineId is only for debug, never use it in production.
4 years ago
func RoutineId() uint64 {
b := make([]byte, 64)
b = b[:runtime.Stack(b, false)]
b = bytes.TrimPrefix(b, []byte("goroutine "))
b = b[:bytes.IndexByte(b, ' ')]
// if error, just return 0
n, _ := strconv.ParseUint(string(b), 10, 64)
return n
}
// RunSafe runs the given fn, recovers if fn panics.
4 years ago
func RunSafe(fn func()) {
defer rescue.Recover()
fn()
}