Optional: Sliding window counter limiter

Implement a limiter that keeps two counters per key, the current and previous fixed windows, and estimates the sliding count by weighting the previous window by how much of it still overlaps.

Objective

This optional exercise introduces the sliding-window counter, which combines two fixed windows to estimate how many requests occurred during a moving time interval. You will implement a limiter that weights requests from the previous window according to their remaining overlap with the current interval. This is extra practice after the required Week 1 exercises and never blocks completion of the week.

By the end:

  • Understand how a sliding-window counter approximates request counts across window boundaries.
  • Maintain current and previous window counters independently for each key.
  • Use a mutex to protect shared state during concurrent requests.
  • Use an injected clock to make time-dependent behaviour deterministic in tests.
  • Store per-key state using Go maps and Go structs.
  • Implement limiter behaviour using Go methods and receivers and the existing Go interface.

Deliverables

  • Create backend/internal/ratelimit/sliding_window_counter.go with the following exported API:
go
func NewSlidingWindowCounter(limit int, size time.Duration, now func() time.Time) *SlidingWindowCounter

func (sw *SlidingWindowCounter) Allow(key string) bool

The implementation must satisfy the existing Limiter interface.

  • The test file backend/internal/ratelimit/sliding_window_counter_test.go is already provided. Run its tests without modifying the file.

Requirements

Constraints

  • Maintain two counters per key: the current window count and the previous window count.
  • Store the start time of the current fixed window for each key.
  • Use the injected clock function for all time reads. If it is nil, default to time.Now from the Go time package.
  • Use the existing fakeClock from limiter_test.go. Do not redeclare it.
  • Protect the state map with a mutex.
  • When a key is first encountered, initialize its state with zero counters.
  • When the current window has not changed, retain both counters.
  • When exactly one window has elapsed, move the current count into the previous count and reset the current count to zero.
  • When more than one window has elapsed, discard both old counts.
  • Calculate the estimated request count by weighting the previous window according to the fraction of that window that overlaps the current sliding interval.
  • Allow a request only when the estimated count plus one does not exceed the configured limit.
  • Increment the current counter only when a request is allowed.
  • Keep state independent between different keys.
  • Do not edit any provided test files or existing limiter implementations.

Acceptance criteria

  • TestW1O2_SlidingWindowCounterLimit: Enforces the configured request limit within a window.
  • TestW1O2_PreviousWindowIsWeightedByOverlap: Weights the previous window's count according to its remaining overlap. Five seconds into a ten-second window, ten previous requests contribute an estimated count of five, allowing exactly five additional requests under a limit of ten.
  • TestW1O2_IdleWindowsAreForgotten: Discards previous counts after more than one complete window has elapsed.
  • TestW1O2_KeysAreIndependent: Maintains independent request counts for different keys.
  • The implementation satisfies the Limiter interface and passes formatting and lint checks.

Run the provided tests:

bash
cd backend && make test W=1O2
Finished Exercise 1.27?

Implementation

Run locally

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

  1. NewSlidingWindowCounter(limit int, size time.Duration, now func() time.Time) returns a *SlidingWindowCounter that satisfies Limiter.
  2. Each key stores the start of the current window, its count and the previous window count.
  3. The estimate is previous x (1 - elapsed/size) + current, and a request is allowed when the estimate plus one does not exceed the limit.
  4. A previous window that is more than one window old counts as zero.
  5. make test W=1O2 passes.

Commands

terminal
cd backend && make test W=1O2

Tests

  • TestW1O2_SlidingWindowCounterLimit in backend/internal/ratelimit/sliding_window_counter_test.go
  • TestW1O2_PreviousWindowIsWeightedByOverlap in backend/internal/ratelimit/sliding_window_counter_test.go
  • TestW1O2_IdleWindowsAreForgotten in backend/internal/ratelimit/sliding_window_counter_test.go
  • TestW1O2_KeysAreIndependent in backend/internal/ratelimit/sliding_window_counter_test.go