vutran1710 / PyrateLimiter

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

Library does not wait on request that overflow the bucket with a max_delay argument #175

Open Sargeanthost opened 1 month ago

Sargeanthost commented 1 month ago

I want my requests to be sent, not dropped, so I'm trying to use max_delay.

You may want to simply slow down your requests to stay within the rate limits instead of canceling them. In that case you pass the max_delay argument the maximum value of delay (typically in ms when use human-clock).

However, I keep getting this error:

Re-acquiring with delay expected to be successful,
if it failed then either clock or bucket is probably unstable

Why? I've set up a limiter in many different ways, here's one:

self.limiter = Limiter(
            SingleBucketFactory(
                InMemoryBucket([Rate(2, Duration.SECOND)]), MonotonicClock()
            ),
            clock=MonotonicClock(),
            max_delay=999999,
        )

the raise_when_fail vs not is kind of useless, since exception/False is all the same. I expect the library to handle delaying the requests when passed in the parameter, so I'm confused why it's not doing this. Pointers would be useful, thanks

Edit: It looks like it's limiting when the weight is set to 2 and not 1, is there a reason for this?

lordscales91 commented 1 month ago

From the provided code snippet I see that you are assigning to some class instance. The limiter instance should be the same for all the calls that you want to apply the limit to. See #172