Optional: Sliding window log limiter

Implement a limiter that records one timestamp per allowed request and counts the ones inside the last window, so it has no boundary burst.

Objective

Implement an optional sliding-window log rate limiter that tracks the timestamp of every allowed request for each key. Unlike the fixed-window counter, which can permit a burst across a window boundary, this algorithm checks the exact age of previous requests before deciding whether another request is allowed. This exercise is optional extra practice and never blocks completion of Week 1.

By the end:

  • Understand how a sliding window log enforces a request limit over a continuously moving time interval.
  • Implement per-key request history using Go maps and Go slices.
  • Use Go structs and Go methods and receivers to implement the limiter.
  • Protect shared request history with a mutex so concurrent calls cannot exceed the limit.
  • Use an injected clock to make time-dependent behaviour deterministic in tests.
  • Implement the existing Go interfaces contract through the Limiter interface.

Deliverables

  • backend/internal/ratelimit/sliding_window_log.go

    Create the SlidingWindowLog implementation with these exact function signatures:

    go
    func NewSlidingWindowLog(limit int, size time.Duration, now func() time.Time) *SlidingWindowLog
    
    func (sw *SlidingWindowLog) Allow(key string) bool

    The implementation must satisfy the existing Limiter interface.

Requirements

Constraints

  • Keep the implementation in the existing ratelimit package.
  • Use a mutex to protect the request history map and all operations that modify or inspect it.
  • Maintain independent request histories for different keys.
  • Record one timestamp for every allowed request.
  • Remove expired timestamps before checking the limit. A timestamp at or before now - size is expired. Only timestamps strictly newer than this cutoff remain in the window.
  • Reject a request when the number of remaining timestamps is greater than or equal to limit.
  • Append the current timestamp only when a request is allowed.
  • Do not record rejected requests.
  • Use the injected clock for all time reads. When now is nil, default to time.Now.
  • Do not modify the existing Limiter interface, shared fake clock, or provided test files.
  • The tests are already provided. Run them without editing them.
  • Create only backend/internal/ratelimit/sliding_window_log.go.

Acceptance criteria

  • TestW1O1_SlidingWindowLogLimit: allows requests up to the configured limit and rejects subsequent requests within the window.
  • TestW1O1_OldRequestsLeaveTheWindowOneByOne: expires individual requests as they leave the sliding window rather than resetting the entire history.
  • TestW1O1_RejectedRequestsAreNotRecorded: rejected requests do not extend the time before another request can be accepted.
  • TestW1O1_NoBoundaryBurst: does not allow a burst across a fixed-window boundary when previous requests are still inside the sliding window.
  • TestW1O1_KeysAreIndependent: maintains separate limits for different keys.
  • The implementation satisfies the Limiter interface and is safe for concurrent calls.
  • The implementation passes the provided tests and lint checks.

Run the provided tests from the backend directory:

bash
cd backend && make test W=1O1
Finished Exercise 1.26?

Implementation

Run locally

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

  1. NewSlidingWindowLog(limit int, size time.Duration, now func() time.Time) returns a *SlidingWindowLog that satisfies Limiter.
  2. Each key keeps the timestamps of its allowed requests, and entries at least one window old are removed before the check.
  3. A rejected request is not recorded.
  4. The map and slices are protected by a mutex held for the whole read-modify-write.
  5. make test W=1O1 passes.

Commands

terminal
cd backend && make test W=1O1

Tests

  • TestW1O1_SlidingWindowLogLimit in backend/internal/ratelimit/sliding_window_log_test.go
  • TestW1O1_OldRequestsLeaveTheWindowOneByOne in backend/internal/ratelimit/sliding_window_log_test.go
  • TestW1O1_RejectedRequestsAreNotRecorded in backend/internal/ratelimit/sliding_window_log_test.go
  • TestW1O1_NoBoundaryBurst in backend/internal/ratelimit/sliding_window_log_test.go
  • TestW1O1_KeysAreIndependent in backend/internal/ratelimit/sliding_window_log_test.go