-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper_chan.go
65 lines (54 loc) · 1.31 KB
/
wrapper_chan.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
65
package wrapper
var _ Wrapper = (*wrapChanImpl)(nil)
// wrapChanImpl wrapper impl
type wrapChanImpl struct {
bufCap int
bufCh chan struct{}
recoveryFunc func()
}
// NewChanWrapper create wrapChanImpl entity
// If the wrapper using the chan method needs to specify the number of
// goroutines to be executed,the WithBufCap method needs to be called.
// Otherwise, after the Wait method is executed, some goroutines
// will exit without execution.
func NewChanWrapper(opts ...Option) Wrapper {
w := &wrapChanImpl{}
option := &Options{}
for _, o := range opts {
o(option)
}
if option.BufCap == 0 {
panic("chan wrapper buf cap must be gt 0")
}
w.recoveryFunc = option.RecoveryFunc
if w.recoveryFunc == nil {
w.recoveryFunc = defaultRecovery
}
w.bufCap = option.BufCap
w.bufCh = make(chan struct{}, w.bufCap)
return w
}
// Wrap exec func in goroutine without recover catch
func (c *wrapChanImpl) Wrap(fn func()) {
go func() {
defer c.done()
fn()
}()
}
// WrapWithRecover safely execute func in goroutine
func (c *wrapChanImpl) WrapWithRecover(fn func()) {
go func() {
defer c.recoveryFunc()
defer c.done()
fn()
}()
}
// Wait wait all goroutine finish
func (c *wrapChanImpl) Wait() {
for i := 0; i < c.bufCap; i++ {
<-c.bufCh
}
}
func (c *wrapChanImpl) done() {
c.bufCh <- struct{}{}
}