forked from retoor/devplacepy
feat: add author-username search to feed/gists/projects listings and partial-index migration
Extend `database.text_search_clause` with an `author_field` parameter that resolves username matches to user UIDs, enabling author-username search across the three public listings (`/feed`, `/gists`, `/projects`). Update the corresponding API docs and README route descriptions to reflect the new search scope. Add six partial indexes (`idx_comments_target_live`, `idx_votes_target_live`, `idx_reactions_target_live`, `idx_gists_live_created`, `idx_projects_live_created`, `idx_attachments_user_created_live`) to optimize filtered queries on non-deleted rows. Introduce a hot-settings cache (`_hot_settings`) with a 2-second TTL for `maintenance_mode`, `rate_limit_per_minute`, and `rate_limit_window_seconds`, plus a periodic rate-limit store sweep (`_sweep_rate_limit_store`) to evict stale IP entries every 60 seconds. Add `GZipMiddleware` to the FastAPI app for response compression.
This commit is contained in:
+50
-8
@@ -12,6 +12,7 @@ from fastapi import FastAPI, Request
|
||||
from fastapi.responses import HTMLResponse, RedirectResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from starlette.middleware.gzip import GZipMiddleware
|
||||
from devplacepy.config import (
|
||||
STATIC_DIR,
|
||||
STATIC_VERSION,
|
||||
@@ -119,6 +120,39 @@ RATE_WINDOW = 60
|
||||
WEB_WORKERS = max(1, int(os.environ.get("DEVPLACE_WEB_WORKERS", "1")))
|
||||
RATE_LIMIT_DISABLED = os.environ.get("DEVPLACE_DISABLE_RATE_LIMIT") == "1"
|
||||
|
||||
HOT_SETTINGS_TTL = 2.0
|
||||
_hot_settings_value: dict = {}
|
||||
_hot_settings_at = 0.0
|
||||
_last_rate_sweep = 0.0
|
||||
RATE_SWEEP_INTERVAL = 60.0
|
||||
|
||||
|
||||
def _hot_settings() -> dict:
|
||||
global _hot_settings_value, _hot_settings_at
|
||||
now = time.monotonic()
|
||||
if not _hot_settings_value or now - _hot_settings_at >= HOT_SETTINGS_TTL:
|
||||
_hot_settings_value = {
|
||||
"maintenance_mode": get_setting("maintenance_mode", "0"),
|
||||
"rate_limit_per_minute": max(
|
||||
1, get_int_setting("rate_limit_per_minute", RATE_LIMIT)
|
||||
),
|
||||
"rate_limit_window_seconds": max(
|
||||
1, get_int_setting("rate_limit_window_seconds", RATE_WINDOW)
|
||||
),
|
||||
}
|
||||
_hot_settings_at = now
|
||||
return _hot_settings_value
|
||||
|
||||
|
||||
def _sweep_rate_limit_store(window_start: float) -> None:
|
||||
stale = [
|
||||
ip
|
||||
for ip, timestamps in _rate_limit_store.items()
|
||||
if not timestamps or timestamps[-1] <= window_start
|
||||
]
|
||||
for ip in stale:
|
||||
del _rate_limit_store[ip]
|
||||
|
||||
|
||||
def _worker_rate_limit(limit: int) -> int:
|
||||
return max(1, -(-limit // WEB_WORKERS))
|
||||
@@ -445,17 +479,21 @@ async def rate_limit_middleware(request: Request, call_next):
|
||||
"DELETE",
|
||||
"PATCH",
|
||||
) and not request.url.path.startswith(("/openai", "/xmlrpc")):
|
||||
limit = _worker_rate_limit(
|
||||
max(1, get_int_setting("rate_limit_per_minute", RATE_LIMIT))
|
||||
)
|
||||
window = max(1, get_int_setting("rate_limit_window_seconds", RATE_WINDOW))
|
||||
settings = _hot_settings()
|
||||
limit = _worker_rate_limit(settings["rate_limit_per_minute"])
|
||||
window = settings["rate_limit_window_seconds"]
|
||||
ip = request.headers.get("X-Real-IP") or (
|
||||
request.client.host if request.client else "unknown"
|
||||
)
|
||||
now = time.time()
|
||||
window_start = now - window
|
||||
_rate_limit_store[ip] = [t for t in _rate_limit_store[ip] if t > window_start]
|
||||
if len(_rate_limit_store[ip]) >= limit:
|
||||
global _last_rate_sweep
|
||||
if now - _last_rate_sweep >= RATE_SWEEP_INTERVAL:
|
||||
_sweep_rate_limit_store(window_start)
|
||||
_last_rate_sweep = now
|
||||
timestamps = [t for t in _rate_limit_store.get(ip, ()) if t > window_start]
|
||||
if len(timestamps) >= limit:
|
||||
_rate_limit_store[ip] = timestamps
|
||||
audit.record(
|
||||
request,
|
||||
"security.rate_limit.block",
|
||||
@@ -473,7 +511,8 @@ async def rate_limit_middleware(request: Request, call_next):
|
||||
status_code=429,
|
||||
headers=retry_after,
|
||||
)
|
||||
_rate_limit_store[ip].append(now)
|
||||
timestamps.append(now)
|
||||
_rate_limit_store[ip] = timestamps
|
||||
return await call_next(request)
|
||||
|
||||
|
||||
@@ -482,7 +521,7 @@ _MAINTENANCE_ALLOWED_PREFIXES = ("/static", "/avatar", "/auth", "/admin", "/open
|
||||
|
||||
@app.middleware("http")
|
||||
async def maintenance_middleware(request: Request, call_next):
|
||||
if get_setting("maintenance_mode", "0") != "1":
|
||||
if _hot_settings()["maintenance_mode"] != "1":
|
||||
return await call_next(request)
|
||||
if request.url.path.startswith(_MAINTENANCE_ALLOWED_PREFIXES):
|
||||
return await call_next(request)
|
||||
@@ -522,6 +561,9 @@ async def response_timing(request: Request, call_next):
|
||||
return response
|
||||
|
||||
|
||||
app.add_middleware(GZipMiddleware, minimum_size=512, compresslevel=6)
|
||||
|
||||
|
||||
_home_cache = TTLCache(ttl=60, max_size=4)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user