Skip to content

Commit 2c09566

Browse files
ZekeLugopherbot
authored andcommitted
rate: the state of the limiter should not be changed when the requests failed
In the following cases, the reserveN is a no-op, and the state of the limiter should not be changed: 1. n exceeds the Limiter's burst size 2. maxFutureReserve is less than the waitDuration Fixes golang/go#52584 Change-Id: I5f38afa5da696bc10178a4bd0640d92062a8b009 GitHub-Last-Rev: e408718 GitHub-Pull-Request: #22 Reviewed-on: https://go-review.googlesource.com/c/time/+/406154 Reviewed-by: Russ Cox <[email protected]> Auto-Submit: Sameer Ajmani <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Sameer Ajmani <[email protected]> Auto-Submit: Russ Cox <[email protected]> Run-TryBot: Russ Cox <[email protected]>
1 parent 80b9fac commit 2c09566

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

rate/rate.go

+8-12
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (lim *Limiter) Burst() int {
8383
// TokensAt returns the number of tokens available at time t.
8484
func (lim *Limiter) TokensAt(t time.Time) float64 {
8585
lim.mu.Lock()
86-
_, _, tokens := lim.advance(t) // does not mutute lim
86+
_, tokens := lim.advance(t) // does not mutate lim
8787
lim.mu.Unlock()
8888
return tokens
8989
}
@@ -183,7 +183,7 @@ func (r *Reservation) CancelAt(t time.Time) {
183183
return
184184
}
185185
// advance time to now
186-
t, _, tokens := r.lim.advance(t)
186+
t, tokens := r.lim.advance(t)
187187
// calculate new number of tokens
188188
tokens += restoreTokens
189189
if burst := float64(r.lim.burst); tokens > burst {
@@ -304,7 +304,7 @@ func (lim *Limiter) SetLimitAt(t time.Time, newLimit Limit) {
304304
lim.mu.Lock()
305305
defer lim.mu.Unlock()
306306

307-
t, _, tokens := lim.advance(t)
307+
t, tokens := lim.advance(t)
308308

309309
lim.last = t
310310
lim.tokens = tokens
@@ -321,7 +321,7 @@ func (lim *Limiter) SetBurstAt(t time.Time, newBurst int) {
321321
lim.mu.Lock()
322322
defer lim.mu.Unlock()
323323

324-
t, _, tokens := lim.advance(t)
324+
t, tokens := lim.advance(t)
325325

326326
lim.last = t
327327
lim.tokens = tokens
@@ -356,7 +356,7 @@ func (lim *Limiter) reserveN(t time.Time, n int, maxFutureReserve time.Duration)
356356
}
357357
}
358358

359-
t, last, tokens := lim.advance(t)
359+
t, tokens := lim.advance(t)
360360

361361
// Calculate the remaining number of tokens resulting from the request.
362362
tokens -= float64(n)
@@ -379,15 +379,11 @@ func (lim *Limiter) reserveN(t time.Time, n int, maxFutureReserve time.Duration)
379379
if ok {
380380
r.tokens = n
381381
r.timeToAct = t.Add(waitDuration)
382-
}
383382

384-
// Update state
385-
if ok {
383+
// Update state
386384
lim.last = t
387385
lim.tokens = tokens
388386
lim.lastEvent = r.timeToAct
389-
} else {
390-
lim.last = last
391387
}
392388

393389
return r
@@ -396,7 +392,7 @@ func (lim *Limiter) reserveN(t time.Time, n int, maxFutureReserve time.Duration)
396392
// advance calculates and returns an updated state for lim resulting from the passage of time.
397393
// lim is not changed.
398394
// advance requires that lim.mu is held.
399-
func (lim *Limiter) advance(t time.Time) (newT time.Time, newLast time.Time, newTokens float64) {
395+
func (lim *Limiter) advance(t time.Time) (newT time.Time, newTokens float64) {
400396
last := lim.last
401397
if t.Before(last) {
402398
last = t
@@ -409,7 +405,7 @@ func (lim *Limiter) advance(t time.Time) (newT time.Time, newLast time.Time, new
409405
if burst := float64(lim.burst); tokens > burst {
410406
tokens = burst
411407
}
412-
return t, last, tokens
408+
return t, tokens
413409
}
414410

415411
// durationFromTokens is a unit conversion function from the number of tokens to the duration

rate/rate_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,12 @@ func TestReserveJumpBack(t *testing.T) {
427427
runReserve(t, lim, request{t1, 2, t1, true}) // start at t1
428428
runReserve(t, lim, request{t0, 1, t1, true}) // should violate Limit,Burst
429429
runReserve(t, lim, request{t2, 2, t3, true})
430+
// burst size is 2, so n=3 always fails, and the state of lim should not be changed
431+
runReserve(t, lim, request{t0, 3, time.Time{}, false})
432+
runReserve(t, lim, request{t2, 1, t4, true})
433+
// the maxReserve is not enough so it fails, and the state of lim should not be changed
434+
runReserveMax(t, lim, request{t0, 2, time.Time{}, false}, d)
435+
runReserve(t, lim, request{t2, 1, t5, true})
430436
}
431437

432438
func TestReserveJumpBackCancel(t *testing.T) {

0 commit comments

Comments
 (0)