-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathperformance.py
More file actions
206 lines (160 loc) · 6.03 KB
/
Copy pathperformance.py
File metadata and controls
206 lines (160 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
"""
Performance optimizations for high-traffic scenarios.
Includes rate limiting, caching, and batch operations.
"""
import asyncio
import hashlib
import time
from collections.abc import Callable
from typing import Any
from fastapi import Request
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.responses import Response
from starlette.status import HTTP_429_TOO_MANY_REQUESTS
from config import logger
class RateLimiter(BaseHTTPMiddleware):
"""
Token bucket rate limiter.
Prevents API abuse and protects against DDoS.
"""
# Per-IP rate limits (requests per minute)
LIMITS = {
"/api/vote/": 60, # 1 per second
"/api/proxies": 30, # 0.5 per second
"/api/add-proxy": 10, # Very restrictive
"/": 120, # Homepage
}
def __init__(self, app):
super().__init__(app)
self.requests: dict = {} # {ip: [(timestamp, path)]}
async def dispatch(self, request: Request, call_next: Callable) -> Response:
"""Check rate limit before processing."""
client_ip = request.client.host if request.client else "unknown"
path = request.url.path
# Find matching limit
limit = self.LIMITS.get(path)
for prefix, rate in self.LIMITS.items():
if path.startswith(prefix):
limit = rate
break
if limit is None:
return await call_next(request)
current_time = time.time()
one_minute_ago = current_time - 60
# Initialize or clean up
if client_ip not in self.requests:
self.requests[client_ip] = []
# Remove old requests
self.requests[client_ip] = [
(ts, p) for ts, p in self.requests[client_ip] if ts > one_minute_ago
]
# Check limit for this path
path_requests = len([p for _, p in self.requests[client_ip] if p == path])
if path_requests >= limit:
logger.warning(
"⚠️ Rate limit exceeded for {client} on {path}",
client=client_ip,
path=path,
)
return Response(
content="Too many requests",
status_code=HTTP_429_TOO_MANY_REQUESTS,
)
# Record request
self.requests[client_ip].append((current_time, path))
return await call_next(request)
class CacheStore:
"""Simple in-memory cache with TTL."""
def __init__(self):
self.cache: dict = {} # {key: (value, expires_at)}
def get(self, key: str) -> Any | None:
"""Get value from cache if not expired."""
if key in self.cache:
value, expires_at = self.cache[key]
if time.time() < expires_at:
return value
del self.cache[key]
return None
def set(self, key: str, value: Any, ttl_seconds: int = 60) -> None:
"""Store value with TTL."""
self.cache[key] = (value, time.time() + ttl_seconds)
def invalidate(self, pattern: str = "") -> None:
"""Invalidate cache entries matching pattern."""
if not pattern:
self.cache.clear()
else:
self.cache = {k: v for k, v in self.cache.items() if pattern not in k}
# Global cache instance
cache_store = CacheStore()
def cached(ttl_seconds: int = 60):
"""Decorator for caching async functions."""
def decorator(func: Callable) -> Callable:
async def wrapper(*args, **kwargs) -> Any:
# Create cache key from function name and arguments
cache_key = f"{func.__name__}:{str(args)}:{str(kwargs)}"
cache_key = hashlib.md5(cache_key.encode()).hexdigest()
# Check cache
cached_value = cache_store.get(cache_key)
if cached_value is not None:
return cached_value
# Call function and cache result
result = await func(*args, **kwargs)
cache_store.set(cache_key, result, ttl_seconds)
return result
return wrapper
return decorator
class BatchProcessor:
"""
Batch multiple operations to reduce database calls.
Useful for vote counting and statistics.
"""
def __init__(self, batch_size: int = 100, flush_interval: float = 1.0):
self.batch_size = batch_size
self.flush_interval = flush_interval
self.batch: dict = {}
self.last_flush = time.time()
def add(self, key: str, value: Any) -> None:
"""Add item to batch."""
if key not in self.batch:
self.batch[key] = []
self.batch[key].append(value)
# Auto-flush if batch is full
if sum(len(v) for v in self.batch.values()) >= self.batch_size:
self.flush()
def flush(self) -> dict:
"""Get and clear batch."""
batch = self.batch.copy()
self.batch.clear()
self.last_flush = time.time()
return batch
def should_flush(self) -> bool:
"""Check if batch should be flushed (time-based)."""
return (time.time() - self.last_flush) >= self.flush_interval
# Global batch processor
batch_processor = BatchProcessor()
class ConnectionPool:
"""
Database connection pooling for high concurrency.
Prevents connection exhaustion.
"""
def __init__(self, min_size: int = 5, max_size: int = 20):
self.min_size = min_size
self.max_size = max_size
self.connections: list = []
self.available: list = []
async def acquire(self):
"""Get connection from pool."""
if self.available:
return self.available.pop()
if len(self.connections) < self.max_size:
# Create new connection
conn = None # Placeholder - implement with actual DB
self.connections.append(conn)
return conn
# Wait for available connection
while not self.available: # noqa: ASYNC110
await asyncio.sleep(0.01)
return self.available.pop()
async def release(self, conn):
"""Return connection to pool."""
self.available.append(conn)