style: clean up unused parameters and imports in examples
This commit is contained in:
@@ -10,8 +10,8 @@ from fastapi.responses import JSONResponse
|
||||
from fastapi_traffic import (
|
||||
Algorithm,
|
||||
MemoryBackend,
|
||||
RateLimitExceeded,
|
||||
RateLimiter,
|
||||
RateLimitExceeded,
|
||||
rate_limit,
|
||||
)
|
||||
from fastapi_traffic.core.limiter import set_limiter
|
||||
@@ -21,7 +21,7 @@ limiter = RateLimiter(backend)
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
async def lifespan(_: FastAPI):
|
||||
await limiter.initialize()
|
||||
set_limiter(limiter)
|
||||
yield
|
||||
@@ -32,7 +32,7 @@ app = FastAPI(title="Rate Limiting Algorithms", lifespan=lifespan)
|
||||
|
||||
|
||||
@app.exception_handler(RateLimitExceeded)
|
||||
async def rate_limit_handler(request: Request, exc: RateLimitExceeded) -> JSONResponse:
|
||||
async def rate_limit_handler(_: Request, exc: RateLimitExceeded) -> JSONResponse:
|
||||
return JSONResponse(
|
||||
status_code=429,
|
||||
content={
|
||||
@@ -53,9 +53,12 @@ async def rate_limit_handler(request: Request, exc: RateLimitExceeded) -> JSONRe
|
||||
window_size=60,
|
||||
algorithm=Algorithm.FIXED_WINDOW,
|
||||
)
|
||||
async def fixed_window(request: Request) -> dict[str, str]:
|
||||
async def fixed_window(_: Request) -> dict[str, str]:
|
||||
"""Fixed window resets counter at fixed time intervals."""
|
||||
return {"algorithm": "fixed_window", "description": "Counter resets every 60 seconds"}
|
||||
return {
|
||||
"algorithm": "fixed_window",
|
||||
"description": "Counter resets every 60 seconds",
|
||||
}
|
||||
|
||||
|
||||
# 2. Sliding Window Log - Most precise
|
||||
@@ -67,9 +70,12 @@ async def fixed_window(request: Request) -> dict[str, str]:
|
||||
window_size=60,
|
||||
algorithm=Algorithm.SLIDING_WINDOW,
|
||||
)
|
||||
async def sliding_window(request: Request) -> dict[str, str]:
|
||||
async def sliding_window(_: Request) -> dict[str, str]:
|
||||
"""Sliding window tracks exact timestamps for precise limiting."""
|
||||
return {"algorithm": "sliding_window", "description": "Precise tracking with timestamp log"}
|
||||
return {
|
||||
"algorithm": "sliding_window",
|
||||
"description": "Precise tracking with timestamp log",
|
||||
}
|
||||
|
||||
|
||||
# 3. Sliding Window Counter - Balance of precision and efficiency
|
||||
@@ -81,9 +87,12 @@ async def sliding_window(request: Request) -> dict[str, str]:
|
||||
window_size=60,
|
||||
algorithm=Algorithm.SLIDING_WINDOW_COUNTER,
|
||||
)
|
||||
async def sliding_window_counter(request: Request) -> dict[str, str]:
|
||||
async def sliding_window_counter(_: Request) -> dict[str, str]:
|
||||
"""Sliding window counter uses weighted counts from current and previous windows."""
|
||||
return {"algorithm": "sliding_window_counter", "description": "Efficient approximation"}
|
||||
return {
|
||||
"algorithm": "sliding_window_counter",
|
||||
"description": "Efficient approximation",
|
||||
}
|
||||
|
||||
|
||||
# 4. Token Bucket - Allows controlled bursts
|
||||
@@ -96,7 +105,7 @@ async def sliding_window_counter(request: Request) -> dict[str, str]:
|
||||
algorithm=Algorithm.TOKEN_BUCKET,
|
||||
burst_size=5, # Allow bursts of up to 5 requests
|
||||
)
|
||||
async def token_bucket(request: Request) -> dict[str, str]:
|
||||
async def token_bucket(_: Request) -> dict[str, str]:
|
||||
"""Token bucket allows bursts up to burst_size, then refills gradually."""
|
||||
return {"algorithm": "token_bucket", "description": "Allows controlled bursts"}
|
||||
|
||||
@@ -111,7 +120,7 @@ async def token_bucket(request: Request) -> dict[str, str]:
|
||||
algorithm=Algorithm.LEAKY_BUCKET,
|
||||
burst_size=5, # Queue capacity
|
||||
)
|
||||
async def leaky_bucket(request: Request) -> dict[str, str]:
|
||||
async def leaky_bucket(_: Request) -> dict[str, str]:
|
||||
"""Leaky bucket smooths traffic to a constant rate."""
|
||||
return {"algorithm": "leaky_bucket", "description": "Constant output rate"}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user