vutran1710 / PyrateLimiter

⚔️Python Rate-Limiter using Leaky-Bucket Algorithm Family
https://pyratelimiter.readthedocs.io
MIT License
334 stars 36 forks source link

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

Open tdesai2017 opened 2 months ago

tdesai2017 commented 2 months ago

Hello!

A usecase I've been hoping to use PyrateLimiter for requires that I be able to

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 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 commented 2 months ago
  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 commented 2 months ago

Awesome, thank you!