Code RoomRate limiter allows burst
MediumPrep Room Coding #2203

Rate limiter allows burst

Code reviewConcurrencyMid–Senior~24 min

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.

0:00 of about 24 min
Mark a line and say what kind of problem it is.0 findings
1import time, threading
2 
3class RateLimiter:
4 def __init__(self, capacity, refill_per_sec):
5 self.capacity = capacity
6 self.tokens = capacity
7 self.rate = refill_per_sec
8 self.updated = time.monotonic()
9 self.lock = threading.Lock()
10 
11 def allow(self):
12 now = time.monotonic()
13 self.tokens = min(self.capacity, self.tokens + (now - self.updated) * self.rate)
14 self.updated = now
15 if self.tokens >= 1:
16 self.tokens -= 1
17 return True
18 return False
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.