-
Notifications
You must be signed in to change notification settings - Fork 175
/
util.go
147 lines (131 loc) · 3.95 KB
/
util.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
package util
import (
"context"
"reflect"
"sync"
"github.com/onflow/flow-go/module"
)
// AllReady calls Ready on all input components and returns a channel that is
// closed when all input components are ready.
func AllReady(components ...module.ReadyDoneAware) <-chan struct{} {
readyChans := make([]<-chan struct{}, len(components))
for i, c := range components {
readyChans[i] = c.Ready()
}
return AllClosed(readyChans...)
}
// AllDone calls Done on all input components and returns a channel that is
// closed when all input components are done.
func AllDone(components ...module.ReadyDoneAware) <-chan struct{} {
doneChans := make([]<-chan struct{}, len(components))
for i, c := range components {
doneChans[i] = c.Done()
}
return AllClosed(doneChans...)
}
// AllClosed returns a channel that is closed when all input channels are closed.
func AllClosed(channels ...<-chan struct{}) <-chan struct{} {
done := make(chan struct{})
var wg sync.WaitGroup
for _, ch := range channels {
wg.Add(1)
go func(ch <-chan struct{}) {
<-ch
wg.Done()
}(ch)
}
go func() {
wg.Wait()
close(done)
}()
return done
}
// WaitClosed waits for either a signal/close on the channel or for the context to be cancelled
// Returns nil if the channel was signalled/closed before returning, otherwise, it returns the context
// error.
//
// This handles the corner case where the context is cancelled at the same time that the channel
// is closed, and the Done case was selected.
// This is intended for situations where ignoring a signal can cause safety issues.
func WaitClosed(ctx context.Context, ch <-chan struct{}) error {
select {
case <-ctx.Done():
select {
case <-ch:
return nil
default:
}
return ctx.Err()
case <-ch:
return nil
}
}
// CheckClosed checks if the provided channel has a signal or was closed.
// Returns true if the channel was signaled/closed, otherwise, returns false.
//
// This is intended to reduce boilerplate code when multiple channel checks are required because
// missed signals could cause safety issues.
func CheckClosed(done <-chan struct{}) bool {
select {
case <-done:
return true
default:
return false
}
}
// MergeChannels merges a list of channels into a single channel
func MergeChannels(channels interface{}) interface{} {
sliceType := reflect.TypeOf(channels)
if sliceType.Kind() != reflect.Slice && sliceType.Kind() != reflect.Array {
panic("argument must be an array or slice")
}
chanType := sliceType.Elem()
if chanType.ChanDir() == reflect.SendDir {
panic("channels cannot be send-only")
}
c := reflect.ValueOf(channels)
var cases []reflect.SelectCase
for i := 0; i < c.Len(); i++ {
cases = append(cases, reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: c.Index(i),
})
}
elemType := chanType.Elem()
out := reflect.MakeChan(reflect.ChanOf(reflect.BothDir, elemType), 0)
go func() {
for len(cases) > 0 {
i, v, ok := reflect.Select(cases)
if !ok {
lastIndex := len(cases) - 1
cases[i], cases[lastIndex] = cases[lastIndex], cases[i]
cases = cases[:lastIndex]
continue
}
out.Send(v)
}
out.Close()
}()
return out.Convert(reflect.ChanOf(reflect.RecvDir, elemType)).Interface()
}
// WaitError waits for either an error on the error channel or the done channel to close
// Returns an error if one is received on the error channel, otherwise it returns nil
//
// This handles a race condition where the done channel could have been closed as a result of an
// irrecoverable error being thrown, so that when the scheduler yields control back to this
// goroutine, both channels are available to read from. If the done case happens to be chosen
// at random to proceed instead of the error case, then we would return without error which could
// result in unsafe continuation.
func WaitError(errChan <-chan error, done <-chan struct{}) error {
select {
case err := <-errChan:
return err
case <-done:
select {
case err := <-errChan:
return err
default:
}
return nil
}
}