Rate limiter
Objective: Implement a rate limiter that restricts the number of function executions within a specified time window.
Instructions:
- Create a function named
rateLimiter
that accepts two arguments:
- A function to limit.
- The maximum number of times the function can be executed within a specified time window (e.g., 5 times per 10 seconds).
- Ensure that after reaching the limit, the function is blocked from executing until the time window resets.
Example Usage:
const limitedFunction = rateLimiter(apiCall, 5, 10000); // 5 calls per 10 seconds
limitedFunction(); // Executes the first call
limitedFunction(); // Executes the second call
// After 5 calls in 10 seconds, the function will be blocked until the next window starts.
Requirements:
- The
rateLimiter
should limit how many times the function can be executed within a specific time window.
- Once the limit is reached, further function calls should be blocked until the time window resets.
- The time window should reset after a specified duration (e.g., every 10 seconds).
Hints:
- Use timestamps to track when the time window starts and the number of function calls.
- Use a counter to track how many times the function has been executed within the time window.
Bonus:
- Add the ability to reset the limiter manually.
- Add key for each action, so different actions can be rate limited separately