feat-(level 4 completed)

This commit is contained in:
2026-11-07 13:06:03 +03:00
parent bb4cc3e1a6
commit 521e197595
11 changed files with 247 additions and 13 deletions
+3 -1
View File
@@ -10,7 +10,9 @@
Ограничения / известные ошибки: TODO
Источники и использование ИИ: TODO
pool_size и pool_timeout защищают БД от исчерпания соединений (Connection Exhaustion) при DDoS или всплесках трафика, параметризованные запросы/безопасный ORM, Проверены гонка регистрации одинакового username(test_duplicate_username) и rollback составной операции
pool_size и pool_timeout защищают БД от исчерпания соединений (Connection Exhaustion) при DDoS или всплесках трафика, параметризованные запросы/безопасный ORM, Проверены гонка регистрации одинакового username(test_duplicate_username) и rollback составной операции. Кэш сессий хранится в Redis, он ускоряет проверку токена сложностью O(1) - cache hit, в случае cache miss/недоступности Redis запрос идёт в БД, а затем кэшируется(Redis is recovered). Источник истины — PostgreSQL(таблица sessions.Политика expiry: TTL = SESSION_TTL_SECONDS (совпадает с БД).
Инвалидация: при logout ключ удаляется; при истечении TTL — автоматически.
Источник истины БД. Так Race condition на создание уже существующего username защищено в миграции CONSTRAINT users_username_unique. Обоснование индекса messages_chat_created_id_idx путём генерации 10 000 сообщений и выполнения EXPLAIN (ANALYZE, BUFFERS)
- [+] Весь обязательный минимум текущей и предыдущих работ сохранён с объявленными переходами.
- [+] Контракт и публичные тесты не ослаблены.
+2
View File
@@ -22,6 +22,8 @@ RUN groupadd -r appuser && useradd -r -g appuser -d /app -s /sbin/nologin appuse
COPY src/ ./src/
COPY scripts/ ./scripts/
RUN chown -R appuser:appuser /app
USER appuser
+31
View File
@@ -0,0 +1,31 @@
COMPARATIVE EXPLAIN REPORT
Generated: 2026-11-07 10:05:04 UTC
Chat ID: 42fc25ff-bf02-40ec-aafb-186d4c9b0f76
Messages: 10000
1. WITH INDEX
Limit (cost=0.29..5.51 rows=51 width=78) (actual time=0.284..0.296 rows=51 loops=1)
Buffers: shared hit=2 read=2
-> Index Scan using messages_chat_created_id_idx on messages (cost=0.29..1024.49 rows=10000 width=78) (actual time=0.282..0.290 rows=51 loops=1)
Index Cond: (chat_id = '42fc25ff-bf02-40ec-aafb-186d4c9b0f76'::uuid)
Buffers: shared hit=2 read=2
Planning:
Buffers: shared hit=21
Planning Time: 0.358 ms
Execution Time: 0.386 ms
2. WITHOUT INDEX
Limit (cost=603.81..603.94 rows=51 width=78) (actual time=4.723..4.731 rows=51 loops=1)
Buffers: shared hit=142
-> Sort (cost=603.81..628.81 rows=10000 width=78) (actual time=4.721..4.724 rows=51 loops=1)
Sort Key: created_at, id
Sort Method: top-N heapsort Memory: 38kB
Buffers: shared hit=142
-> Seq Scan on messages (cost=0.00..270.19 rows=10000 width=78) (actual time=0.041..1.098 rows=10000 loops=1)
Filter: (chat_id = '42fc25ff-bf02-40ec-aafb-186d4c9b0f76'::uuid)
Rows Removed by Filter: 255
Buffers: shared hit=142
Planning Time: 0.395 ms
Execution Time: 5.279 ms
KEY OBSERVATIONS
Variant 1 uses 'Index Scan using messages_chat_created_id_idx'
Variant 2 uses 'Seq Scan' + 'Sort' (full table scan)
Compare 'Buffers: shared hit=...' and 'Execution Time'
+101
View File
@@ -0,0 +1,101 @@
import asyncio
import uuid
from pathlib import Path
from datetime import datetime, timezone
from sqlalchemy import text
from src.database import engine
async def main():
async with engine.begin() as conn:
row = await conn.execute(text("SELECT id FROM chats LIMIT 1"))
chat_row = row.first()
if not chat_row:
print("No chats found. Create a chat first via API.")
return
chat_id = chat_row[0]
row = await conn.execute(text("SELECT id FROM users LIMIT 1"))
user_id = row.first()[0]
row = await conn.execute(text(
"SELECT COUNT(*) FROM messages WHERE chat_id = :cid"
), {"cid": chat_id})
current_count = row.scalar()
needed = max(0, 10_000 - current_count)
if needed > 0:
print(f"Generating {needed} messages for chat {chat_id}...")
await conn.execute(text("""
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
SELECT
gen_random_uuid(),
:chat_id,
:user_id,
'benchmark message ' || g,
NOW() - (interval '1 second' * g)
FROM generate_series(1, :needed) AS g
"""), {"chat_id": chat_id, "user_id": user_id, "needed": needed})
print(f"Inserted {needed} messages (total: {current_count + needed})")
else:
print(f"Already have {current_count} messages (≥10 000)")
await conn.execute(text("ANALYZE messages"))
print("Statistics updated via ANALYZE")
report_lines = []
report_lines.append("COMPARATIVE EXPLAIN REPORT")
report_lines.append(f" Generated: {datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M:%S UTC')}")
report_lines.append(f" Chat ID: {chat_id}")
report_lines.append(f" Messages: {current_count + needed}")
report_lines.append("1. WITH INDEX")
result = await conn.execute(text("""
EXPLAIN (ANALYZE, BUFFERS)
SELECT id, chat_id, sender_id, text, created_at
FROM messages
WHERE chat_id = :chat_id
ORDER BY created_at ASC, id ASC
LIMIT 51
"""), {"chat_id": chat_id})
for r in result:
report_lines.append(r[0])
report_lines.append("2. WITHOUT INDEX")
await conn.execute(text("SET LOCAL enable_indexscan = off"))
await conn.execute(text("SET LOCAL enable_bitmapscan = off"))
result = await conn.execute(text("""
EXPLAIN (ANALYZE, BUFFERS)
SELECT id, chat_id, sender_id, text, created_at
FROM messages
WHERE chat_id = :chat_id
ORDER BY created_at ASC, id ASC
LIMIT 51
"""), {"chat_id": chat_id})
for r in result:
report_lines.append(r[0])
await conn.execute(text("SET LOCAL enable_indexscan = on"))
await conn.execute(text("SET LOCAL enable_bitmapscan = on"))
report_lines.append("KEY OBSERVATIONS")
report_lines.append("Variant 1 uses 'Index Scan using messages_chat_created_id_idx'")
report_lines.append("Variant 2 uses 'Seq Scan' + 'Sort' (full table scan)")
report_lines.append("Compare 'Buffers: shared hit=...' and 'Execution Time'")
report = "\n".join(report_lines)
print("\n" + report)
Path("results").mkdir(exist_ok=True)
output_path = Path("results/explain_comparison.txt")
output_path.write_text(report, encoding="utf-8")
print(f"\nSaved to {output_path}")
if __name__ == "__main__":
asyncio.run(main())
+10 -1
View File
@@ -5,6 +5,7 @@ from src.models.users import User
from src.auth.tokens import hash_token
from sqlalchemy.ext.asyncio import AsyncSession
from src.database import get_db
from src.services.session_cache import invalidate_session, get_cached_user_id, cache_session
type DbSession = Annotated[AsyncSession, Depends(get_db)] ##Dependency Injection
@@ -24,6 +25,13 @@ async def get_current_user(request: Request, session: DbSession) -> User:
raw_token = auth_header[7:]
token_hash = hash_token(raw_token)
cached_user_id = await get_cached_user_id(token_hash)
if cached_user_id is not None:
user = await repo.users.get(cached_user_id)
if user:
return user
await invalidate_session(token_hash)
current_session = await repo.sessions.get_by_token_hash(token_hash)
if not current_session:
raise HTTPException(
@@ -39,7 +47,8 @@ async def get_current_user(request: Request, session: DbSession) -> User:
detail="user not found",
headers={"WWW-Authenticate": 'Bearer realm="messenger"'},
)
await cache_session(token_hash, user.id)
return user
+7 -2
View File
@@ -1,6 +1,7 @@
import uuid
from typing import Optional
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.exc import IntegrityError
from src.models.users import User
from src.api.repositories.core import DatabaseManager
from src.utils import generate_id
@@ -19,6 +20,10 @@ class UserRepo(DatabaseManager):
return result
async def create(self, username: str, display_name: str, password_hash: str) -> Optional[User]:
result = await super().create(id=generate_id(), username=username.lower(), display_name=display_name, password_hash=password_hash)
return result
try:
result = await super().create(id=generate_id(), username=username.lower(), display_name=display_name, password_hash=password_hash)
return result
except IntegrityError:
return None
+4 -1
View File
@@ -6,6 +6,7 @@ from src.api.repositories.main_repo import Repository
from src.auth.password import verify_password
from src.auth.tokens import generate_token, hash_token, calculate_expires_at
from src.config import settings
from src.services.session_cache import cache_session, invalidate_session
router = APIRouter(prefix="/v1/auth")
@@ -51,6 +52,7 @@ async def create_session(request: Request, session: DbSession):
expires_at = calculate_expires_at(settings.SESSION_TTL_SECONDS)
await repo.sessions.create(token_hash=token_hash, user_id=user.id, expires_at=expires_at)
await cache_session(token_hash, user.id)
return SessionCreateResponse(
token=raw_token,
@@ -90,7 +92,8 @@ async def delete_current_session(request: Request, session: DbSession, current_u
status_code=status.HTTP_401_UNAUTHORIZED,
detail="token in request doesn't align with current_user",
headers={"WWW-Authenticate": 'Bearer realm="messenger"'})
await invalidate_session(token_hash)
deleted = await repo.sessions.delete_by_token_hash(token_hash)
if not deleted:
raise HTTPException(
+6 -2
View File
@@ -4,7 +4,7 @@ from sqlalchemy import text
from src.database import engine, close_engine
from src.migrations.runner import run_migrations
from src.redis_client import init_redis, close_redis
@asynccontextmanager
async def lifespan(app: FastAPI):
@@ -12,6 +12,9 @@ async def lifespan(app: FastAPI):
print("lifespan startup: applying migrations...")
await run_migrations()
print("[lifespan] startup: initializing Redis cache...")
await init_redis()
print("lifespan startup: initializing database engine...")
async with engine.connect() as conn:
await conn.execute(text("SELECT 1"))
@@ -19,6 +22,7 @@ async def lifespan(app: FastAPI):
yield
print("lifespan shutdown: closing database engine...")
print("lifespan shutdown")
await close_redis()
await close_engine()
print("lifespan shutdown: complete")
+18
View File
@@ -9,6 +9,9 @@ from src.api.routes.chats import chat
from src.api.routes.messages import message
from src.api.routes.auth import auth
from src.lifespan import lifespan
from sqlalchemy.exc import OperationalError as SAOperationalError
from redis.exceptions import RedisError
application = FastAPI(title="MTUSI Messenger", version="1.0.0", lifespan=lifespan)
@@ -43,6 +46,21 @@ async def validation_exception_handler(request: Request, exc: RequestValidationE
headers={"X-Request-Id": request_id},
)
@application.exception_handler(SAOperationalError)
async def db_unavailable_handler(request: Request, exc: SAOperationalError):
request_id = getattr(request.state, "request_id", str(generate_id()))
return JSONResponse(
status_code=503,
content={
"error": {
"code": "unavailable",
"message": "database unavailable",
"request_id": request_id,
}
},
headers={"X-Request-Id": request_id},
)
@application.exception_handler(HTTPException)
async def http_exception_handler(request: Request, exc: HTTPException):
request_id = getattr(request.state, "request_id", None)
+19 -6
View File
@@ -1,15 +1,28 @@
import os
import redis.asyncio as redis
import json
import redis.asyncio as aioredis
from redis.exceptions import RedisError
REDIS_URL = os.getenv("REDIS_URL")
redis_client = redis.from_url(REDIS_URL, decode_responses=True)
redis_client: aioredis.Redis | None = None
async def get_redis() -> redis.Redis:
async def init_redis() -> aioredis.Redis:
global redis_client
redis_client = aioredis.from_url(REDIS_URL, decode_responses=True, socket_connect_timeout=2, socket_timeout=2)
try:
await redis_client.ping()
except RedisError:
redis_client = None
return redis_client
async def close_redis():
await redis_client.close()
async def close_redis():
global redis_client
if redis_client:
await redis_client.close()
redis_client = None
def get_redis() -> aioredis.Redis | None:
return redis_client
+46
View File
@@ -0,0 +1,46 @@
import uuid
from src.redis_client import get_redis
from redis.exceptions import RedisError
from src.config import settings
SESSION_PREFIX = "session:"
def _key(token_hash: str) -> str:
return f"{SESSION_PREFIX}{token_hash}"
async def cache_session(token_hash: str, user_id: uuid.UUID) -> None:
r = get_redis()
if not r:
return
try:
await r.setex(_key(token_hash), settings.SESSION_TTL_SECONDS, str(user_id))
except RedisError:
pass
async def get_cached_user_id(token_hash: str) -> uuid.UUID | None:
r = get_redis()
if not r:
return
try:
value = await r.get(_key(token_hash))
return uuid.UUID(value) if value else None
except RedisError:
return None
async def invalidate_session(token_hash: str) -> None:
r = get_redis()
if not r:
return
try:
await r.delete(_key(token_hash))
except RedisError:
pass