写业务代码的时候,想在里面启一个中间件(比如go实现的mqtt broker hrotti),中间件里面会自己开出gorouting,没处理好,会panic
搜了一下文章,貌似就是避免不了这种情况了?
This almost fixes the problem if you can't change otherPackage:
```
go func() {
defer func() {
if err := recover(); err != nil {
someMonitoringChannel <- err
}
}()
otherPackage.SomeFunc()
}()
```
But it doesn't protect you from otherPackage.SomeFunc() creating unprotected goroutines.
Maybe there should be a way to set a function to get called by default for unhanded panics, something like:
```
func panicking(err os.Error) {
fmt.Println("unhandled panic:", err)
panic(err) //go ahead and crash the program (omit this to fail silently)
}
func init() {
runtime.DefaultPanicRecover(panicking)
}
```
The problem with this is that you don't know what goroutine the error happened in...
有疑问加站长微信联系(非本文作者)