Requests the Token Bucket Admits
easyThe gateway token bucket is the first thing a request meets, and it is one comparison under the surface. The bucket starts holding burst tokens, refills at rate tokens per second, and admits a request by spending its cost. After elapsed seconds of refill, the tokens available are burst + rate x elapsed: the reservation is granted instantly against those tokens, and the count is reconciled after. The request total a burst lets through is exactly min(requested, burst + rate x elapsed), which is why a bucket absorbs a short burst completely and then admits at a steady rate when demand outlasts the cushion. Interviewers ask for this arithmetic plus the follow-up: where does the bucket live, and the answer to name is a shared in-memory store such as Redis, because limiter state must be atomic across concurrent gateway instances. Implement tokens_admitted(rate, burst, elapsed, requested): return the number of requests the bucket admits, with burst the starting tokens, rate the refill per second, elapsed the seconds that pass, and requested the arrivals. It is min(requested, burst + rate x elapsed), floored, because a request costs a whole token.
Run your code to see output