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
Limiterinterface.
Deliverables
-
backend/internal/ratelimit/sliding_window_log.goCreate the
SlidingWindowLogimplementation with these exact function signatures:go func NewSlidingWindowLog(limit int, size time.Duration, now func() time.Time) *SlidingWindowLog func (sw *SlidingWindowLog) Allow(key string) boolThe implementation must satisfy the existing
Limiterinterface.
Requirements
Constraints
- Keep the implementation in the existing
ratelimitpackage. - 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 - sizeis 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
nowisnil, default totime.Now. - Do not modify the existing
Limiterinterface, 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
Limiterinterface and is safe for concurrent calls. - The implementation passes the provided tests and lint checks.
Run the provided tests from the backend directory:
cd backend && make test W=1O1The 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.
NewSlidingWindowLog(limit int, size time.Duration, now func() time.Time)returns a*SlidingWindowLogthat satisfiesLimiter.- Each key keeps the timestamps of its allowed requests, and entries at least one window old are removed before the check.
- A rejected request is not recorded.
- The map and slices are protected by a mutex held for the whole read-modify-write.
make test W=1O1passes.
Commands
cd backend && make test W=1O1Tests
TestW1O1_SlidingWindowLogLimitinbackend/internal/ratelimit/sliding_window_log_test.goTestW1O1_OldRequestsLeaveTheWindowOneByOneinbackend/internal/ratelimit/sliding_window_log_test.goTestW1O1_RejectedRequestsAreNotRecordedinbackend/internal/ratelimit/sliding_window_log_test.goTestW1O1_NoBoundaryBurstinbackend/internal/ratelimit/sliding_window_log_test.goTestW1O1_KeysAreIndependentinbackend/internal/ratelimit/sliding_window_log_test.go