-
Notifications
You must be signed in to change notification settings - Fork 0
/
gogo.go
64 lines (57 loc) · 1005 Bytes
/
gogo.go
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
56
57
58
59
60
61
62
63
64
package gogo
import (
"fmt"
"runtime/debug"
"sync"
)
type Fn func() error
type Fns []Fn
func Run(fnQueue ...Fns) error {
sumFn := 0
for _, q := range fnQueue {
sumFn += len(q)
}
errChan := make(chan error, sumFn)
isExit := false
var mux sync.RWMutex
defer func() {
mux.Lock()
defer mux.Unlock()
isExit = true
close(errChan)
}()
for i := 0; i < len(fnQueue); i++ {
fns := fnQueue[i]
for j := 0; j < len(fns); j++ {
fn := fns[j]
go func() {
defer func() {
e := recover()
if e != nil {
mux.RLock()
defer mux.RUnlock()
if !isExit {
errChan <- fmt.Errorf("catch function panic(%v) , %s", e, debug.Stack())
}
}
}()
err := fn()
mux.RLock()
defer mux.RUnlock()
if !isExit {
errChan <- err
}
}()
}
// for again for reading return value of functions
for j := 0; j < len(fns); j++ {
select {
case err := <-errChan:
if err != nil {
return err
}
}
}
}
return nil
}