Skip to content

Instantly share code, notes, and snippets.

@mourginakis
Created March 6, 2026 01:55
Show Gist options
  • Select an option

  • Save mourginakis/f1d87346f1c1342b031821cf8fe88a4b to your computer and use it in GitHub Desktop.

Select an option

Save mourginakis/f1d87346f1c1342b031821cf8fe88a4b to your computer and use it in GitHub Desktop.
rate limiting
### 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