-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgo.go
More file actions
55 lines (45 loc) · 1.03 KB
/
go.go
File metadata and controls
55 lines (45 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package crashproof
import (
"runtime/debug"
"sync"
callstack "github.com/codemodify/systemkit-callstack"
)
// ConcurrentCodeCrashCatcherDelegate -
type ConcurrentCodeCrashCatcherDelegate func(err interface{}, callStack []callstack.Frame)
// ConcurrentCodeCrashCatcher -
var ConcurrentCodeCrashCatcher ConcurrentCodeCrashCatcherDelegate
// Go -
func Go(concurrentCode func()) {
GoWithArgs(func(args ...interface{}) {
concurrentCode()
}, nil)
}
// GoWithArgs -
func GoWithArgs(concurrentCode func(args ...interface{}), args ...interface{}) {
go func() {
defer func() {
debug.SetPanicOnFault(true)
if err := recover(); err != nil {
callStack := callstack.GetFrames()
if ConcurrentCodeCrashCatcher != nil {
ConcurrentCodeCrashCatcher(err, callStack)
}
}
}()
if concurrentCode != nil {
concurrentCode(args...)
}
}()
}
// RunAppAndCatchCrashes -
func RunAppAndCatchCrashes(appCode func()) {
wg := sync.WaitGroup{}
wg.Add(1)
Go(func() {
if appCode != nil {
appCode()
}
wg.Done()
})
wg.Wait()
}