Manually Leak Buckets & Check Bucket Capacity Prior to try_acquire
Opened this issue · 2 comments
Hello!
A usecase I've been hoping to use PyrateLimiter for requires that I be able to
- manually leak buckets a certain weight (i.e. manually add back capacity to a bucket), and
- check whether a bucket has capacity prior to executring a try_acquire.
My specific use case is as follows. Let's say I have 3 buckets. I only want to make a request if all 3 buckets have capacity. If all 3 do, I want to fill each bucket with a weight of one, but if at least 1 of the 3 buckets does not have capacity, I do not want to fill any of them. The reason is because I have to actively rate-limit over 3 different variables in order to make my request, so I only want to make my request when the buckets that represent each of these 3 variables each have >0 capacity.
The easiest way I can see to implement this is with a while true loop, something like
def can_I_send_request():
while True:
check_if_bucket_1_has_capacity
check_if_bucket_2_has_capacity
check_if_bucket_3_has_capacity
if all three buckets have capacity:
try_acquire from bucket_1
try_acquire from bucket_2
try_acquire from bucket_3
if all three try_acquires were successful:
return true
else:
manually leak from the buckets that I added capacity to (i.e. buckets I successfully try_acquired) and go back to beginning of for loop
Aside/Additional context
In my use-case, other requests may require that some N other buckets have capacity. Ultimately, I may have 100 buckets, and
- some requests that I make should only take place if 5 out of those 100 buckets have capacity, while
- a different request should only take place if 3 out of those 100 buckets need to have capacity.
Some of the 5 buckets for request-1 may or may not coincide with some of the 3 buckets for request-2)
As far as I can tell, PyrateLimiter does not support these two functionalities yet - so I was wondering I was by chance mistaken, or if these functionalities may be coming soon
Thank you!
-
Currently there is no method to manually leak a certain weight. Either you flush it completely or leave it be.
-
There is a method called
(async) def waiting() -> int
in the bucket interface. If it returns 0 then the bucket is able to acquire item. You can loop check the buckets to see if all of them are available for acquiring data.
Regarding the manual leak, I'll see what I can do when I have some spare time.
Awesome, thank you!