Skip to content

Commit

Permalink
storelimit: fix datarace on reset
Browse files Browse the repository at this point in the history
Signed-off-by: lhy1024 <[email protected]>
  • Loading branch information
lhy1024 authored and ti-chi-bot committed Jun 5, 2024
1 parent 795940f commit 8da5859
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion pkg/core/storelimit/store_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package storelimit
import (
"github.com/tikv/pd/pkg/core/constant"
"github.com/tikv/pd/pkg/ratelimit"
"github.com/tikv/pd/pkg/utils/syncutil"
)

const (
Expand Down Expand Up @@ -106,7 +107,7 @@ func (l *StoreRateLimit) Rate(typ Type) float64 {
if l.limits[typ] == nil {
return 0.0
}
return l.limits[typ].ratePerSec
return l.limits[typ].GetRatePerSec()
}

// Take takes count tokens from the bucket without blocking.
Expand All @@ -128,12 +129,15 @@ func (l *StoreRateLimit) Reset(rate float64, typ Type) {

// limit the operators of a store
type limit struct {
syncutil.RWMutex
limiter *ratelimit.RateLimiter
ratePerSec float64
}

// Reset resets the rate limit.
func (l *limit) Reset(ratePerSec float64) {
l.Lock()
defer l.Unlock()
if l.ratePerSec == ratePerSec {
return
}
Expand All @@ -155,6 +159,8 @@ func (l *limit) Reset(ratePerSec float64) {
// Available returns the number of available tokens
// It returns true if the rate per second is zero.
func (l *limit) Available(n int64) bool {
l.RLock()
defer l.RUnlock()
if l.ratePerSec == 0 {
return true
}
Expand All @@ -164,8 +170,16 @@ func (l *limit) Available(n int64) bool {

// Take takes count tokens from the bucket without blocking.
func (l *limit) Take(count int64) bool {
l.RLock()
defer l.RUnlock()
if l.ratePerSec == 0 {
return true
}
return l.limiter.AllowN(int(count))
}

func (l *limit) GetRatePerSec() float64 {
l.RLock()
defer l.RUnlock()
return l.ratePerSec
}

0 comments on commit 8da5859

Please sign in to comment.