-
Notifications
You must be signed in to change notification settings - Fork 1
/
gonads.go
156 lines (129 loc) · 2.67 KB
/
gonads.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package gonads
// A Gonad can be used to chain set of concurrent operations. Each step in the
// chain contains a set of operations. If any error occurs the chain is broken
// and skips to the next error handler.
type Gonad struct {
done chan struct{}
err chan error
}
// Do an operation concurrently.
func Do(f func() error) *Gonad {
g := &Gonad{
done: make(chan struct{}),
err: make(chan error),
}
go func() {
err := f()
g.done <- struct{}{}
g.err <- err
}()
return g
}
// DoAll operations concurrently.
func DoAll(fs ...func() error) *Gonad {
g := &Gonad{
done: make(chan struct{}),
err: make(chan error),
}
dones := make(chan struct{}, len(fs))
errs := make(chan error, len(fs))
for _, f := range fs {
go func(f func() error) {
err := f()
dones <- struct{}{}
errs <- err
}(f)
}
go func() {
var err error
for i := 0; i < len(fs); i++ {
<-dones
}
for i := 0; err == nil && i < len(fs); i++ {
err = <-errs
}
g.done <- struct{}{}
g.err <- err
}()
return g
}
// Then do an operation concurrently, after all operations in the Gonad
// complete without error.
func (g *Gonad) Then(f func() error) *Gonad {
nextG := &Gonad{
done: make(chan struct{}),
err: make(chan error),
}
go func() {
<-g.done
err := <-g.err
if err == nil {
err = f()
}
nextG.done <- struct{}{}
nextG.err <- err
}()
return nextG
}
// ThenAll operations are done concurrently, after all operations in the
// Gonad complete without error.
func (g *Gonad) ThenAll(fs ...func() error) *Gonad {
nextG := &Gonad{
done: make(chan struct{}),
err: make(chan error),
}
go func() {
<-g.done
err := <-g.err
if err != nil {
nextG.done <- struct{}{}
nextG.err <- err
return
}
dones := make(chan struct{}, len(fs))
errs := make(chan error, len(fs))
for _, f := range fs {
go func(f func() error) {
err := f()
dones <- struct{}{}
errs <- err
}(f)
}
for i := 0; i < len(fs); i++ {
<-dones
}
for i := 0; err == nil && i < len(fs); i++ {
err = <-errs
}
nextG.done <- struct{}{}
nextG.err <- err
}()
return nextG
}
// Else do an error handling operation concurrently, after any operation in
// the Gonad completes with an error.
func (g *Gonad) Else(f func(err error)) *Gonad {
nextG := &Gonad{
done: make(chan struct{}),
err: make(chan error),
}
go func() {
<-g.done
err := <-g.err
if err != nil {
f(err)
}
nextG.done <- struct{}{}
nextG.err <- err
}()
return nextG
}
// Finally do an operation concurrently, after all operations in the Gonad
// have completed, regardless of any errors.
func (g *Gonad) Finally(f func()) {
go func() {
<-g.done
<-g.err
f()
}()
}