A small Python implementation of an in-memory token bucket. It refills tokens from elapsed time, caps the bucket at its configured capacity, and uses a lock around each request check.
python rate_limiter.pyThe script creates a bucket with capacity 5 and refill rate 2 tokens per second, then simulates eight requests. allow_request() returns True when a token is available and False when the bucket is empty.
This is a single-process demonstration, not an HTTP service or a DDoS defense. The token state is held in memory and is not shared across processes or machines. The implementation uses time.time(), so system clock adjustments can affect refill calculations; a production implementation should use a monotonic clock and define validation, client scoping, and distributed-state behavior.