Created
March 6, 2026 01:55
-
-
Save mourginakis/f1d87346f1c1342b031821cf8fe88a4b to your computer and use it in GitHub Desktop.
rate limiting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ### Q1: Rate Limiting | |
| # ========================== | |
| # Say you have a function called `get_foo`. | |
| # The rate limit forbids it from being called more than 12 times a minute. | |
| # How do you properly rate limit this? Assume it's being called synchronously (from just one thread). | |
| import requests | |
| def get_foo(): | |
| return requests.get("https://my-api.com/asdf") | |
| # Solution: sliding window over time series | |
| import time | |
| bucket = [] | |
| def rate_limited_get_foo(): | |
| # time.monotonic() only increases, is ideal. | |
| # time.time() tracks system clock and can jump backward | |
| now = time.monotonic() | |
| t0 = now - 60 | |
| bucket[:] = [t for t in bucket if t > t0] | |
| if len(bucket) >= 12: | |
| raise Exception("Rate limit exceeded") | |
| bucket.append(now) | |
| result = get_foo() | |
| return result | |
| # note: you could also use a deque here. I find this more readable. | |
| # note: [:] mutates the list in place, keeping the reference. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment