-
Notifications
You must be signed in to change notification settings - Fork 179
/
util.go
210 lines (187 loc) · 6.13 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package util
import (
"context"
"math"
"reflect"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/module/irrecoverable"
)
// 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{})
if len(channels) == 0 {
close(done)
return done
}
go func() {
for _, ch := range channels {
<-ch
}
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
}
}
// readyDoneAwareMerger is a utility structure which implements module.ReadyDoneAware and module.Startable interfaces
// and is used to merge []T into one T.
type readyDoneAwareMerger struct {
components []module.ReadyDoneAware
}
func (m readyDoneAwareMerger) Start(signalerContext irrecoverable.SignalerContext) {
for _, component := range m.components {
startable, ok := component.(module.Startable)
if ok {
startable.Start(signalerContext)
}
}
}
func (m readyDoneAwareMerger) Ready() <-chan struct{} {
return AllReady(m.components...)
}
func (m readyDoneAwareMerger) Done() <-chan struct{} {
return AllDone(m.components...)
}
var _ module.ReadyDoneAware = (*readyDoneAwareMerger)(nil)
var _ module.Startable = (*readyDoneAwareMerger)(nil)
// MergeReadyDone merges []module.ReadyDoneAware into one module.ReadyDoneAware.
func MergeReadyDone(components ...module.ReadyDoneAware) module.ReadyDoneAware {
return readyDoneAwareMerger{components: components}
}
// DetypeSlice converts a typed slice containing any kind of elements into an
// untyped []any type, in effect removing the element type information from the slice.
// It is useful for passing data into structpb.NewValue, which accepts []any but not
// []T for any specific type T.
func DetypeSlice[T any](typedSlice []T) []any {
untypedSlice := make([]any, len(typedSlice))
for i, t := range typedSlice {
untypedSlice[i] = t
}
return untypedSlice
}
// SampleN computes a percentage of the given number 'n', and returns the result as an unsigned integer.
// If the calculated sample is greater than the provided 'max' value, it returns the ceil of 'max'.
// If 'n' is less than or equal to 0, it returns 0.
//
// Parameters:
// - n: The input number, used as the base to compute the percentage.
// - max: The maximum value that the computed sample should not exceed.
// - percentage: The percentage (in range 0.0 to 1.0) to be applied to 'n'.
//
// Returns:
// - The computed sample as an unsigned integer, with consideration to the given constraints.
func SampleN(n int, max, percentage float64) uint {
if n <= 0 {
return 0
}
sample := float64(n) * percentage
if sample > max {
sample = max
}
return uint(math.Ceil(sample))
}