Code Room
Code reviewMedium
Question
Review this Python token-bucket rate limiter used across threads.
What a strong answer looks like
Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix — specific and kind.
Learn the concepts
import time, threading class RateLimiter: def __init__(self, capacity, refill_per_sec): self.capacity = capacity self.tokens = capacity self.rate = refill_per_sec self.updated = time.monotonic() self.lock = threading.Lock() def allow(self): now = time.monotonic() self.tokens = min(self.capacity, self.tokens + (now - self.updated) * self.rate) self.updated = now if self.tokens >= 1: self.tokens -= 1 return True return FalseRun or narrate your approach, then ask the coach.