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 Limiter interface:

    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 ratelimit package.
  • 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 to time.Now.
  • Implement Allow as a Go method on LeakyBucket.
  • Ensure LeakyBucket satisfies the existing Go interface named Limiter.
  • Use float64 for 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_LeakyBucket passes:

    • 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_KeysAreIndependent passes:

    • 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 Limiter interface.

  • The implementation is formatted with gofmt and passes the repository's lint checks.

Finished Exercise 1.28?

Implementation

Run locally

Write the code in your editor inside zomato-prep, then work through this list.

  1. NewLeakyBucket(capacity int, leakPerSec float64, now func() time.Time) returns a *LeakyBucket that satisfies Limiter.
  2. The level drains lazily from the elapsed time and never goes below zero.
  3. A request is allowed when level plus one does not exceed the capacity, and only allowed requests raise the level.
  4. The map is protected by a mutex held for the whole read-modify-write.
  5. make test W=1O3 passes.

Commands

terminal
cd backend && make test W=1O3

Tests

  • TestW1O3_LeakyBucket in backend/internal/ratelimit/leaky_bucket_test.go
  • TestW1O3_KeysAreIndependent in backend/internal/ratelimit/leaky_bucket_test.go