Rate Limiting Implementations

May 30, 2026 | 5 min read

Token Bucket

class TokenBucket:
    def __init__(self, rate: float, capacity: int):
        self.rate = rate
        self.capacity = capacity
        self.tokens = capacity
        self.last_update = time.time()

    def allow(self) -> bool:
        now = time.time()
        elapsed = now - self.last_update
        self.tokens = min(self.capacity, self.tokens + elapsed * self.rate)
        self.last_update = now
        if self.tokens >= 1:
            self.tokens -= 1
            return True
        return False

Sliding Window

class SlidingWindow:
    def __init__(self, window: float, limit: int):
        self.window = window
        self.limit = limit
        self.requests = []

    def allow(self) -> bool:
        now = time.time()
        cutoff = now - self.window
        self.requests = [t for t in self.requests if t > cutoff]
        if len(self.requests) < self.limit:
            self.requests.append(now)
            return True
        return False