You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, thanks for all your work on pyrate limiter. I've been testing my code after upgrading from the 2 series to 3.4.1 and have run into some issues with max delay.
In the previous major version of the library it was easy to create a requester that just blocked until it was safe to make a new request, now it seems like max_delay must be set in order to block and setting max_delay requires a formula based on the maximum wait period and knowledge of the internals of the engine. (specifically that 50ms is added to the max_delay so you can't just set it to 1000ms if you want 1 request a second.
For example:
#!/usr/bin/env python3
from __future__ import annotations
from pyrate_limiter import Duration, Limiter, Rate
import time
def make_reqs(l: Limiter):
start = time.time()
for i in range(3):
print(f"\tAttempt: {i} Success: {l.try_acquire('some-bucket')} at T {time.time() - start:.2f}s")
print("With raise_when_fail=False, no max_delay")
make_reqs(Limiter(Rate(1, Duration.SECOND), raise_when_fail=False))
print("With raise_when_fail=False, max_delay=1000")
make_reqs(Limiter(Rate(1, Duration.SECOND), raise_when_fail=False, max_delay=1000))
print("With raise_when_fail=False, max_delay=1500")
make_reqs(Limiter(Rate(1, Duration.SECOND), raise_when_fail=False, max_delay=1500))
Produces the following output:
With raise_when_fail=False, no max_delay
Attempt: 0 Success: True at T 0.00s
Attempt: 1 Success: False at T 0.00s
Attempt: 2 Success: False at T 0.00s
With raise_when_fail=False, max_delay=1000
Attempt: 0 Success: True at T 0.00s
Required delay too large: actual=1050, expected=1000
Attempt: 1 Success: False at T 0.00s
Required delay too large: actual=1050, expected=1000
Attempt: 2 Success: False at T 0.00s
With raise_when_fail=False, max_delay=1500
Attempt: 0 Success: True at T 0.00s
Attempt: 1 Success: True at T 1.05s
Attempt: 2 Success: True at T 2.10s
With a set of rates (e..g 1 request /second 500/hour) it becomes quite complicated to calculate max_delay such that the API actually blocks, otherwise it's just immediately going to return false and you have to implement your own loop to retry.
The text was updated successfully, but these errors were encountered:
Hi, thanks for all your work on pyrate limiter. I've been testing my code after upgrading from the 2 series to 3.4.1 and have run into some issues with max delay.
In the previous major version of the library it was easy to create a requester that just blocked until it was safe to make a new request, now it seems like max_delay must be set in order to block and setting max_delay requires a formula based on the maximum wait period and knowledge of the internals of the engine. (specifically that 50ms is added to the max_delay so you can't just set it to 1000ms if you want 1 request a second.
For example:
Produces the following output:
With a set of rates (e..g 1 request /second 500/hour) it becomes quite complicated to calculate max_delay such that the API actually blocks, otherwise it's just immediately going to return false and you have to implement your own loop to retry.
The text was updated successfully, but these errors were encountered: