forked from mtusi2026/mtusi2026lab6
Лабораторная работа 6
This commit is contained in:
+894
@@ -0,0 +1,894 @@
|
||||
import asyncio
|
||||
import base64
|
||||
import hashlib
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import threading
|
||||
import time
|
||||
import unicodedata
|
||||
import uuid as uuid_mod
|
||||
from contextlib import asynccontextmanager
|
||||
from datetime import datetime
|
||||
from typing import Callable, Dict, Optional, Tuple
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
import psycopg
|
||||
from opentelemetry import trace
|
||||
from fastapi import BackgroundTasks, Depends, FastAPI, Query, Request, Response
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.concurrency import run_in_threadpool
|
||||
from fastapi.responses import JSONResponse, StreamingResponse
|
||||
from psycopg_pool import PoolTimeout
|
||||
from starlette.exceptions import HTTPException as StarletteHTTPException
|
||||
from starlette.requests import ClientDisconnect
|
||||
from starlette.middleware.base import BaseHTTPMiddleware
|
||||
|
||||
from . import telemetry
|
||||
from .config import load_settings
|
||||
from .db import create_pool, required_schema_version, schema_has_version, wait_and_migrate
|
||||
from .images import ImageRejected, inspect as inspect_image
|
||||
from .models import (
|
||||
Attachment,
|
||||
Chat,
|
||||
ChatCreate,
|
||||
ChatPage,
|
||||
Message,
|
||||
MessageCreate,
|
||||
MessagePage,
|
||||
Session,
|
||||
User,
|
||||
UserCreate,
|
||||
)
|
||||
from .objectstore import ObjectMissing, ObjectStore, ObjectStoreUnavailable
|
||||
from .broker import Broker
|
||||
from .jobs import job_payload
|
||||
from .outbox import Relay
|
||||
from .reconcile import STAGING_PREFIX, gc_loop
|
||||
from .runtime import DRAIN, INSTANCE_ID
|
||||
from .security import (
|
||||
configure_kdf_concurrency,
|
||||
hash_password,
|
||||
parse_basic,
|
||||
parse_bearer,
|
||||
verify_password,
|
||||
)
|
||||
from .sessions import SessionCache, SessionService
|
||||
from .storage import (
|
||||
AttachmentFailed,
|
||||
AttachmentNotFound,
|
||||
IdempotencyConflict,
|
||||
IdempotencyRace,
|
||||
NotChatMember,
|
||||
Storage,
|
||||
UnknownMember,
|
||||
UsernameTaken,
|
||||
)
|
||||
|
||||
# lab6: OTLP traces/metrics/logs + JSON-логи. До create_pool (инструментация psycopg).
|
||||
telemetry.setup("messenger-api")
|
||||
log = logging.getLogger("messenger.api")
|
||||
access_log = logging.getLogger("messenger.http")
|
||||
|
||||
BASIC_CHALLENGE = 'Basic realm="messenger", charset="UTF-8"'
|
||||
BEARER_CHALLENGE = 'Bearer realm="messenger"'
|
||||
REQUEST_ID_RE = re.compile(r"^[A-Za-z0-9._:-]{1,128}$")
|
||||
# Контракт lab3: Idempotency-Key — ASCII [A-Za-z0-9._:-]{1,128}.
|
||||
IDEMPOTENCY_KEY_RE = re.compile(r"[A-Za-z0-9._:-]{1,128}")
|
||||
|
||||
|
||||
def _idempotency_cleanup_loop(storage: Storage, interval: float, stop: threading.Event) -> None:
|
||||
"""Фоновая очистка истёкших ключей. Advisory lock — одна реплика за раз."""
|
||||
while not stop.wait(interval):
|
||||
try:
|
||||
deleted = storage.delete_expired_idempotency_keys()
|
||||
if deleted:
|
||||
log.info("deleted %d expired idempotency keys", deleted)
|
||||
except Exception as exc: # БД недоступна — попробуем в следующий раз
|
||||
log.warning("idempotency cleanup skipped: %s", type(exc).__name__)
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
settings = load_settings()
|
||||
if settings.run_migrations_on_start:
|
||||
# Только для локальной разработки. В compose миграции выполняет
|
||||
# отдельный one-shot сервис `migrate` до старта реплик.
|
||||
applied = wait_and_migrate(settings)
|
||||
log.info("migrations applied at startup: %s", applied or "none")
|
||||
configure_kdf_concurrency(settings.password_hash_concurrency)
|
||||
pool = create_pool(settings)
|
||||
storage = Storage(pool)
|
||||
cache = SessionCache(settings)
|
||||
app.state.settings = settings
|
||||
app.state.storage = storage
|
||||
app.state.cache = cache
|
||||
app.state.sessions = SessionService(storage, cache, settings)
|
||||
app.state.required_schema = required_schema_version()
|
||||
# lab6: DB pool и очередь (из PostgreSQL) — метрики живут, даже когда worker остановлен.
|
||||
telemetry.register_pool_metrics(pool)
|
||||
telemetry.register_queue_metrics(pool, settings.broker_stream)
|
||||
app.state.schema_ok = False
|
||||
stop_cleanup = threading.Event()
|
||||
cleanup = threading.Thread(
|
||||
target=_idempotency_cleanup_loop,
|
||||
args=(storage, settings.idempotency_cleanup_interval_seconds, stop_cleanup),
|
||||
name="idempotency-cleanup",
|
||||
daemon=True,
|
||||
)
|
||||
cleanup.start()
|
||||
# lab4: S3 и ограничители ресурсов загрузок (на реплику).
|
||||
app.state.objects = ObjectStore(settings) if ObjectStore.configured(settings) else None
|
||||
app.state.upload_slots = asyncio.Semaphore(settings.max_concurrent_uploads)
|
||||
app.state.decode_slots = threading.BoundedSemaphore(settings.image_decode_concurrency)
|
||||
if app.state.objects is not None:
|
||||
threading.Thread(
|
||||
target=gc_loop,
|
||||
args=(pool, app.state.objects, settings, stop_cleanup),
|
||||
name="attachment-gc",
|
||||
daemon=True,
|
||||
).start()
|
||||
else:
|
||||
log.warning("S3 is not configured: attachment endpoints return 503")
|
||||
# lab5: outbox relay в каждой реплике (быстрый путь после 202 + sweeper).
|
||||
# Брокер НЕ входит в синхронный путь: при его недоступности upload всё равно
|
||||
# отвечает 202, задание ждёт в job_outbox (PostgreSQL).
|
||||
app.state.relay = None
|
||||
if settings.broker_url and settings.relay_enabled:
|
||||
broker = Broker(settings.broker_url, settings.broker_stream, settings.broker_group,
|
||||
settings.broker_timeout_seconds)
|
||||
app.state.relay = Relay(pool, broker, settings, "api:" + INSTANCE_ID)
|
||||
threading.Thread(target=app.state.relay.loop, args=(stop_cleanup,), name="outbox-relay",
|
||||
daemon=True).start()
|
||||
elif not settings.broker_url:
|
||||
log.warning("BROKER_URL is not configured: jobs stay in job_outbox until a relay publishes them")
|
||||
log.info(
|
||||
"attachments: s3=%s bucket=%s max_bytes=%d max_pixels=%d uploads<=%d decode<=%d gc_grace=%ss fault=%s",
|
||||
"on" if app.state.objects is not None else "off",
|
||||
settings.s3_bucket,
|
||||
settings.max_upload_bytes,
|
||||
settings.max_image_pixels,
|
||||
settings.max_concurrent_uploads,
|
||||
settings.image_decode_concurrency,
|
||||
settings.attachment_gc_grace_seconds,
|
||||
settings.fault_injection or "none",
|
||||
)
|
||||
log.info(
|
||||
"started: instance=%s version=%s session_ttl=%ss cache=%s pool=%s..%s schema>=%s",
|
||||
INSTANCE_ID,
|
||||
settings.app_version,
|
||||
settings.session_ttl_seconds,
|
||||
"redis" if cache.client is not None else "disabled",
|
||||
settings.db_pool_min,
|
||||
settings.db_pool_max,
|
||||
app.state.required_schema,
|
||||
)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
log.info("shutdown: instance=%s closing pool", INSTANCE_ID)
|
||||
stop_cleanup.set()
|
||||
pool.close()
|
||||
if cache.client is not None:
|
||||
cache.client.close()
|
||||
telemetry.shutdown()
|
||||
|
||||
|
||||
app = FastAPI(
|
||||
title="MTUSI Messenger (lab6: observability)",
|
||||
version="6.0.0",
|
||||
lifespan=lifespan,
|
||||
)
|
||||
|
||||
|
||||
# Request ID
|
||||
|
||||
class RequestIdMiddleware(BaseHTTPMiddleware):
|
||||
async def dispatch(self, request: Request, call_next: Callable):
|
||||
incoming = request.headers.get("X-Request-Id", "")
|
||||
request_id = incoming if REQUEST_ID_RE.match(incoming) else str(uuid4())
|
||||
request.state.request_id = request_id
|
||||
# lab6: X-Request-Id в server span — связь «ответ с ошибкой -> trace -> лог».
|
||||
span = trace.get_current_span()
|
||||
if span.is_recording():
|
||||
span.set_attribute("messenger.request_id", request_id)
|
||||
started = time.perf_counter()
|
||||
response = await call_next(request)
|
||||
path = request.url.path
|
||||
if not path.startswith("/health/"):
|
||||
route = request.scope.get("route")
|
||||
# Только шаблон маршрута, без реальных UUID и query string.
|
||||
access_log.info(
|
||||
"%s %s -> %d", request.method, getattr(route, "path", "unmatched"), response.status_code,
|
||||
extra={"event": "http.request", "http_method": request.method,
|
||||
"http_route": getattr(route, "path", "unmatched"),
|
||||
"http_status_code": response.status_code, "request_id": request_id,
|
||||
"duration_ms": round((time.perf_counter() - started) * 1000, 1)},
|
||||
)
|
||||
response.headers["X-Request-Id"] = request_id
|
||||
response.headers["X-Instance-Id"] = INSTANCE_ID
|
||||
if DRAIN.active:
|
||||
# Во время осушения не держим keep-alive: следующий запрос клиент/LB
|
||||
# откроет новым соединением — уже к другой реплике.
|
||||
response.headers["Connection"] = "close"
|
||||
return response
|
||||
|
||||
|
||||
app.add_middleware(RequestIdMiddleware)
|
||||
# Добавляется последним -> самый внешний middleware: RequestIdMiddleware и
|
||||
# обработчики работают внутри server span (логи получают trace_id/span_id).
|
||||
telemetry.instrument_fastapi(app)
|
||||
|
||||
|
||||
# errors
|
||||
|
||||
class ApiError(Exception):
|
||||
def __init__(self, status_code: int, code: str, message: str, headers: Optional[Dict[str, str]] = None):
|
||||
self.status_code = status_code
|
||||
self.code = code
|
||||
self.message = message
|
||||
self.headers = headers or {}
|
||||
|
||||
|
||||
def unauthorized(challenge: str = BEARER_CHALLENGE) -> ApiError:
|
||||
return ApiError(401, "unauthorized", "Authentication required", {"WWW-Authenticate": challenge})
|
||||
|
||||
|
||||
def bad_request(message: str = "Invalid request") -> ApiError:
|
||||
return ApiError(400, "invalid_request", message)
|
||||
|
||||
|
||||
def not_found() -> ApiError:
|
||||
return ApiError(404, "not_found", "Not found")
|
||||
|
||||
|
||||
def error_response(
|
||||
request: Request,
|
||||
status_code: int,
|
||||
code: str,
|
||||
message: str,
|
||||
headers: Optional[Dict[str, str]] = None,
|
||||
) -> JSONResponse:
|
||||
request_id = getattr(request.state, "request_id", None) or str(uuid4())
|
||||
all_headers = {"X-Request-Id": request_id, "X-Instance-Id": INSTANCE_ID}
|
||||
all_headers.update(headers or {})
|
||||
return JSONResponse(
|
||||
status_code=status_code,
|
||||
content={"error": {"code": code, "message": message, "request_id": request_id}},
|
||||
headers=all_headers,
|
||||
)
|
||||
|
||||
|
||||
@app.exception_handler(ApiError)
|
||||
async def api_error_handler(request: Request, exc: ApiError) -> JSONResponse:
|
||||
return error_response(request, exc.status_code, exc.code, exc.message, exc.headers)
|
||||
|
||||
|
||||
@app.exception_handler(RequestValidationError)
|
||||
async def validation_exception_handler(request: Request, exc: RequestValidationError) -> JSONResponse:
|
||||
return error_response(request, 400, "invalid_request", "Invalid request")
|
||||
|
||||
|
||||
@app.exception_handler(StarletteHTTPException)
|
||||
async def http_exception_handler(request: Request, exc: StarletteHTTPException) -> JSONResponse:
|
||||
status_to_code = {
|
||||
400: "invalid_request",
|
||||
401: "unauthorized",
|
||||
404: "not_found",
|
||||
409: "conflict",
|
||||
413: "payload_too_large",
|
||||
415: "unsupported_media_type",
|
||||
429: "rate_limited",
|
||||
503: "unavailable",
|
||||
}
|
||||
status_code = exc.status_code if exc.status_code in status_to_code else 500
|
||||
code = status_to_code.get(status_code, "internal_error")
|
||||
message = str(exc.detail) if status_code != 500 else "Internal server error"
|
||||
return error_response(request, status_code, code, message)
|
||||
|
||||
|
||||
async def dependency_unavailable(request: Request, exc: Exception) -> JSONResponse:
|
||||
# Наружу — только общий 503, без SQL, адресов и текста драйвера.
|
||||
log.warning("database unavailable: %s", type(exc).__name__)
|
||||
return error_response(request, 503, "unavailable", "Service temporarily unavailable")
|
||||
|
||||
|
||||
app.add_exception_handler(psycopg.OperationalError, dependency_unavailable)
|
||||
app.add_exception_handler(psycopg.InterfaceError, dependency_unavailable)
|
||||
app.add_exception_handler(PoolTimeout, dependency_unavailable)
|
||||
|
||||
|
||||
@app.exception_handler(Exception)
|
||||
async def unexpected_exception_handler(request: Request, exc: Exception) -> JSONResponse:
|
||||
log.exception("unexpected error")
|
||||
return error_response(request, 500, "internal_error", "Internal server error")
|
||||
|
||||
|
||||
# helpers
|
||||
|
||||
def require_json_content_type(request: Request) -> None:
|
||||
content_type = request.headers.get("content-type", "")
|
||||
if not content_type.lower().startswith("application/json"):
|
||||
raise ApiError(415, "unsupported_media_type", "Unsupported media type")
|
||||
|
||||
|
||||
def ensure_not_blank(value: str) -> None:
|
||||
if not value.strip():
|
||||
raise bad_request()
|
||||
|
||||
|
||||
def storage_of(request: Request) -> Storage:
|
||||
return request.app.state.storage
|
||||
|
||||
|
||||
def current_user(request: Request) -> User:
|
||||
"""Только Authorization: Bearer. Basic и X-User-Id здесь не дают доступа."""
|
||||
token = parse_bearer(request.headers.get("Authorization"))
|
||||
if token is None:
|
||||
raise unauthorized()
|
||||
user = request.app.state.sessions.resolve(token)
|
||||
if user is None:
|
||||
raise unauthorized()
|
||||
request.state.token = token
|
||||
return user
|
||||
|
||||
|
||||
def encode_cursor(resource_id: UUID, created_at: datetime, item_id: UUID) -> str:
|
||||
payload = {
|
||||
"resource_id": str(resource_id),
|
||||
"created_at": created_at.isoformat(),
|
||||
"item_id": str(item_id),
|
||||
}
|
||||
raw = json.dumps(payload, separators=(",", ":")).encode("utf-8")
|
||||
return base64.urlsafe_b64encode(raw).decode("ascii").rstrip("=")
|
||||
|
||||
|
||||
def decode_cursor(cursor: Optional[str], expected_resource_id: UUID) -> Optional[Tuple[datetime, UUID]]:
|
||||
if cursor is None:
|
||||
return None
|
||||
try:
|
||||
padding = "=" * (-len(cursor) % 4)
|
||||
raw = base64.urlsafe_b64decode(cursor + padding)
|
||||
payload = json.loads(raw.decode("utf-8"))
|
||||
resource_id = UUID(payload["resource_id"])
|
||||
created_at = datetime.fromisoformat(payload["created_at"])
|
||||
item_id = UUID(payload["item_id"])
|
||||
if created_at.tzinfo is None or created_at.utcoffset() is None:
|
||||
raise ValueError("invalid_cursor")
|
||||
except (ValueError, KeyError, TypeError, AttributeError, json.JSONDecodeError):
|
||||
raise bad_request("Invalid cursor")
|
||||
if resource_id != expected_resource_id:
|
||||
raise bad_request("Invalid cursor")
|
||||
return created_at, item_id
|
||||
|
||||
|
||||
# health
|
||||
|
||||
@app.get("/health/live", response_model=Dict[str, str])
|
||||
def liveness() -> Dict[str, str]:
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
@app.get("/health/ready", response_model=Dict[str, str])
|
||||
def readiness(request: Request) -> Dict[str, str]:
|
||||
"""Готовность к обязательному синхронному пути.
|
||||
|
||||
* SIGTERM получен (фаза осушения) -> 503: LB выводит реплику из ротации;
|
||||
* PostgreSQL недоступна за READY_DB_TIMEOUT_SECONDS -> 503;
|
||||
* схема ещё не мигрирована до версии, которую знает образ -> 503
|
||||
(миграции выполняет отдельный сервис `migrate`).
|
||||
Redis не критичен: это кэш с обходом (см. REPORT.md).
|
||||
"""
|
||||
if DRAIN.active:
|
||||
raise ApiError(503, "unavailable", "Instance is shutting down")
|
||||
state = request.app.state
|
||||
timeout = state.settings.ready_db_timeout_seconds
|
||||
try:
|
||||
if state.schema_ok:
|
||||
with state.storage.pool.connection(timeout=timeout) as conn:
|
||||
conn.execute("SELECT 1")
|
||||
elif schema_has_version(state.storage.pool, state.required_schema, timeout):
|
||||
state.schema_ok = True
|
||||
else:
|
||||
raise ApiError(503, "unavailable", "Database schema is not migrated")
|
||||
except (psycopg.Error, PoolTimeout):
|
||||
raise ApiError(503, "unavailable", "Database is not available")
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
# users
|
||||
|
||||
@app.post("/api/v1/users", response_model=User, status_code=201)
|
||||
def register_user(request: Request, data: UserCreate) -> User:
|
||||
require_json_content_type(request)
|
||||
ensure_not_blank(data.display_name)
|
||||
ensure_not_blank(data.password)
|
||||
password_hash = hash_password(data.password)
|
||||
try:
|
||||
return storage_of(request).create_user(data.username, data.display_name, password_hash)
|
||||
except UsernameTaken:
|
||||
raise ApiError(409, "conflict", "Username already exists")
|
||||
|
||||
|
||||
@app.get("/api/v1/users/me", response_model=User)
|
||||
def get_current_user(user: User = Depends(current_user)) -> User:
|
||||
return user
|
||||
|
||||
|
||||
# sessions
|
||||
|
||||
async def require_empty_body(request: Request) -> None:
|
||||
"""Вход — только заголовок Authorization: Basic, без тела (контракт).
|
||||
|
||||
Любые байты в теле (JSON или нет) — 400. Проверка выполняется до
|
||||
дорогой проверки пароля и до создания сессии.
|
||||
"""
|
||||
if await request.body():
|
||||
raise bad_request("Request body is not allowed")
|
||||
|
||||
|
||||
@app.post("/api/v1/auth/sessions", response_model=Session, status_code=201,
|
||||
dependencies=[Depends(require_empty_body)])
|
||||
def create_session(request: Request) -> Session:
|
||||
credentials = parse_basic(request.headers.get("Authorization"))
|
||||
if credentials is None:
|
||||
raise unauthorized(BASIC_CHALLENGE)
|
||||
username, password = credentials
|
||||
found = storage_of(request).get_credentials(username)
|
||||
user, password_hash = found if found else (None, None)
|
||||
# verify_password выполняется и для несуществующего username (с хешем-пустышкой).
|
||||
if not verify_password(password_hash, password) or user is None:
|
||||
raise unauthorized(BASIC_CHALLENGE)
|
||||
token, expires_at = request.app.state.sessions.create(user)
|
||||
return Session(token=token, token_type="Bearer", expires_at=expires_at, user=user)
|
||||
|
||||
|
||||
@app.delete("/api/v1/auth/sessions/current", status_code=204)
|
||||
def delete_current_session(request: Request, user: User = Depends(current_user)) -> Response:
|
||||
if not request.app.state.sessions.revoke(request.state.token):
|
||||
raise unauthorized()
|
||||
return Response(status_code=204)
|
||||
|
||||
|
||||
# chats
|
||||
|
||||
@app.post("/api/v1/chats", response_model=Chat, status_code=201)
|
||||
def create_chat(request: Request, data: ChatCreate, user: User = Depends(current_user)) -> Chat:
|
||||
require_json_content_type(request)
|
||||
ensure_not_blank(data.title)
|
||||
if len(data.member_ids) != len(set(data.member_ids)):
|
||||
raise bad_request("Duplicate member ids")
|
||||
if user.id not in data.member_ids:
|
||||
raise bad_request("Caller must be a chat member")
|
||||
try:
|
||||
return storage_of(request).create_chat(data.title, data.member_ids)
|
||||
except UnknownMember:
|
||||
raise bad_request("Unknown member")
|
||||
|
||||
|
||||
@app.get("/api/v1/chats", response_model=ChatPage)
|
||||
def list_chats(
|
||||
request: Request,
|
||||
user: User = Depends(current_user),
|
||||
limit: int = Query(default=50, ge=1, le=100),
|
||||
cursor: Optional[str] = Query(default=None, min_length=1),
|
||||
) -> ChatPage:
|
||||
after = decode_cursor(cursor, user.id)
|
||||
items, has_more = storage_of(request).list_user_chats(user.id, limit, after)
|
||||
next_cursor = None
|
||||
if has_more and items:
|
||||
next_cursor = encode_cursor(user.id, items[-1].created_at, items[-1].id)
|
||||
return ChatPage(items=items, next_cursor=next_cursor)
|
||||
|
||||
|
||||
@app.get("/api/v1/chats/{chat_id}", response_model=Chat)
|
||||
def get_chat(request: Request, chat_id: UUID, user: User = Depends(current_user)) -> Chat:
|
||||
chat = storage_of(request).get_chat_for_member(chat_id, user.id)
|
||||
if chat is None:
|
||||
raise not_found()
|
||||
return chat
|
||||
|
||||
|
||||
# messages
|
||||
|
||||
def request_fingerprint(data: MessageCreate) -> bytes:
|
||||
"""SHA-256 канонического JSON тела.
|
||||
|
||||
Канонизация: проверенная модель -> JSON с сортировкой ключей и без пробелов.
|
||||
Поэтому порядок ключей и форматирование исходного JSON не влияют на hash.
|
||||
lab4: attachment_ids имеет значение по умолчанию [], поэтому отсутствующее
|
||||
поле и явный [] дают одинаковый hash (контракт). Порядок id значим.
|
||||
"""
|
||||
canonical = json.dumps(
|
||||
data.model_dump(mode="json"), sort_keys=True, separators=(",", ":"), ensure_ascii=False
|
||||
)
|
||||
return hashlib.sha256(canonical.encode("utf-8")).digest()
|
||||
|
||||
|
||||
@app.post("/api/v1/chats/{chat_id}/messages", response_model=Message, status_code=201)
|
||||
def create_message(
|
||||
request: Request,
|
||||
response: Response,
|
||||
chat_id: UUID,
|
||||
data: MessageCreate,
|
||||
user: User = Depends(current_user),
|
||||
) -> Message:
|
||||
# Порядок: аутентификация (dependency) -> ключ -> тело -> доступ к чату.
|
||||
key = request.headers.get("Idempotency-Key")
|
||||
if key is None or not IDEMPOTENCY_KEY_RE.fullmatch(key):
|
||||
raise bad_request("Missing or invalid Idempotency-Key")
|
||||
require_json_content_type(request)
|
||||
# lab4: пустой/пробельный текст допустим только вместе с вложениями.
|
||||
if not data.attachment_ids:
|
||||
ensure_not_blank(data.text)
|
||||
if len(set(data.attachment_ids)) != len(data.attachment_ids):
|
||||
raise bad_request("Duplicate attachment ids")
|
||||
settings = request.app.state.settings
|
||||
try:
|
||||
message, replayed = storage_of(request).create_message(
|
||||
chat_id,
|
||||
user.id,
|
||||
data.text,
|
||||
key,
|
||||
request_fingerprint(data),
|
||||
settings.idempotency_ttl_seconds,
|
||||
data.attachment_ids,
|
||||
)
|
||||
except (NotChatMember, AttachmentNotFound):
|
||||
# Чужое, неизвестное или ещё не принятое (pending) вложение — как и чужой чат: 404.
|
||||
raise not_found()
|
||||
except AttachmentFailed:
|
||||
raise ApiError(409, "conflict", "Attachment processing failed; it cannot be attached")
|
||||
except IdempotencyConflict:
|
||||
raise ApiError(409, "conflict", "Idempotency-Key was used with a different request")
|
||||
except IdempotencyRace:
|
||||
raise ApiError(503, "unavailable", "Please retry with the same Idempotency-Key")
|
||||
if replayed:
|
||||
# Информационный заголовок (не часть контракта): ответ — повтор.
|
||||
response.headers["Idempotency-Replayed"] = "true"
|
||||
return message
|
||||
|
||||
|
||||
@app.get("/api/v1/chats/{chat_id}/messages", response_model=MessagePage)
|
||||
def list_messages(
|
||||
request: Request,
|
||||
chat_id: UUID,
|
||||
user: User = Depends(current_user),
|
||||
limit: int = Query(default=50, ge=1, le=100),
|
||||
cursor: Optional[str] = Query(default=None, min_length=1),
|
||||
) -> MessagePage:
|
||||
after = decode_cursor(cursor, chat_id)
|
||||
result = storage_of(request).list_messages(chat_id, user.id, limit, after)
|
||||
if result is None:
|
||||
raise not_found()
|
||||
items, has_more = result
|
||||
next_cursor = None
|
||||
if has_more and items:
|
||||
next_cursor = encode_cursor(chat_id, items[-1].created_at, items[-1].id)
|
||||
return MessagePage(items=items, next_cursor=next_cursor)
|
||||
|
||||
|
||||
# attachments (lab4)
|
||||
|
||||
ALLOWED_UPLOAD_TYPES = ("image/png", "image/jpeg")
|
||||
UPLOAD_CHUNK = 64 * 1024
|
||||
KNOWN_VARIANTS = ("thumbnail", "crop", "optimized")
|
||||
|
||||
|
||||
async def s3_call(store: ObjectStore, deadline: float, fn, *args):
|
||||
"""Вызов S3 из запроса с жёстким общим дедлайном.
|
||||
|
||||
connect/read timeout boto3 не покрывают DNS: в Docker остановленный
|
||||
контейнер s3 пропадает из DNS, и getaddrinfo висит секундами (замер на
|
||||
Docker Desktop: 10–13 с, у скачивания это давало 504 от HAProxy). Дедлайн
|
||||
ограничивает всё вместе; по истечении — 503 и открытый circuit breaker.
|
||||
Поток с зависшим вызовом доработает в фоне; для PUT это может дать
|
||||
объект-сироту, его уберёт сборщик (src/reconcile.py, проход B).
|
||||
"""
|
||||
try:
|
||||
return await asyncio.wait_for(run_in_threadpool(fn, *args), timeout=deadline)
|
||||
except asyncio.TimeoutError:
|
||||
store.breaker.failure()
|
||||
log.warning("s3 call exceeded %.1fs deadline", deadline)
|
||||
raise ObjectStoreUnavailable()
|
||||
|
||||
|
||||
def objects_of(request: Request) -> ObjectStore:
|
||||
store = request.app.state.objects
|
||||
if store is None:
|
||||
raise ApiError(503, "unavailable", "Attachment storage is not available")
|
||||
return store
|
||||
|
||||
|
||||
def s3_unavailable() -> ApiError:
|
||||
return ApiError(503, "unavailable", "Attachment storage is temporarily unavailable",
|
||||
{"Retry-After": "2"})
|
||||
|
||||
|
||||
def validate_filename(name: Optional[str]) -> str:
|
||||
"""Отображаемое имя: 1–255 code points, без /, \\, NUL и управляющих символов.
|
||||
|
||||
Имя никогда не становится путём или ключом S3; ограничения защищают
|
||||
клиентов, которые покажут/сохранят файл под этим именем.
|
||||
"""
|
||||
if name is None or not name.strip() or len(name) > 255:
|
||||
raise bad_request("Invalid filename")
|
||||
if name in (".", ".."):
|
||||
raise bad_request("Invalid filename")
|
||||
for ch in name:
|
||||
if ch in "/\\\x00" or unicodedata.category(ch) in ("Cc", "Cs") \
|
||||
or ch in "
":
|
||||
raise bad_request("Invalid filename")
|
||||
return name
|
||||
|
||||
|
||||
def upload_media_type(request: Request) -> str:
|
||||
raw = request.headers.get("content-type", "")
|
||||
media = raw.split(";", 1)[0].strip().lower()
|
||||
if media not in ALLOWED_UPLOAD_TYPES:
|
||||
raise ApiError(415, "unsupported_media_type", "Only image/png and image/jpeg are accepted")
|
||||
return media
|
||||
|
||||
|
||||
def declared_length(request: Request) -> Optional[int]:
|
||||
raw = request.headers.get("content-length")
|
||||
if raw is None:
|
||||
return None
|
||||
if not raw.strip().isdigit():
|
||||
raise bad_request("Invalid Content-Length")
|
||||
return int(raw)
|
||||
|
||||
|
||||
async def drain(request: Request, limit: int) -> None:
|
||||
"""Дочитать и выбросить тело (не больше limit байт), чтобы клиент увидел 413,
|
||||
а не обрыв соединения посреди отправки. Память — O(chunk)."""
|
||||
seen = 0
|
||||
try:
|
||||
async for chunk in request.stream():
|
||||
seen += len(chunk)
|
||||
if seen > limit:
|
||||
break
|
||||
except ClientDisconnect:
|
||||
pass
|
||||
|
||||
|
||||
async def read_limited_body(request: Request, limit: int):
|
||||
"""Потоковое чтение тела кусками; не больше limit байт в памяти.
|
||||
|
||||
Возвращает (bytes, md5, sha256). Превышение лимита -> 413 сразу, как только
|
||||
счётчик перешёл limit (остаток тела отбрасывается, не накапливается).
|
||||
"""
|
||||
chunks = []
|
||||
size = 0
|
||||
md5 = hashlib.md5(usedforsecurity=False)
|
||||
sha = hashlib.sha256()
|
||||
async for chunk in request.stream():
|
||||
if not chunk:
|
||||
continue
|
||||
size += len(chunk)
|
||||
if size > limit:
|
||||
chunks.clear()
|
||||
await drain(request, limit)
|
||||
raise ApiError(413, "payload_too_large", "File exceeds %d bytes" % limit)
|
||||
chunks.append(chunk)
|
||||
md5.update(chunk)
|
||||
sha.update(chunk)
|
||||
# Одна склейка в bytes точного размера; список кусков сразу освобождается.
|
||||
# Дальше bytes не копируются: BytesIO(bytes) и memoryview разделяют буфер.
|
||||
data = b"".join(chunks)
|
||||
chunks.clear()
|
||||
return data, md5.digest(), sha.hexdigest()
|
||||
|
||||
|
||||
def _validate_image(request: Request, data: bytes, declared: str):
|
||||
settings = request.app.state.settings
|
||||
with request.app.state.decode_slots: # потолок одновременных разборов на реплику
|
||||
return inspect_image(data, declared, settings.max_image_pixels)
|
||||
|
||||
|
||||
def _inject(settings, point: str) -> None:
|
||||
"""Внедрение отказов — ТОЛЬКО для экземпляра api-faults (профиль compose faults)."""
|
||||
fault = settings.fault_injection
|
||||
if not fault:
|
||||
return
|
||||
if fault == "crash_after_s3_put" and point == "after_put":
|
||||
log.error("FAULT_INJECTION: process exit between S3 PUT and DB COMMIT")
|
||||
os._exit(70)
|
||||
if fault == "db_error_after_s3_put" and point == "after_put":
|
||||
raise psycopg.OperationalError("injected: database failure after S3 PUT")
|
||||
if fault == "s3_error_on_put" and point == "before_put":
|
||||
raise ObjectStoreUnavailable()
|
||||
|
||||
|
||||
@app.post("/api/v1/chats/{chat_id}/attachments", response_model=Attachment, status_code=202)
|
||||
async def upload_attachment(
|
||||
request: Request,
|
||||
background: BackgroundTasks,
|
||||
chat_id: UUID,
|
||||
filename: Optional[str] = Query(default=None),
|
||||
user: User = Depends(current_user),
|
||||
) -> Attachment:
|
||||
"""lab5: приём оригинала -> 202 queued; обработку выполняет worker.
|
||||
|
||||
Порядок проверок (как в lab4): аутентификация -> filename (400) ->
|
||||
Content-Type (415) -> Content-Length (413 без чтения тела) -> членство (404) ->
|
||||
слот загрузки (503) -> потоковое чтение с лимитом (413) -> содержимое (415/413).
|
||||
Затем durable handoff:
|
||||
[1] INSERT attachments(state=pending, staging_key) COMMIT
|
||||
[2] S3 PUT staging/<random> (Content-MD5, SHA-256 в метаданных)
|
||||
[3] UPDATE pending->queued + INSERT attachment_jobs + job_outbox COMMIT -> 202
|
||||
В момент 202 устойчиво сохранены: все байты оригинала (S3 staging), метаданные и
|
||||
намерение обработать (задание + outbox в PostgreSQL). Публикация в брокер — после
|
||||
ответа (background task) и/или sweeper'ом; остановка API после 202 работу не теряет.
|
||||
"""
|
||||
settings = request.app.state.settings
|
||||
storage = storage_of(request)
|
||||
name = validate_filename(filename)
|
||||
declared = upload_media_type(request)
|
||||
length = declared_length(request)
|
||||
if length is not None and length > settings.max_upload_bytes:
|
||||
if length <= 2 * settings.max_upload_bytes:
|
||||
await drain(request, length)
|
||||
raise ApiError(413, "payload_too_large", "File exceeds %d bytes" % settings.max_upload_bytes,
|
||||
{"Connection": "close"} if length > 2 * settings.max_upload_bytes else None)
|
||||
if not await run_in_threadpool(storage.is_chat_member, chat_id, user.id):
|
||||
raise not_found()
|
||||
store = objects_of(request)
|
||||
|
||||
slots: asyncio.Semaphore = request.app.state.upload_slots
|
||||
try:
|
||||
await asyncio.wait_for(slots.acquire(), timeout=settings.upload_queue_timeout_seconds)
|
||||
except asyncio.TimeoutError:
|
||||
raise ApiError(503, "unavailable", "Too many concurrent uploads", {"Retry-After": "1"})
|
||||
try:
|
||||
try:
|
||||
data, md5_digest, sha256_hex = await asyncio.wait_for(
|
||||
read_limited_body(request, settings.max_upload_bytes),
|
||||
timeout=settings.upload_read_timeout_seconds,
|
||||
)
|
||||
except asyncio.TimeoutError:
|
||||
raise bad_request("Upload was not completed in time")
|
||||
except ClientDisconnect:
|
||||
# Клиент оборвал загрузку: в БД и S3 ничего не записано.
|
||||
log.info("upload aborted by client before completion")
|
||||
raise bad_request("Upload was interrupted")
|
||||
|
||||
try:
|
||||
content_type, width, height = await run_in_threadpool(_validate_image, request, data, declared)
|
||||
except ImageRejected as exc:
|
||||
raise ApiError(exc.status, exc.code, exc.message)
|
||||
|
||||
attachment_id = uuid_mod.uuid4()
|
||||
job_id = uuid_mod.uuid4()
|
||||
# Ключи назначает сервер: не связаны с filename. staging — случайный;
|
||||
# конечный ключ оригинала детерминирован от id (идемпотентная публикация worker'ом).
|
||||
staging_key = "%s%s" % (STAGING_PREFIX, uuid_mod.uuid4().hex)
|
||||
final_key = "final/%s/original" % attachment_id
|
||||
# [1] намерение
|
||||
created = await run_in_threadpool(
|
||||
storage.create_pending_attachment, attachment_id, chat_id, user.id, name,
|
||||
content_type, len(data), sha256_hex, width, height, final_key, staging_key,
|
||||
settings.pipeline_version,
|
||||
)
|
||||
if not created:
|
||||
raise not_found()
|
||||
# [2] байты в устойчивый staging
|
||||
try:
|
||||
_inject(settings, "before_put")
|
||||
await s3_call(store, settings.s3_upload_deadline_seconds,
|
||||
store.put, staging_key, data, content_type, md5_digest, sha256_hex)
|
||||
except ObjectStoreUnavailable:
|
||||
await _compensate(storage, store, attachment_id, None)
|
||||
raise s3_unavailable()
|
||||
finally:
|
||||
del data
|
||||
# [3] задание + outbox одной транзакцией
|
||||
try:
|
||||
_inject(settings, "after_put")
|
||||
accepted = await run_in_threadpool(
|
||||
storage.accept_attachment, attachment_id, job_id,
|
||||
job_payload(job_id, attachment_id, settings.pipeline_version, 1),
|
||||
settings.job_max_attempts, settings.relay_fast_path_grace_seconds,
|
||||
)
|
||||
except (psycopg.Error, PoolTimeout):
|
||||
await _compensate(storage, store, attachment_id, staging_key)
|
||||
raise
|
||||
if accepted is None:
|
||||
# Строку уже забрал сборщик (загрузка шла дольше grace) — объект лишний.
|
||||
await _compensate(storage, store, attachment_id, staging_key)
|
||||
raise ApiError(503, "unavailable", "Upload expired, please retry")
|
||||
attachment, outbox_id = accepted
|
||||
relay = request.app.state.relay
|
||||
if relay is not None:
|
||||
# Выполняется ПОСЛЕ отправки ответа 202.
|
||||
background.add_task(relay.publish_now, outbox_id)
|
||||
return attachment
|
||||
finally:
|
||||
slots.release()
|
||||
|
||||
|
||||
async def _compensate(storage: Storage, store: ObjectStore, attachment_id: UUID, object_key: Optional[str]) -> None:
|
||||
"""Best-effort откат в рамках запроса. Если не удалось — уберёт сборщик."""
|
||||
if object_key is not None:
|
||||
try:
|
||||
await s3_call(store, 5.0, store.delete, object_key)
|
||||
except Exception as exc:
|
||||
log.warning("compensation: object delete deferred to gc (%s)", type(exc).__name__)
|
||||
try:
|
||||
await run_in_threadpool(storage.delete_pending_attachment, attachment_id)
|
||||
except Exception as exc:
|
||||
log.warning("compensation: pending row left for gc (%s)", type(exc).__name__)
|
||||
|
||||
|
||||
@app.get("/api/v1/attachments/{attachment_id}", response_model=Attachment)
|
||||
def get_attachment(request: Request, attachment_id: UUID, user: User = Depends(current_user)) -> Attachment:
|
||||
found = storage_of(request).get_attachment_for_member(attachment_id, user.id)
|
||||
if found is None:
|
||||
raise not_found()
|
||||
return found[0]
|
||||
|
||||
|
||||
VARIANT_CONTENT_TYPES = {"thumbnail": "image/jpeg", "crop": "image/jpeg", "optimized": "image/webp"}
|
||||
|
||||
|
||||
def content_disposition(filename: str) -> str:
|
||||
from urllib.parse import quote
|
||||
ascii_name = "".join(c if 32 <= ord(c) < 127 and c not in ('"', "\\", ";") else "_" for c in filename)
|
||||
return "inline; filename=\"%s\"; filename*=UTF-8''%s" % (ascii_name, quote(filename, safe=""))
|
||||
|
||||
|
||||
@app.get("/api/v1/attachments/{attachment_id}/content")
|
||||
async def download_attachment(
|
||||
request: Request,
|
||||
attachment_id: UUID,
|
||||
variant: Optional[str] = Query(default=None),
|
||||
user: User = Depends(current_user),
|
||||
):
|
||||
"""Байты оригинала или варианта через авторизованный API (без redirect), потоком из S3.
|
||||
|
||||
lab5: queued/processing/failed -> 409 conflict; у ready нет такого варианта -> 404.
|
||||
"""
|
||||
if variant is not None and variant != "original" and variant not in KNOWN_VARIANTS:
|
||||
raise bad_request("Invalid variant")
|
||||
storage = storage_of(request)
|
||||
found = await run_in_threadpool(storage.get_attachment_for_member, attachment_id, user.id)
|
||||
if found is None:
|
||||
raise not_found()
|
||||
attachment, object_key, pipeline_version = found
|
||||
if attachment.status != "ready":
|
||||
raise ApiError(409, "conflict", "Attachment is not ready (status: %s)" % attachment.status)
|
||||
if variant in KNOWN_VARIANTS:
|
||||
meta = await run_in_threadpool(storage.variant_object, attachment_id, pipeline_version, variant)
|
||||
if meta is None:
|
||||
raise not_found()
|
||||
object_key, media_type, size_bytes, etag = meta
|
||||
disposition_name = "%s-%s.%s" % (variant, attachment.filename.rsplit(".", 1)[0][:200],
|
||||
"webp" if media_type == "image/webp" else "jpg")
|
||||
else:
|
||||
media_type, size_bytes, etag = attachment.content_type, attachment.size_bytes, attachment.sha256
|
||||
disposition_name = attachment.filename
|
||||
store = objects_of(request)
|
||||
try:
|
||||
chunks, length = await s3_call(store, request.app.state.settings.s3_download_deadline_seconds,
|
||||
store.open, object_key)
|
||||
except ObjectStoreUnavailable:
|
||||
raise s3_unavailable()
|
||||
except ObjectMissing:
|
||||
log.error("ready attachment %s has no object in S3 (see python -m src.reconcile)", attachment.id)
|
||||
raise ApiError(500, "internal_error", "Internal server error")
|
||||
if length != size_bytes:
|
||||
chunks.close() # закрыть поток S3
|
||||
log.error("attachment %s: object size differs from metadata", attachment.id)
|
||||
raise ApiError(500, "internal_error", "Internal server error")
|
||||
return StreamingResponse(
|
||||
chunks,
|
||||
media_type=media_type,
|
||||
headers={
|
||||
"Content-Length": str(size_bytes),
|
||||
"Cache-Control": "private, no-store",
|
||||
"X-Content-Type-Options": "nosniff",
|
||||
"Content-Disposition": content_disposition(disposition_name),
|
||||
"ETag": '"%s"' % etag,
|
||||
},
|
||||
)
|
||||
Reference in New Issue
Block a user