release: bump version to 0.3.0
- Refactor Redis backend connection handling and pool management - Update algorithm implementations with improved type annotations - Enhance config loader validation with stricter Pydantic schemas - Improve decorator and middleware error handling - Expand example scripts with better docstrings and usage patterns - Add new 00_basic_usage.py example for quick start - Reorganize examples directory structure - Fix type annotation inconsistencies across core modules - Update dependencies in pyproject.toml
This commit is contained in:
@@ -29,13 +29,18 @@ from fastapi_traffic import (
|
||||
from fastapi_traffic.backends.redis import RedisBackend
|
||||
from fastapi_traffic.core.limiter import set_limiter
|
||||
|
||||
DEFAULT_HOST = "127.0.0.1"
|
||||
DEFAULT_PORT = 8001
|
||||
|
||||
|
||||
async def create_redis_backend():
|
||||
"""Create Redis backend with fallback to memory."""
|
||||
try:
|
||||
from fastapi_traffic import RedisBackend
|
||||
|
||||
redis_url = os.getenv("REDIS_URL", "redis://localhost:6379/0")
|
||||
redis_url = os.getenv(
|
||||
"REDIS_URL", "redis://localhost:6379/0"
|
||||
) # tip: `docker run -d --name my-redis -p 6379:6379 redis:latest` to start a redis instance in docker and access it on redis://0.0.0.0:6379/0
|
||||
backend = await RedisBackend.from_url(
|
||||
redis_url,
|
||||
key_prefix="myapp",
|
||||
@@ -188,10 +193,28 @@ async def stats(backend: BackendDep) -> dict[str, object]:
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import argparse
|
||||
|
||||
import uvicorn
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Redis distributed rate limiting example"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--host",
|
||||
default=DEFAULT_HOST,
|
||||
help=f"Host to bind to (default: {DEFAULT_HOST})",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--port",
|
||||
type=int,
|
||||
default=DEFAULT_PORT,
|
||||
help=f"Port to bind to (default: {DEFAULT_PORT})",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
# Run multiple instances on different ports to test distributed limiting:
|
||||
# REDIS_URL=redis://localhost:6379/0 python 07_redis_distributed.py
|
||||
# In another terminal:
|
||||
# uvicorn 07_redis_distributed:app --port 8001
|
||||
uvicorn.run(app, host="0.0.0.0", port=8000)
|
||||
# uvicorn 07_redis_distributed:app --port 8002
|
||||
uvicorn.run(app, host=args.host, port=args.port)
|
||||
|
||||
Reference in New Issue
Block a user