Code Room
Code reviewMediumcr-g579
Subject Concurrency missing lock compoundLevel Mid–Senior~24 minCommon in Concurrency interviewsIndustries Software development

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.

Talk through your review
Code to reviewpython
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 False
Run or narrate your approach, then ask the coach.