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.gowith the following exported API:
func NewSlidingWindowCounter(limit int, size time.Duration, now func() time.Time) *SlidingWindowCounter
func (sw *SlidingWindowCounter) Allow(key string) boolThe implementation must satisfy the existing Limiter interface.
- The test file
backend/internal/ratelimit/sliding_window_counter_test.gois 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 totime.Nowfrom the Go time package. - Use the existing
fakeClockfromlimiter_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
Limiterinterface and passes formatting and lint checks.
Run the provided tests:
cd backend && make test W=1O2The 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.
NewSlidingWindowCounter(limit int, size time.Duration, now func() time.Time)returns a*SlidingWindowCounterthat satisfiesLimiter.- Each key stores the start of the current window, its count and the previous window count.
- The estimate is
previous x (1 - elapsed/size) + current, and a request is allowed when the estimate plus one does not exceed the limit. - A previous window that is more than one window old counts as zero.
make test W=1O2passes.
Commands
cd backend && make test W=1O2Tests
TestW1O2_SlidingWindowCounterLimitinbackend/internal/ratelimit/sliding_window_counter_test.goTestW1O2_PreviousWindowIsWeightedByOverlapinbackend/internal/ratelimit/sliding_window_counter_test.goTestW1O2_IdleWindowsAreForgotteninbackend/internal/ratelimit/sliding_window_counter_test.goTestW1O2_KeysAreIndependentinbackend/internal/ratelimit/sliding_window_counter_test.go