This repository has been archived by the owner on Apr 14, 2021. It is now read-only.
/
concurrent.go
69 lines (60 loc) · 1.55 KB
/
concurrent.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
66
67
68
69
package fritz
import (
"sync"
)
// result is a simple model of a concurrent task, having a
// simple payload and an error.
type result struct {
msg string
err error
}
type successHandler func(string, string) result
type errorHandler func(string, string, error) result
type workTable map[string]func() (string, error)
// scatterGather forks the workTable into separate goroutines
// with callbacks onSuccess and onError. The results are gathered
// in slice. Neither onSuccess nor onError should panic, otherwise
// scatterGather panics.
func scatterGather(wt workTable, onSuccess successHandler, onError errorHandler) []result {
ch := fanOut(wt, onSuccess, onError)
res := fanIn(ch)
results := <-res
return results
}
func fanOut(wt workTable, onSuccess successHandler, onError errorHandler) <-chan result {
wg := new(sync.WaitGroup)
wg.Add(len(wt))
ch := scatter(wt, onSuccess, onError, wg)
go func() {
wg.Wait()
close(ch)
}()
return ch
}
func scatter(wt workTable, onSuccess successHandler, onError errorHandler, wg *sync.WaitGroup) chan result {
ch := make(chan result)
for key, work := range wt {
go func(k string, w func() (string, error)) {
defer wg.Done()
msg, err := w()
if err == nil {
ch <- onSuccess(k, msg)
} else {
ch <- onError(k, msg, err)
}
}(key, work)
}
return ch
}
func fanIn(ch <-chan result) <-chan []result {
collect := make(chan []result)
go func() {
results := make([]result, 0)
for res := range ch {
results = append(results, res)
}
collect <- results
close(collect)
}()
return collect
}