Token bucket rate limiter
Allow bursts but cap the sustained request rate with a small thread-safe token bucket.
- Python
- GreenSnippets
- MIT
- Executed with Python 3.11
import threading
import time
class TokenBucket:
def __init__(self, rate, capacity, clock=time.monotonic):
self.rate = rate
self.capacity = capacity
self._tokens = float(capacity)
self._clock = clock
self._updated = clock()
self._lock = threading.Lock()
def acquire(self, cost=1):
"""Return 0 if allowed, otherwise seconds to wait before retrying."""
with self._lock:
now = self._clock()
self._tokens = min(self.capacity, self._tokens + (now - self._updated) * self.rate)
self._updated = now
if self._tokens >= cost:
self._tokens -= cost
return 0.0
return (cost - self._tokens) / self.rateExample / expected output
bucket = TokenBucket(rate=5, capacity=10)
wait = bucket.acquire() # 0.0 when allowedHow it works
Tokens refill continuously at rate per second up to capacity. Each call consumes one; if none is available the request is rejected (or the caller can sleep for the returned wait). The clock is injectable, which keeps the class testable without real sleeping.
Related snippets
- JavaScript Tested
Respect Retry-After on 429 and 503 responses
Retry rate-limited requests after the delay the server asks for, instead of guessing.
- Python Tested
Time a block of code with a context manager
Measure elapsed wall-clock time of a with-block using time.perf_counter.
- Python Tested
Slugify a string
Turn text into a lowercase, hyphenated, ASCII-only slug using only the standard library.
- Python Tested
Sliding window over an iterable
Yield overlapping windows of fixed size, useful for moving averages and n-grams.

