How yawpremuh/api-rate-limiter works
This is a FastAPI service that enforces a per-client sliding-window quota. Requests are keyed by API key, or by client IP when no key is supplied; the middleware asks the limiter for a decision, dispatches allowed requests to the endpoint, and adds rate-limit headers or returns HTTP 429. The in-memory limiter keeps nanosecond timestamp logs under a lock. An optional async simulator generates concurrent traffic. The runtime flow is supported by the sampled source; other README capabilities are not separate runtime subsystems.
Main entry points: main.py Rate-limit middleware · rate_limiter.py Sliding-window check · simulate_requests.py Async traffic simulator
Components 9
API service
- Rate-limit middleware
main.py - Client identification
main.py - Test endpoint
main.py
Rate limiter
- Sliding-window check
rate_limiter.py - Timestamp logs
rate_limiter.py - Thread lock
rate_limiter.py
Traffic client
- Async traffic simulator
simulate_requests.py
Other components
- API caller
- Running API service
Connections 10
- API caller → Rate-limit middleware: sends request
- Rate-limit middleware → Client identification: derives key
- Rate-limit middleware → Sliding-window check: checks quota
- Sliding-window check → Timestamp logs: prunes and records
- Sliding-window check → Thread lock: locks access
- Sliding-window check → Rate-limit middleware: returns decision
- Rate-limit middleware → Test endpoint: dispatches if allowed
- Rate-limit middleware → API caller: returns status and headers
- Async traffic simulator → Running API service: sends concurrent requests
- Running API service → Async traffic simulator: returns statuses