Apply independent quotas to incoming actions during fixed time windows.
Interface
Export create_rate_limiter(limit, window_ms, now=None). It returns allow(key), which returns a dictionary such as {"allowed": true}. A limit of 1 in a 1,000 ms window accepts key a at both 1,999 and 2,000 ms.
Implement the contract in the supplied source file using Python 3. Discuss framework integration in design.md.
Behavior
- [R1] Reject limit and window_ms unless they are positive integers up to 2**53 - 1. Boolean values are invalid. Raise ValueError.
- [R2] Use fixed half-open windows aligned to time zero. The boundary starts a new window.
- [R3] Count accepted calls independently per key. Return {"allowed": true} until its limit and {"allowed": false} for additional calls in that window.
- [R4] Call now for each request. It returns nonnegative, nondecreasing integer milliseconds. Default to the current time in milliseconds when omitted. Keys are nonempty strings. Instances have independent state.
Design and verification
- Place the component in Django services, views and repository boundaries and explain the adapter/core boundary.
- Describe who supplies the key and the clock a