Deviopath › TypeScript

Sliding-Window Rate Limiter

Medium · Classes

Implement RateLimiter with allow(user, time): boolean. Times are in milliseconds and never decrease.

A request at time is allowed when the same user has FEWER than limit allowed requests with a time in the window (time - windowMs, time] (the left end is excluded). Denied requests do not count. Each user has their own window.

The driver rateLimit(limit, windowMs, requests) is already written and returns the decision for every request.

Next: Stable Priority Queue