Skip to content

Commit 6dd8240

Browse files
authored
pkg/ratelimit/ratelimiter: reduce the usage of CPU in Available (#5036)
close #5034 Signed-off-by: shirly <[email protected]>
1 parent 04d6881 commit 6dd8240

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

pkg/ratelimit/ratelimiter.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,14 @@ func (l *RateLimiter) Available(n int) bool {
4242
defer l.mu.Unlock()
4343
now := time.Now()
4444
r := l.Limiter.ReserveN(now, n)
45-
delay := r.Delay()
45+
delay := r.DelayFrom(now)
4646
r.CancelAt(now)
4747
return delay == 0
4848
}
4949

5050
// Allow is same as `rate.Limiter.Allow`.
5151
func (l *RateLimiter) Allow() bool {
52-
l.mu.Lock()
53-
defer l.mu.Unlock()
54-
return l.Limiter.Allow()
52+
return l.AllowN(1)
5553
}
5654

5755
// AllowN is same as `rate.Limiter.AllowN`.

0 commit comments

Comments
 (0)