From 7b0a6aea9b97d4fab67245a30612c98d3f572099 Mon Sep 17 00:00:00 2001 From: Sona Date: Tue, 3 Nov 2026 04:06:04 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=A0=202:=20Valkey=20=D0=BA=D0=B0=D0=BA?= =?UTF-8?q?=20=D0=BA=D1=8D=D1=88=20=D1=81=D0=B5=D1=81=D1=81=D0=B8=D0=B9=20?= =?UTF-8?q?=D1=81=20=D0=BE=D0=B1=D1=85=D0=BE=D0=B4=D0=BE=D0=BC=20=D0=BF?= =?UTF-8?q?=D1=80=D0=B8=20=D1=81=D0=B1=D0=BE=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- compose.yaml | 13 ++++++++ requirements.txt | 3 +- scripts/restart.sh | 2 +- src/auth.py | 19 +++++++---- src/cache.py | 82 ++++++++++++++++++++++++++++++++++++++++++++++ src/config.py | 5 ++- src/main.py | 4 +++ 7 files changed, 119 insertions(+), 9 deletions(-) create mode 100644 src/cache.py diff --git a/compose.yaml b/compose.yaml index ef8e710..6c1b5ab 100644 --- a/compose.yaml +++ b/compose.yaml @@ -17,6 +17,16 @@ services: interval: 5s timeout: 3s retries: 10 + + valkey: + image: valkey/valkey:8.0.1-alpine + restart: unless-stopped + command: ["valkey-server", "--save", "", "--appendonly", "no", "--maxmemory", "64mb", "--maxmemory-policy", "allkeys-lru"] + healthcheck: + test: ["CMD", "valkey-cli", "ping"] + interval: 5s + timeout: 3s + retries: 10 api: build: . @@ -25,10 +35,13 @@ services: depends_on: postgres: condition: service_healthy + valkey: + condition: service_started environment: APP_ENV: ${APP_ENV:-lab} SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} DATABASE_URL_FILE: /run/secrets/database_url + CACHE_URL: redis://valkey:6379/0 secrets: - database_url ports: diff --git a/requirements.txt b/requirements.txt index 15476b6..23aaa3e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,4 +12,5 @@ typing-inspection==0.4.4 uvicorn==0.32.0 asyncpg==0.30.0 argon2-cffi==23.1.0 -argon2-cffi-bindings==21.2.0 \ No newline at end of file +argon2-cffi-bindings==21.2.0 +redis==5.2.1 \ No newline at end of file diff --git a/scripts/restart.sh b/scripts/restart.sh index f69d731..a19427e 100755 --- a/scripts/restart.sh +++ b/scripts/restart.sh @@ -3,5 +3,5 @@ set -eu cd "$(dirname "$0")/.." docker compose --env-file .env stop api -docker compose --env-file .env restart postgres +docker compose --env-file .env restart postgres valkey docker compose --env-file .env start api \ No newline at end of file diff --git a/src/auth.py b/src/auth.py index 80ff095..4653429 100644 --- a/src/auth.py +++ b/src/auth.py @@ -61,20 +61,27 @@ def parse_bearer(header: Optional[str]) -> Optional[str]: _SESSION_SQL = """ -SELECT u.id, u.username, u.display_name, u.created_at +SELECT u.id, u.username, u.display_name, u.created_at, + EXTRACT(EPOCH FROM (s.expires_at - now())) AS remaining FROM sessions s JOIN users u ON u.id = s.user_id WHERE s.token_hash = $1 AND s.revoked_at IS NULL AND s.expires_at > now() """ async def current_user(request: Request): - """Зависимость для всех защищённых endpoint: только Bearer, X-User-Id игнорируется.""" + # Только Bearer. Сначала кэш, при промахе или сбое - PostgreSQL (источник истины). token = parse_bearer(request.headers.get("Authorization")) if token is None: raise HTTPException(401, headers=BEARER_CHALLENGE) digest = token_hash(token) - row = await request.app.state.pool.fetchrow(_SESSION_SQL, digest) - if row is None: - raise HTTPException(401, headers=BEARER_CHALLENGE) + cache = request.app.state.cache + + user = await cache.get(digest) + if user is None: + row = await request.app.state.pool.fetchrow(_SESSION_SQL, digest) + if row is None: + raise HTTPException(401, headers=BEARER_CHALLENGE) + user = {k: row[k] for k in ("id", "username", "display_name", "created_at")} + await cache.put(digest, user, int(row["remaining"])) request.state.token_hash = digest - return row \ No newline at end of file + return user \ No newline at end of file diff --git a/src/cache.py b/src/cache.py new file mode 100644 index 0000000..a2f14c8 --- /dev/null +++ b/src/cache.py @@ -0,0 +1,82 @@ +import asyncio +import json +import logging +import time +import uuid +from datetime import datetime +from typing import Optional + +import redis.asyncio as redis +from redis.exceptions import RedisError + +from . import config +from .common import iso + +logger = logging.getLogger("messenger") + +_FAILURES = (RedisError, OSError, asyncio.TimeoutError) +_RETRY_AFTER = 5.0 # после сбоя 5 секунд не обращаемся к кэшу, чтобы не ждать таймаут на каждом запросе + + +def _key(digest: bytes) -> str: + return "sess:" + digest.hex() + + +class SessionCache: + """Кэш проверенных сессий. Любой сбой = промах: ответ берётся из PostgreSQL.""" + + def __init__(self, url: str): + self._client = ( + redis.Redis.from_url( + url, decode_responses=True, + socket_timeout=config.CACHE_TIMEOUT, socket_connect_timeout=config.CACHE_TIMEOUT, + ) if url else None + ) + self._down_until = 0.0 + + def _usable(self) -> bool: + return self._client is not None and time.monotonic() >= self._down_until + + def _mark_down(self, exc: Exception) -> None: + self._down_until = time.monotonic() + _RETRY_AFTER + logger.warning("Cache unavailable, bypassing: %s", type(exc).__name__) + + async def get(self, digest: bytes) -> Optional[dict]: + if not self._usable(): + return None + try: + raw = await self._client.get(_key(digest)) + except _FAILURES as exc: + self._mark_down(exc) + return None + if raw is None: + return None + try: + d = json.loads(raw) + return {"id": uuid.UUID(d["id"]), "username": d["username"], + "display_name": d["display_name"], "created_at": datetime.fromisoformat(d["created_at"])} + except (ValueError, KeyError): + return None + + async def put(self, digest: bytes, user: dict, remaining_seconds: int) -> None: + ttl = min(remaining_seconds, config.CACHE_TTL_SECONDS) # кэш никогда не живёт дольше сессии + if ttl < 1 or not self._usable(): + return + value = json.dumps({"id": str(user["id"]), "username": user["username"], + "display_name": user["display_name"], "created_at": iso(user["created_at"])}) + try: + await self._client.set(_key(digest), value, ex=ttl) + except _FAILURES as exc: + self._mark_down(exc) + + async def delete(self, digest: bytes) -> None: + if not self._usable(): + return + try: + await self._client.delete(_key(digest)) + except _FAILURES as exc: + self._mark_down(exc) + + async def close(self) -> None: + if self._client is not None: + await self._client.aclose() \ No newline at end of file diff --git a/src/config.py b/src/config.py index e11dbc2..8e18983 100644 --- a/src/config.py +++ b/src/config.py @@ -18,4 +18,7 @@ DB_POOL_MAX = int(os.environ.get("DB_POOL_MAX", "10")) DB_CONNECT_TIMEOUT = float(os.environ.get("DB_CONNECT_TIMEOUT", "5")) DB_COMMAND_TIMEOUT = float(os.environ.get("DB_COMMAND_TIMEOUT", "5")) -MIGRATIONS_DIR = Path(os.environ.get("MIGRATIONS_DIR", Path(__file__).resolve().parent.parent / "migrations")) \ No newline at end of file +MIGRATIONS_DIR = Path(os.environ.get("MIGRATIONS_DIR", Path(__file__).resolve().parent.parent / "migrations")) +CACHE_URL = _read_secret("CACHE_URL_FILE", "CACHE_URL") +CACHE_TTL_SECONDS = int(os.environ.get("CACHE_TTL_SECONDS", "60")) +CACHE_TIMEOUT = float(os.environ.get("CACHE_TIMEOUT", "0.5")) \ No newline at end of file diff --git a/src/main.py b/src/main.py index 3ab0ac3..89325e0 100644 --- a/src/main.py +++ b/src/main.py @@ -17,6 +17,7 @@ from .auth import (BASIC_CHALLENGE, current_user, hash_password, new_token, from .chats import router as chats_router from .common import iso, make_error_response, user_dict from .db import create_pool_with_retry, run_migrations +from .cache import SessionCache logging.basicConfig( level=logging.INFO, @@ -43,9 +44,11 @@ USERNAME_RE = re.compile(r"[a-z0-9_]{3,32}") async def lifespan(app: FastAPI): app.state.pool = await create_pool_with_retry() await run_migrations(app.state.pool) + app.state.cache = SessionCache(config.CACHE_URL) yield global is_shutting_down is_shutting_down = True + await app.state.cache.close() await app.state.pool.close() logger.info("Application shutdown complete") @@ -180,4 +183,5 @@ async def logout(request: Request, user=Depends(current_user)): await request.app.state.pool.execute( "UPDATE sessions SET revoked_at = now() WHERE token_hash = $1", request.state.token_hash ) + await request.app.state.cache.delete(request.state.token_hash) return Response(status_code=204) \ No newline at end of file