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

How do you block until the rate has capacity? #221

Open
iainelder opened this issue Jul 17, 2024 · 1 comment
Open

How do you block until the rate has capacity? #221

iainelder opened this issue Jul 17, 2024 · 1 comment

Comments

@iainelder
Copy link

Came here from Justin Van Winkle's survey of Python rate limiters. He said limits "seems to work", which is high praise because the rest were somehow broken.

Am I missing something with the hit function?

Quickstart shows how to assert whether it returns True or False and to sleep to make a previous False turn True.

assert True == moving_window.hit(one_per_minute, "test_namespace", "foo")
assert False == moving_window.hit(one_per_minute, "test_namespace", "foo")
assert True == moving_window.hit(one_per_minute, "test_namespace", "bar")

assert True == moving_window.hit(one_per_second, "test_namespace", "foo")
assert False == moving_window.hit(one_per_second, "test_namespace", "foo")
time.sleep(1)
assert True == moving_window.hit(one_per_second, "test_namespace", "foo")

I want a rate limiter that blocks until the request would satisfy the limit; that is, as soon as hit would return True.

I suppose I could built something like that using hit, but I don't want to. If I have to start writing sleeps into my code, then I think there's something missing from my rate limiter library.

@iainelder
Copy link
Author

iainelder commented Jul 17, 2024

The Check without consuming example gave me a clue.

I need to add this pattern into any function that I want to be rate-limited.

while not moving_window.test(one_per_minute, "test_namespace"):
    time.sleep(0.01)
assert moving_window.hit(one_per_minute, "test_namespace")

Is that how you expect me to use this? If so, I would make a PR that clarifies this in the docs.

Better still would be a decorator (or indications on how to build one) so I don't have to add this inside my functions.

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

No branches or pull requests

1 participant