Limiter interface and a FixedWindow implementation

Add a second Limiter, a mutex-protected fixed window counter, and see the window boundary behaviour that the token bucket does not have.

Objective

Add a second implementation of the Limiter interface from exercise 1.11: a per-key fixed-window counter. Unlike the token bucket, this limiter counts requests within fixed time intervals and resets a key's count when a new interval begins.

By the end:

  • FixedWindow implements the existing Limiter interface.
  • Each key has an independent request count and window start time.
  • Requests beyond the configured limit are rejected until the next window.
  • The implementation is safe for concurrent access.
  • Both FixedWindow and TokenBucket are checked against the interface at compile time.

Deliverables

  • backend/internal/ratelimit/fixed_window.go: Replace the temporary stub from exercise 1.11 with the real implementation.

    Define a window record using a struct to store the window start time and request count. Define FixedWindow with a sync.Mutex, a map of per-key window records, the request limit, the window size, and an injected clock.

    Implement the constructor:

    go
    func NewFixedWindow(limit int, size time.Duration, now func() time.Time) *FixedWindow

    A nil clock must default to time.Now.

    Implement the admission method:

    go
    func (fw *FixedWindow) Allow(key string) bool

    Add a compile-time interface assertion:

    go
    var _ Limiter = (*FixedWindow)(nil)
  • backend/internal/ratelimit/token_bucket.go: Keep the token bucket implementation from exercise 1.11. Add a compile-time assertion to verify that it also implements Limiter:

    go
    var _ Limiter = (*TokenBucket)(nil)
  • backend/internal/ratelimit/limiter.go: Keep the existing Limiter interface unchanged.

    go
    type Limiter interface {
        Allow(key string) bool
    }
  • backend/internal/ratelimit/limiter_test.go: The provided test file already contains TestW1B3_FixedWindowBoundary. Do not rewrite it.

    The test uses a fake clock with a limit of 2 and a window size of one second. The first two calls for key u return true, the third returns false, and the first call after advancing the clock by one second returns true because a new window has started.

Requirements

Constraints

  • Implement FixedWindow as another implementation of the existing Limiter interface. Review the interface and method concepts from lessons 1.7 and 1.6.
  • Store a window start time and request count for each key.
  • Protect the map and all per-key window state with a mutex.
  • The mutex must cover the entire read-modify-write operation for a key: lookup, window comparison, reset, limit check, and increment.
  • Calculate the current window start using now.Truncate(size), where now comes only from the injected clock.
  • Read time only through the now field. Call time.Now only when selecting the default clock in the constructor.
  • When a key has no record, or its stored window start is older than the current window start, begin a fresh record with count zero.
  • Reject a request when the count is already at the configured limit. Otherwise, increment the count and allow the request.
  • Keep counts independent across keys.
  • Do not carry unused requests or counts into the next window.
  • Remove the temporary FixedWindow stub from exercise 1.11. The fixed_window.go file must now contain the real implementation, not both the stub and the real type.
  • Keep limiter.go unchanged.
  • Do not introduce package-level mutable state or global variables.
  • Use the existing fake clock and tests. Do not redeclare test helpers or test functions.
  • Every access to the map must happen while holding the lock. In exercise 1.15, the limiter will be called from many goroutines under the race detector. An unsynchronized map access can cause a data race or a concurrent map access panic.

Acceptance criteria

Run the tests from backend/:

bash
make test W=1B3

For verbose output, run:

bash
make test W=1B3 V=1

You can also run both the token bucket and fixed-window tests directly:

bash
go test -race -count=1 -run "TestW1B2|TestW1B3" ./internal/ratelimit/

The implementation must satisfy these behaviors:

  • Limit within one window: With a limit of 3 and a window size of 10 seconds, three calls for the same key at the same instant return true, true, and true. The fourth returns false.
  • Reset at the next window: At the first instant of the next 10-second window, the same key can make another request, which returns true.
  • Independent keys: Two different keys each have their own count. Reaching the limit for one key must not prevent requests for another.
  • No carryover: If a key uses only part of its allowance in one window, the unused requests do not carry into the next window. The new window starts with a fresh count of zero.
  • Boundary behavior: A fixed window can allow up to twice the configured limit across a boundary: requests can use the remaining allowance just before the boundary and the full allowance immediately after it.
  • Interface compatibility: Both FixedWindow and TokenBucket satisfy Limiter, verified by compile-time assertions.
  • Package tests compile: The real FixedWindow replaces the temporary stub, so make test W=1B2 and make test W=1B3 both run without the stub.
Finished Exercise 1.14?

Implementation

Run locally

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

  1. NewFixedWindow(limit int, size time.Duration, now func() time.Time) returns a *FixedWindow that satisfies Limiter.
  2. Within one window at most limit calls per key are allowed.
  3. The window start is the injected clock truncated to the window size, so a new window resets the count.
  4. make test W=1B3 passes.

Commands

terminal
cd backend && make test W=1B3

Tests

  • TestW1B3_FixedWindowBoundary in backend/internal/ratelimit/limiter_test.go