-
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: document the behavior of negative n #54082
Comments
I'm using this mechanism for managing potential operations: get token, check if operation does actually execute or errors, in case of error return token. It would be helpful to establish that this is the desired behavior. |
I was planning on using it for something similar:
|
What is the expected behavior for negative n? Does the implementation have the expected behavior, so just needs to be documented? Feel free to send a CL. Thanks. |
I would expect the behavior to be equivalent to returning N tokens back to the bucket. If doing so exceeds the burst size, then it clamps the number tokens to the bucket size. It might make sense to add an explicit |
@andig, out of curiosity, which method were you using to return tokens back to the limiter: |
|
Hi @andig is there any progress on this? Can I keep using this behavior? |
I think it's worth mentioning that the current implementation of Example: https://go.dev/play/p/Nkmjt24P3uR // Rate limit, 1 event per 10 seconds, no burst.
l := rate.NewLimiter(0.1, 1)
// After 1s, return a token to the bucket.
// You might expect this to unblock Wait, but it doesn't.
go func() {
<-time.After(1 * time.Second)
l.AllowN(time.Now(), -1)
log.Println("Token returned to bucket")
}()
// Observe: wait is not unblocked when a token is added.
for i := 0; ; i++ {
_ = l.Wait(context.Background())
log.Printf("Allowed #%d!", i)
} Results in the following. Note how a token being returned doesn't unblock Wait.
I think this would need to be documented, and potentially the behaviour of At a glance, this should be doable without spawning additional goroutines, and could be done in a backwards-compatible way if the "return to bucket" capability was via a new method. |
I did not validate though if that would unblock any waiting routines as I've always used |
The methods
AllowN
,ReserveN
,WaitN
take in a user-specified number of tokens to take. The documentation does not specify what happens when the number of tokens is negative.Without digging into the details, it appears that you can actually specify a negative n, in which case it returns that number of tokens to the rate limiter.
The text was updated successfully, but these errors were encountered: