如何避免第三方库自己开的gorouting发生的panic导致整个程序挂掉?

ddatsh · · 1346 次点击 · 开始浏览    置顶
这是一个创建于 的主题,其中的信息可能已经有所发展或是发生改变。

写业务代码的时候,想在里面启一个中间件(比如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...

有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1346 次点击  
加入收藏 微博
3 回复  |  直到 2017-08-22 17:22:25
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传