Optional: Leaky bucket limiter
Implement the leaky bucket as a meter: each request adds one unit to a bucket that drains at a constant rate, and a request is rejected when it would overflow.
Objective
Implement a leaky bucket rate limiter that tracks how much capacity has been consumed by requests and gradually releases that capacity at a constant rate. Unlike a token bucket, which accumulates tokens over time, this implementation tracks a bucket level that decreases as time passes. Each request adds one unit if sufficient capacity is available. This exercise is optional extra practice and never blocks completion of Week 1.
By the end:
- Understand the leaky bucket as a meter and how it differs from the token bucket.
- Implement time-based leakage using an injected clock.
- Maintain independent bucket state for each key.
- Protect shared state against concurrent access.
Deliverables
-
Create
backend/internal/ratelimit/leaky_bucket.go.Implement the following constructor and method:
go func NewLeakyBucket(capacity int, leakPerSec float64, now func() time.Time) *LeakyBucket func (lb *LeakyBucket) Allow(key string) bool -
Use the existing
Limiterinterface:go type Limiter interface { Allow(key string) bool } -
The tests are already provided in
backend/internal/ratelimit/leaky_bucket_test.go. Do not create, modify, or edit this test file.
Requirements
Constraints
- Implement the limiter in the existing
ratelimitpackage. - Represent each key's state using a Go struct containing its current bucket level and last update time.
- Use a Go map to maintain independent state for each key.
- Use a mutex to protect shared bucket state during concurrent calls.
- Accept an injected clock through the constructor. If the supplied clock is
nil, fall back totime.Now. - Implement
Allowas a Go method onLeakyBucket. - Ensure
LeakyBucketsatisfies the existing Go interface namedLimiter. - Use
float64for bucket levels and the leakage rate so fractional leakage can be represented. - A new key must start with an empty bucket.
- Clamp the bucket level to zero after leakage. It must never become negative.
- Update the last-seen time on every call, whether the request is accepted or rejected.
- Do not add external dependencies or change existing limiter implementations.
Acceptance criteria
-
Run the provided tests using:
bash cd backend && make test W=1O3 -
TestW1O3_LeakyBucketpasses:- A bucket accepts requests until its capacity is reached.
- Additional requests are rejected when the bucket is full.
- The bucket leaks one unit per second at the configured rate.
- Fractional leakage is handled correctly across half-second intervals.
- A long idle period empties the bucket without allowing its level to become negative.
-
TestW1O3_KeysAreIndependentpasses:- A new key receives its own empty bucket.
- Requests for one key do not consume another key's capacity.
- A full bucket for one key does not prevent another key from accepting requests.
-
The implementation satisfies the
Limiterinterface. -
The implementation is formatted with
gofmtand passes the repository's lint checks.
The reference solution passes every check for this exercise. Try the hints first; reading the answer before you have struggled with the problem teaches much less.
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
NewLeakyBucket(capacity int, leakPerSec float64, now func() time.Time)returns a*LeakyBucketthat satisfiesLimiter.- The level drains lazily from the elapsed time and never goes below zero.
- A request is allowed when level plus one does not exceed the capacity, and only allowed requests raise the level.
- The map is protected by a mutex held for the whole read-modify-write.
make test W=1O3passes.
Commands
cd backend && make test W=1O3Tests
TestW1O3_LeakyBucketinbackend/internal/ratelimit/leaky_bucket_test.goTestW1O3_KeysAreIndependentinbackend/internal/ratelimit/leaky_bucket_test.go