-
Notifications
You must be signed in to change notification settings - Fork 10
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
Allow for bounding of pending acquisitions? #34
Comments
It's doable, but we'd have to change the internal architecture. Rather than bounding on the number of waiters though it would at least to me make more sense to bound on the number of pending tokens. Uses outside of acquiring one token are typically intended to indicate that a task has more "weight" than another, and if you really want to limit the number of waiters then each task would simply specify that it wants one token. In practice this means that the state of the rate limiter would also have to store the number of pending tokens. I suppose the easiest way to achieve this might be to convert the balance into an Another thing I would have to think hard about is whether narrowing the max supported balance to support this change is a breaking change or not. Making the balance a signed value would half its range (one bit is used to determine whether the core task is held). |
Ah, that makes sense to me! I am mapping "one waiter = one token", but I agree "max pending tokens" is generally reasonable. Regarding breaking changes: I would say that it is likely this would require a breaking change, as Would it make sense to have a separate |
My expectation was that if the internals were changed to support this, then you could have functions which do yield by a pending token limit that are fallible, and the existing suit of functions just ignores them. So the added functions could be: impl RateLimiter {
fn acquire_bounded(&self) -> AcquireBounded<'_>;
fn try_acquire_bounded(&self) -> Result<(), TryAcquireBoundedError>;
}
pub struct AcquireBounded<'a> {
/* .. */
}
impl<'a> Future for AcquireBounded<'a> {
type Output = Result<(), AcquireBoundedError>;
} It depends a bit on whether you think the ability to "bypass" the configured bound is a feature or a bug that should be avoided on the type level (with a |
@udoprog if you are asking for my opinion, I'd probably prefer a separate type, in order to avoid confusion when bounding does/doesn't apply! However you know your library best, and I'm open to either approach, so take it only as a weak opinion. |
Hey there!
I'm planning to use
leaky-bucket
for a reverse proxy rate limiter, and one feature that I would be interested in is to bound the number of waiters to return-early at theacquire
stage.For example, if the number of tokens is 3, the refill interval is 100ms, and the waiter-bound is 5, then we would get a behavior like this if 10 requests arrived at the same time:
Right now I can emulate this by adding a
timeout
of ~600ms toacquire_one
, but that means that the tasks for Req 8 and Req 9 will sleep for 600ms, and only error out then, when it could be possible to "know" that there are already too many pending waiters.Is this something you'd be interested in supporting, and if so, do you have any suggestions on how to implement it?
Thanks!
The text was updated successfully, but these errors were encountered: