-
Notifications
You must be signed in to change notification settings - Fork 47
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
KeyedRateLimiter with different quota per key #193
Comments
Oho, yeah, I think I see that this could be useful! I originally (prior to middleware) intentionally didn't add this functionality because keyed rate limiters can use much less memory this way, and don't need as much care and feeding. However, now that we have middleware, the logic around "what even is the quota" could be arbitrarily complex! My thinking is that we could introduce a method on the RateLimitingMiddleware that takes the key, and then returns the quota that applies to that key. That requires restructuring some things: The If you want to take a stab at this, I'd love that! Otherwise I can throw it on my pile of stuff to try (: |
Can you explain your proposal a bit better. I looked into how it can be implemented with an additional function on So just to explain my understanding and assumptions a little better. I imagine a MultiQuotaKeyedRateLimiter will be used roughly like this - let mut limiter = MQKRL::new(Quota::new(20)); // default quota for any key
limiter.add_quota("post", Quota::new(10)); // specific quota for this particular key
limiter.add_quota("get", Quota::new(30)); // and so on Internally MQKRL has a middleware that has state, say a DashMap to map keys to quotas. And in check_key we do something like this. pub fn check_key(&self, key: &K) -> Result<MW::PositiveOutcome, MW::NegativeOutcome> {
let gcra = match self.middleware.get_quota(key).unwrap_or_else(self.gcra);
gcra.test_and_update....
} Some questions -
|
Any thoughts on these questions? I can submit a draft PR once these are clear. |
Hi @antifuchs are you still considering this feature? |
@antifuchs any updates on this guy? |
No, I haven't made any attempt at getting this off the ground yet. I believe the comment I left above still is just about the outline of what needs to be done, so if anyone wants to take a stab at it, I'd more than happily take a look! |
Updates? |
Thanks for this fantastic library. I took a look at the implementation and it's extremely extensible.
I'm trying rate limit API requests but the each URL has a different quota. As far as I understand this cannot be done by the governor currently. However it doesn't appear to be impossible either.
At it's core gcra takes rate limiter configuration and returns a positive or negative outcome. But the function itself doesn't modify
&self
, so gcra is just providing readonly quota related data. Is it possible to store the gcra quota values in a keyed store? That way the quota for that particular key can be retrieved and then tested.The mapping of keys to quotas can be immutable and defined when the rate limiter is initialized. A default quota can also be assigned to any keys that are not part of the mapping. This way the QuotaStore is readonly and does not require complex blocking/syncronization.
Is it possible to have something like this? Are you planning on support such a rate limiter?
I believe there is a somewhat related issue on multiple quotas #156. I think this case is slightly different and might need less changes. I'm happy to contribute.
The text was updated successfully, but these errors were encountered: