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: CancelAt returns less than expected tokens #56924
Comments
CancelAt
, rate.go
CancelAt
, rate.go
cc @Sajmani |
The code in rate.go has a comment explaining the logic:
In your example code, 2 tokens are reserved by the call |
YoungReese is right, and the logic of What's more, I would like to recommend a rate limiter that is more concise + more correct and effective than the official go implementation watchdog
At the usage level, For specific introduction and implementation, please refer to https://github.com/1996Paul-Wen/watchdog#readme |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
https://github.com/golang/time/blob/master/rate/rate.go
What operating system and processor architecture are you using (
go env
)?mac os, m1 chip
go env
OutputWhat did you do?
Using rate.go to build my token bucket algorithm, when I read the source code, I got one question that puzzled me.
The question is that we call function
CancelAt
, the variablerestoreTokens
calculated is not right.Then, I googled to see if anyone had this problem. As follows:
https://learnku.com/go/t/71323
https://stackoverflow.com/questions/70993567/rate-limiting-cancellation-token-restore
https://lailin.xyz/post/go-training-week6-3-token-bucket-2.html#comments
Now, let us look a piece of code.
What did you expect to see?
we expected logic is: -5 + 1 + 10 = 6, this is a right answer
What did you see instead?
// when we see source code in CancelAt, now logic is: -5 + 1 + (10 - 2) = 4
Why ?
Then I debug the code, I found the is we sub the
tokensFromDuration
twice.if we use restoreTokens = float64(r.tokens), then we got right answer.
The text was updated successfully, but these errors were encountered: