chore: add SafeGoroutine

This commit is contained in:
CaIon committed 2024-01-12 13:49:53 +08:00
1 parent f8b7286341
commit aa8b722a13
2 files changed
+19 -1

No files matched your search

+16
View File
@@ -1,5 +1,21 @@
package common
import (
"fmt"
"runtime/debug"
)
func SafeGoroutine(f func()) {
go func() {
defer func() {
if r := recover(); r != nil {
SysError(fmt.Sprintf("child goroutine panic occured: error: %v, stack: %s", r, string(debug.Stack())))
}
}()
f()
}()
}
func SafeSend(ch chan bool, value bool) (closed bool) {
defer func() {
// Recover from panic if one occured. A panic would mean the channel was closed.