-
Notifications
You must be signed in to change notification settings - Fork 17.5k
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
x/time/rate: unexpected behaviour with 0 limit #68541
Comments
CC @Sajmani |
Having skimmed the code a bit more, I think this would actually make sense. The overflow described in the original issue is specifically guarded against. If we remove this case entirely, a
I can submit a PR with this change |
@leesio initializing tokens to the burst size makes sense to me based on the "initially full" phrase in the Limiter doc comment: I also agree that burst should only be set in the constructor and SetBurst. Honestly I've lost any familiarity I've had with this code, so I'd feel better with a second set of eyes to review any changes and help us make sure we're not making a backwards-incompatible behavioral change. |
Cool, glad we're on the same page. There's no particular rush on my side to get this resolved—we've implemented a workaround. Here's my suggested patch. It's very possible I've done something very wrong, it's my first time using the tooling 😅 |
Go version
$ go version go version go1.22.1 darwin/arm64
Output of
go env
in your module/workspace:What did you do?
rate.Limiter
withburst: 1
andlimit: 0
Allow
on the limiterSetLimit
Wait
on the limiterhttps://go.dev/play/p/25EIPtjqvDs
What did you see happen?
Wait
returns a non-nilerror
:rate: Wait(n=1) exceeds limiter's burst 0
.This happens because the burst is decremented when
reserveN
is called on aLimiter
withLimit
of 0 which seems strange/incorrect to me.The code appeared here but the conversation doesn't really reflect the code: the comment says:
Which makes sense to me, but I'm not sure how we ended up decrementing the burst. To me the solution to the reported problem (limiters not actually being full) would be to set
lim.tokens = burst
on the constructor. I don't thinkburst
should be changed by anything exceptSetBurst
What did you expect to see?
I expected
Wait
to return anil
error after an appropriate delay!The text was updated successfully, but these errors were encountered: