Skip to content
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

Manually Leak Buckets & Check Bucket Capacity Prior to try_acquire #174

Open
tdesai2017 opened this issue Jun 14, 2024 · 2 comments
Open

Comments

@tdesai2017
Copy link

tdesai2017 commented Jun 14, 2024

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!

@vutran1710
Copy link
Owner

  1. Currently there is no method to manually leak a certain weight. Either you flush it completely or leave it be.

  2. 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.

@tdesai2017
Copy link
Author

Awesome, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants