Files
origin/util/Coroutine.go
boyce 7fd97033ec 1.优化网络库
2.rpc协议在宕机时不会导致整个rpc无法响应
2020-02-05 17:51:51 +08:00

47 lines
936 B
Go

package util
import (
"fmt"
"reflect"
"runtime/debug"
)
func F(callback interface{},recoverNum int, args ...interface{}) {
defer func() {
if r := recover(); r != nil {
var coreInfo string
coreInfo = string(debug.Stack())
coreInfo += "\n" + fmt.Sprintf("Core information is %v\n", r)
if Log != nil {
Log(5, coreInfo)
} else {
fmt.Print(coreInfo)
}
if recoverNum==-1 ||recoverNum-1 >= 0 {
recoverNum -= 1
go F(callback,recoverNum, args...)
}
}
}()
v := reflect.ValueOf(callback)
if v.Kind() != reflect.Func {
panic("not a function")
}
vargs := make([]reflect.Value, len(args))
for i, arg := range args {
vargs[i] = reflect.ValueOf(arg)
}
v.Call(vargs)
}
func Go(callback interface{}, args ...interface{}) {
go F(callback,0, args...)
}
//-1表示一直恢复
func GoRecover(callback interface{},recoverNum int, args ...interface{}) {
go F(callback,recoverNum, args...)
}