Rate limiting
The idea of a leaky-bucket rate limiter is as follows. There is a bucket with a limited capacity for water, and each request adds some water to the bucket.
If a request would have overflowed the bucket then it is rejected instead. The capacity of the bucket is written LC.
The bucket leaks its contents at a particular rate, r, freeing up capacity for further requests.
As a function of time the level L of water in the bucket follows a path like this as requests arrive.
It is possible to simulate the leak of water from the bucket with a simple loop that subtracts water from the bucket on each iteration, or else on each request, the new level L’ of water in the bucket can be calculated as follows.
It follows that the amount of water that can be added to the bucket in a time interval Δt is at most rΔt + LC.