-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiblocker.go
51 lines (41 loc) · 1.14 KB
/
multiblocker.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
package blockit
import (
"context"
)
// MultiBlocker represent many blockers to monitor and determine when they are
// done.
type MultiBlocker struct {
blockers []Blocker
}
// Add takes one to many blockers to append into the multiblocker
func (b *MultiBlocker) Add(blocker ...Blocker) {
b.blockers = append(b.blockers, blocker...)
}
// Blockit meets the blocker interface and summarizes multiple blockers into a
// single channel for blocking on.
func (b *MultiBlocker) Blockit() <-chan struct{} {
return b.BlockitWithContext(context.Background())
}
// BlockitWithContext takes a context to include with the blockers. The
// multiblocker will return if the context is cancelled before the blockers
// finish.
func (b *MultiBlocker) BlockitWithContext(ctx context.Context) <-chan struct{} {
out := make(chan struct{})
dones := make([]chan struct{}, len(b.blockers))
for i := range dones {
dones[i] = make(chan struct{})
}
for i := range b.blockers {
go func(j int) {
dones[j] <- <-b.blockers[j].BlockitWithContext(ctx)
}(i)
}
go func() {
for i := range dones {
<-dones[i]
}
out <- struct{}{}
close(out)
}()
return out
}