How shahzaibyy/fastapi-rate-limitter-middleware works
Project type and purpose
• This is an open-source Python middleware library (“fastapi-rate-limit”) designed to plug into FastAPI applications and enforce request rate limits.
• Its core function is to intercept incoming HTTP requests, apply user-defined rate-limit rules (path patterns, request counts, time periods), track usage in a storage backend (in-memory or Redis), and then either forward the request or emit a “rate limit exceeded” response with informative headers.
File structure
• Top-level files: setup.py, pyproject.toml, requirements.txt, LICENSE, README.md – standard Python packaging boilerplate.
• Package directory (fastapi_rate_limit/):
• __init__.py – package metadata and public API
• middleware.py – core middleware implementation, rule parsing, storage backend abstraction, request interception
• examples/example_app.py – a sample FastAPI application showcasing integration points.
• tests/ – unit tests validating middleware behavior and storage backends.
• CI workflow (.github/workflows/python-package.yml) – build/test pipeline.
README for additional insights
• Shows how users configure the middleware (passing Rule objects) and add it via app.add_middleware(...).
• Mentions multiple storage backends (in-memory, Redis), custom exception responses, and response headers carrying rate-limit stats.
a. Main components
• Clients (browser, mobile app, API consumer)
• FastAPI Application
• RateLimitMiddleware
• Rule Engine (configuration module inside middleware)
• Storage Backends
• In-Memory Store (built into library)
• Redis Server (external service)
• Endpoint Handlers (user’s route functions)
• Exception Handler (middleware for rate-limit errors)
b. Relationships and interactions
1. Client → HTTP request → FastAPI stack
2. FastAPI invokes RateLimitMiddleware on each request
3. Middleware extracts path, matches against configured Rules
4. Middleware calls Storage Backend API to increment/check request counters
5. Storage returns current count and TTL; middleware decides “allow” or “reject”
6. If allowed, middleware forwards request to endpoint handler; handler returns response
7. Middleware attaches rate-limit headers to the response
8. If rejected, middleware invokes the exception handler to return a 429 response
c. Architectural patterns and design principles
• Middleware pipeline: standard WSGI/ASGI interception pattern
• Strategy pattern for storage backends (in-memory vs. Redis)
• Rule configuration as a pluggable policy object
• Clear separation of concerns: request interception, rule evaluation, counter storage, response augmentation
d. Key technologies and libraries
• FastAPI (ASGI web framework)
• Starlette middleware interface (base class for RateLimitMiddleware)
• Redis (via aioredis or similar) for distributed rate limiting
• Python typing and dataclasses for Rule definitions
Detailing essential architectural elements
• Inside RateLimitMiddleware, decompose into subcomponents:
• Path matching
• Rule lookup
• Storage adapter interface
• Exception factory
• Header injector
• Conditional flow: “If count > limit → Exception path” vs. “Else → Forward request.”