-
Notifications
You must be signed in to change notification settings - Fork 18k
x/time/rate: Limiter allows more than configured #45245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@Morainy You might want to read the documentation for Allow(). It just says whether there is room. You want to use Wait() and you will get your expected behavior. |
Guys, this is a real issue and the fix is roughly mentioned in #23145: --- a/rate/rate.go
+++ b/rate/rate.go
@@ -320,7 +320,7 @@ func (lim *Limiter) reserveN(now time.Time, n int, maxFutureReserve time.Duratio
}
}
- now, last, tokens := lim.advance(now)
+ now, _, tokens := lim.advance(now)
// Calculate the remaining number of tokens resulting from the request.
tokens -= float64(n)
@@ -350,8 +350,6 @@ func (lim *Limiter) reserveN(now time.Time, n int, maxFutureReserve time.Duratio
lim.last = now
lim.tokens = tokens
lim.lastEvent = r.timeToAct
- } else {
- lim.last = last
}
lim.mu.Unlock() (Now the second return value of
This is the output after applied the fix:
When |
@fraenkel I believe that the documentation for
|
@b97tsk why couldn't this fix be merged into main? |
@Morainy I'm not a contributor. I was trying to help. I suggested a fix. It could be wrong. This is an old package. It's hard to tell if there's any existing application relies on this behavior. So even I am correct, it could still be marked as WON'T FIX. Just my opinion. |
It could be a security issue, you know, if the package is used to mitigate DoS attacks or web scraping, it simply won't work. |
As a glance, logic you're trying to remove may be related to handling time shift backward (which, you know, may happens). |
I can confirm this is an issue. The lowest |
Relevant issue: #52584 And two PRs pending for review: |
Duplicate of #23145 |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
What did you expect to see?
succCount should almost be 20 but it could get a number beyond 10000
What did you see instead?
elapsed= 2.01049986s succCount= 25629 failCount 6806127
The text was updated successfully, but these errors were encountered: