forked from mtusi2026/mtusi2026lab2
Add first lab code
This commit is contained in:
+10
-9
@@ -1,11 +1,12 @@
|
||||
# Copy to .env for your LOCAL synthetic test environment; never commit .env.
|
||||
# These are logical interface suggestions; map actual framework names in REPORT.md.
|
||||
# Used by python -m src. Docker run reads this file through --env-file.
|
||||
APP_HOST=0.0.0.0
|
||||
APP_PORT=8080
|
||||
APP_ENV=lab
|
||||
SESSION_TTL_SECONDS=3600
|
||||
# Choose credentials locally. File paths contain no secret values.
|
||||
DATABASE_URL_FILE=/run/secrets/database_url
|
||||
# Optional on 3, required use on 4 in lab2; Redis OR Valkey:
|
||||
SESSION_STORE=postgres
|
||||
CACHE_URL_FILE=/run/secrets/cache_url
|
||||
# Single-process resource limits. Text bytes are UTF-8 payload, not process RSS.
|
||||
MAX_USERS=1000
|
||||
MAX_CHATS=1000
|
||||
MAX_MESSAGES=10000
|
||||
MAX_TEXT_BYTES=16777216
|
||||
MAX_REQUEST_BYTES=65536
|
||||
MAX_INFLIGHT=32
|
||||
MESSAGE_RATE=1000
|
||||
MESSAGE_BURST=1000
|
||||
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
FROM python:3.12.8-slim-bookworm
|
||||
|
||||
ENV PYTHONDONTWRITEBYTECODE=1 \
|
||||
PYTHONUNBUFFERED=1 \
|
||||
APP_HOST=0.0.0.0 \
|
||||
APP_PORT=8080
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY requirements.txt .
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
|
||||
COPY contracts ./contracts
|
||||
COPY src ./src
|
||||
|
||||
RUN useradd --create-home --shell /usr/sbin/nologin appuser
|
||||
USER appuser
|
||||
|
||||
EXPOSE 8080
|
||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
|
||||
CMD python -c "import os, urllib.request; urllib.request.urlopen('http://127.0.0.1:' + os.getenv('APP_PORT', '8080') + '/health/ready', timeout=2).read()"
|
||||
|
||||
STOPSIGNAL SIGTERM
|
||||
CMD ["python", "-m", "src"]
|
||||
@@ -0,0 +1,2 @@
|
||||
# Only needed to draw the saved experiment data, not by the API.
|
||||
matplotlib==3.10.0
|
||||
@@ -0,0 +1,13 @@
|
||||
fastapi==0.115.6
|
||||
uvicorn==0.34.0
|
||||
pydantic==2.13.5
|
||||
starlette==0.41.3
|
||||
annotated-types==0.8.0
|
||||
anyio==4.15.1
|
||||
click==8.5.0
|
||||
colorama==0.4.6; sys_platform == "win32"
|
||||
h11==0.16.0
|
||||
idna==3.20
|
||||
pydantic_core==2.46.5
|
||||
typing-inspection==0.4.4
|
||||
typing_extensions==4.16.0
|
||||
@@ -0,0 +1,13 @@
|
||||
import os
|
||||
|
||||
import uvicorn
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
uvicorn.run(
|
||||
'src.main:app',
|
||||
host=os.getenv('APP_HOST', '0.0.0.0'),
|
||||
port=int(os.getenv('APP_PORT', '8080')),
|
||||
workers=1,
|
||||
timeout_graceful_shutdown=5,
|
||||
)
|
||||
@@ -0,0 +1,27 @@
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import Header, Query
|
||||
|
||||
from .errors import APIError
|
||||
from .store import store
|
||||
|
||||
|
||||
def page_limit(limit: str = Query(default="50")) -> int:
|
||||
if not limit.isascii() or not limit.isdecimal() or len(limit) > 3:
|
||||
raise APIError(400, "invalid_request", "Неверный limit")
|
||||
number = int(limit)
|
||||
if not 1 <= number <= 100:
|
||||
raise APIError(400, "invalid_request", "Неверный limit")
|
||||
return number
|
||||
|
||||
|
||||
|
||||
def current_user(x_user_id: Optional[str] = Header(default=None, alias="X-User-Id")) -> dict:
|
||||
if not x_user_id:
|
||||
raise APIError(401, "unauthorized", "Нет действительной идентификации")
|
||||
try:
|
||||
return store.require_user(x_user_id)
|
||||
except APIError as exc:
|
||||
if exc.status_code == 400:
|
||||
raise APIError(401, "unauthorized", "Нет действительной идентификации") from exc
|
||||
raise
|
||||
@@ -0,0 +1,50 @@
|
||||
from fastapi import Request
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.responses import JSONResponse
|
||||
from starlette.exceptions import HTTPException as StarletteHTTPException
|
||||
|
||||
|
||||
class APIError(Exception):
|
||||
def __init__(self, status_code: int, code: str, message: str):
|
||||
self.status_code = status_code
|
||||
self.code = code
|
||||
self.message = message
|
||||
|
||||
|
||||
def error_response(request: Request, status_code: int, code: str, message: str) -> JSONResponse:
|
||||
request_id = getattr(request.state, "request_id", "")
|
||||
return JSONResponse(
|
||||
status_code=status_code,
|
||||
headers={"X-Request-Id": request_id},
|
||||
content={
|
||||
"error": {
|
||||
"code": code,
|
||||
"message": message,
|
||||
"request_id": request_id,
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def api_error_handler(request: Request, exc: APIError) -> JSONResponse:
|
||||
response = error_response(request, exc.status_code, exc.code, exc.message)
|
||||
if exc.status_code == 429:
|
||||
response.headers['Retry-After'] = '1'
|
||||
return response
|
||||
|
||||
|
||||
def validation_error_handler(request: Request, exc: RequestValidationError) -> JSONResponse:
|
||||
return error_response(request, 400, "invalid_request", "Неверный запрос")
|
||||
|
||||
|
||||
def http_error_handler(request: Request, exc: StarletteHTTPException) -> JSONResponse:
|
||||
if exc.status_code == 404:
|
||||
return error_response(request, 404, "not_found", "Объект не найден")
|
||||
if exc.status_code == 405:
|
||||
return error_response(request, 400, "invalid_request", "Метод не поддерживается")
|
||||
else:
|
||||
return error_response(request, exc.status_code, "invalid_request", "Неверный запрос")
|
||||
|
||||
|
||||
def internal_error_handler(request: Request, exc: Exception) -> JSONResponse:
|
||||
return error_response(request, 500, "internal_error", "Внутренняя ошибка")
|
||||
@@ -0,0 +1,95 @@
|
||||
import asyncio
|
||||
import os
|
||||
from dataclasses import dataclass
|
||||
|
||||
from starlette.requests import Request
|
||||
|
||||
from .errors import error_response
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Limits:
|
||||
max_users: int = 1000
|
||||
max_chats: int = 1000
|
||||
max_messages: int = 10000
|
||||
max_text_bytes: int = 16 * 1024 * 1024
|
||||
max_request_bytes: int = 64 * 1024
|
||||
max_inflight: int = 32
|
||||
message_rate: int = 1000
|
||||
message_burst: int = 1000
|
||||
|
||||
@classmethod
|
||||
def from_env(cls):
|
||||
values = {}
|
||||
for name, default in cls().__dict__.items():
|
||||
value = int(os.getenv(name.upper(), str(default)))
|
||||
if value < 1:
|
||||
raise ValueError(f'{name.upper()} must be positive')
|
||||
values[name] = value
|
||||
return cls(**values)
|
||||
|
||||
|
||||
limits = Limits.from_env()
|
||||
|
||||
|
||||
class ResourceLimitMiddleware:
|
||||
def __init__(self, app, settings=limits):
|
||||
self.app = app
|
||||
self.settings = settings
|
||||
self.active = 0
|
||||
|
||||
async def __call__(self, scope, receive, send):
|
||||
if scope['type'] != 'http' or scope['path'].startswith('/health/'):
|
||||
return await self.app(scope, receive, send)
|
||||
|
||||
request = Request(scope)
|
||||
|
||||
async def reject(status, code, message):
|
||||
response = error_response(request, status, code, message)
|
||||
if status == 503:
|
||||
response.headers['Retry-After'] = '1'
|
||||
await response(scope, receive, send)
|
||||
|
||||
if self.active >= self.settings.max_inflight:
|
||||
return await reject(503, 'unavailable', 'Слишком много одновременных запросов')
|
||||
self.active += 1
|
||||
try:
|
||||
length = request.headers.get('content-length')
|
||||
if length is not None:
|
||||
try:
|
||||
size = int(length)
|
||||
if size < 0:
|
||||
raise ValueError()
|
||||
except ValueError:
|
||||
return await reject(400, 'invalid_request', 'Неверный Content-Length')
|
||||
if size > self.settings.max_request_bytes:
|
||||
return await reject(413, 'payload_too_large', 'Тело запроса слишком большое')
|
||||
|
||||
body = bytearray()
|
||||
try:
|
||||
async with asyncio.timeout(5):
|
||||
while True:
|
||||
message = await receive()
|
||||
if message['type'] == 'http.disconnect':
|
||||
return
|
||||
chunk = message.get('body', b'')
|
||||
if len(body) + len(chunk) > self.settings.max_request_bytes:
|
||||
return await reject(413, 'payload_too_large', 'Тело запроса слишком большое')
|
||||
body.extend(chunk)
|
||||
if not message.get('more_body', False):
|
||||
break
|
||||
except TimeoutError:
|
||||
return await reject(503, 'unavailable', 'Истекло время чтения запроса')
|
||||
|
||||
delivered = False
|
||||
|
||||
async def buffered_receive():
|
||||
nonlocal delivered
|
||||
if not delivered:
|
||||
delivered = True
|
||||
return {'type': 'http.request', 'body': bytes(body), 'more_body': False}
|
||||
return await receive()
|
||||
|
||||
await self.app(scope, buffered_receive, send)
|
||||
finally:
|
||||
self.active -= 1
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from starlette.exceptions import HTTPException
|
||||
|
||||
from .errors import APIError, api_error_handler, validation_error_handler, http_error_handler, internal_error_handler
|
||||
from .middleware import RequestIdMiddleware
|
||||
from .limits import ResourceLimitMiddleware
|
||||
from .routers.health import router as health_router
|
||||
from .routers.users import router as users_router
|
||||
from .routers.chats import router as chats_router
|
||||
|
||||
app = FastAPI(title="MTUSI Messenger")
|
||||
app.add_middleware(ResourceLimitMiddleware)
|
||||
app.add_middleware(RequestIdMiddleware)
|
||||
app.add_exception_handler(APIError, api_error_handler)
|
||||
app.add_exception_handler(RequestValidationError, validation_error_handler)
|
||||
app.add_exception_handler(HTTPException, http_error_handler)
|
||||
app.add_exception_handler(Exception, internal_error_handler)
|
||||
|
||||
app.include_router(health_router)
|
||||
app.include_router(users_router)
|
||||
app.include_router(chats_router)
|
||||
@@ -0,0 +1,30 @@
|
||||
from uuid import uuid4
|
||||
from starlette.responses import JSONResponse
|
||||
from starlette.middleware.base import BaseHTTPMiddleware
|
||||
from starlette.requests import Request
|
||||
|
||||
|
||||
class RequestIdMiddleware(BaseHTTPMiddleware):
|
||||
async def dispatch(self, request: Request, call_next):
|
||||
request_id = request.headers.get("X-Request-Id", "").strip() or str(uuid4())
|
||||
request.state.request_id = request_id
|
||||
|
||||
if request.method in {"POST", "PUT", "PATCH"} and request.url.path.startswith("/api/"):
|
||||
content_type = request.headers.get("content-type", "").split(";", 1)[0].strip().lower()
|
||||
if content_type != "application/json":
|
||||
response = JSONResponse(
|
||||
status_code=415,
|
||||
content={
|
||||
"error": {
|
||||
"code": "unsupported_media_type",
|
||||
"message": "Ожидается Content-Type application/json",
|
||||
"request_id": request_id,
|
||||
}
|
||||
},
|
||||
)
|
||||
response.headers["X-Request-Id"] = request_id
|
||||
return response
|
||||
|
||||
response = await call_next(request)
|
||||
response.headers["X-Request-Id"] = request_id
|
||||
return response
|
||||
@@ -0,0 +1,44 @@
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, Query, status
|
||||
|
||||
from ..dependencies import current_user, page_limit
|
||||
from ..schemas.chat import Chat, ChatPage, CreateChat
|
||||
from ..schemas.message import CreateMessage, Message, MessagePage
|
||||
from ..store import store
|
||||
|
||||
router = APIRouter(prefix="/api/v1/chats", tags=["chats"])
|
||||
|
||||
|
||||
@router.post("", response_model=Chat, status_code=status.HTTP_201_CREATED)
|
||||
def create_chat(payload: CreateChat, user: dict = Depends(current_user)):
|
||||
return store.create_chat(user["id"], payload.title, payload.member_ids)
|
||||
|
||||
|
||||
@router.get("", response_model=ChatPage)
|
||||
def list_chats(
|
||||
limit: int = Depends(page_limit),
|
||||
cursor: Optional[str] = Query(default=None, min_length=1),
|
||||
user: dict = Depends(current_user),
|
||||
):
|
||||
return store.list_chats(user["id"], limit, cursor)
|
||||
|
||||
|
||||
@router.get("/{chat_id}", response_model=Chat)
|
||||
def get_chat(chat_id: str, user: dict = Depends(current_user)):
|
||||
return store.get_chat_for_user(chat_id, user["id"])
|
||||
|
||||
|
||||
@router.post("/{chat_id}/messages", response_model=Message, status_code=status.HTTP_201_CREATED)
|
||||
def create_message(chat_id: str, payload: CreateMessage, user: dict = Depends(current_user)):
|
||||
return store.create_message(chat_id, user["id"], payload.text)
|
||||
|
||||
|
||||
@router.get("/{chat_id}/messages", response_model=MessagePage)
|
||||
def list_messages(
|
||||
chat_id: str,
|
||||
limit: int = Depends(page_limit),
|
||||
cursor: Optional[str] = Query(default=None, min_length=1),
|
||||
user: dict = Depends(current_user),
|
||||
):
|
||||
return store.list_messages(chat_id, user["id"], limit, cursor)
|
||||
@@ -0,0 +1,15 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
from ..schemas.common import Health
|
||||
|
||||
router = APIRouter(prefix='/health')
|
||||
|
||||
|
||||
@router.get("/live", response_model=Health)
|
||||
def health_live():
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
@router.get("/ready", response_model=Health)
|
||||
def health_ready():
|
||||
return {"status": "ok"}
|
||||
@@ -0,0 +1,19 @@
|
||||
from fastapi import APIRouter, Depends, status
|
||||
|
||||
from ..dependencies import current_user
|
||||
from ..schemas.user import CreateUser, User
|
||||
from ..store import store
|
||||
|
||||
|
||||
|
||||
router = APIRouter(prefix="/api/v1/users", tags=["users"])
|
||||
|
||||
|
||||
@router.post("", response_model=User, status_code=status.HTTP_201_CREATED)
|
||||
def create_user(payload: CreateUser):
|
||||
return store.create_user(payload.username, payload.display_name)
|
||||
|
||||
|
||||
@router.get("/me", response_model=User)
|
||||
def get_current_user(user: dict = Depends(current_user)):
|
||||
return user
|
||||
@@ -0,0 +1,35 @@
|
||||
from typing import List, Optional
|
||||
|
||||
from pydantic import Field, StrictStr, field_validator
|
||||
|
||||
from .common import ContractModel
|
||||
|
||||
|
||||
|
||||
class CreateChat(ContractModel):
|
||||
title: StrictStr = Field(min_length=1, max_length=100)
|
||||
member_ids: List[StrictStr] = Field(min_length=1, max_length=100)
|
||||
|
||||
@field_validator("title")
|
||||
def validate_title(cls, value):
|
||||
if not value.strip():
|
||||
raise ValueError("blank title")
|
||||
return value
|
||||
|
||||
@field_validator("member_ids")
|
||||
def validate_member_ids(cls, value):
|
||||
if len(value) != len(set(value)):
|
||||
raise ValueError("duplicate member_ids")
|
||||
return value
|
||||
|
||||
|
||||
class Chat(ContractModel):
|
||||
id: str
|
||||
title: str
|
||||
member_ids: List[str]
|
||||
created_at: str
|
||||
|
||||
|
||||
class ChatPage(ContractModel):
|
||||
items: List[Chat]
|
||||
next_cursor: Optional[str]
|
||||
@@ -0,0 +1,9 @@
|
||||
from pydantic import BaseModel, ConfigDict
|
||||
|
||||
|
||||
class ContractModel(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
|
||||
class Health(ContractModel):
|
||||
status: str
|
||||
@@ -0,0 +1,29 @@
|
||||
from typing import List, Optional
|
||||
|
||||
from pydantic import Field, StrictStr, field_validator
|
||||
|
||||
from .common import ContractModel
|
||||
|
||||
|
||||
|
||||
class CreateMessage(ContractModel):
|
||||
text: StrictStr = Field(min_length=1, max_length=4000)
|
||||
|
||||
@field_validator("text")
|
||||
def validate_text(cls, value):
|
||||
if not value.strip():
|
||||
raise ValueError("blank text")
|
||||
return value
|
||||
|
||||
|
||||
class Message(ContractModel):
|
||||
id: str
|
||||
chat_id: str
|
||||
sender_id: str
|
||||
text: str
|
||||
created_at: str
|
||||
|
||||
|
||||
class MessagePage(ContractModel):
|
||||
items: List[Message]
|
||||
next_cursor: Optional[str]
|
||||
@@ -0,0 +1,33 @@
|
||||
import re
|
||||
|
||||
from pydantic import Field, StrictStr, field_validator
|
||||
|
||||
from .common import ContractModel
|
||||
|
||||
|
||||
|
||||
USERNAME_RE = re.compile(r"^[a-z0-9_]{3,32}$")
|
||||
|
||||
|
||||
class CreateUser(ContractModel):
|
||||
username: StrictStr
|
||||
display_name: StrictStr = Field(min_length=1, max_length=100)
|
||||
|
||||
@field_validator("username")
|
||||
def validate_username(cls, value):
|
||||
if not USERNAME_RE.fullmatch(value):
|
||||
raise ValueError("invalid username")
|
||||
return value
|
||||
|
||||
@field_validator("display_name")
|
||||
def validate_display_name(cls, value):
|
||||
if not value.strip():
|
||||
raise ValueError("blank display_name")
|
||||
return value
|
||||
|
||||
|
||||
class User(ContractModel):
|
||||
id: str
|
||||
username: str
|
||||
display_name: str = Field(min_length=1, max_length=100)
|
||||
created_at: str
|
||||
+218
@@ -0,0 +1,218 @@
|
||||
import base64
|
||||
import json
|
||||
import time
|
||||
from datetime import datetime, timezone
|
||||
from threading import RLock
|
||||
from typing import Optional, Tuple
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
from .errors import APIError
|
||||
from .limits import limits
|
||||
|
||||
|
||||
def utc_now() -> str:
|
||||
return datetime.now(timezone.utc).isoformat(timespec="microseconds").replace("+00:00", "Z")
|
||||
|
||||
|
||||
def ensure_uuid(value: str) -> str:
|
||||
try:
|
||||
if not isinstance(value, str):
|
||||
raise ValueError("UUID must be a string")
|
||||
return str(UUID(value))
|
||||
except (TypeError, ValueError) as exc:
|
||||
raise APIError(400, "invalid_request", "Неверный UUID") from exc
|
||||
|
||||
|
||||
class InMemoryStore:
|
||||
def __init__(self, settings=limits):
|
||||
self.settings = settings
|
||||
self._lock = RLock()
|
||||
self._users = {}
|
||||
self._usernames = set()
|
||||
self._chats = {}
|
||||
self._messages = {}
|
||||
self._message_count = 0
|
||||
self._text_bytes = 0
|
||||
self._tokens = float(settings.message_burst)
|
||||
self._refilled_at = time.monotonic()
|
||||
|
||||
def create_user(self, username: str, display_name: str) -> dict:
|
||||
with self._lock:
|
||||
if username in self._usernames:
|
||||
raise APIError(409, "conflict", "Имя пользователя уже занято")
|
||||
if len(self._users) >= self.settings.max_users:
|
||||
raise APIError(503, 'unavailable', 'Достигнут лимит пользователей')
|
||||
|
||||
user = {
|
||||
"id": str(uuid4()),
|
||||
"username": username,
|
||||
"display_name": display_name,
|
||||
"created_at": utc_now(),
|
||||
}
|
||||
self._users[user["id"]] = user
|
||||
self._usernames.add(username)
|
||||
return dict(user)
|
||||
|
||||
def get_user(self, user_id: str) -> Optional[dict]:
|
||||
user_id = ensure_uuid(user_id)
|
||||
with self._lock:
|
||||
user = self._users.get(user_id)
|
||||
return dict(user) if user else None
|
||||
|
||||
def require_user(self, user_id: str) -> dict:
|
||||
user = self.get_user(user_id)
|
||||
if not user:
|
||||
raise APIError(401, "unauthorized", "Пользователь не авторизован")
|
||||
return user
|
||||
|
||||
def create_chat(self, actor_id: str, title: str, member_ids: list[str]) -> dict:
|
||||
actor_id = ensure_uuid(actor_id)
|
||||
normalized_members = [ensure_uuid(member_id) for member_id in member_ids]
|
||||
if len(normalized_members) != len(set(normalized_members)):
|
||||
raise APIError(400, "invalid_request", "Участники не должны повторяться")
|
||||
if actor_id not in normalized_members:
|
||||
raise APIError(400, "invalid_request", "Создатель должен быть участником чата")
|
||||
|
||||
with self._lock:
|
||||
if any(member_id not in self._users for member_id in normalized_members):
|
||||
raise APIError(400, "invalid_request", "Неизвестный участник")
|
||||
if len(self._chats) >= self.settings.max_chats:
|
||||
raise APIError(503, 'unavailable', 'Достигнут лимит чатов')
|
||||
|
||||
chat = {
|
||||
"id": str(uuid4()),
|
||||
"title": title,
|
||||
"member_ids": normalized_members,
|
||||
"created_at": utc_now(),
|
||||
}
|
||||
self._chats[chat["id"]] = chat
|
||||
self._messages[chat["id"]] = []
|
||||
return self._copy_chat(chat)
|
||||
|
||||
def get_chat_for_user(self, chat_id: str, actor_id: str) -> dict:
|
||||
chat_id = ensure_uuid(chat_id)
|
||||
actor_id = ensure_uuid(actor_id)
|
||||
with self._lock:
|
||||
chat = self._chats.get(chat_id)
|
||||
if not chat or actor_id not in chat["member_ids"]:
|
||||
raise APIError(404, "not_found", "Объект не найден")
|
||||
return self._copy_chat(chat)
|
||||
|
||||
def list_chats(self, actor_id: str, limit: int, cursor: Optional[str]) -> dict:
|
||||
actor_id = ensure_uuid(actor_id)
|
||||
after = self._decode_cursor(cursor, "chats", actor_id) if cursor else None
|
||||
|
||||
with self._lock:
|
||||
chats = [
|
||||
self._copy_chat(chat)
|
||||
for chat in self._chats.values()
|
||||
if actor_id in chat["member_ids"]
|
||||
]
|
||||
chats.sort(key=lambda item: (item["created_at"], item["id"]))
|
||||
if after:
|
||||
chats = [chat for chat in chats if (chat["created_at"], chat["id"]) > after]
|
||||
|
||||
page = chats[:limit]
|
||||
next_cursor = None
|
||||
if len(chats) > limit and page:
|
||||
last = page[-1]
|
||||
next_cursor = self._encode_cursor("chats", actor_id, last["created_at"], last["id"])
|
||||
return {"items": page, "next_cursor": next_cursor}
|
||||
|
||||
def create_message(self, chat_id: str, actor_id: str, text: str) -> dict:
|
||||
chat = self.get_chat_for_user(chat_id, actor_id)
|
||||
try:
|
||||
text_size = len(text.encode('utf-8'))
|
||||
except UnicodeEncodeError as exc:
|
||||
raise APIError(400, 'invalid_request', 'Некорректный Unicode') from exc
|
||||
with self._lock:
|
||||
if self._message_count >= self.settings.max_messages:
|
||||
raise APIError(503, 'unavailable', 'Достигнут лимит сообщений')
|
||||
if self._text_bytes + text_size > self.settings.max_text_bytes:
|
||||
raise APIError(503, 'unavailable', 'Достигнут лимит объёма текста')
|
||||
now = time.monotonic()
|
||||
self._tokens = min(self.settings.message_burst,
|
||||
self._tokens + (now - self._refilled_at) * self.settings.message_rate)
|
||||
self._refilled_at = now
|
||||
if self._tokens < 1:
|
||||
raise APIError(429, 'rate_limited', 'Слишком частая отправка сообщений')
|
||||
self._tokens -= 1
|
||||
message = {
|
||||
"id": str(uuid4()),
|
||||
"chat_id": chat["id"],
|
||||
"sender_id": ensure_uuid(actor_id),
|
||||
"text": text,
|
||||
"created_at": utc_now(),
|
||||
}
|
||||
self._messages[chat["id"]].append(message)
|
||||
self._message_count += 1
|
||||
self._text_bytes += text_size
|
||||
return dict(message)
|
||||
|
||||
def list_messages(self, chat_id: str, actor_id: str, limit: int, cursor: Optional[str]) -> dict:
|
||||
chat = self.get_chat_for_user(chat_id, actor_id)
|
||||
after = self._decode_cursor(cursor, "messages", chat["id"]) if cursor else None
|
||||
|
||||
with self._lock:
|
||||
messages = [dict(message) for message in self._messages.get(chat["id"], [])]
|
||||
messages.sort(key=lambda item: (item["created_at"], item["id"]))
|
||||
if after:
|
||||
messages = [
|
||||
message
|
||||
for message in messages
|
||||
if (message["created_at"], message["id"]) > after
|
||||
]
|
||||
|
||||
page = messages[:limit]
|
||||
next_cursor = None
|
||||
if len(messages) > limit and page:
|
||||
last = page[-1]
|
||||
next_cursor = self._encode_cursor("messages", chat["id"], last["created_at"], last["id"])
|
||||
return {"items": page, "next_cursor": next_cursor}
|
||||
|
||||
@staticmethod
|
||||
def _copy_chat(chat: dict) -> dict:
|
||||
copy = dict(chat)
|
||||
copy["member_ids"] = list(chat["member_ids"])
|
||||
return copy
|
||||
|
||||
@staticmethod
|
||||
def _encode_cursor(resource: str, owner_id: str, created_at: str, item_id: str) -> str:
|
||||
payload = {
|
||||
"resource": resource,
|
||||
"owner_id": owner_id,
|
||||
"created_at": created_at,
|
||||
"id": item_id,
|
||||
}
|
||||
raw = json.dumps(payload, separators=(",", ":"), ensure_ascii=False).encode("utf-8")
|
||||
return base64.urlsafe_b64encode(raw).decode("ascii").rstrip("=")
|
||||
|
||||
@staticmethod
|
||||
def _decode_cursor(cursor: str, resource: str, owner_id: str) -> Tuple[str, str]:
|
||||
try:
|
||||
padded = cursor + "=" * (-len(cursor) % 4)
|
||||
payload = json.loads(base64.b64decode(padded.encode("ascii"), altchars=b"-_", validate=True).decode("utf-8"))
|
||||
except (ValueError, UnicodeError, json.JSONDecodeError) as exc:
|
||||
raise APIError(400, "invalid_request", "Неверный cursor") from exc
|
||||
|
||||
if not isinstance(payload, dict):
|
||||
raise APIError(400, "invalid_request", "Неверный cursor")
|
||||
|
||||
if payload.get("resource") != resource or payload.get("owner_id") != owner_id:
|
||||
raise APIError(400, "invalid_request", "Неверный cursor")
|
||||
|
||||
item_id = ensure_uuid(payload.get("id"))
|
||||
created_at = payload.get("created_at")
|
||||
if not isinstance(created_at, str) or not created_at:
|
||||
raise APIError(400, "invalid_request", "Неверный cursor")
|
||||
try:
|
||||
parsed = datetime.fromisoformat(created_at.replace("Z", "+00:00"))
|
||||
if not created_at.endswith("Z") or parsed.tzinfo != timezone.utc:
|
||||
raise ValueError("Expected UTC timestamp")
|
||||
created_at = parsed.isoformat(timespec="microseconds").replace("+00:00", "Z")
|
||||
except ValueError as exc:
|
||||
raise APIError(400, "invalid_request", "Неверный cursor") from exc
|
||||
return created_at, item_id
|
||||
|
||||
|
||||
store = InMemoryStore()
|
||||
@@ -0,0 +1,201 @@
|
||||
import json
|
||||
import os
|
||||
import socket
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import unittest
|
||||
import base64
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from unittest.mock import patch
|
||||
from pathlib import Path
|
||||
from urllib.error import HTTPError
|
||||
from urllib.request import Request, urlopen
|
||||
from uuid import uuid4
|
||||
from urllib.parse import urlencode
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
class APITests(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
with socket.socket() as sock:
|
||||
sock.bind(('127.0.0.1', 0))
|
||||
port = sock.getsockname()[1]
|
||||
cls.base_url = f'http://127.0.0.1:{port}'
|
||||
cls.server = subprocess.Popen(
|
||||
[sys.executable, '-m', 'src'], cwd=ROOT,
|
||||
env={**os.environ, 'APP_HOST': '127.0.0.1', 'APP_PORT': str(port)},
|
||||
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
|
||||
)
|
||||
cls.addClassCleanup(cls.stop_server)
|
||||
for _ in range(100):
|
||||
try:
|
||||
with urlopen(cls.base_url + '/health/ready', timeout=1):
|
||||
return
|
||||
except OSError:
|
||||
if cls.server.poll() is not None:
|
||||
raise RuntimeError('Server stopped before startup')
|
||||
time.sleep(0.1)
|
||||
raise RuntimeError('Server startup timed out')
|
||||
|
||||
@classmethod
|
||||
def stop_server(cls):
|
||||
cls.server.terminate()
|
||||
try:
|
||||
cls.server.wait(timeout=10)
|
||||
except subprocess.TimeoutExpired:
|
||||
cls.server.kill()
|
||||
cls.server.wait()
|
||||
|
||||
def call(self, method, path, status=200, body=None, actor=None, raw=None, content_type='application/json'):
|
||||
headers = {'Content-Type': content_type}
|
||||
if actor:
|
||||
headers['X-User-Id'] = actor
|
||||
data = json.dumps(body, ensure_ascii=False).encode() if body is not None else raw
|
||||
request = Request(self.base_url + path, data=data, headers=headers, method=method)
|
||||
try:
|
||||
response = urlopen(request, timeout=10)
|
||||
except HTTPError as error:
|
||||
response = error
|
||||
with response:
|
||||
result = json.load(response)
|
||||
self.assertEqual(response.status, status, result)
|
||||
self.assertTrue(response.headers.get('X-Request-Id'))
|
||||
if status >= 400:
|
||||
self.assertEqual(result['error']['request_id'], response.headers['X-Request-Id'])
|
||||
return result
|
||||
|
||||
def user(self):
|
||||
return self.call('POST', '/api/v1/users', 201,
|
||||
{'username': 'u_' + uuid4().hex[:20], 'display_name': 'Алиса 👋'})['id']
|
||||
|
||||
def test_users(self):
|
||||
body = {'username': 'u_' + uuid4().hex[:20], 'display_name': 'Боб 👋'}
|
||||
user = self.call('POST', '/api/v1/users', 201, body)
|
||||
self.assertEqual(user['display_name'], body['display_name'])
|
||||
self.assertTrue(user['created_at'].endswith('Z'))
|
||||
self.assertEqual(self.call('GET', '/api/v1/users/me', actor=user['id']), user)
|
||||
self.call('POST', '/api/v1/users', 409, body)
|
||||
for actor in [None, 'bad-uuid', str(uuid4())]:
|
||||
self.call('GET', '/api/v1/users/me', 401, actor=actor)
|
||||
|
||||
def test_validation_and_health(self):
|
||||
for path in ['/health/live', '/health/ready']:
|
||||
self.assertEqual(self.call('GET', path), {'status': 'ok'})
|
||||
self.call('POST', '/api/v1/users', 400, raw=b'{broken')
|
||||
self.call('POST', '/api/v1/users', 415, raw=b'{}', content_type='text/plain')
|
||||
for body in [None, {}, {'username': 123, 'display_name': 'Name'},
|
||||
{'username': 'valid', 'display_name': ' '},
|
||||
{'username': 'UPPER', 'display_name': 'Name'},
|
||||
{'username': 'valid', 'display_name': 'Name', 'extra': True}]:
|
||||
self.call('POST', '/api/v1/users', 400, body)
|
||||
self.call('GET', '/missing', 404)
|
||||
|
||||
def chat(self, actor, members=None):
|
||||
return self.call('POST', '/api/v1/chats', 201,
|
||||
{'title': 'Учебный чат', 'member_ids': members or [actor]}, actor)
|
||||
|
||||
def test_chats(self):
|
||||
alice, bob, eve = self.user(), self.user(), self.user()
|
||||
chat = self.chat(alice, [alice, bob])
|
||||
path = '/api/v1/chats/' + chat['id']
|
||||
self.assertEqual(self.call('GET', path, actor=bob), chat)
|
||||
self.call('GET', path, 404, actor=eve)
|
||||
self.call('GET', path, 401)
|
||||
self.call('GET', '/api/v1/chats/not-a-uuid', 400, actor=alice)
|
||||
self.call('GET', '/api/v1/chats/not-a-uuid', 401)
|
||||
self.assertEqual(self.call('GET', '/api/v1/chats', actor=eve)['items'], [])
|
||||
for members in [[alice, alice], [bob], [alice, str(uuid4())], [], [alice, 42]]:
|
||||
self.call('POST', '/api/v1/chats', 400,
|
||||
{'title': 'Чат', 'member_ids': members}, alice)
|
||||
self.assertEqual(len(self.call('GET', '/api/v1/chats', actor=alice)['items']), 1)
|
||||
|
||||
def test_chat_pages(self):
|
||||
actor = self.user()
|
||||
expected = [self.chat(actor) for _ in range(4)]
|
||||
expected.sort(key=lambda item: (item['created_at'], item['id']))
|
||||
page = self.call('GET', '/api/v1/chats?limit=1', actor=actor)
|
||||
self.assertEqual(page['items'], expected[:1])
|
||||
query = urlencode({'limit': 100, 'cursor': page['next_cursor']})
|
||||
last = self.call('GET', '/api/v1/chats?' + query, actor=actor)
|
||||
self.assertEqual(last['items'], expected[1:])
|
||||
self.assertIsNone(last['next_cursor'])
|
||||
self.call('GET', '/api/v1/chats?' + query, 400, actor=self.user())
|
||||
for query in ['limit=0', 'limit=101', 'limit=1.0', 'limit=true', 'cursor=', 'cursor=bad']:
|
||||
self.call('GET', '/api/v1/chats?' + query, 400, actor=actor)
|
||||
|
||||
def test_messages_and_cursors(self):
|
||||
alice, bob, eve = self.user(), self.user(), self.user()
|
||||
chat = self.chat(alice, [alice, bob])
|
||||
path = '/api/v1/chats/' + chat['id'] + '/messages'
|
||||
self.assertEqual(self.call('GET', path, actor=alice), {'items': [], 'next_cursor': None})
|
||||
messages = [self.call('POST', path, 201, {'text': text}, alice)
|
||||
for text in ['Привет 👋', 'Second', '🙂' * 4000]]
|
||||
self.assertEqual(messages[0]['sender_id'], alice)
|
||||
self.assertEqual(messages[-1]['text'], '🙂' * 4000)
|
||||
messages.sort(key=lambda item: (item['created_at'], item['id']))
|
||||
first = self.call('GET', path + '?limit=1', actor=bob)
|
||||
query = urlencode({'limit': 2, 'cursor': first['next_cursor']})
|
||||
last = self.call('GET', path + '?' + query, actor=bob)
|
||||
self.assertEqual(first['items'] + last['items'], messages)
|
||||
self.assertIsNone(last['next_cursor'])
|
||||
other = '/api/v1/chats/' + self.chat(alice)['id'] + '/messages'
|
||||
self.call('GET', other + '?' + query, 400, actor=alice)
|
||||
for body in [{'text': ''}, {'text': ' \n\t'}, {'text': 'a' * 4001},
|
||||
{'text': 123}, {'text': 'Hi', 'sender_id': bob}]:
|
||||
self.call('POST', path, 400, body, alice)
|
||||
for method, body in [('GET', None), ('POST', {'text': 'No access'})]:
|
||||
self.call(method, path, 404, body, eve)
|
||||
self.call(method, path, 401, body)
|
||||
for payload in [{}, {'resource': 'messages', 'owner_id': chat['id'],
|
||||
'id': None, 'created_at': 'bad'},
|
||||
{'resource': 'messages', 'owner_id': chat['id'],
|
||||
'id': messages[0]['id'], 'created_at': 'bad'}]:
|
||||
cursor = base64.urlsafe_b64encode(json.dumps(payload).encode()).decode()
|
||||
self.call('GET', path + '?' + urlencode({'cursor': cursor}), 400, actor=alice)
|
||||
|
||||
def test_concurrent_messages(self):
|
||||
actor = self.user()
|
||||
chat = self.chat(actor)
|
||||
path = '/api/v1/chats/' + chat['id'] + '/messages'
|
||||
def send(number):
|
||||
return self.call('POST', path, 201, {'text': str(number)}, actor)['id']
|
||||
with ThreadPoolExecutor(max_workers=10) as pool:
|
||||
sent = list(pool.map(send, range(100)))
|
||||
page = self.call('GET', path + '?limit=100', actor=actor)
|
||||
self.assertEqual(len(page['items']), 100)
|
||||
self.assertEqual(len(set(sent)), 100)
|
||||
self.assertEqual({item['id'] for item in page['items']}, set(sent))
|
||||
self.assertIsNone(page['next_cursor'])
|
||||
|
||||
def test_same_timestamp_order(self):
|
||||
from src.store import InMemoryStore
|
||||
store = InMemoryStore()
|
||||
actor = store.create_user('alice', 'Alice')['id']
|
||||
chat = store.create_chat(actor, 'Chat', [actor])['id']
|
||||
with patch('src.store.utc_now', return_value='2026-09-01T12:00:00.000000Z'):
|
||||
sent = [store.create_message(chat, actor, str(i)) for i in range(5)]
|
||||
received, cursor = [], None
|
||||
while True:
|
||||
page = store.list_messages(chat, actor, 2, cursor)
|
||||
received.extend(page['items'])
|
||||
cursor = page['next_cursor']
|
||||
if cursor is None:
|
||||
break
|
||||
self.assertEqual(received, sorted(sent, key=lambda item: item['id']))
|
||||
|
||||
def test_restart_clears_data(self):
|
||||
actor = self.user()
|
||||
chat = self.chat(actor)
|
||||
path = '/api/v1/chats/' + chat['id'] + '/messages'
|
||||
message = self.call('POST', path, 201, {'text': 'Before restart'}, actor)
|
||||
self.assertEqual(self.call('GET', path, actor=actor)['items'], [message])
|
||||
self.stop_server()
|
||||
self.setUpClass()
|
||||
self.call('GET', '/api/v1/users/me', 401, actor=actor)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,45 @@
|
||||
import asyncio
|
||||
import json
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from src.main import app
|
||||
|
||||
|
||||
class ErrorTests(unittest.TestCase):
|
||||
def test_unexpected_error_has_request_id(self):
|
||||
async def request():
|
||||
sent = []
|
||||
received = False
|
||||
|
||||
async def receive():
|
||||
nonlocal received
|
||||
if not received:
|
||||
received = True
|
||||
return {'type': 'http.request', 'body': b'', 'more_body': False}
|
||||
await asyncio.Event().wait()
|
||||
|
||||
async def send(message):
|
||||
sent.append(message)
|
||||
|
||||
scope = {
|
||||
'type': 'http', 'asgi': {'version': '3.0'}, 'http_version': '1.1',
|
||||
'method': 'GET', 'scheme': 'http', 'path': '/api/v1/users/me',
|
||||
'raw_path': b'/api/v1/users/me', 'query_string': b'',
|
||||
'headers': [(b'x-user-id', b'test'), (b'x-request-id', b'failure-test')],
|
||||
'server': ('localhost', 8080), 'client': ('localhost', 1234),
|
||||
}
|
||||
with patch('src.dependencies.store.require_user', side_effect=RuntimeError('private detail')):
|
||||
try:
|
||||
await app(scope, receive, send)
|
||||
except RuntimeError:
|
||||
pass
|
||||
return sent
|
||||
|
||||
sent = asyncio.run(request())
|
||||
start = next(item for item in sent if item['type'] == 'http.response.start')
|
||||
body = b''.join(item.get('body', b'') for item in sent if item['type'] == 'http.response.body')
|
||||
self.assertEqual(start['status'], 500)
|
||||
self.assertEqual(dict(start['headers'])[b'x-request-id'], b'failure-test')
|
||||
self.assertEqual(json.loads(body)['error']['request_id'], 'failure-test')
|
||||
self.assertNotIn(b'private detail', body)
|
||||
@@ -0,0 +1,106 @@
|
||||
import asyncio
|
||||
import json
|
||||
import unittest
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from dataclasses import replace
|
||||
from unittest.mock import patch
|
||||
|
||||
from src.errors import APIError
|
||||
from src.limits import Limits, ResourceLimitMiddleware
|
||||
from src.store import InMemoryStore
|
||||
|
||||
|
||||
class StoreLimitTests(unittest.TestCase):
|
||||
def make_store(self, **values):
|
||||
store = InMemoryStore(replace(Limits(), **values))
|
||||
actor = store.create_user('alice', 'Alice')['id']
|
||||
chat = store.create_chat(actor, 'Chat', [actor])['id']
|
||||
return store, actor, chat
|
||||
|
||||
def test_capacity_is_atomic_and_does_not_evict(self):
|
||||
store, actor, chat = self.make_store(max_messages=20)
|
||||
def send(i):
|
||||
try:
|
||||
return store.create_message(chat, actor, str(i))
|
||||
except APIError as exc:
|
||||
self.assertEqual(exc.status_code, 503)
|
||||
return None
|
||||
with ThreadPoolExecutor(max_workers=10) as pool:
|
||||
accepted = [item for item in pool.map(send, range(100)) if item]
|
||||
self.assertEqual(len(accepted), 20)
|
||||
page = store.list_messages(chat, actor, 100, None)
|
||||
self.assertEqual({item['id'] for item in page['items']}, {item['id'] for item in accepted})
|
||||
|
||||
def test_byte_limit_counts_utf8_and_other_objects_are_bounded(self):
|
||||
store, actor, chat = self.make_store(max_text_bytes=4, max_users=1, max_chats=1)
|
||||
saved = store.create_message(chat, actor, '🙂')
|
||||
for operation in [lambda: store.create_message(chat, actor, 'a'),
|
||||
lambda: store.create_user('bob', 'Bob'),
|
||||
lambda: store.create_chat(actor, 'Other', [actor])]:
|
||||
with self.assertRaises(APIError) as error:
|
||||
operation()
|
||||
self.assertEqual(error.exception.status_code, 503)
|
||||
self.assertEqual(store.list_messages(chat, actor, 100, None)['items'], [saved])
|
||||
|
||||
def test_rate_recovers_without_losing_messages(self):
|
||||
with patch('src.store.time.monotonic', return_value=0) as clock:
|
||||
store, actor, chat = self.make_store(message_rate=2, message_burst=1)
|
||||
first = store.create_message(chat, actor, 'one')
|
||||
with self.assertRaises(APIError) as error:
|
||||
store.create_message(chat, actor, 'rejected')
|
||||
self.assertEqual(error.exception.status_code, 429)
|
||||
clock.return_value = 0.5
|
||||
second = store.create_message(chat, actor, 'two')
|
||||
self.assertEqual(store.list_messages(chat, actor, 100, None)['items'],
|
||||
sorted([first, second], key=lambda item: (item['created_at'], item['id'])))
|
||||
|
||||
|
||||
class MiddlewareLimitTests(unittest.IsolatedAsyncioTestCase):
|
||||
async def call(self, app, chunks, headers=()):
|
||||
messages = iter(chunks)
|
||||
output = []
|
||||
async def receive():
|
||||
return next(messages)
|
||||
async def send(message):
|
||||
output.append(message)
|
||||
scope = {'type': 'http', 'path': '/api/v1/users', 'method': 'POST',
|
||||
'headers': headers, 'state': {'request_id': 'limit-test'}}
|
||||
await app(scope, receive, send)
|
||||
return output
|
||||
|
||||
async def test_body_limit_with_and_without_content_length(self):
|
||||
async def should_not_run(scope, receive, send):
|
||||
self.fail('Oversized body reached the API')
|
||||
app = ResourceLimitMiddleware(should_not_run, replace(Limits(), max_request_bytes=4))
|
||||
for headers, chunks in [([(b'content-length', b'5')], []), ([], [
|
||||
{'type': 'http.request', 'body': b'123', 'more_body': True},
|
||||
{'type': 'http.request', 'body': b'45', 'more_body': False}])]:
|
||||
result = await self.call(app, chunks, headers)
|
||||
self.assertEqual(result[0]['status'], 413)
|
||||
self.assertEqual(json.loads(result[1]['body'])['error']['request_id'], 'limit-test')
|
||||
self.assertEqual(app.active, 0)
|
||||
|
||||
async def test_admission_recovers_after_inflight_request_finishes(self):
|
||||
entered, release = asyncio.Event(), asyncio.Event()
|
||||
async def slow_app(scope, receive, send):
|
||||
entered.set()
|
||||
await release.wait()
|
||||
await send({'type': 'http.response.start', 'status': 200, 'headers': []})
|
||||
app = ResourceLimitMiddleware(slow_app, replace(Limits(), max_inflight=1))
|
||||
body = [{'type': 'http.request', 'body': b'', 'more_body': False}]
|
||||
first = asyncio.create_task(self.call(app, body))
|
||||
await entered.wait()
|
||||
overloaded = await self.call(app, body)
|
||||
self.assertEqual(overloaded[0]['status'], 503)
|
||||
release.set()
|
||||
await first
|
||||
self.assertEqual((await self.call(app, body))[0]['status'], 200)
|
||||
self.assertEqual(app.active, 0)
|
||||
|
||||
async def test_exception_releases_slot(self):
|
||||
async def broken_app(scope, receive, send):
|
||||
raise RuntimeError('test')
|
||||
app = ResourceLimitMiddleware(broken_app)
|
||||
with self.assertRaises(RuntimeError):
|
||||
await self.call(app, [{'type': 'http.request', 'body': b''}])
|
||||
self.assertEqual(app.active, 0)
|
||||
Reference in New Issue
Block a user