-
Notifications
You must be signed in to change notification settings - Fork 1
/
fanout.go
61 lines (51 loc) · 1.29 KB
/
fanout.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
/*
© 2021–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
ISC License
*/
package parl
import (
"sync"
)
type FanOut struct {
wg sync.WaitGroup
ErrCh chan error
Results chan interface{}
}
type FanThunk func() (result interface{}, err error)
type FanProc func() (err error)
func NewFanOut() (fo *FanOut) {
return &FanOut{
ErrCh: make(chan error),
Results: make(chan interface{}),
}
}
// Do executes a procedure in a goroutine that has no result other than a possible non-nil error
func (cr *FanOut) Do(name string, proc FanProc) {
cr.wg.Add(1)
go cr.fanout(name, nil, proc)
}
// Run executes a thunk in a goroutine with a possible non-nil result and a possible non-nil error
func (cr *FanOut) Run(name string, thunk FanThunk) {
cr.wg.Add(1)
go cr.fanout(name, thunk, nil)
}
func (cr *FanOut) fanout(name string, thunk FanThunk, proc FanProc) {
defer cr.wg.Done()
var err error
defer Recover(name, &err, func(e error) { cr.ErrCh <- err })
if thunk == nil {
err = proc()
} else {
var result interface{}
result, err = thunk()
if result != nil {
cr.Results <- result
}
}
}
// Wait waits for all Do and Run invocations to complete, then shuts down
func (cr *FanOut) Wait() {
cr.wg.Wait()
close(cr.ErrCh)
close(cr.Results)
}