未验证 提交 a2d5947f 编写于 作者: B bigsheeper 提交者: GitHub

Change limiter burst type to float64 (#20215)

Signed-off-by: Nbigsheeper <yihao.dai@zilliz.com>
Signed-off-by: Nbigsheeper <yihao.dai@zilliz.com>
上级 d0de1028
...@@ -123,7 +123,7 @@ func (rl *rateLimiter) registerLimiters() { ...@@ -123,7 +123,7 @@ func (rl *rateLimiter) registerLimiters() {
r = Params.QuotaConfig.DQLMaxQueryRate r = Params.QuotaConfig.DQLMaxQueryRate
} }
limit := ratelimitutil.Limit(r) limit := ratelimitutil.Limit(r)
burst := int(r) // use rate as burst, because Limiter is with punishment mechanism, burst is insignificant. burst := r // use rate as burst, because Limiter is with punishment mechanism, burst is insignificant.
rl.limiters[internalpb.RateType(rt)] = ratelimitutil.NewLimiter(limit, burst) rl.limiters[internalpb.RateType(rt)] = ratelimitutil.NewLimiter(limit, burst)
log.Info("RateLimiter register for rateType", log.Info("RateLimiter register for rateType",
zap.String("rateType", internalpb.RateType_name[rt]), zap.String("rateType", internalpb.RateType_name[rt]),
......
...@@ -70,11 +70,11 @@ func TestMultiRateLimiter(t *testing.T) { ...@@ -70,11 +70,11 @@ func TestMultiRateLimiter(t *testing.T) {
Params.QuotaConfig.QuotaAndLimitsEnabled = bak Params.QuotaConfig.QuotaAndLimitsEnabled = bak
Params.QuotaConfig.DMLMaxInsertRate = bakInsertRate Params.QuotaConfig.DMLMaxInsertRate = bakInsertRate
} }
run(math.MaxInt) run(math.MaxFloat64)
run(math.MaxInt / 1.2) run(math.MaxFloat64 / 1.2)
run(math.MaxInt / 2) run(math.MaxFloat64 / 2)
run(math.MaxInt / 3) run(math.MaxFloat64 / 3)
run(math.MaxInt / 10000) run(math.MaxFloat64 / 10000)
}) })
} }
......
...@@ -29,7 +29,7 @@ import ( ...@@ -29,7 +29,7 @@ import (
const ( const (
// defaultMax is the default unlimited rate or threshold. // defaultMax is the default unlimited rate or threshold.
defaultMax = float64(math.MaxInt) defaultMax = float64(math.MaxFloat64)
// MBSize used to convert megabytes and bytes. // MBSize used to convert megabytes and bytes.
MBSize = 1024.0 * 1024.0 MBSize = 1024.0 * 1024.0
// defaultDiskQuotaInMB is the default disk quota in megabytes. // defaultDiskQuotaInMB is the default disk quota in megabytes.
......
...@@ -31,7 +31,7 @@ import ( ...@@ -31,7 +31,7 @@ import (
type Limit float64 type Limit float64
// Inf is the infinite rate limit; it allows all events. // Inf is the infinite rate limit; it allows all events.
const Inf = Limit(math.MaxInt) const Inf = Limit(math.MaxFloat64)
// A Limiter controls how frequently events are allowed to happen. // A Limiter controls how frequently events are allowed to happen.
// It implements a "token bucket" of size b, initially full and refilled // It implements a "token bucket" of size b, initially full and refilled
...@@ -47,14 +47,14 @@ const Inf = Limit(math.MaxInt) ...@@ -47,14 +47,14 @@ const Inf = Limit(math.MaxInt)
type Limiter struct { type Limiter struct {
mu sync.Mutex mu sync.Mutex
limit Limit limit Limit
burst int burst float64
tokens float64 tokens float64
// last is the last time the limiter's tokens field was updated // last is the last time the limiter's tokens field was updated
last time.Time last time.Time
} }
// NewLimiter returns a new Limiter that allows events up to rate r. // NewLimiter returns a new Limiter that allows events up to rate r.
func NewLimiter(r Limit, b int) *Limiter { func NewLimiter(r Limit, b float64) *Limiter {
return &Limiter{ return &Limiter{
limit: r, limit: r,
burst: b, burst: b,
...@@ -77,9 +77,9 @@ func (lim *Limiter) AllowN(now time.Time, n int) bool { ...@@ -77,9 +77,9 @@ func (lim *Limiter) AllowN(now time.Time, n int) bool {
return true return true
} else if lim.limit == 0 { } else if lim.limit == 0 {
var ok bool var ok bool
if lim.burst >= n { if lim.burst >= float64(n) {
ok = true ok = true
lim.burst -= n lim.burst -= float64(n)
} }
return ok return ok
} }
...@@ -113,11 +113,11 @@ func (lim *Limiter) SetLimit(newLimit Limit) { ...@@ -113,11 +113,11 @@ func (lim *Limiter) SetLimit(newLimit Limit) {
lim.last = now lim.last = now
lim.tokens = tokens lim.tokens = tokens
lim.limit = newLimit lim.limit = newLimit
if newLimit >= math.MaxInt { if newLimit >= math.MaxFloat64 {
lim.burst = math.MaxInt lim.burst = math.MaxInt
} else { } else {
// use rate as burst, because Limiter is with punishment mechanism, burst is insignificant. // use rate as burst, because Limiter is with punishment mechanism, burst is insignificant.
lim.burst = int(newLimit) lim.burst = float64(newLimit)
} }
} }
...@@ -133,7 +133,7 @@ func (lim *Limiter) advance(now time.Time) (newNow time.Time, newLast time.Time, ...@@ -133,7 +133,7 @@ func (lim *Limiter) advance(now time.Time) (newNow time.Time, newLast time.Time,
elapsed := now.Sub(last) elapsed := now.Sub(last)
delta := lim.limit.tokensFromDuration(elapsed) delta := lim.limit.tokensFromDuration(elapsed)
tokens := lim.tokens + delta tokens := lim.tokens + delta
if burst := float64(lim.burst); tokens > burst { if burst := lim.burst; tokens > burst {
tokens = burst tokens = burst
} }
return now, last, tokens return now, last, tokens
......
...@@ -131,7 +131,7 @@ func TestLimit(t *testing.T) { ...@@ -131,7 +131,7 @@ func TestLimit(t *testing.T) {
}) })
limit := Inf limit := Inf
burst := math.MaxInt burst := math.MaxFloat64
limiter := NewLimiter(limit, burst) limiter := NewLimiter(limit, burst)
runWithoutCheckToken(t, limiter, []allow{ runWithoutCheckToken(t, limiter, []allow{
{t0, 1 * 1024 * 1024, true, 0}, {t0, 1 * 1024 * 1024, true, 0},
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册