File tree 3 files changed +43
-7
lines changed
3 files changed +43
-7
lines changed Original file line number Diff line number Diff line change @@ -12,8 +12,12 @@ type BackOff func(currentRetryCount int) time.Duration
12
12
func ExponentialBackOff (minTimeout time.Duration ) BackOff {
13
13
return func (currentRetryCount int ) time.Duration {
14
14
jitter := rand .Float64 ()
15
- strategy := 1 << currentRetryCount
16
- backoff := (1 + float64 (strategy )* jitter ) * minTimeout .Seconds () * float64 (time .Second )
15
+ jitterMax := 200 * time .Millisecond
16
+ if currentRetryCount < 1 {
17
+ currentRetryCount = 1
18
+ }
19
+ strategy := 1 << (currentRetryCount - 1 )
20
+ backoff := (float64 (strategy ) * float64 (minTimeout .Nanoseconds ())) + (jitter * float64 (jitterMax .Nanoseconds ()))
17
21
18
22
return time .Duration (backoff )
19
23
}
@@ -23,9 +27,11 @@ func ExponentialBackOff(minTimeout time.Duration) BackOff {
23
27
func LinearBackOff (minTimeout time.Duration ) BackOff {
24
28
return func (currentRetryCount int ) time.Duration {
25
29
jitter := rand .Float64 ()
26
- strategy := float64 (currentRetryCount )
27
-
28
- backoff := (1 + strategy * jitter ) * minTimeout .Seconds () * float64 (time .Second )
30
+ jitterMax := 200 * time .Millisecond
31
+ if currentRetryCount < 1 {
32
+ currentRetryCount = 1
33
+ }
34
+ backoff := (float64 (currentRetryCount ) * float64 (minTimeout .Nanoseconds ())) + (jitter * float64 (jitterMax .Nanoseconds ()))
29
35
return time .Duration (backoff )
30
36
}
31
37
}
Original file line number Diff line number Diff line change @@ -172,8 +172,27 @@ func RangeBig(from, to *big.Int) <-chan *big.Int {
172
172
return newCh
173
173
}
174
174
175
+ // DrainOpen enumerates all items from the channel and discards them.
176
+ // Returns number of items drained as soon as channel is empty.
177
+ func DrainOpen [T any ](channel <- chan T ) int {
178
+ drained := 0
179
+
180
+ for {
181
+ select {
182
+ case _ , ok := <- channel :
183
+ if ok {
184
+ drained ++
185
+ } else {
186
+ return drained
187
+ }
188
+ default :
189
+ return drained
190
+ }
191
+ }
192
+ }
193
+
175
194
// Drain enumerates all items from the channel and discards them.
176
- // Returns number of items drained.
195
+ // Blocks until the channel is closed and returns number of items drained.
177
196
func Drain [T any ](channel <- chan T ) int {
178
197
drained := 0
179
198
for range channel {
Original file line number Diff line number Diff line change @@ -203,7 +203,7 @@ func TestPushPop(t *testing.T) {
203
203
204
204
func TestDrain (t * testing.T ) {
205
205
c := make (chan string , 10 )
206
-
206
+
207
207
c <- "a"
208
208
c <- "b"
209
209
c <- "c"
@@ -214,3 +214,14 @@ func TestDrain(t *testing.T) {
214
214
_ , ok := <- c
215
215
assert .False (t , ok )
216
216
}
217
+
218
+ func TestDrainOpen (t * testing.T ) {
219
+ o := make (chan string , 10 )
220
+
221
+ o <- "a"
222
+ o <- "b"
223
+ o <- "c"
224
+ o <- "d"
225
+
226
+ assert .Equal (t , 4 , chans .DrainOpen (o ))
227
+ }
You can’t perform that action at this time.
0 commit comments