Migrating from slowapi¶
Overview¶
drogue is a drop-in replacement for slowapi with additional features like DDoS detection, WebSocket support, and trust caching.
Before (slowapi)¶
from slowapi import Limiter
from slowapi.util import get_remote_address
from starlette.requests import Request
limiter = Limiter(key_func=get_remote_address)
@app.get("/api/data")
@limiter.limit("10/minute")
async def get_data(request: Request): # polluted signature
return {"data": "value"}
After (drogue)¶
from drogue.adapters.fastapi import DrogueLimiter
limiter = DrogueLimiter(app)
@app.get("/api/data")
@limiter.limit("10/minute")
async def get_data(): # clean signature
return {"data": "value"}
Migration Checklist¶
- Replace imports:
# Before
from slowapi import Limiter
from slowapi.util import get_remote_address
# After
from drogue.adapters.fastapi import DrogueLimiter
- Replace limiter initialization:
- Remove
request: Requestfrom decorated endpoints:
# Before
@app.get("/api/data")
@limiter.limit("10/minute")
async def get_data(request: Request):
return {"data": "value"}
# After
@app.get("/api/data")
@limiter.limit("10/minute")
async def get_data():
return {"data": "value"}
- Rate limit headers work the same:
Both libraries inject the same headers:
X-RateLimit-LimitX-RateLimit-RemainingX-RateLimit-Reset-
Retry-After -
Redis configuration:
# Before (slowapi)
from slowapi.backends.redis import RedisBackend
limiter = Limiter(key_func=get_remote_address, backend=RedisBackend("redis://localhost:6379"))
# After (drogue)
from drogue.core.storage.redis import RedisStorage
storage = RedisStorage(url="redis://localhost:6379")
limiter = DrogueLimiter(app, storage=storage)
Key Differences¶
| Aspect | slowapi | drogue |
|---|---|---|
| Request parameter | Required | Not needed |
| WebSocket | Not supported | Supported |
| DDoS detection | Not available | Z-score + streaming |
| Trust caching | Not available | 9x throughput |
| Algorithms | Token Bucket | Token Bucket, Sliding Window, Fixed Window |
| Storage | Redis, Memcached | Memory, Redis, Count-Min Sketch |
Additional Features¶
Once migrated, you gain access to:
- DDoS detection -- Enable with
ddos_enabled=True - Trust caching -- Automatic, no configuration needed
- Probe detection -- Enable with probe detector
- Defense randomization -- Enable with randomizer
- Shadow mode -- Test rules without enforcing
- CIDR filtering -- Block/allow IP ranges
- Adaptive limits -- System-aware throttling
Rollback¶
If you need to rollback to slowapi:
- Revert imports
- Revert limiter initialization
- Add
request: Requestback to endpoints - Revert Redis configuration