Introducing Ratethrottle
The Swiss Army knife of rate limiting - everything you need in one library.
What is RateThrottle?
An advanced rate limiting library for Python that goes far beyond basic REST API protection.
The problem: Most rate limiting libraries are limited to one framework and only handle basic REST APIs.
The solution: RateThrottle supports everything - multiple frameworks, multiple protocols, advanced features - all in one well-tested package.
π Core Features
1. Universal Framework Support
Works with all major Python web frameworks:
# Flask
@app.route('/api')
@limiter.limit("100/minute")
def api(): return {'data': 'value'}
# FastAPI
@app.get("/api")
async def api(_=Depends(limiter.limit(100, 60))):
return {"data": "value"}
# Django
@django_ratelimit(limit=100, window=60)
def api(request): return JsonResponse({'data': 'value'})
2. Multiple Protocols
Beyond REST - WebSocket, gRPC, GraphQL:
# WebSocket - limit connections & messages
limiter = WebSocketRateLimiter(
WebSocketLimits(
connections_per_minute=10,
messages_per_minute=100
)
)
# gRPC - server interceptors
interceptor = GRPCRateLimitInterceptor(
GRPCLimits(requests_per_minute=100)
)
# GraphQL - complexity & depth analysis
limiter = GraphQLRateLimiter(
GraphQLLimits(
queries_per_minute=100,
max_complexity=1000
)
)
3. Four Rate Limiting Strategies
Choose the algorithm that fits your needs:
- Token Bucket - APIs with bursts
- Leaky Bucket - Smooth traffic
- Fixed Window - Simple use cases
- Sliding Window - Accurate limiting
# Choose your strategy
RateThrottleRule(
name='api',
limit=100,
window=60,
strategy='sliding_window' # or token_bucket, leaky_bucket, fixed_window
)
4. DDoS Protection
Built-in attack detection:
from ratethrottle import DDoSProtection
ddos = DDoSProtection(threshold=1000)
is_attack, pattern = ddos.analyze_traffic(client_ip)
if is_attack:
ddos.block_ip(client_ip)
alert_ops_team(pattern)
Detection:
- Volume-based (request spikes)
- Pattern-based (behavior analysis)
- Distributed (coordinated attacks)
5. Distributed Systems Ready
Redis for multi-server deployments:
# Development: In-memory
storage = InMemoryStorage()
# Production: Redis
storage = RedisStorage('redis://localhost:6379/0')
limiter = RateThrottleCore(storage=storage)
6. Analytics & Monitoring
Track everything:
from ratethrottle import RateThrottleAnalytics
analytics = RateThrottleAnalytics(limiter)
report = analytics.generate_report(last_24_hours)
# Metrics:
# - Total requests: 152,384
# - Blocked: 1,523 (1.0%)
# - Top violators
# - Attack patterns
7. CLI Tools
Manage from command line:
# Monitor real-time
ratethrottle monitor --interval 1
# Test rules
ratethrottle test --rule api --requests 150
# Manage IPs
ratethrottle manage --blacklist-add 192.168.1.50
π― Real-World Use Cases
Public API
@app.route('/api/public')
@limiter.limit("60/minute") # Conservative
def public(): return {'data': 'public'}
WebSocket Chat
limiter = SocketIOLimiter(
WebSocketLimits(messages_per_minute=200)
)
Microservices Gateway
# Different services, different limits
limiter.add_rule(RateThrottleRule(
name='auth_service', limit=1000
))
limiter.add_rule(RateThrottleRule(
name='payment_service', limit=100
))
GraphQL API
# Prevent expensive queries
limiter = GraphQLRateLimiter(
GraphQLLimits(max_complexity=1000)
)
π¦ Installation
# Basic
pip install ratethrottle
# With framework
pip install ratethrottle[flask]
pip install ratethrottle[fastapi]
# With protocols
pip install ratethrottle[grpc]
pip install ratethrottle[graphql]
pip install ratethrottle[websocket]
# Everything
pip install ratethrottle[all]
π Quick Start (2 minutes)
Flask:
from flask import Flask
from ratethrottle import FlaskRateLimiter
app = Flask(__name__)
limiter = FlaskRateLimiter(app)
@app.route('/api')
@limiter.limit("100/minute")
def api():
return {'data': 'protected'}
FastAPI:
from fastapi import FastAPI, Depends
from ratethrottle import FastAPIRateLimiter
app = FastAPI()
limiter = FastAPIRateLimiter()
@app.get("/api")
async def api(_=Depends(limiter.limit(100, 60))):
return {"data": "protected"}
π Resources
π¦ Install: pip install ratethrottle
π Docs: Documentation
β GitHub: GitHub
πΊοΈ What Makes It Complete?
- β Frameworks: Flask, FastAPI, Django, Starlette
- β Protocols:REST, WebSocket, gRPC, GraphQL
- β Strategies: Token Bucket, Leaky Bucket, Fixed Window, Sliding Window
- β Features: DDoS Protection, Analytics, CLI Tools.
- β Storage: In-Memory, Redis
- β Quality: comprehensive docs
π‘ Why Choose RateThrottle?
If you need:
- β Multiple frameworks β RateThrottle
- β WebSocket/gRPC/GraphQL β RateThrottle
- β DDoS protection β RateThrottle
- β Advanced features β RateThrottle
- β Everything in one place β RateThrottle
π Try It Today!
pip install ratethrottle
β Star on GitHub if useful!
Questions? Feature requests? Issues?
Top comments (0)