Упростить код API и обновить доказательства
Skeleton contracts / contracts (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
- Выбранный трек 5: трек 1, согласованность кэша
|
||||
- Ветка: `solution/bvt2304-borzov`
|
||||
- Commit: `f87929c`
|
||||
- MR: https://gitea.privcloud.tech/mtusi2026/mtusi2026lab2/pulls/28
|
||||
- MR: заполняется после публикации новой версии
|
||||
|
||||
В этой работе я продолжил REST-мессенджер из лабораторной 1. In-memory-хранилище заменено на PostgreSQL, добавлены пароль при регистрации, Basic-аутентификация только для входа и непрозрачные Bearer-сессии. Redis используется для TTL-сессий и кэша списка чатов. Сессии записываются в Redis с AOF и переживают перезапуск API и Redis-volume.
|
||||
|
||||
@@ -59,7 +59,7 @@ PostgreSQL является источником истины для польз
|
||||
|
||||
Гипотеза: Redis-кэш уменьшит задержку повторного чтения списка чатов, а явная инвалидация после создания чата сохранит read-after-write.
|
||||
|
||||
Методика: один синтетический пользователь на локальном Docker Compose, холодный `GET /api/v1/chats`, 30 последовательных чтений и 20 одновременных чтений после удаления ключа. Счётчик SQL-запросов включён только в локальном debug-режиме. В последнем прогоне получены cold 3.221 ms, warm p50 1.992 ms, warm p95 2.350 ms; 20 конкурентных запросов выполнили 1 SQL-чтение благодаря блокировке, после удаления ключа и рестарта Redis восстановление выполнило 1 SQL-чтение. После `POST /api/v1/chats` ключ пользователя удаляется; следующий GET заново читает PostgreSQL и возвращает новый чат. Ограничение измерения — один ноутбук и небольшой синтетический набор данных, поэтому результат показывает поведение защиты от stampede и направление задержки, а не универсальный RPS.
|
||||
Методика: один синтетический пользователь на локальном Docker Compose, холодный `GET /api/v1/chats`, 30 последовательных чтений и 20 одновременных чтений после удаления ключа. Счётчик SQL-запросов включён только в локальном debug-режиме. В последнем прогоне получены cold 5.189 ms, warm p50 2.104 ms, warm p95 2.530 ms; 20 конкурентных запросов выполнили 1 SQL-чтение благодаря блокировке, после удаления ключа и рестарта Redis восстановление выполнило 1 SQL-чтение. После `POST /api/v1/chats` ключ пользователя удаляется; следующий GET заново читает PostgreSQL и возвращает новый чат. Ограничение измерения — один ноутбук и небольшой синтетический набор данных, поэтому результат показывает поведение защиты от stampede и направление задержки, а не универсальный RPS.
|
||||
|
||||
## Эксперименты и отказы
|
||||
|
||||
|
||||
+433
-214
@@ -1,7 +1,11 @@
|
||||
import base64, hashlib, json, os, secrets, threading, uuid
|
||||
from datetime import datetime, timezone, timedelta
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
import base64
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import secrets
|
||||
import threading
|
||||
import uuid
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
import redis
|
||||
from argon2 import PasswordHasher
|
||||
@@ -10,253 +14,468 @@ from fastapi.responses import JSONResponse, Response
|
||||
from psycopg.rows import dict_row
|
||||
from psycopg_pool import ConnectionPool
|
||||
|
||||
DATABASE_URL = os.getenv("DATABASE_URL", "postgresql://messenger:messenger@postgres:5432/messenger")
|
||||
REDIS_URL = os.getenv("REDIS_URL", "redis://redis:6379/0")
|
||||
SESSION_TTL = int(os.getenv("SESSION_TTL_SECONDS", "3600"))
|
||||
INSTANCE_ID = os.getenv("INSTANCE_ID", uuid.uuid4().hex[:12])
|
||||
ph = PasswordHasher()
|
||||
pool = ConnectionPool(DATABASE_URL, min_size=1, max_size=int(os.getenv("DB_POOL_SIZE", "10")), timeout=5, kwargs={"row_factory": dict_row}, open=False)
|
||||
cache = redis.Redis.from_url(REDIS_URL, decode_responses=True, socket_connect_timeout=1)
|
||||
DATABASE_URL = os.getenv('DATABASE_URL', 'postgresql://messenger:messenger@postgres:5432/messenger')
|
||||
REDIS_URL = os.getenv('REDIS_URL', 'redis://redis:6379/0')
|
||||
SESSION_TTL_SECONDS = int(os.getenv('SESSION_TTL_SECONDS', '3600'))
|
||||
INSTANCE_ID = os.getenv('INSTANCE_ID', uuid.uuid4().hex[:12])
|
||||
DB_POOL_SIZE = int(os.getenv('DB_POOL_SIZE', '10'))
|
||||
|
||||
password_hasher = PasswordHasher()
|
||||
db_pool = ConnectionPool(
|
||||
DATABASE_URL, min_size=1, max_size=DB_POOL_SIZE, timeout=5,
|
||||
kwargs={'row_factory': dict_row}, open=False,
|
||||
)
|
||||
redis_client = redis.Redis.from_url(
|
||||
REDIS_URL, decode_responses=True, socket_connect_timeout=1,
|
||||
)
|
||||
chat_cache_lock = threading.Lock()
|
||||
cache_sql_reads = 0
|
||||
app = FastAPI(title="MTUSI Messenger Lab 2", version="1.2.0")
|
||||
chat_cache_sql_reads = 0
|
||||
app = FastAPI(title='MTUSI Messenger Lab 2', version='1.2.0')
|
||||
|
||||
def now(): return datetime.now(timezone.utc)
|
||||
def iso(value): return value.astimezone(timezone.utc).isoformat().replace("+00:00", "Z")
|
||||
def uid(value):
|
||||
try: return uuid.UUID(str(value))
|
||||
except (ValueError, TypeError): raise ValueError("invalid uuid")
|
||||
def user_view(row): return {"id": str(row["id"]), "username": row["username"], "display_name": row["display_name"], "created_at": iso(row["created_at"])}
|
||||
def chat_view(row, members=None):
|
||||
return {"id": str(row["id"]), "title": row["title"], "member_ids": [str(x) for x in (members or [])], "created_at": iso(row["created_at"])}
|
||||
def message_view(row):
|
||||
return {"id": str(row["id"]), "chat_id": str(row["chat_id"]), "sender_id": str(row["sender_id"]), "text": row["text"], "created_at": iso(row["created_at"])}
|
||||
|
||||
def error(request, status, code, message, headers=None):
|
||||
rid = getattr(request.state, "request_id", uuid.uuid4().hex)
|
||||
h = {"X-Request-Id": rid, "X-Instance-Id": INSTANCE_ID}
|
||||
if headers: h.update(headers)
|
||||
return JSONResponse({"error": {"code": code, "message": message, "request_id": rid}}, status_code=status, headers=h)
|
||||
def utc_now():
|
||||
return datetime.now(timezone.utc)
|
||||
|
||||
@app.middleware("http")
|
||||
|
||||
def format_time(value):
|
||||
return value.astimezone(timezone.utc).isoformat().replace('+00:00', 'Z')
|
||||
|
||||
|
||||
def parse_uuid(value):
|
||||
try:
|
||||
return uuid.UUID(str(value))
|
||||
except (TypeError, ValueError) as exc:
|
||||
raise ValueError('invalid uuid') from exc
|
||||
|
||||
|
||||
def user_response(row):
|
||||
return {
|
||||
'id': str(row['id']), 'username': row['username'],
|
||||
'display_name': row['display_name'], 'created_at': format_time(row['created_at']),
|
||||
}
|
||||
|
||||
|
||||
def chat_response(row, member_ids):
|
||||
return {
|
||||
'id': str(row['id']), 'title': row['title'],
|
||||
'member_ids': [str(item) for item in member_ids],
|
||||
'created_at': format_time(row['created_at']),
|
||||
}
|
||||
|
||||
|
||||
def message_response(row):
|
||||
return {
|
||||
'id': str(row['id']), 'chat_id': str(row['chat_id']),
|
||||
'sender_id': str(row['sender_id']), 'text': row['text'],
|
||||
'created_at': format_time(row['created_at']),
|
||||
}
|
||||
|
||||
|
||||
def error_response(request, status_code, code, message, extra_headers=None):
|
||||
request_id = getattr(request.state, 'request_id', uuid.uuid4().hex)
|
||||
headers = {'X-Request-Id': request_id, 'X-Instance-Id': INSTANCE_ID}
|
||||
if extra_headers:
|
||||
headers.update(extra_headers)
|
||||
return JSONResponse(
|
||||
status_code=status_code,
|
||||
content={'error': {'code': code, 'message': message, 'request_id': request_id}},
|
||||
headers=headers,
|
||||
)
|
||||
|
||||
|
||||
@app.middleware('http')
|
||||
async def request_context(request: Request, call_next):
|
||||
request.state.request_id = request.headers.get("X-Request-Id") or uuid.uuid4().hex
|
||||
request.state.request_id = request.headers.get('X-Request-Id') or uuid.uuid4().hex
|
||||
try:
|
||||
response = await call_next(request)
|
||||
except Exception:
|
||||
return error(request, 500, "internal_error", "Внутренняя ошибка")
|
||||
response.headers["X-Request-Id"] = request.state.request_id
|
||||
response.headers["X-Instance-Id"] = INSTANCE_ID
|
||||
return error_response(request, 500, 'internal_error', 'Внутренняя ошибка')
|
||||
response.headers['X-Request-Id'] = request.state.request_id
|
||||
response.headers['X-Instance-Id'] = INSTANCE_ID
|
||||
return response
|
||||
|
||||
@app.on_event("startup")
|
||||
def startup():
|
||||
pool.open(wait=True)
|
||||
with pool.connection() as conn:
|
||||
conn.execute("SELECT 1")
|
||||
|
||||
@app.on_event("shutdown")
|
||||
def shutdown(): pool.close()
|
||||
@app.on_event('startup')
|
||||
def start_application():
|
||||
db_pool.open(wait=True)
|
||||
with db_pool.connection() as connection:
|
||||
connection.execute('SELECT 1')
|
||||
|
||||
async def body(request):
|
||||
try: value = await request.json()
|
||||
except Exception: raise ValueError("malformed json")
|
||||
if not isinstance(value, dict): raise ValueError("object expected")
|
||||
|
||||
@app.on_event('shutdown')
|
||||
def stop_application():
|
||||
db_pool.close()
|
||||
|
||||
|
||||
async def read_json_body(request):
|
||||
try:
|
||||
value = await request.json()
|
||||
except Exception as exc:
|
||||
raise ValueError('malformed json') from exc
|
||||
if not isinstance(value, dict):
|
||||
raise ValueError('request body must be an object')
|
||||
return value
|
||||
def require_fields(data, required, allowed):
|
||||
if set(data) != set(required) if allowed is None else not set(data).issubset(set(allowed)) or not set(required).issubset(set(data)): raise ValueError("invalid fields")
|
||||
def bearer(request):
|
||||
value = request.headers.get("Authorization", "")
|
||||
if not value.startswith("Bearer ") or len(value) < 40: return None
|
||||
token = value[7:]
|
||||
|
||||
|
||||
def require_fields(data, required):
|
||||
if set(data) != required:
|
||||
raise ValueError('unexpected or missing fields')
|
||||
|
||||
|
||||
def get_bearer_session(request):
|
||||
authorization = request.headers.get('Authorization', '')
|
||||
if not authorization.startswith('Bearer '):
|
||||
return None
|
||||
token = authorization.removeprefix('Bearer ')
|
||||
if len(token) < 40:
|
||||
return None
|
||||
try:
|
||||
raw = cache.get("session:" + token)
|
||||
except Exception: raw = None
|
||||
if not raw: return None
|
||||
try: return json.loads(raw)
|
||||
except Exception: return None
|
||||
def auth(request):
|
||||
session = bearer(request)
|
||||
if not session: return None
|
||||
with pool.connection() as conn:
|
||||
return conn.execute("SELECT * FROM users WHERE id=%s", (session["user_id"],)).fetchone()
|
||||
def auth_or_error(request):
|
||||
user = auth(request)
|
||||
if not user: return None, error(request, 401, "unauthorized", "Требуется действующая Bearer-сессия", {"WWW-Authenticate": 'Bearer realm="messenger"'})
|
||||
value = redis_client.get('session:' + token)
|
||||
return json.loads(value) if value else None
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
def get_current_user(request):
|
||||
session = get_bearer_session(request)
|
||||
if session is None:
|
||||
return None
|
||||
with db_pool.connection() as connection:
|
||||
return connection.execute(
|
||||
'SELECT * FROM users WHERE id=%s', (session['user_id'],)
|
||||
).fetchone()
|
||||
|
||||
|
||||
def require_current_user(request):
|
||||
user = get_current_user(request)
|
||||
if user is None:
|
||||
return None, error_response(
|
||||
request, 401, 'unauthorized', 'Требуется действующая Bearer-сессия',
|
||||
{'WWW-Authenticate': 'Bearer realm="messenger"'},
|
||||
)
|
||||
return user, None
|
||||
def member(conn, chat_id, user_id):
|
||||
return conn.execute("SELECT 1 FROM chat_members WHERE chat_id=%s AND user_id=%s", (chat_id, user_id)).fetchone() is not None
|
||||
|
||||
@app.get("/health/live")
|
||||
def live(): return {"status": "ok"}
|
||||
@app.get("/health/ready")
|
||||
def ready(request: Request):
|
||||
|
||||
def is_chat_member(connection, chat_id, user_id):
|
||||
row = connection.execute(
|
||||
'SELECT 1 FROM chat_members WHERE chat_id=%s AND user_id=%s',
|
||||
(chat_id, user_id),
|
||||
).fetchone()
|
||||
return row is not None
|
||||
|
||||
|
||||
def make_cursor(scope, owner_id, row):
|
||||
created_at = row['created_at']
|
||||
if isinstance(created_at, datetime):
|
||||
created_at = format_time(created_at)
|
||||
data = [scope, owner_id, created_at, str(row['id'])]
|
||||
return base64.urlsafe_b64encode(json.dumps(data).encode()).decode().rstrip('=')
|
||||
|
||||
|
||||
def read_cursor(cursor, expected_scope, expected_owner):
|
||||
try:
|
||||
with pool.connection() as conn: conn.execute("SELECT 1")
|
||||
cache.ping()
|
||||
return {"status": "ok"}
|
||||
except Exception: return error(request, 503, "unavailable", "Зависимость недоступна")
|
||||
padding = '=' * ((4 - len(cursor) % 4) % 4)
|
||||
value = json.loads(base64.urlsafe_b64decode((cursor + padding).encode()))
|
||||
if value[0] != expected_scope or value[1] != expected_owner:
|
||||
raise ValueError
|
||||
timestamp = datetime.fromisoformat(value[2].replace('Z', '+00:00'))
|
||||
return timestamp, parse_uuid(value[3])
|
||||
except (IndexError, TypeError, ValueError, json.JSONDecodeError) as exc:
|
||||
raise ValueError('invalid cursor') from exc
|
||||
|
||||
@app.get("/debug/cache-stats")
|
||||
|
||||
def page_chats(items, limit, cursor_data, owner_id):
|
||||
if cursor_data:
|
||||
items = [
|
||||
item for item in items
|
||||
if (
|
||||
datetime.fromisoformat(item['created_at'].replace('Z', '+00:00')),
|
||||
uuid.UUID(item['id']),
|
||||
) > cursor_data
|
||||
]
|
||||
page = items[:limit]
|
||||
next_cursor = make_cursor('chats', owner_id, page[-1]) if len(items) > limit and page else None
|
||||
return {'items': page, 'next_cursor': next_cursor}
|
||||
|
||||
|
||||
@app.get('/health/live')
|
||||
def health_live():
|
||||
return {'status': 'ok'}
|
||||
|
||||
|
||||
@app.get('/health/ready')
|
||||
def health_ready(request: Request):
|
||||
try:
|
||||
with db_pool.connection() as connection:
|
||||
connection.execute('SELECT 1')
|
||||
redis_client.ping()
|
||||
except Exception:
|
||||
return error_response(request, 503, 'unavailable', 'Зависимость недоступна')
|
||||
return {'status': 'ok'}
|
||||
|
||||
|
||||
@app.get('/debug/cache-stats')
|
||||
def cache_stats(request: Request):
|
||||
if os.getenv("ENABLE_DEBUG_METRICS") != "1": return error(request, 404, "not_found", "Ресурс не найден")
|
||||
return {"chat_list_sql_reads": cache_sql_reads}
|
||||
if os.getenv('ENABLE_DEBUG_METRICS') != '1':
|
||||
return error_response(request, 404, 'not_found', 'Ресурс не найден')
|
||||
return {'chat_list_sql_reads': chat_cache_sql_reads}
|
||||
|
||||
@app.post("/api/v1/users", status_code=201)
|
||||
|
||||
@app.post('/api/v1/users', status_code=201)
|
||||
async def create_user(request: Request):
|
||||
try:
|
||||
data = await body(request); require_fields(data, ["username", "display_name", "password"], ["username", "display_name", "password"])
|
||||
username, display, password = data["username"], data["display_name"], data["password"]
|
||||
if not isinstance(username, str) or not __import__("re").fullmatch(r"[a-z0-9_]{3,32}", username) or not isinstance(display, str) or not display.strip() or not 1 <= len(display) <= 100 or not isinstance(password, str) or not 12 <= len(password) <= 128: raise ValueError("invalid fields")
|
||||
user_id, created = uuid.uuid4(), now(); hashed = ph.hash(password)
|
||||
with pool.connection() as conn:
|
||||
try: conn.execute("INSERT INTO users VALUES (%s,%s,%s,%s,%s)", (user_id, username, display, hashed, created)); conn.commit()
|
||||
data = await read_json_body(request)
|
||||
require_fields(data, {'username', 'display_name', 'password'})
|
||||
username, display_name, password = data['username'], data['display_name'], data['password']
|
||||
if not isinstance(username, str) or not re.fullmatch(r'[a-z0-9_]{3,32}', username):
|
||||
raise ValueError('invalid username')
|
||||
if not isinstance(display_name, str) or not display_name.strip() or not 1 <= len(display_name) <= 100:
|
||||
raise ValueError('invalid display name')
|
||||
if not isinstance(password, str) or not 12 <= len(password) <= 128:
|
||||
raise ValueError('invalid password')
|
||||
|
||||
user_id, created_at = uuid.uuid4(), utc_now()
|
||||
password_hash = password_hasher.hash(password)
|
||||
with db_pool.connection() as connection:
|
||||
try:
|
||||
connection.execute(
|
||||
'INSERT INTO users VALUES (%s,%s,%s,%s,%s)',
|
||||
(user_id, username, display_name, password_hash, created_at),
|
||||
)
|
||||
connection.commit()
|
||||
except Exception as exc:
|
||||
conn.rollback()
|
||||
if "unique" in str(exc).lower(): return error(request, 409, "conflict", "Имя пользователя уже занято")
|
||||
connection.rollback()
|
||||
if 'unique' in str(exc).lower():
|
||||
return error_response(request, 409, 'conflict', 'Имя пользователя уже занято')
|
||||
raise
|
||||
return user_view({"id": user_id, "username": username, "display_name": display, "created_at": created})
|
||||
except ValueError: return error(request, 400, "invalid_request", "Неверные поля запроса")
|
||||
return user_response({'id': user_id, 'username': username, 'display_name': display_name, 'created_at': created_at})
|
||||
except ValueError:
|
||||
return error_response(request, 400, 'invalid_request', 'Неверные поля запроса')
|
||||
|
||||
@app.post("/api/v1/auth/sessions", status_code=201)
|
||||
|
||||
@app.post('/api/v1/auth/sessions', status_code=201)
|
||||
def create_session(request: Request):
|
||||
header = request.headers.get("Authorization", "")
|
||||
authorization = request.headers.get('Authorization', '')
|
||||
try:
|
||||
if not header.startswith("Basic "): raise ValueError
|
||||
decoded = base64.b64decode(header[6:], validate=True).decode("utf-8"); username, password = decoded.split(":", 1)
|
||||
with pool.connection() as conn: row = conn.execute("SELECT * FROM users WHERE username=%s", (username,)).fetchone()
|
||||
if not row: raise ValueError
|
||||
ph.verify(row["password_hash"], password)
|
||||
except Exception: return error(request, 401, "unauthorized", "Неверные учётные данные", {"WWW-Authenticate": 'Basic realm="messenger", charset="UTF-8"'})
|
||||
token = secrets.token_urlsafe(32); expires = now() + timedelta(seconds=SESSION_TTL)
|
||||
cache.setex("session:" + token, SESSION_TTL, json.dumps({"user_id": str(row["id"]), "expires_at": iso(expires)}))
|
||||
return {"token": token, "token_type": "Bearer", "expires_at": iso(expires), "user": user_view(row)}
|
||||
if not authorization.startswith('Basic '):
|
||||
raise ValueError
|
||||
credentials = base64.b64decode(authorization.removeprefix('Basic '), validate=True).decode()
|
||||
username, password = credentials.split(':', 1)
|
||||
with db_pool.connection() as connection:
|
||||
user = connection.execute('SELECT * FROM users WHERE username=%s', (username,)).fetchone()
|
||||
if user is None:
|
||||
raise ValueError
|
||||
password_hasher.verify(user['password_hash'], password)
|
||||
except Exception:
|
||||
return error_response(
|
||||
request, 401, 'unauthorized', 'Неверные учётные данные',
|
||||
{'WWW-Authenticate': 'Basic realm="messenger", charset="UTF-8"'},
|
||||
)
|
||||
|
||||
@app.delete("/api/v1/auth/sessions/current", status_code=204)
|
||||
token = secrets.token_urlsafe(32)
|
||||
expires_at = utc_now() + timedelta(seconds=SESSION_TTL_SECONDS)
|
||||
redis_client.setex(
|
||||
'session:' + token,
|
||||
SESSION_TTL_SECONDS,
|
||||
json.dumps({'user_id': str(user['id']), 'expires_at': format_time(expires_at)}),
|
||||
)
|
||||
return {
|
||||
'token': token, 'token_type': 'Bearer',
|
||||
'expires_at': format_time(expires_at), 'user': user_response(user),
|
||||
}
|
||||
|
||||
|
||||
@app.delete('/api/v1/auth/sessions/current', status_code=204)
|
||||
def logout(request: Request):
|
||||
if not bearer(request): return error(request, 401, "unauthorized", "Сессия недействительна", {"WWW-Authenticate": 'Bearer realm="messenger"'})
|
||||
cache.delete("session:" + request.headers["Authorization"][7:]); return Response(status_code=204)
|
||||
if get_bearer_session(request) is None:
|
||||
return error_response(
|
||||
request, 401, 'unauthorized', 'Сессия недействительна',
|
||||
{'WWW-Authenticate': 'Bearer realm="messenger"'},
|
||||
)
|
||||
token = request.headers['Authorization'].removeprefix('Bearer ')
|
||||
redis_client.delete('session:' + token)
|
||||
return Response(status_code=204)
|
||||
|
||||
@app.get("/api/v1/users/me")
|
||||
def me(request: Request):
|
||||
user, failure = auth_or_error(request)
|
||||
if failure: return failure
|
||||
return user_view(user)
|
||||
|
||||
@app.post("/api/v1/chats", status_code=201)
|
||||
@app.get('/api/v1/users/me')
|
||||
def get_me(request: Request):
|
||||
user, failure = require_current_user(request)
|
||||
return failure if failure else user_response(user)
|
||||
|
||||
|
||||
@app.post('/api/v1/chats', status_code=201)
|
||||
async def create_chat(request: Request):
|
||||
user, failure = auth_or_error(request)
|
||||
if failure: return failure
|
||||
user, failure = require_current_user(request)
|
||||
if failure:
|
||||
return failure
|
||||
try:
|
||||
data = await body(request); require_fields(data, ["title", "member_ids"], ["title", "member_ids"])
|
||||
title, members = data["title"], data["member_ids"]
|
||||
if not isinstance(title, str) or not title.strip() or not 1 <= len(title) <= 100 or not isinstance(members, list) or not 1 <= len(members) <= 100 or len(set(members)) != len(members): raise ValueError
|
||||
ids = [uid(x) for x in members]; me_id = user["id"]
|
||||
if me_id not in ids: raise ValueError
|
||||
chat_id, created = uuid.uuid4(), now()
|
||||
with pool.connection() as conn:
|
||||
if conn.execute("SELECT count(*) AS n FROM users WHERE id = ANY(%s)", (ids,)).fetchone()["n"] != len(ids): raise ValueError
|
||||
with conn.transaction():
|
||||
conn.execute("INSERT INTO chats VALUES (%s,%s,%s)", (chat_id,title,created))
|
||||
for member_id in ids:
|
||||
conn.execute("INSERT INTO chat_members VALUES (%s,%s)", (chat_id, member_id))
|
||||
for x in ids: cache.delete("chats:" + str(x))
|
||||
return chat_view({"id": chat_id, "title": title, "created_at": created}, ids)
|
||||
except ValueError: return error(request, 400, "invalid_request", "Неверные данные чата")
|
||||
data = await read_json_body(request)
|
||||
require_fields(data, {'title', 'member_ids'})
|
||||
title, member_values = data['title'], data['member_ids']
|
||||
if not isinstance(title, str) or not title.strip() or not 1 <= len(title) <= 100:
|
||||
raise ValueError('invalid title')
|
||||
if not isinstance(member_values, list) or not 1 <= len(member_values) <= 100:
|
||||
raise ValueError('invalid members')
|
||||
if len(set(member_values)) != len(member_values):
|
||||
raise ValueError('duplicate members')
|
||||
member_ids = [parse_uuid(value) for value in member_values]
|
||||
if user['id'] not in member_ids:
|
||||
raise ValueError('current user must be a member')
|
||||
|
||||
@app.get("/api/v1/chats")
|
||||
chat_id, created_at = uuid.uuid4(), utc_now()
|
||||
with db_pool.connection() as connection:
|
||||
count = connection.execute(
|
||||
'SELECT count(*) AS n FROM users WHERE id = ANY(%s)', (member_ids,)
|
||||
).fetchone()['n']
|
||||
if count != len(member_ids):
|
||||
raise ValueError('unknown member')
|
||||
with connection.transaction():
|
||||
connection.execute('INSERT INTO chats VALUES (%s,%s,%s)', (chat_id, title, created_at))
|
||||
for member_id in member_ids:
|
||||
connection.execute('INSERT INTO chat_members VALUES (%s,%s)', (chat_id, member_id))
|
||||
for member_id in member_ids:
|
||||
try:
|
||||
redis_client.delete('chats:' + str(member_id))
|
||||
except Exception:
|
||||
pass
|
||||
return chat_response({'id': chat_id, 'title': title, 'created_at': created_at}, member_ids)
|
||||
except ValueError:
|
||||
return error_response(request, 400, 'invalid_request', 'Неверные данные чата')
|
||||
|
||||
|
||||
@app.get('/api/v1/chats')
|
||||
def list_chats(request: Request, limit: int = 50, cursor: str | None = None):
|
||||
global cache_sql_reads
|
||||
user, failure = auth_or_error(request)
|
||||
if failure: return failure
|
||||
if limit < 1 or limit > 100: return error(request, 400, "invalid_request", "limit должен быть от 1 до 100")
|
||||
if cursor == "": return error(request, 400, "invalid_request", "Неверный cursor")
|
||||
key = "chats:" + str(user["id"])
|
||||
after = None
|
||||
if cursor:
|
||||
global chat_cache_sql_reads
|
||||
user, failure = require_current_user(request)
|
||||
if failure:
|
||||
return failure
|
||||
if not 1 <= limit <= 100:
|
||||
return error_response(request, 400, 'invalid_request', 'limit должен быть от 1 до 100')
|
||||
|
||||
cursor_data = None
|
||||
if cursor is not None:
|
||||
if not cursor:
|
||||
return error_response(request, 400, 'invalid_request', 'Неверный cursor')
|
||||
try:
|
||||
decoded = json.loads(base64.urlsafe_b64decode(cursor.encode() + b"=" * ((4 - len(cursor) % 4) % 4)))
|
||||
if decoded[0] != "chats" or decoded[1] != str(user["id"]): raise ValueError
|
||||
after = (datetime.fromisoformat(decoded[2].replace("Z", "+00:00")), uid(decoded[3]))
|
||||
except Exception: return error(request, 400, "invalid_request", "Неверный cursor")
|
||||
cursor_data = read_cursor(cursor, 'chats', str(user['id']))
|
||||
except ValueError:
|
||||
return error_response(request, 400, 'invalid_request', 'Неверный cursor')
|
||||
|
||||
cache_key = 'chats:' + str(user['id'])
|
||||
try:
|
||||
cached = cache.get(key)
|
||||
cached = redis_client.get(cache_key)
|
||||
if cached:
|
||||
result = json.loads(cached)
|
||||
if after: result = [x for x in result if (datetime.fromisoformat(x["created_at"].replace("Z", "+00:00")), uuid.UUID(x["id"])) > after]
|
||||
page = result[:limit]
|
||||
next_cursor = None
|
||||
if len(result) > limit and page:
|
||||
tail = page[-1]; next_cursor = base64.urlsafe_b64encode(json.dumps(["chats", str(user["id"]), tail["created_at"], tail["id"]]).encode()).decode().rstrip("=")
|
||||
return {"items": page, "next_cursor": next_cursor}
|
||||
except Exception: pass
|
||||
return page_chats(json.loads(cached), limit, cursor_data, str(user['id']))
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
with chat_cache_lock:
|
||||
try: cached = cache.get(key)
|
||||
except Exception: cached = None
|
||||
if cached:
|
||||
result = json.loads(cached)
|
||||
else:
|
||||
with pool.connection() as conn:
|
||||
rows = conn.execute("SELECT c.* FROM chats c JOIN chat_members m ON m.chat_id=c.id WHERE m.user_id=%s ORDER BY c.created_at,c.id", (user["id"],)).fetchall(); result=[]
|
||||
for row in rows:
|
||||
ids = [x["user_id"] for x in conn.execute("SELECT user_id FROM chat_members WHERE chat_id=%s ORDER BY user_id", (row["id"],)).fetchall()]; result.append(chat_view(row,ids))
|
||||
cache_sql_reads += 1
|
||||
try: cache.setex(key, 30, json.dumps(result))
|
||||
except Exception: pass
|
||||
if after: result = [x for x in result if (datetime.fromisoformat(x["created_at"].replace("Z", "+00:00")), uuid.UUID(x["id"])) > after]
|
||||
page = result[:limit]
|
||||
next_cursor = None
|
||||
if len(result) > limit and page:
|
||||
tail = page[-1]
|
||||
next_cursor = base64.urlsafe_b64encode(json.dumps(["chats", str(user["id"]), iso(datetime.fromisoformat(tail["created_at"].replace("Z", "+00:00"))), tail["id"]]).encode()).decode().rstrip("=")
|
||||
return {"items": page, "next_cursor": next_cursor}
|
||||
|
||||
@app.get("/api/v1/chats/{chat_id}")
|
||||
def get_chat(chat_id: str, request: Request):
|
||||
user, failure = auth_or_error(request)
|
||||
if failure: return failure
|
||||
try: cid=uid(chat_id)
|
||||
except ValueError: return error(request,400,"invalid_request","Неверный UUID")
|
||||
with pool.connection() as conn:
|
||||
row=conn.execute("SELECT * FROM chats WHERE id=%s",(cid,)).fetchone()
|
||||
if not row or not member(conn,cid,user["id"]): return error(request,404,"not_found","Чат не найден")
|
||||
ids=[x["user_id"] for x in conn.execute("SELECT user_id FROM chat_members WHERE chat_id=%s",(cid,)).fetchall()]
|
||||
return chat_view(row,ids)
|
||||
|
||||
@app.post("/api/v1/chats/{chat_id}/messages", status_code=201)
|
||||
async def create_message(chat_id: str, request: Request):
|
||||
user, failure = auth_or_error(request)
|
||||
if failure: return failure
|
||||
try: cid=uid(chat_id); data=await body(request); require_fields(data,["text"],["text"]); text=data["text"]
|
||||
except ValueError: return error(request,400,"invalid_request","Неверные данные сообщения")
|
||||
if not isinstance(text,str) or not text.strip() or not 1 <= len(text) <= 4000: return error(request,400,"invalid_request","Текст должен быть от 1 до 4000 символов")
|
||||
with pool.connection() as conn:
|
||||
if not member(conn,cid,user["id"]): return error(request,404,"not_found","Чат не найден")
|
||||
mid, created=uuid.uuid4(),now(); conn.execute("INSERT INTO messages VALUES (%s,%s,%s,%s,%s)",(mid,cid,user["id"],text,created)); conn.commit()
|
||||
return message_view({"id":mid,"chat_id":cid,"sender_id":user["id"],"text":text,"created_at":created})
|
||||
|
||||
@app.get("/api/v1/chats/{chat_id}/messages")
|
||||
def list_messages(chat_id: str, request: Request, limit: int = 50, cursor: str | None = None):
|
||||
user, failure = auth_or_error(request)
|
||||
if failure: return failure
|
||||
if limit<1 or limit>100: return error(request,400,"invalid_request","limit должен быть от 1 до 100")
|
||||
if cursor == "": return error(request,400,"invalid_request","Неверный cursor")
|
||||
try: cid=uid(chat_id)
|
||||
except ValueError: return error(request,400,"invalid_request","Неверный UUID")
|
||||
last=None
|
||||
if cursor:
|
||||
try:
|
||||
last=json.loads(base64.urlsafe_b64decode(cursor.encode()+b"="*((4-len(cursor)%4)%4)))
|
||||
if last[0] != "messages" or last[1] != str(cid): raise ValueError
|
||||
last = last[2:]
|
||||
except Exception: return error(request,400,"invalid_request","Неверный cursor")
|
||||
with pool.connection() as conn:
|
||||
if not member(conn,cid,user["id"]): return error(request,404,"not_found","Чат не найден")
|
||||
if last:
|
||||
try: rows=conn.execute("SELECT * FROM messages WHERE chat_id=%s AND (created_at,id)>(%s,%s) ORDER BY created_at,id LIMIT %s",(cid,datetime.fromisoformat(last[0].replace("Z","+00:00")),uid(last[1]),limit+1)).fetchall()
|
||||
except Exception: return error(request,400,"invalid_request","Неверный cursor")
|
||||
else: rows=conn.execute("SELECT * FROM messages WHERE chat_id=%s ORDER BY created_at,id LIMIT %s",(cid,limit+1)).fetchall()
|
||||
more=len(rows)>limit; rows=rows[:limit]; next_cursor=None
|
||||
if more:
|
||||
tail=rows[-1]; next_cursor=base64.urlsafe_b64encode(json.dumps(["messages", str(cid), iso(tail["created_at"]),str(tail["id"])]).encode()).decode().rstrip("=")
|
||||
return {"items":[message_view(x) for x in rows],"next_cursor":next_cursor}
|
||||
cached = redis_client.get(cache_key)
|
||||
except Exception:
|
||||
cached = None
|
||||
if cached:
|
||||
chats = json.loads(cached)
|
||||
else:
|
||||
with db_pool.connection() as connection:
|
||||
rows = connection.execute(
|
||||
'SELECT c.* FROM chats c JOIN chat_members m ON m.chat_id=c.id '
|
||||
'WHERE m.user_id=%s ORDER BY c.created_at,c.id', (user['id'],)
|
||||
).fetchall()
|
||||
chats = []
|
||||
for row in rows:
|
||||
members = connection.execute(
|
||||
'SELECT user_id FROM chat_members WHERE chat_id=%s ORDER BY user_id', (row['id'],)
|
||||
).fetchall()
|
||||
chats.append(chat_response(row, [item['user_id'] for item in members]))
|
||||
chat_cache_sql_reads += 1
|
||||
try:
|
||||
redis_client.setex(cache_key, 30, json.dumps(chats))
|
||||
except Exception:
|
||||
pass
|
||||
return page_chats(chats, limit, cursor_data, str(user['id']))
|
||||
|
||||
|
||||
@app.get('/api/v1/chats/{chat_id}')
|
||||
def get_chat(chat_id: str, request: Request):
|
||||
user, failure = require_current_user(request)
|
||||
if failure:
|
||||
return failure
|
||||
try:
|
||||
parsed_chat_id = parse_uuid(chat_id)
|
||||
except ValueError:
|
||||
return error_response(request, 400, 'invalid_request', 'Неверный UUID')
|
||||
with db_pool.connection() as connection:
|
||||
chat = connection.execute('SELECT * FROM chats WHERE id=%s', (parsed_chat_id,)).fetchone()
|
||||
if chat is None or not is_chat_member(connection, parsed_chat_id, user['id']):
|
||||
return error_response(request, 404, 'not_found', 'Чат не найден')
|
||||
members = connection.execute('SELECT user_id FROM chat_members WHERE chat_id=%s', (parsed_chat_id,)).fetchall()
|
||||
return chat_response(chat, [item['user_id'] for item in members])
|
||||
|
||||
|
||||
@app.post('/api/v1/chats/{chat_id}/messages', status_code=201)
|
||||
async def create_message(chat_id: str, request: Request):
|
||||
user, failure = require_current_user(request)
|
||||
if failure:
|
||||
return failure
|
||||
try:
|
||||
parsed_chat_id = parse_uuid(chat_id)
|
||||
data = await read_json_body(request)
|
||||
require_fields(data, {'text'})
|
||||
text = data['text']
|
||||
if not isinstance(text, str) or not text.strip() or not 1 <= len(text) <= 4000:
|
||||
raise ValueError('invalid text')
|
||||
except ValueError:
|
||||
return error_response(request, 400, 'invalid_request', 'Неверные данные сообщения')
|
||||
|
||||
with db_pool.connection() as connection:
|
||||
if not is_chat_member(connection, parsed_chat_id, user['id']):
|
||||
return error_response(request, 404, 'not_found', 'Чат не найден')
|
||||
message_id, created_at = uuid.uuid4(), utc_now()
|
||||
connection.execute(
|
||||
'INSERT INTO messages VALUES (%s,%s,%s,%s,%s)',
|
||||
(message_id, parsed_chat_id, user['id'], text, created_at),
|
||||
)
|
||||
connection.commit()
|
||||
return message_response({'id': message_id, 'chat_id': parsed_chat_id, 'sender_id': user['id'], 'text': text, 'created_at': created_at})
|
||||
|
||||
|
||||
@app.get('/api/v1/chats/{chat_id}/messages')
|
||||
def list_messages(chat_id: str, request: Request, limit: int = 50, cursor: str | None = None):
|
||||
user, failure = require_current_user(request)
|
||||
if failure:
|
||||
return failure
|
||||
if not 1 <= limit <= 100:
|
||||
return error_response(request, 400, 'invalid_request', 'limit должен быть от 1 до 100')
|
||||
try:
|
||||
parsed_chat_id = parse_uuid(chat_id)
|
||||
except ValueError:
|
||||
return error_response(request, 400, 'invalid_request', 'Неверный UUID')
|
||||
|
||||
cursor_data = None
|
||||
if cursor is not None:
|
||||
if not cursor:
|
||||
return error_response(request, 400, 'invalid_request', 'Неверный cursor')
|
||||
try:
|
||||
cursor_data = read_cursor(cursor, 'messages', str(parsed_chat_id))
|
||||
except ValueError:
|
||||
return error_response(request, 400, 'invalid_request', 'Неверный cursor')
|
||||
|
||||
with db_pool.connection() as connection:
|
||||
if not is_chat_member(connection, parsed_chat_id, user['id']):
|
||||
return error_response(request, 404, 'not_found', 'Чат не найден')
|
||||
if cursor_data:
|
||||
rows = connection.execute(
|
||||
'SELECT * FROM messages WHERE chat_id=%s AND (created_at,id)>(%s,%s) '
|
||||
'ORDER BY created_at,id LIMIT %s',
|
||||
(parsed_chat_id, cursor_data[0], cursor_data[1], limit + 1),
|
||||
).fetchall()
|
||||
else:
|
||||
rows = connection.execute(
|
||||
'SELECT * FROM messages WHERE chat_id=%s ORDER BY created_at,id LIMIT %s',
|
||||
(parsed_chat_id, limit + 1),
|
||||
).fetchall()
|
||||
|
||||
rows_to_return = rows[:limit]
|
||||
next_cursor = make_cursor('messages', str(parsed_chat_id), rows_to_return[-1]) if len(rows) > limit else None
|
||||
return {'items': [message_response(row) for row in rows_to_return], 'next_cursor': next_cursor}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
{
|
||||
"hypothesis": "a lock prevents concurrent cache stampede and invalidation preserves read-after-write",
|
||||
"cold_ms": 3.221,
|
||||
"warm_p50_ms": 1.992,
|
||||
"warm_p95_ms": 2.35,
|
||||
"cold_ms": 5.189,
|
||||
"warm_p50_ms": 2.104,
|
||||
"warm_p95_ms": 2.53,
|
||||
"sql_reads_for_20_concurrent_cold_requests": 1,
|
||||
"sql_reads_after_redis_restart": 1,
|
||||
"read_after_write": "POST /chats deletes chats:<user_id>"
|
||||
|
||||
Reference in New Issue
Block a user