-
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: convert Limit to an Interface #43003
Comments
Conversion into interfaces allows for easier implementation and swapping of rate limit algorithms such as leaky bucket, fixed window, or sliding window. The original Limiter struct as been converted into the TokenBucket struct. The original Reservation struct has been converted into the TokenBucketReservation struct. The API remains backwards compatable, with NewLimiter() returning a TokenBucket via a call to NewTokenLimiter(). fixes golang/go#43003
Conversion into interfaces allows for easier implementation and swapping of rate limit algorithms such as leaky bucket, fixed window, or sliding window. The original Limiter struct as been converted into the TokenBucket struct. The original Reservation struct has been converted into the TokenBucketReservation struct. The API remains backwards compatable, with NewLimiter() returning a TokenBucket via a call to NewTokenLimiter(). fixes golang/go#43003
I just threw together a quick migration to an interface to play with here. I had to make the reservation returned by I think the name of the Reservation interface should be kept to Reservation, breaking the 'er' rule, but it just works better than anything else I could find. The only other changes to the API would be the addition of I wouldn't have jumped the gun on the proposal process, but I need a sliding window implementation soon, and if i'm writing it anyway, I might as well do so in a manner than benefits everyone. |
The x/time/rate package only has one implementation, so why does it need an interface? If you need an abstraction across multiple types of rate limiters, that can live in your code. To quote the Code Review Comments page:
|
(In any case, it can't be changed now in a backward-compatible way.) |
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?
examined using rate.Limiter to interact with an API
What did you expect to see?
A way to interact with an API where the api implements token bucket rate limiting, or The ability to create an implementation of the rate limit interface to do so.
What did you see instead?
rate.Limiter is not an interface.
Limiter should likely be an interface providing the programmer the options to choose between a token bucket or a leaky bucket implementations, if not sliding and fixed windows as well.
Looking at the API, Allow, Wait and Reserve (and their N counterparts) would all be part of the interface, while get/set limit and get/set burst would likely be specific to only some implementations, and so not part of the interface.
The text was updated successfully, but these errors were encountered: