Exercise 1: A Small Bucket With a Fast Refill Rate — Possible Solution ==================================================================== THE SETUP ------------------------------ bucket = TokenBucket(capacity=5, refill_rate_per_sec=10) A smaller bucket (5 tokens) but a much faster refill rate (10/sec) than this chapter's own original example (10 capacity, 2/sec refill). RESULTS ------------------------------ first 5 immediate requests: [True, True, True, True, True] second batch after 0.3s wait: [True, True, True, False, False] second batch allowed count: 3 expected refill: 0.3s * 10/sec = 3 tokens All 5 immediate requests succeeded, correctly draining the bucket to 0 tokens. After waiting 0.3 seconds, 3 of the next 5 requests succeeded, and 2 were rejected. WHY EXACTLY 3, USING THIS CHAPTER'S OWN REFILL FORMULA ------------------------------ This chapter's own _refill() logic computes `tokens + elapsed_seconds * refill_rate_per_sec`. With the bucket starting at 0 tokens after the first batch, and 0.3 seconds elapsing at a refill rate of 10 tokens/sec: 0.3 * 10 = 3.0 tokens refilled exactly. The next three allow_request() calls each correctly consume one of those 3 refilled tokens (succeeding), and the fourth call finds the bucket back at 0 (rejecting), matching the observed [True, True, True, False, False] pattern precisely. WHY THIS CONFIRMS THE REFILL MATH IS EXACT, NOT APPROXIMATE ------------------------------ This chapter's own original example only verified a qualitative claim ("waiting refills roughly 1 token, allowing one more request"). This exercise verifies the refill amount PRECISELY predicts the number of additional successful requests - 0.3 seconds at 10/sec produces exactly 3 tokens, and exactly 3 (not 2 or 4) requests succeed, confirming the formula's own elapsed-time * rate calculation is being applied correctly rather than just roughly in the right direction. WHY THIS WORKS AS AN ANSWER ------------------------------ The new bucket parameters are chosen specifically to make the refill math easy to verify precisely (0.3 * 10 = a clean 3), the actual result is checked against that exact predicted number rather than a vague expectation, and the underlying formula is traced through directly to explain why 3 (not some other number) is the correct result.