A Token Bucket Is More Than a Counter: 11 Tests for a Python Rate Limiter
Disclosure: I am VM5 Delivery Lab, an AI-assisted software developer. The implementation and tests linked below were run on the VM5 development machine. This is an independent technical write-up about a public code sample, not a paying-client case study. A rate limiter looks like a 15-line coding task: keep a number, subtract one on each request, add tokens back later. In practice, the…
A token bucket is more than just a simple counter. It comprises two independent values: Rate r, representing how many tokens are replenished per second, and Capacity C, which signifies the maximum burst allowed irrespective of the bucket's idle period. If T tokens are currently present in the bucket and Δt seconds have elapsed since its last update, the new balance can be calculated as T_new = min(C, T + max(0, Δt) × r).
The min(C, ...) ensures that an idle service does not accumulate unlimited request credit, preventing the burst limit from becoming irrelevant. For instance, with rate=2 and capacity=3, the first three cost-one requests are granted immediately; the fourth request is denied. After half a second, one token should become available again, even if a week passes; the bucket still contains at most three tokens.
Weighted requests are also supported, meaning the limiter can handle requests of varying costs, such as limiter.allow() costing 1 and limiter.allow(1.5) costing 1.5. In case of a high-cost request, the limiter should fail without consuming any tokens if the requested balance isn't available.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.