add lock for getting burst
Burst can be modified in `func (lim *Limiter) SetBurst(newBurst int)`, so it should be protected in `func (lim *Limiter) Burst() int`.
Change-Id: Ife2a733b035067d34e3165d2b2636666fcf2ee1d
GitHub-Last-Rev: bc8e73110ffccf7e42325e34865b473bc4624c31
GitHub-Pull-Request: golang/time#9
Reviewed-on: https://go-review.googlesource.com/c/time/+/205020
Reviewed-by: Sameer Ajmani <sameer@golang.org>
Run-TryBot: Sameer Ajmani <sameer@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
diff --git a/rate/rate.go b/rate/rate.go
index a114b1a..a98fe77 100644
--- a/rate/rate.go
+++ b/rate/rate.go
@@ -53,10 +53,9 @@
//
// The methods AllowN, ReserveN, and WaitN consume n tokens.
type Limiter struct {
- limit Limit
- burst int
-
mu sync.Mutex
+ limit Limit
+ burst int
tokens float64
// last is the last time the limiter's tokens field was updated
last time.Time
@@ -76,6 +75,8 @@
// Burst values allow more events to happen at once.
// A zero Burst allows no events, unless limit == Inf.
func (lim *Limiter) Burst() int {
+ lim.mu.Lock()
+ defer lim.mu.Unlock()
return lim.burst
}
@@ -229,7 +230,7 @@
lim.mu.Unlock()
if n > burst && limit != Inf {
- return fmt.Errorf("rate: Wait(n=%d) exceeds limiter's burst %d", n, lim.burst)
+ return fmt.Errorf("rate: Wait(n=%d) exceeds limiter's burst %d", n, burst)
}
// Check if ctx is already cancelled
select {
@@ -359,6 +360,7 @@
// advance calculates and returns an updated state for lim resulting from the passage of time.
// lim is not changed.
+// advance requires that lim.mu is held.
func (lim *Limiter) advance(now time.Time) (newNow time.Time, newLast time.Time, newTokens float64) {
last := lim.last
if now.Before(last) {