Skip to content

Commit 711ca1c

Browse files
Sajmanibradfitz
authored andcommitted
rate: ignore burst in WaitN when rate limit is Inf
WaitN fails when the number of tokens requested exceeds the limiter's burst size, even when the rate limit is Inf. The documented behavior is that the burst size is ignored when the rate limit is Inf. Change WaitN to conform with the documentation, and add tests. Fixes golang/go#16854 Change-Id: I3ca0a8dac47641c76bed4258cd7010f0a704ff8c Reviewed-on: https://go-review.googlesource.com/28610 Run-TryBot: Sameer Ajmani <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 2bc1b4f commit 711ca1c

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

rate/rate.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,9 @@ func (lim *Limiter) Wait(ctx context.Context) (err error) {
221221
// WaitN blocks until lim permits n events to happen.
222222
// It returns an error if n exceeds the Limiter's burst size, the Context is
223223
// canceled, or the expected wait time exceeds the Context's Deadline.
224+
// The burst limit is ignored if the rate limit is Inf.
224225
func (lim *Limiter) WaitN(ctx context.Context, n int) (err error) {
225-
if n > lim.burst {
226+
if n > lim.burst && lim.limit != Inf {
226227
return fmt.Errorf("rate: Wait(n=%d) exceeds limiter's burst %d", n, lim.burst)
227228
}
228229
// Check if ctx is already cancelled

rate/rate_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ func TestWaitSimple(t *testing.T) {
396396
cancel()
397397
runWait(t, lim, wait{"already-cancelled", ctx, 1, 0, false})
398398

399-
runWait(t, lim, wait{"n-gt-burst", context.Background(), 4, 0, false})
399+
runWait(t, lim, wait{"exceed-burst-error", context.Background(), 4, 0, false})
400400

401401
runWait(t, lim, wait{"act-now", context.Background(), 2, 0, true})
402402
runWait(t, lim, wait{"act-later", context.Background(), 3, 2, true})
@@ -426,6 +426,12 @@ func TestWaitTimeout(t *testing.T) {
426426
runWait(t, lim, wait{"w-timeout-err", ctx, 3, 0, false})
427427
}
428428

429+
func TestWaitInf(t *testing.T) {
430+
lim := NewLimiter(Inf, 0)
431+
432+
runWait(t, lim, wait{"exceed-burst-no-error", context.Background(), 3, 0, true})
433+
}
434+
429435
func BenchmarkAllowN(b *testing.B) {
430436
lim := NewLimiter(Every(1*time.Second), 1)
431437
now := time.Now()

0 commit comments

Comments
 (0)