forked from mtusi2026/mtusi2026lab3
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b53e5d1077 | ||
|
|
aa971fbd27 |
@@ -18,3 +18,20 @@ __pycache__
|
||||
node_modules
|
||||
evidence
|
||||
.DS_Store
|
||||
.pytest_cache
|
||||
*.py[cod]
|
||||
tests
|
||||
student-tests
|
||||
contracts
|
||||
scripts
|
||||
examples
|
||||
docs
|
||||
*.md
|
||||
Makefile
|
||||
Dockerfile*
|
||||
requirements-dev.txt
|
||||
lab.json
|
||||
.mypy_cache
|
||||
.ruff_cache
|
||||
infra
|
||||
.venv-validate
|
||||
|
||||
+30
-5
@@ -3,11 +3,36 @@
|
||||
APP_HOST=0.0.0.0
|
||||
APP_PORT=8080
|
||||
APP_ENV=lab
|
||||
LOG_LEVEL=INFO
|
||||
SHUTDOWN_TIMEOUT_SEC=5
|
||||
|
||||
SHUTDOWN_DRAIN_SEC=1
|
||||
APP_KEEPALIVE_TIMEOUT_SEC=75
|
||||
|
||||
DB_USER=test
|
||||
DB_PASS=test
|
||||
DB_NAME=test
|
||||
DB_HOST=localhost
|
||||
DB_PORT=5432
|
||||
DB_POOL_MIN_SIZE=2
|
||||
DB_POOL_MAX_SIZE=10
|
||||
DB_CONNECT_TIMEOUT_SEC=3
|
||||
DB_COMMAND_TIMEOUT_SEC=5
|
||||
DB_STARTUP_TIMEOUT_SEC=30
|
||||
|
||||
VALKEY_HOST=localhost
|
||||
VALKEY_PORT=6379
|
||||
VALKEY_PASS=test
|
||||
VALKEY_POOL_MAX_SIZE=20
|
||||
VALKEY_TIMEOUT_SEC=0.5
|
||||
VALKEY_RETRY_AFTER_SEC=5
|
||||
|
||||
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
|
||||
SESSION_CACHE_TTL_SECONDS=60
|
||||
PASSWORD_HASH_WORKERS=4
|
||||
|
||||
# lab3: число API-реплик за балансировщиком и срок хранения Idempotency-Key
|
||||
API_REPLICAS=2
|
||||
# Публичный порт балансировщика на 127.0.0.1 (порт для make test-expiry — LB_EXPIRY_PORT=8081)
|
||||
LB_PORT=8080
|
||||
IDEMPOTENCY_TTL_SECONDS=86400
|
||||
|
||||
@@ -30,3 +30,7 @@ coverage/
|
||||
.vscode/
|
||||
# Keep small, sanitized evidence summaries in evidence/ under version control.
|
||||
# Inspect git diff --cached: ignore rules are not a secret scanner.
|
||||
.pytest_cache/
|
||||
.mypy_cache/
|
||||
.ruff_cache/
|
||||
.venv-validate/
|
||||
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
FROM python:3.12.12-slim-bookworm
|
||||
|
||||
ENV PYTHONDONTWRITEBYTECODE=1 \
|
||||
PYTHONUNBUFFERED=1 \
|
||||
POETRY_VIRTUALENVS_CREATE=false
|
||||
|
||||
WORKDIR /app
|
||||
COPY poetry.lock pyproject.toml ./
|
||||
RUN pip install --no-cache-dir --timeout 60 --retries 10 "poetry==2.2.1" \
|
||||
&& poetry install --only main --no-root --no-interaction
|
||||
COPY migrations ./migrations
|
||||
COPY src ./src
|
||||
|
||||
# Процесс работает не от root
|
||||
RUN useradd --system --uid 10001 --no-create-home app
|
||||
USER app
|
||||
|
||||
ARG APP_REVISION=dev
|
||||
ENV APP_REVISION=${APP_REVISION}
|
||||
LABEL org.opencontainers.image.revision=${APP_REVISION}
|
||||
|
||||
EXPOSE 8080
|
||||
HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \
|
||||
CMD ["python", "src/healthcheck.py"]
|
||||
CMD ["python", "src/gateway.py"]
|
||||
@@ -4,10 +4,16 @@ CA_FILE ?=
|
||||
ACTION_SCRIPT ?=
|
||||
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
|
||||
|
||||
.PHONY: help check validate test load test-persistence test-failover
|
||||
export BUILDX_NO_DEFAULT_ATTESTATIONS := 1
|
||||
|
||||
.PHONY: help check validate test load test-persistence test-failover install up down restart restore instances rolling-update ps logs wait-ready destroy-data student-test concurrency-test test-expiry test-failures test-migrations explain sigterm-check experiment-failover experiment-rolling
|
||||
|
||||
help:
|
||||
@echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes"
|
||||
@echo "up/down/restart/ps/logs: compose stack; destroy-data: DELETE volumes intentionally"
|
||||
@echo "instances: API containers <-> X-Instance-Id; restore: start replicas stopped by stop-one.sh; rolling-update: replace replicas one by one"
|
||||
@echo "experiment-failover / experiment-rolling: load + stop each replica / rolling update, metrics to evidence/"
|
||||
@echo "student-test: own tests; test-migrations: from empty volume; test-expiry: short SESSION_TTL; test-failures: stop Valkey/PostgreSQL; explain: EXPLAIN history"
|
||||
|
||||
check:
|
||||
$(PYTHON) scripts/check_contract.py
|
||||
@@ -28,3 +34,70 @@ test-persistence:
|
||||
test-failover:
|
||||
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2)
|
||||
$(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||
|
||||
install:
|
||||
poetry install --no-root
|
||||
|
||||
up:
|
||||
docker compose up -d --build --wait
|
||||
|
||||
down:
|
||||
docker compose --profile expiry down
|
||||
|
||||
restart:
|
||||
./scripts/restart.sh
|
||||
@$(MAKE) --no-print-directory wait-ready
|
||||
|
||||
restore:
|
||||
./scripts/restore.sh
|
||||
|
||||
instances:
|
||||
./scripts/instances.sh
|
||||
|
||||
rolling-update:
|
||||
./scripts/rolling-update.sh
|
||||
|
||||
ps:
|
||||
docker compose ps
|
||||
|
||||
logs:
|
||||
docker compose logs -f api nginx
|
||||
|
||||
wait-ready:
|
||||
@for i in $$(seq 1 60); do \
|
||||
curl -fsS "$(BASE_URL)/health/ready" >/dev/null 2>&1 && echo "ready: $(BASE_URL)" && exit 0; \
|
||||
sleep 1; \
|
||||
done; echo "API is not ready after 60s"; exit 1
|
||||
|
||||
destroy-data:
|
||||
docker compose --profile expiry down -v
|
||||
|
||||
student-test:
|
||||
BASE_URL="$(BASE_URL)" poetry run pytest
|
||||
|
||||
concurrency-test:
|
||||
BASE_URL="$(BASE_URL)" poetry run pytest student-tests/test_concurrency.py -s
|
||||
|
||||
docker compose --profile expiry up -d --build --wait api-short-ttl
|
||||
EXPIRY_BASE_URL=http://localhost:8081 poetry run pytest student-tests/test_expiry.py -s; \
|
||||
status=$$?; docker compose --profile expiry stop api-short-ttl; exit $$status
|
||||
|
||||
test-failures:
|
||||
BASE_URL="$(BASE_URL)" RUN_FAILURE_TESTS=1 poetry run pytest student-tests/test_dependency_failures.py -s
|
||||
|
||||
test-migrations:
|
||||
./scripts/check_migrations.sh
|
||||
|
||||
explain:
|
||||
docker compose exec -T postgres sh -c 'psql -X -q -v ON_ERROR_STOP=1 -U "$$POSTGRES_USER" -d "$$POSTGRES_DB"' \
|
||||
< scripts/explain_history.sql > evidence/explain_history.txt
|
||||
@echo "saved: evidence/explain_history.txt"
|
||||
|
||||
sigterm-check:
|
||||
$(PYTHON) scripts/sigterm_check.py --base-url "$(BASE_URL)"
|
||||
|
||||
experiment-failover:
|
||||
$(PYTHON) scripts/experiment.py failover --base-url "$(BASE_URL)" --output evidence/experiment-failover.json
|
||||
|
||||
experiment-rolling:
|
||||
$(PYTHON) scripts/experiment.py rolling --base-url "$(BASE_URL)" --output evidence/experiment-rolling.json
|
||||
|
||||
@@ -1,37 +1,160 @@
|
||||
# Отчёт — лабораторная 3
|
||||
|
||||
- Автор, группа: TODO
|
||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
||||
- Commit/PR: TODO после публикации
|
||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
||||
- Автор, группа: Саватеев Артём, БВТ2302
|
||||
- Целевая оценка: **4** (весь уровень 3 + весь уровень 4);
|
||||
- Источники: документация [nginx upstream](https://nginx.org/en/docs/http/ngx_http_upstream_module.html) (`resolve`, `max_fails`, `keepalive_timeout`) и [proxy](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_next_upstream) (`proxy_next_upstream`, повтор неидемпотентных запросов), [Compose services/deploy](https://docs.docker.com/reference/compose-file/services/), исходники uvicorn 0.54 (`Server.shutdown`, h11 keep-alive). Библиотеки — из lab2 (FastAPI, asyncpg, redis-py, argon2-cffi), новых зависимостей нет. Помощь ии
|
||||
|
||||
## Запуск из чистого clone
|
||||
|
||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
||||
|
||||
```sh
|
||||
cp .env.example .env # затем задать свои DB_PASS и VALKEY_PASS (синтетические)
|
||||
docker compose --env-file .env config --quiet
|
||||
make up # = docker compose up -d --build --wait: build → postgres/valkey →
|
||||
# migrate (однократно) → api ×API_REPLICAS → nginx; ждёт healthy
|
||||
make check test # стенд доступен только через LB: http://localhost:8080
|
||||
python3 -m venv .venv-validate && .venv-validate/bin/python -m pip install -r requirements-dev.txt
|
||||
make validate PYTHON=.venv-validate/bin/python # полная OpenAPI-валидация; не в .venv —
|
||||
# его Poetry принял бы за окружение проекта (Python 3.9)
|
||||
make test-persistence ACTION_SCRIPT=scripts/restart.sh
|
||||
make test-failover ACTION_SCRIPT=scripts/stop-one.sh
|
||||
make instances # какой контейнер остановлен: контейнер ↔ X-Instance-Id ↔ PID ↔ ревизия
|
||||
make restore # вернуть остановленную реплику (работающие не пересоздаются)
|
||||
make student-test # собственные тесты (poetry install --no-root один раз)
|
||||
make rolling-update # выкладка новой сборки по одной реплике без простоя
|
||||
make down # безопасная остановка, volume с данными сохраняется
|
||||
make destroy-data # намеренное удаление учебных данных (down -v)
|
||||
```
|
||||
|
||||
Действия оператора: создать `.env` с секретами (в Git не попадает), после `stop-one.sh` вручную выполнить `make restore`. Миграции отдельно запускать не нужно — их выполняет сервис `migrate` внутри `make up`/`rolling-update`. Сборку через `make`/скрипты лучше, чем голый `docker compose up --build`: Makefile экспортирует `BUILDX_NO_DEFAULT_ATTESTATIONS=1`, иначе Docker Desktop добавляет к образу provenance-attestation, ID образа меняется на каждой сборке и Compose пересоздаёт все реплики разом. Обновлять работающий стенд — только `make rolling-update`.
|
||||
|
||||
Воспроизводимость проверена копией ровно тех файлов, что попадут в Git (без `.env`), отдельным compose-проектом на пустом volume: build, migrate с нуля, smoke, failover, persistence — [evidence/15-clean-clone.txt](evidence/15-clean-clone.txt).
|
||||
|
||||
## Архитектура и решения
|
||||
|
||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab2, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
||||
```
|
||||
клиент ──► nginx :8080 (127.0.0.1, единственный опубликованный порт)
|
||||
│ round-robin, без sticky; Docker DNS «api» перечитывается каждые 2 с
|
||||
├──► api-1 ─┐ сеть edge: только LB и API
|
||||
└──► api-2 ─┤
|
||||
├──► postgres (источник истины: пользователи, чаты, сообщения,
|
||||
│ сессии, ключи идемпотентности; volume postgres_data)
|
||||
migrate ────┤
|
||||
└──► valkey (read-through кэш сессий, не источник истины)
|
||||
сеть backend: internal, LB туда не подключён
|
||||
```
|
||||
|
||||
**Перенесено из lab2:** API, PostgreSQL как источник истины, сессии в PostgreSQL с кэшем в Valkey (logout инвалидирует общий кэш), Argon2id, keyset-пагинация, монотонный `created_at` под advisory lock чата, обработка отказов Valkey/PostgreSQL. Локального состояния, от которого зависит корректность, в API нет: circuit breaker Valkey и пул соединений — только оптимизации процесса.
|
||||
|
||||
**Изменено в lab3:**
|
||||
|
||||
| Что | Решение | Где |
|
||||
| --- | --- | --- |
|
||||
| Реплики и LB | `api` с `deploy.replicas: ${API_REPLICAS:-2}`, без `container_name` и портов; nginx 1.28 `upstream api { server api:8080 resolve; }` — новые/остановленные контейнеры подхватываются без reload | [compose.yaml](compose.yaml), [infra/nginx/nginx.conf](infra/nginx/nginx.conf) |
|
||||
| X-Instance-Id | случайный alias процесса (`secrets.token_hex(6)`), без hostname/IP; в каждом ответе API; процесс пишет его в `/tmp/instance-id` для оператора | [request_context.py](src/application/api/middlewares/request_context.py), [settings.py](src/config/settings.py) |
|
||||
| X-Request-Id | nginx передаёт `$request_id`, API принимает его (только 32 hex) — одна строка access-лога LB и журнал реплики связаны одним ID | там же, [evidence/04](evidence/04-balancing-idempotency.txt) |
|
||||
| Idempotency-Key | обязательный заголовок `[A-Za-z0-9._:-]{1,128}` (нет/неверный — 400); таблица `message_idempotency_keys`, PK `(sender_id, chat_id, key)`, SHA-256 канонического тела, TTL `IDEMPOTENCY_TTL_SECONDS` = 24 ч | [0002_message_idempotency.sql](migrations/0002_message_idempotency.sql), [postgres.py](src/infrastructure/repositories/message_repository/postgres.py), [message_service.py](src/infrastructure/services/message_service/message_service.py) |
|
||||
| Миграции | one-shot сервис `migrate` (`src/migrate.py`) до реплик (`service_completed_successfully`); реплика миграции не применяет, а проверяет версию схемы и не стартует, если схема старее кода | [database.py](src/infrastructure/dependencies/database.py), [migrate.py](src/migrate.py) |
|
||||
| Graceful shutdown | SIGTERM → drain 1 с (порт закрыт, readiness 503, ответы по открытым keep-alive с `Connection: close`) → graceful uvicorn до 5 с → закрытие пулов | [gateway.py](src/gateway.py) |
|
||||
| Ошибки LB | 502/503/504, созданные nginx, отдаются как `503 {"error":{"code":"unavailable",…}}` с `X-Request-Id`; ответы реплик проходят без изменений | [infra/nginx/errors.conf](infra/nginx/errors.conf) |
|
||||
|
||||
**Транзакционная граница идемпотентности.** В одной транзакции: `pg_advisory_xact_lock(чат)` → поиск действующего ключа → (повтор: вернуть сохранённое сообщение или 409 при другом теле) → `INSERT` сообщения → `INSERT` ключа. Ключ привязан к чату, поэтому блокировка чата, уже нужная для монотонного `created_at`, сериализует и одновременные повторы с разных реплик: второй ждёт COMMIT первого и находит ключ (READ COMMITTED). Ошибка до COMMIT откатывает и сообщение, и ключ — транзиентный 5xx не фиксируется как результат. Повтор заново проходит проверку сессии и членства. Область «пользователь + метод + путь + ключ» сведена к `(sender_id, chat_id, key)`: метод и путь фиксированы. Порядок ключей и пробелы JSON не влияют (хеш от тела, сериализованного по схеме). Истёкший ключ переиспользуется (`ON CONFLICT … DO UPDATE`) и создаёт новое сообщение.
|
||||
|
||||
**Почему nginx не повторяет POST сам.** `proxy_next_upstream error timeout` без `non_idempotent`: запрос, не дошедший до реплики (connect refused/timeout), nginx отправляет следующей реплике — это безопасно для любого метода. POST, уже отправленный реплике, не повторяется: прозрачный повтор `POST /users`/`/chats` создал бы дубль (контракт это запрещает). Неопределённый POST сообщения повторяет клиент с тем же ключом. 503 приложения тоже не повторяется и не исключает реплику: он означает отказ общей зависимости, другая реплика зависит от той же БД.
|
||||
|
||||
### Health, readiness, таймауты и время обнаружения
|
||||
|
||||
| Механизм | Значение | Назначение |
|
||||
| --- | --- | --- |
|
||||
| `/health/live` | 200 пока жив процесс | liveness, проверки через LB |
|
||||
| `/health/ready` | 200 если не завершается и PostgreSQL отвечает (Valkey не критичен — сессии читаются из БД) | Docker healthcheck API: `interval 5s, timeout 3s, retries 2, start_period 20s, start_interval 1s`; используется `up --wait`, `depends_on` nginx, `restore.sh`, `rolling-update.sh` |
|
||||
| Порт реплики | открывается только после startup (БД + версия схемы), закрывается первым при SIGTERM | LB не шлёт трафик в неготовую/завершающуюся реплику |
|
||||
| nginx `proxy_connect_timeout` | 0,5 с | исчезнувший IP (SIGKILL/крах контейнера) |
|
||||
| nginx `proxy_read_timeout` / `send` | 10 с | зависшая реплика; дольше самого долгого запроса API (ожидание пула 3 с + statement_timeout 5 с) |
|
||||
| `max_fails=1 fail_timeout=5s` | | после ошибки соединения реплика исключена на 5 с, затем пробный запрос |
|
||||
| `resolver 127.0.0.11 valid=2s` | | остановленный контейнер пропадает из upstream ≤ 2 с, новый появляется ≤ 2 с |
|
||||
| keep-alive | nginx → API `keepalive_timeout 60s` < uvicorn `timeout_keep_alive 75 s` | idle-соединение закрывает LB, а не реплика: нет гонки «запрос в закрытое соединение» |
|
||||
| SIGTERM | drain 1 с + graceful ≤ 5 с < `stop_grace_period 10s` | SIGKILL не прерывает завершение |
|
||||
|
||||
У nginx open source нет активных health checks — проверка пассивная, по реальным запросам. **Максимальное время обнаружения:** остановка процесса/SIGTERM — 0 с (порт закрыт → `connection refused` → тот же запрос сразу уходит на другую реплику, клиент ошибки не видит); крах контейнера — ≤ 0,5 с на запрос (connect timeout), затем исключение на 5 с и удаление из DNS ≤ 2 с; зависание без закрытия порта — ≤ 10 с (read timeout; GET повторяется, POST получает 503 и повторяется клиентом с тем же ключом). Docker healthcheck обнаруживает неготовность за ≤ 5 с × 2 = 10 с, но в Compose он не исключает контейнер из DNS — поэтому балансировку обеспечивает nginx, а не `depends_on`/healthcheck.
|
||||
|
||||
### Ресурсы, сети, секреты
|
||||
|
||||
| Роль | Service | Образ | CPU / RAM | Сети | Порт наружу | Volume | Credentials |
|
||||
| --- | --- | --- | --- | --- | --- | --- | --- |
|
||||
| LB | `nginx` | nginx:1.28.0-alpine3.21 | 0,5 / 64M | edge | 127.0.0.1:8080 (+8081 только для `test-expiry`) | — | нет |
|
||||
| API ×2 | `api` | mtusi-messenger:lab3 (из исходников) | 1,0 / 512M каждая | edge, backend | нет | — | DB_PASS, VALKEY_PASS из `.env` |
|
||||
| Миграции | `migrate` | тот же | 0,5 / 256M | backend | нет | — | DB_PASS |
|
||||
| БД | `postgres` | postgres:18.2-alpine3.22 | 1,5 / 512M | backend | нет | `postgres_data` | POSTGRES_PASSWORD = DB_PASS |
|
||||
| Кэш сессий | `valkey` | valkey/valkey:8.1.4-alpine3.22 | 0,25 / 96M | backend | нет | нет (не источник истины) | VALKEY_PASS |
|
||||
|
||||
Сеть `backend` — `internal`: nginx не видит БД и Valkey (даже не резолвит имя), хранилища не имеют выхода наружу. Порт PostgreSQL больше не публикуется на хост (в lab2 был 127.0.0.1:5432); тесты обращаются к БД через `docker compose exec`. Подтверждение: [evidence/11-limits-ports-networks.txt](evidence/11-limits-ports-networks.txt). 512M на реплику: Argon2id RFC 9106 low-memory — 64 MiB × `PASSWORD_HASH_WORKERS=4`. Пул БД: 2 реплики × `DB_POOL_MAX_SIZE=10` + migrate + реплика при rolling update = 40 ≤ `max_connections=100`.
|
||||
|
||||
### Соответствие .env.example
|
||||
|
||||
| Переменная | Используется | Примечание |
|
||||
| --- | --- | --- |
|
||||
| `APP_HOST`, `APP_PORT` | uvicorn | 0.0.0.0:8080 внутри контейнера |
|
||||
| `APP_ENV`, `LOG_LEVEL` | журнал | |
|
||||
| `APP_KEEPALIVE_TIMEOUT_SEC=75` | uvicorn `timeout_keep_alive` | новое; > keepalive_timeout nginx |
|
||||
| `SHUTDOWN_TIMEOUT_SEC=5` | uvicorn `timeout_graceful_shutdown` | |
|
||||
| `SHUTDOWN_DRAIN_SEC=1` | `DrainingServer` | новое |
|
||||
| `DB_*` | asyncpg pool | `DB_HOST/DB_PORT` переопределены в compose (`postgres:5432`); `DB_PORT` больше не публикуется |
|
||||
| `VALKEY_*` | redis-py | `VALKEY_HOST/PORT` переопределены в compose |
|
||||
| `SESSION_TTL_SECONDS`, `SESSION_CACHE_TTL_SECONDS`, `PASSWORD_HASH_WORKERS` | как в lab2 | `api-short-ttl` переопределяет TTL = 2 |
|
||||
| `API_REPLICAS=2` | `deploy.replicas` | |
|
||||
| `IDEMPOTENCY_TTL_SECONDS=86400` | срок ключа | |
|
||||
| `LB_PORT=8080` (+ `LB_EXPIRY_PORT`, по умолчанию 8081) | публикация nginx | новое |
|
||||
| `APP_REVISION` | build arg → метка образа и журнал старта | не в `.env`; задаёт `rolling-update.sh` |
|
||||
|
||||
## Доказательства критериев
|
||||
|
||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
||||
| --- | --- | --- | --- |
|
||||
| Публичный smoke | make test | TODO | TODO |
|
||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
||||
Формат файлов — транскрипт терминала: `$ команда`, затем сырой вывод.
|
||||
|
||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл |
|
||||
| --- | --- | --- | --- |
|
||||
| Публичный smoke | `make check test` | PASS lab3 public API scenario | [02](evidence/02-check-smoke.txt) |
|
||||
| 3: стенд одной командой, ≥2 API, один вход, общие БД и сессии | `make up`, `docker compose ps`, `make instances` | 2 реплики healthy, migrate Exited (0), наружу только nginx | [01](evidence/01-up.txt) |
|
||||
| 3: одна сессия → разные реплики без повторного входа | `pytest test_balancing.py -s`, access-лог nginx | 20 запросов одним токеном: 10/10 по двум `X-Instance-Id`; logout на одной реплике действует на всех | [04](evidence/04-balancing-idempotency.txt) |
|
||||
| 3: остановка любой реплики, чтение/запись ≤ 10 с | `make test-failover` для `api-1`, затем для `api-2` | 129 / 128 / 129 успешных записей, **0 неудачных попыток**, первая запись после возврата hook 1,6–2,7 с; `instances` и `docker inspect` показывают реально остановленный контейнер (exit 0, журнал drain/shutdown) | [05a](evidence/05-failover-replica-a.txt), [05b](evidence/05-failover-replica-b.txt) |
|
||||
| 3: Idempotency-Key без дублей, включая конкурентный повтор и смену реплики | `pytest test_idempotency.py -s` | 20 одновременных POST с одним ключом через обе реплики (10/10): все 201, одно тело, 1 строка в БД; разные тела под одним ключом — один победитель, остальные 409; 400 для нет/неверного ключа; область user+chat; повтор после logout — 401, чужой — 404; истёкший ключ создаёт новое | [04](evidence/04-balancing-idempotency.txt) |
|
||||
| 3: persistence | `make test-persistence ACTION_SCRIPT=scripts/restart.sh` | PASS; `StartedAt` всех 5 контейнеров изменился, volume тот же (created 16:10:27Z) | [06](evidence/06-persistence.txt) |
|
||||
| Регрессия lab2 | `make student-test`, `test-expiry`, `test-failures` | 107 passed; TTL 2 с через LB :8081; отказ Valkey/PostgreSQL как в lab2 | [03](evidence/03-student-tests.txt), [07](evidence/07-expiry.txt), [08](evidence/08-dependency-failures.txt) |
|
||||
| 4: health/readiness/таймауты/SIGTERM согласованы; LB исключает backend | `make sigterm-check` | exit 0 за 1,53 с; 354 запроса через LB после SIGTERM без ошибок; после выхода реплика не отвечает | [10](evidence/10-sigterm.txt) |
|
||||
| 4: поочерёдная остановка каждой реплики, RPS/ошибки/восстановление | `make experiment-failover` | см. «Эксперименты»: SIGTERM — 0 ошибок; SIGKILL — 1 и 3 ошибки 503, повтор тем же ключом; потерь/дублей 0 | [13](evidence/13-experiment-failover.txt), [json](evidence/experiment-failover.json) |
|
||||
| 4: идемпотентность параллельными запросами | `test_parallel_*` | см. строку уровня 3 | [04](evidence/04-balancing-idempotency.txt) |
|
||||
| 4: лимиты CPU/RAM, API без портов, сети/секреты, нет `container_name` | `docker inspect`, `docker network inspect` | лимиты у всех контейнеров; порты только у nginx; backend internal | [11](evidence/11-limits-ports-networks.txt) |
|
||||
| 4: rolling update под нагрузкой | `make experiment-rolling` | обе реплики заменены новой ревизией, 0 ошибок, наибольший разрыв между подтверждениями записи 0,098 с | [14](evidence/14-experiment-rolling.txt), [json](evidence/experiment-rolling.json) |
|
||||
| 4: миграции однократно и совместимы с работающей версией | `make test-migrations`, `scripts/check_previous_version.sh` | с пустого volume, 3 повтора идемпотентны; API до migrate отказывается стартовать; **lab2-код (`aa971fb`) работает на схеме v2**: вход, чат, запись, чтение | [09](evidence/09-migrations.txt) |
|
||||
| Чистый clone | копия файлов Git, отдельный проект | build → smoke → failover → persistence PASS, затем `down -v` | [15](evidence/15-clean-clone.txt) |
|
||||
|
||||
## Эксперименты и отказы
|
||||
|
||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
||||
**Гипотеза.** При остановке одной из двух реплик (SIGTERM) клиент не видит ошибок: порт закрывается раньше, чем реплика перестаёт отвечать, и nginx переотправляет ещё не доставленные запросы; при SIGKILL теряются только запросы, бывшие в обработке у убитой реплики, а повтор с тем же ключом не создаёт дублей. Пропускная способность при одной реплике падает, так как API ограничена 1 CPU.
|
||||
|
||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
||||
**Методика** ([scripts/experiment.py](scripts/experiment.py)). Стенд из таблицы ресурсов на Apple M4 / Docker Desktop VM 10 CPU, 8 GiB. 4 потока closed loop через LB: `POST` сообщения (неподтверждённый POST повторяется с тем же ключом и телом каждые 50 мс до 201) + `GET ?limit=1`; timeout клиента 5 с; новый чат на прогон (≈ 10–12 тыс. сообщений за прогон). Фазы: 8 с до отказа → отказ (`docker stop` = SIGTERM, затем `docker kill -s SIGKILL`) → 10 с с одной репликой → `scripts/restore.sh` → 8 с. Порядок: `api-1`, `api-2` для каждого сигнала, с восстановлением предыдущей. После прогона история читается целиком: каждое подтверждённое сообщение на месте и не изменено, нет дублей id/текстов. «Время восстановления записи» — от первой ошибки после отказа до первой успешной записи после последней ошибки; «разрыв» — наибольший интервал между подтверждениями записи. Один прогон на сценарий; CPU/RAM — `docker stats` в середине фазы «до».
|
||||
|
||||
| Воздействие | Что ожидаем | Что наблюдали (запись ok RPS до / во время / после) | Ошибки, восстановление, данные |
|
||||
| --- | --- | --- | --- |
|
||||
| TODO | TODO | TODO | TODO |
|
||||
| SIGTERM `api-1` | 0 ошибок | 382 / 277 / 399 | 0 ошибок; разрыв 0,063 с; реплика в ротации через 0,97 с после restore; потерь/дублей 0 |
|
||||
| SIGTERM `api-2` | 0 ошибок | 401 / 282 / 362 | 0 ошибок; разрыв 0,071 с; в ротации через 3,7 с; 0 / 0 |
|
||||
| SIGKILL `api-1` | ошибки только у запросов в обработке | 427 / 281 / 408 | 1 × 503 (POST, оборван убитой репликой), восстановление < 0,01 с, разрыв 0,070 с; повтор ключом создал сообщение один раз; 0 / 0 |
|
||||
| SIGKILL `api-2` | то же | 387 / 277 / 373 | 3 × 503, восстановление 0,006 с, разрыв 0,061 с; 0 / 0 |
|
||||
| Rolling update 2 → 3 → 2 → 3 → 2 | 0 ошибок | 420 / 308 / 345 | 0 ошибок; разрыв 0,098 с; старые alias исчезли через 8,1 и 14,9 с от начала, новые вошли на 4,9 и 13,9 с; обе реплики на новой ревизии; 0 / 0 |
|
||||
| Valkey / PostgreSQL недоступны | обход кэша / ограниченный 503 | как в lab2, через LB | [08](evidence/08-dependency-failures.txt) |
|
||||
| Рестарт всего стенда | данные и токен сохраняются | PASS | [06](evidence/06-persistence.txt) |
|
||||
|
||||
Нагрузка до отказа: API ≈ 84 % от лимита 1 CPU каждая, PostgreSQL ≈ 28 %, nginx ≈ 9 %, память API ≈ 48 MiB. Во время отказа запись падает на ≈ 30 % (не вдвое: при двух репликах узким местом частично являются клиент и БД). Базовый публичный генератор (`tests/load.py`, 30 с, concurrency 4, только POST): 713 успешных RPS, 0 ошибок, p50/p95/p99 = 4,7 / 8,4 / 22,5 мс — [evidence/12](evidence/12-load-baseline.txt), [load.json](evidence/load.json).
|
||||
|
||||
**Подтверждённое улучшение — drain при SIGTERM.** Без drain uvicorn по SIGTERM сразу закрывает idle keep-alive соединения, и nginx изредка отправлял запрос в уже закрываемое соединение: в пробном rolling update был `upstream prematurely closed connection` (GET nginx повторил; POST в той же ситуации получил бы 503). С drain в итоговых прогонах SIGTERM и rolling update таких событий 0 — все 4 случая «prematurely closed» в журнале nginx пришлись на прогоны SIGKILL (сопоставлено по времени, где это ожидаемо).
|
||||
|
||||
**Ограничения измерения.** Closed loop с 4 потоками страдает coordinated omission и ограничен клиентом на той же машине; по одному прогону на сценарий, без доверительных интервалов; RPS — подтверждения HTTP, а не COMMIT. Числа описывают этот ноутбук, не универсальную ёмкость.
|
||||
|
||||
## Ограничения и следующие шаги
|
||||
|
||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|
||||
- **Выдерживается:** отказ/остановка/крах одного API-процесса или контейнера, поочерёдная замена реплик, перезапуск всего стенда с volumes, отказ Valkey. **Не выдерживается:** отказ nginx, PostgreSQL (одна реплика БД — SPOF; API честно отвечает 503) и Docker-host — все контейнеры на одном узле. Отказ процесса ≠ отказ узла: здесь проверен только первый.
|
||||
- nginx open source — только пассивные проверки: реплика, которая принимает соединения, но не отвечает, обнаруживается за `proxy_read_timeout` (10 с) и снова получает пробный запрос каждые 5 с; healthcheck Docker в Compose её не исключает. Активные проверки дал бы HAProxy/Traefik или nginx Plus.
|
||||
- `docker stop`/`docker kill` не перезапускаются политикой `unless-stopped` (так и задумано для `stop-one.sh`); автоматического восстановления упавшей реплики Compose не даёт — `make restore` выполняет оператор.
|
||||
- Истёкшие ключи идемпотентности удаляются только при повторном использовании ключа; фоновой очистки нет (строки маленькие, но таблица растёт).
|
||||
- Совместимость миграций обеспечивается правилом expand-only и проверена для пары lab2 → lab3 на уровне схемы; поведение API при смешанных версиях (старая реплика не требует ключа и не отдаёт `X-Instance-Id`) во время такой выкладки различается — это переход контракта, а не схемы.
|
||||
- `api-short-ttl` для `make test-expiry` тоже за LB (порт 8081), но 8081 публикуется nginx постоянно; без профиля `expiry` он отвечает 503.
|
||||
+169
@@ -0,0 +1,169 @@
|
||||
name: messenger-lab3
|
||||
|
||||
x-app: &app
|
||||
build:
|
||||
context: .
|
||||
args:
|
||||
APP_REVISION: ${APP_REVISION:-dev}
|
||||
image: mtusi-messenger:lab3
|
||||
env_file:
|
||||
- .env
|
||||
environment: &app-network
|
||||
DB_HOST: postgres
|
||||
DB_PORT: "5432"
|
||||
VALKEY_HOST: valkey
|
||||
VALKEY_PORT: "6379"
|
||||
|
||||
x-api-resources: &api-resources
|
||||
resources:
|
||||
limits:
|
||||
cpus: "1.0"
|
||||
memory: 512M
|
||||
reservations:
|
||||
cpus: "0.25"
|
||||
memory: 128M
|
||||
|
||||
x-api: &api
|
||||
<<: *app
|
||||
restart: unless-stopped
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
valkey:
|
||||
condition: service_started
|
||||
networks:
|
||||
- edge
|
||||
- backend
|
||||
healthcheck:
|
||||
test: ["CMD", "python", "src/healthcheck.py"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 2
|
||||
start_period: 20s
|
||||
start_interval: 1s
|
||||
stop_grace_period: 10s
|
||||
|
||||
services:
|
||||
nginx:
|
||||
image: nginx:1.28.0-alpine3.21
|
||||
restart: unless-stopped
|
||||
depends_on:
|
||||
api:
|
||||
condition: service_healthy
|
||||
ports:
|
||||
- "127.0.0.1:${LB_PORT:-8080}:8080"
|
||||
- "127.0.0.1:${LB_EXPIRY_PORT:-8081}:8081"
|
||||
volumes:
|
||||
- ./infra/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
|
||||
- ./infra/nginx/errors.conf:/etc/nginx/errors.conf:ro
|
||||
networks:
|
||||
- edge
|
||||
healthcheck:
|
||||
test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:8090/healthz"]
|
||||
interval: 5s
|
||||
timeout: 2s
|
||||
retries: 3
|
||||
stop_grace_period: 10s
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.5"
|
||||
memory: 64M
|
||||
|
||||
api:
|
||||
<<: *api
|
||||
deploy:
|
||||
<<: *api-resources
|
||||
replicas: ${API_REPLICAS:-2}
|
||||
|
||||
migrate:
|
||||
<<: *app
|
||||
command: ["python", "src/migrate.py"]
|
||||
restart: "no"
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
networks:
|
||||
- backend
|
||||
healthcheck:
|
||||
disable: true
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.5"
|
||||
memory: 256M
|
||||
|
||||
postgres:
|
||||
image: postgres:18.2-alpine3.22
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
POSTGRES_USER: ${DB_USER}
|
||||
POSTGRES_PASSWORD: ${DB_PASS}
|
||||
POSTGRES_DB: ${DB_NAME}
|
||||
volumes:
|
||||
- postgres_data:/var/lib/postgresql
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U \"$$POSTGRES_USER\" -d \"$$POSTGRES_DB\""]
|
||||
interval: 2s
|
||||
timeout: 3s
|
||||
retries: 15
|
||||
start_period: 10s
|
||||
networks:
|
||||
- backend
|
||||
stop_grace_period: 30s
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "1.5"
|
||||
memory: 512M
|
||||
|
||||
valkey:
|
||||
image: valkey/valkey:8.1.4-alpine3.22
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
VALKEY_PASS: ${VALKEY_PASS}
|
||||
command:
|
||||
- valkey-server
|
||||
- --requirepass
|
||||
- ${VALKEY_PASS}
|
||||
- --save
|
||||
- ""
|
||||
- --appendonly
|
||||
- "no"
|
||||
- --maxmemory
|
||||
- 64mb
|
||||
- --maxmemory-policy
|
||||
- allkeys-lru
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "valkey-cli --no-auth-warning -a \"$$VALKEY_PASS\" ping | grep -q PONG"]
|
||||
interval: 2s
|
||||
timeout: 3s
|
||||
retries: 15
|
||||
networks:
|
||||
- backend
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.25"
|
||||
memory: 96M
|
||||
|
||||
api-short-ttl:
|
||||
<<: *api
|
||||
profiles:
|
||||
- expiry
|
||||
environment:
|
||||
<<: *app-network
|
||||
SESSION_TTL_SECONDS: "2"
|
||||
deploy:
|
||||
<<: *api-resources
|
||||
replicas: 1
|
||||
|
||||
networks:
|
||||
edge: {}
|
||||
backend:
|
||||
internal: true
|
||||
|
||||
volumes:
|
||||
postgres_data:
|
||||
@@ -0,0 +1,21 @@
|
||||
$ date -u +%Y-%m-%dT%H:%M:%SZ
|
||||
2026-10-19T16:59:47Z
|
||||
$ git log -1 --format="%h %s" && git status --short | wc -l | xargs echo "uncommitted files:"
|
||||
aa971fb feat: перенос core-логики 2 лабы
|
||||
uncommitted files: 42
|
||||
$ sysctl -n machdep.cpu.brand_string hw.ncpu hw.memsize
|
||||
Apple M4
|
||||
10
|
||||
17179869184
|
||||
$ docker version --format "Docker Engine {{.Server.Version}}, client {{.Client.Version}}"
|
||||
Docker Engine 29.0.1, client 29.0.1
|
||||
$ docker compose version
|
||||
Docker Compose version v2.40.3-desktop.1
|
||||
$ docker info --format "Docker VM: {{.NCPU}} CPU, {{.MemTotal}} bytes RAM, {{.OperatingSystem}}, kernel {{.KernelVersion}}"
|
||||
Docker VM: 10 CPU, 8217448448 bytes RAM, Docker Desktop, kernel 6.12.54-linuxkit
|
||||
$ python3 --version
|
||||
Python 3.9.6
|
||||
$ poetry run python --version
|
||||
/Users/enterxx/Library/Python/3.9/lib/python/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020
|
||||
warnings.warn(
|
||||
Python 3.12.12
|
||||
@@ -0,0 +1,150 @@
|
||||
$ docker compose --env-file .env config --quiet && echo "compose config: OK"
|
||||
compose config: OK
|
||||
$ make up
|
||||
docker compose up -d --build --wait
|
||||
#1 [internal] load local bake definitions
|
||||
#1 reading from stdin 1.17kB done
|
||||
#1 DONE 0.0s
|
||||
|
||||
#2 [api internal] load build definition from Dockerfile
|
||||
#2 transferring dockerfile: 914B done
|
||||
#2 DONE 0.0s
|
||||
|
||||
#3 [api internal] load metadata for docker.io/library/python:3.12.12-slim-bookworm
|
||||
#3 DONE 0.3s
|
||||
|
||||
#4 [migrate internal] load .dockerignore
|
||||
#4 transferring context: 358B done
|
||||
#4 DONE 0.0s
|
||||
|
||||
#5 [migrate 1/7] FROM docker.io/library/python:3.12.12-slim-bookworm@sha256:593bd06efe90efa80dc4eee3948be7c0fde4134606dd40d8dd8dbcade98e669c
|
||||
#5 resolve docker.io/library/python:3.12.12-slim-bookworm@sha256:593bd06efe90efa80dc4eee3948be7c0fde4134606dd40d8dd8dbcade98e669c done
|
||||
#5 DONE 0.0s
|
||||
|
||||
#6 [api internal] load build context
|
||||
#6 transferring context: 16.17kB done
|
||||
#6 DONE 0.0s
|
||||
|
||||
#7 [api 3/7] COPY poetry.lock pyproject.toml ./
|
||||
#7 CACHED
|
||||
|
||||
#8 [api 4/7] RUN pip install --no-cache-dir --timeout 60 --retries 10 "poetry==2.2.1" && poetry install --only main --no-root --no-interaction
|
||||
#8 CACHED
|
||||
|
||||
#9 [api 2/7] WORKDIR /app
|
||||
#9 CACHED
|
||||
|
||||
#10 [api 5/7] COPY migrations ./migrations
|
||||
#10 CACHED
|
||||
|
||||
#11 [migrate 6/7] COPY src ./src
|
||||
#11 DONE 0.0s
|
||||
|
||||
#12 [migrate 7/7] RUN useradd --system --uid 10001 --no-create-home app
|
||||
#12 0.110 useradd warning: app's uid 10001 is greater than SYS_UID_MAX 999
|
||||
#12 DONE 0.1s
|
||||
|
||||
#13 [api] exporting to image
|
||||
#13 exporting layers 0.0s done
|
||||
#13 exporting manifest sha256:8d0545b5703e6c70c35f1136bdfd37c4ceb4a70d08b5bad21b70d6755df05b5d done
|
||||
#13 exporting config sha256:28c6ec56ed30844b944958f2749a370cea79cfd768ca3f491807eae51ad534a0 done
|
||||
#13 naming to docker.io/library/mtusi-messenger:lab3 done
|
||||
#13 unpacking to docker.io/library/mtusi-messenger:lab3 0.0s done
|
||||
#13 DONE 0.1s
|
||||
|
||||
#14 [migrate] exporting to image
|
||||
#14 exporting layers 0.0s done
|
||||
#14 exporting manifest sha256:9802819ef7b235f44ad03fed838cf83fcd4573a342405936538c7c969adfd573 done
|
||||
#14 exporting config sha256:e2b0c133c10bc42d53160db701c32694ed2cbac54f0522999289f4cf7cef21e1 done
|
||||
#14 naming to docker.io/library/mtusi-messenger:lab3 done
|
||||
#14 unpacking to docker.io/library/mtusi-messenger:lab3 0.0s done
|
||||
#14 DONE 0.1s
|
||||
|
||||
#15 [api] resolving provenance for metadata file
|
||||
#15 DONE 0.0s
|
||||
|
||||
#16 [migrate] resolving provenance for metadata file
|
||||
#16 DONE 0.0s
|
||||
mtusi-messenger:lab3 Built
|
||||
mtusi-messenger:lab3 Built
|
||||
Network messenger-lab3_edge Creating
|
||||
Network messenger-lab3_edge Created
|
||||
Network messenger-lab3_backend Creating
|
||||
Network messenger-lab3_backend Created
|
||||
Container messenger-lab3-postgres-1 Creating
|
||||
Container messenger-lab3-valkey-1 Creating
|
||||
Container messenger-lab3-valkey-1 Created
|
||||
Container messenger-lab3-postgres-1 Created
|
||||
Container messenger-lab3-migrate-1 Creating
|
||||
Container messenger-lab3-migrate-1 Created
|
||||
Container messenger-lab3-api-2 Creating
|
||||
Container messenger-lab3-api-1 Creating
|
||||
Container messenger-lab3-api-1 Created
|
||||
Container messenger-lab3-api-2 Created
|
||||
Container messenger-lab3-nginx-1 Creating
|
||||
Container messenger-lab3-nginx-1 Created
|
||||
Container messenger-lab3-postgres-1 Starting
|
||||
Container messenger-lab3-valkey-1 Starting
|
||||
Container messenger-lab3-valkey-1 Started
|
||||
Container messenger-lab3-postgres-1 Started
|
||||
Container messenger-lab3-postgres-1 Waiting
|
||||
Container messenger-lab3-postgres-1 Healthy
|
||||
Container messenger-lab3-migrate-1 Starting
|
||||
Container messenger-lab3-migrate-1 Started
|
||||
Container messenger-lab3-migrate-1 Waiting
|
||||
Container messenger-lab3-postgres-1 Waiting
|
||||
Container messenger-lab3-postgres-1 Healthy
|
||||
Container messenger-lab3-migrate-1 Exited
|
||||
Container messenger-lab3-api-1 Starting
|
||||
Container messenger-lab3-api-1 Started
|
||||
Container messenger-lab3-api-2 Starting
|
||||
Container messenger-lab3-api-2 Started
|
||||
Container messenger-lab3-api-1 Waiting
|
||||
Container messenger-lab3-api-2 Waiting
|
||||
Container messenger-lab3-api-1 Healthy
|
||||
Container messenger-lab3-api-2 Healthy
|
||||
Container messenger-lab3-nginx-1 Starting
|
||||
Container messenger-lab3-nginx-1 Started
|
||||
Container messenger-lab3-valkey-1 Waiting
|
||||
Container messenger-lab3-nginx-1 Waiting
|
||||
Container messenger-lab3-api-1 Waiting
|
||||
Container messenger-lab3-api-2 Waiting
|
||||
Container messenger-lab3-migrate-1 Waiting
|
||||
Container messenger-lab3-postgres-1 Waiting
|
||||
Container messenger-lab3-migrate-1 Exited
|
||||
Container messenger-lab3-postgres-1 Healthy
|
||||
Container messenger-lab3-valkey-1 Healthy
|
||||
Container messenger-lab3-api-1 Healthy
|
||||
Container messenger-lab3-api-2 Healthy
|
||||
Container messenger-lab3-nginx-1 Healthy
|
||||
$ docker compose ps -a
|
||||
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
|
||||
messenger-lab3-api-1 mtusi-messenger:lab3 "python src/gateway.…" api 15 seconds ago Up 7 seconds (healthy) 8080/tcp
|
||||
messenger-lab3-api-2 mtusi-messenger:lab3 "python src/gateway.…" api 15 seconds ago Up 7 seconds (healthy) 8080/tcp
|
||||
messenger-lab3-migrate-1 mtusi-messenger:lab3 "python src/migrate.…" migrate 15 seconds ago Exited (0) 7 seconds ago
|
||||
messenger-lab3-nginx-1 nginx:1.28.0-alpine3.21 "/docker-entrypoint.…" nginx 15 seconds ago Up 5 seconds (healthy) 127.0.0.1:8080-8081->8080-8081/tcp
|
||||
messenger-lab3-postgres-1 postgres:18.2-alpine3.22 "docker-entrypoint.s…" postgres 15 seconds ago Up 14 seconds (healthy) 5432/tcp
|
||||
messenger-lab3-valkey-1 valkey/valkey:8.1.4-alpine3.22 "docker-entrypoint.s…" valkey 15 seconds ago Up 14 seconds (healthy) 6379/tcp
|
||||
$ make instances
|
||||
./scripts/instances.sh
|
||||
CONTAINER STATE INSTANCE PID REVISION
|
||||
messenger-lab3-api-1 running/healthy 978f2777b379 95632 dev
|
||||
messenger-lab3-api-2 running/healthy 389b271f592b 95685 dev
|
||||
$ docker compose logs migrate
|
||||
migrate-1 | 2026-10-19 17:00:03.868 | INFO | infrastructure.dependencies.database:create_pool:69 - Установка соединения с базой данных psql
|
||||
migrate-1 | 2026-10-19 17:00:03.937 | INFO | infrastructure.dependencies.database:create_pool:99 - Соединение с базой данных psql установлено. DB_NAME: test, pool: 2..10
|
||||
migrate-1 | 2026-10-19 17:00:03.939 | INFO | infrastructure.dependencies.database:apply_migrations:157 - Схема БД в актуальной версии
|
||||
migrate-1 | 2026-10-19 17:00:03.940 | INFO | __main__:main:20 - Миграции применены, migrate завершается
|
||||
$ docker compose logs api | grep -E "Схема БД|Application started"
|
||||
api-2 | 2026-10-19 17:00:05.191 | INFO | infrastructure.dependencies.database:check_migrations:183 - Схема БД совместима: версия 2
|
||||
api-2 | 2026-10-19 17:00:05.226 | INFO | __main__:lifespan:45 - Application started, env=lab, revision=dev, instance=389b271f592b, session ttl=3600 s
|
||||
api-1 | 2026-10-19 17:00:05.106 | INFO | infrastructure.dependencies.database:check_migrations:183 - Схема БД совместима: версия 2
|
||||
api-1 | 2026-10-19 17:00:05.173 | INFO | __main__:lifespan:45 - Application started, env=lab, revision=dev, instance=978f2777b379, session ttl=3600 s
|
||||
$ curl -s -o /dev/null -w "%{http_code}\n" --max-time 2 http://localhost:8080/health/ready
|
||||
200
|
||||
$ docker compose exec -T nginx sh -c "wget -q -O - --timeout 2 http://api:8080/health/live; echo; nslookup api 127.0.0.11 | tail -4"
|
||||
{"status":"ok"}
|
||||
Address: 192.168.48.3
|
||||
Name: api
|
||||
Address: 192.168.48.2
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
$ make check
|
||||
python3 scripts/check_contract.py
|
||||
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
|
||||
$ make test
|
||||
python3 tests/smoke.py --base-url "http://localhost:8080"
|
||||
PASS: lab3 public API scenario; infrastructure and grading evidence remain separate.
|
||||
@@ -0,0 +1,128 @@
|
||||
$ make student-test
|
||||
BASE_URL="http://localhost:8080" poetry run pytest
|
||||
/Users/enterxx/Library/Python/3.9/lib/python/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020
|
||||
warnings.warn(
|
||||
============================= test session starts ==============================
|
||||
platform darwin -- Python 3.12.12, pytest-9.1.1, pluggy-1.6.0 -- /Users/enterxx/Library/Caches/pypoetry/virtualenvs/max2-jSniokBX-py3.12/bin/python
|
||||
cachedir: .pytest_cache
|
||||
rootdir: /Users/enterxx/Desktop/university/4course/mtusi2026lab3
|
||||
configfile: pyproject.toml
|
||||
testpaths: student-tests
|
||||
plugins: anyio-4.15.1
|
||||
collecting ... collected 113 items
|
||||
|
||||
student-tests/test_auth.py::test_login_returns_opaque_bearer_session PASSED [ 0%]
|
||||
student-tests/test_auth.py::test_wrong_password_and_unknown_user_are_indistinguishable PASSED [ 1%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_unauthorized[None] PASSED [ 2%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_unauthorized[] PASSED [ 3%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_unauthorized[Basic] PASSED [ 4%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_unauthorized[Basic !!!not-base64!!!] PASSED [ 5%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_unauthorized[Basic bm8tY29sb24taGVyZQ==] PASSED [ 6%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_unauthorized[Basic //46cGFzc3dvcmQ=] PASSED [ 7%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_unauthorized[Bearer some-token] PASSED [ 7%]
|
||||
student-tests/test_auth.py::test_utf8_password_with_colon PASSED [ 8%]
|
||||
student-tests/test_auth.py::test_basic_is_rejected_on_business_endpoints PASSED [ 9%]
|
||||
student-tests/test_auth.py::test_logout_revokes_only_current_session PASSED [ 10%]
|
||||
student-tests/test_auth.py::test_logout_without_token PASSED [ 11%]
|
||||
student-tests/test_auth.py::test_relogin_after_logout PASSED [ 12%]
|
||||
student-tests/test_auth.py::test_password_is_not_stored_in_plaintext PASSED [ 13%]
|
||||
student-tests/test_balancing.py::test_one_session_is_served_by_several_replicas PASSED [ 14%]
|
||||
student-tests/test_balancing.py::test_write_is_visible_through_every_replica PASSED [ 15%]
|
||||
student-tests/test_balancing.py::test_logout_applies_to_all_replicas PASSED [ 15%]
|
||||
student-tests/test_balancing.py::test_every_api_response_has_instance_id PASSED [ 16%]
|
||||
student-tests/test_concurrency.py::test_parallel_messages_are_not_lost_or_duplicated PASSED [ 17%]
|
||||
student-tests/test_dependency_failures.py::test_valkey_down_cache_is_bypassed SKIPPED [ 18%]
|
||||
student-tests/test_dependency_failures.py::test_logout_during_valkey_outage_is_not_resurrected SKIPPED [ 19%]
|
||||
student-tests/test_dependency_failures.py::test_postgres_down_returns_bounded_503_and_no_anonymous_access SKIPPED [ 20%]
|
||||
student-tests/test_dependency_failures.py::test_session_survives_postgres_restart SKIPPED [ 21%]
|
||||
student-tests/test_expiry.py::test_session_expires_after_short_ttl SKIPPED [ 22%]
|
||||
student-tests/test_expiry.py::test_expires_at_matches_short_ttl SKIPPED [ 23%]
|
||||
student-tests/test_foreign_chat.py::test_outsider_cannot_access_chat PASSED [ 23%]
|
||||
student-tests/test_foreign_chat.py::test_foreign_and_missing_chat_are_indistinguishable PASSED [ 24%]
|
||||
student-tests/test_foreign_chat.py::test_chat_list_contains_only_own_chats PASSED [ 25%]
|
||||
student-tests/test_health.py::test_health_endpoints_have_request_id PASSED [ 26%]
|
||||
student-tests/test_health.py::test_unknown_route_uses_error_format PASSED [ 27%]
|
||||
student-tests/test_idempotency.py::test_replay_returns_same_message PASSED [ 28%]
|
||||
student-tests/test_idempotency.py::test_replay_ignores_json_formatting PASSED [ 29%]
|
||||
student-tests/test_idempotency.py::test_same_key_with_other_body_is_conflict PASSED [ 30%]
|
||||
student-tests/test_idempotency.py::test_missing_or_invalid_key_is_rejected[headers0] PASSED [ 30%]
|
||||
student-tests/test_idempotency.py::test_missing_or_invalid_key_is_rejected[headers1] PASSED [ 31%]
|
||||
student-tests/test_idempotency.py::test_missing_or_invalid_key_is_rejected[headers2] PASSED [ 32%]
|
||||
student-tests/test_idempotency.py::test_missing_or_invalid_key_is_rejected[headers3] PASSED [ 33%]
|
||||
student-tests/test_idempotency.py::test_missing_or_invalid_key_is_rejected[headers4] PASSED [ 34%]
|
||||
student-tests/test_idempotency.py::test_missing_or_invalid_key_is_rejected[headers5] PASSED [ 35%]
|
||||
student-tests/test_idempotency.py::test_key_boundaries_are_accepted[xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] PASSED [ 36%]
|
||||
student-tests/test_idempotency.py::test_key_boundaries_are_accepted[Az09._:-] PASSED [ 37%]
|
||||
student-tests/test_idempotency.py::test_key_scope_is_user_and_chat PASSED [ 38%]
|
||||
student-tests/test_idempotency.py::test_replay_rechecks_session_and_access PASSED [ 38%]
|
||||
student-tests/test_idempotency.py::test_expired_key_can_be_reused PASSED [ 39%]
|
||||
student-tests/test_idempotency.py::test_parallel_replays_through_replicas_create_one_message PASSED [ 40%]
|
||||
student-tests/test_idempotency.py::test_parallel_same_key_with_different_bodies PASSED [ 41%]
|
||||
student-tests/test_invalid_json.py::test_malformed_json_on_register[{"username": "abc",] PASSED [ 42%]
|
||||
student-tests/test_invalid_json.py::test_malformed_json_on_register[not json at all] PASSED [ 43%]
|
||||
student-tests/test_invalid_json.py::test_malformed_json_on_register[{"text": "\xff\xfe"}] PASSED [ 44%]
|
||||
student-tests/test_invalid_json.py::test_malformed_json_on_register[] PASSED [ 45%]
|
||||
student-tests/test_invalid_json.py::test_malformed_json_on_create_chat_and_message[{"username": "abc",] PASSED [ 46%]
|
||||
student-tests/test_invalid_json.py::test_malformed_json_on_create_chat_and_message[not json at all] PASSED [ 46%]
|
||||
student-tests/test_invalid_json.py::test_malformed_json_on_create_chat_and_message[{"text": "\xff\xfe"}] PASSED [ 47%]
|
||||
student-tests/test_invalid_json.py::test_malformed_json_on_create_chat_and_message[] PASSED [ 48%]
|
||||
student-tests/test_invalid_json.py::test_invalid_user_payload[body0] PASSED [ 49%]
|
||||
student-tests/test_invalid_json.py::test_invalid_user_payload[alice] PASSED [ 50%]
|
||||
student-tests/test_invalid_json.py::test_invalid_user_payload[body2] PASSED [ 51%]
|
||||
student-tests/test_invalid_json.py::test_invalid_user_payload[body3] PASSED [ 52%]
|
||||
student-tests/test_invalid_json.py::test_invalid_user_payload[body4] PASSED [ 53%]
|
||||
student-tests/test_invalid_json.py::test_invalid_user_payload[body5] PASSED [ 53%]
|
||||
student-tests/test_invalid_json.py::test_invalid_user_payload[body6] PASSED [ 54%]
|
||||
student-tests/test_invalid_json.py::test_invalid_user_payload[body7] PASSED [ 55%]
|
||||
student-tests/test_invalid_json.py::test_invalid_user_payload[body8] PASSED [ 56%]
|
||||
student-tests/test_invalid_json.py::test_invalid_user_payload[body9] PASSED [ 57%]
|
||||
student-tests/test_invalid_json.py::test_invalid_user_payload[body10] PASSED [ 58%]
|
||||
student-tests/test_invalid_json.py::test_invalid_message_payload[body0] PASSED [ 59%]
|
||||
student-tests/test_invalid_json.py::test_invalid_message_payload[body1] PASSED [ 60%]
|
||||
student-tests/test_invalid_json.py::test_invalid_message_payload[body2] PASSED [ 61%]
|
||||
student-tests/test_invalid_json.py::test_invalid_chat_payload PASSED [ 61%]
|
||||
student-tests/test_messages.py::test_empty_or_blank_message_is_rejected[] PASSED [ 62%]
|
||||
student-tests/test_messages.py::test_empty_or_blank_message_is_rejected[ ] PASSED [ 63%]
|
||||
student-tests/test_messages.py::test_empty_or_blank_message_is_rejected[ \n\t ] PASSED [ 64%]
|
||||
student-tests/test_messages.py::test_empty_or_blank_message_is_rejected[\u3000] PASSED [ 65%]
|
||||
student-tests/test_messages.py::test_message_length_is_counted_in_code_points PASSED [ 66%]
|
||||
student-tests/test_messages.py::test_unicode_is_preserved PASSED [ 67%]
|
||||
student-tests/test_messages.py::test_nul_character_is_rejected PASSED [ 68%]
|
||||
student-tests/test_messages.py::test_sender_is_taken_from_session PASSED [ 69%]
|
||||
student-tests/test_pagination.py::test_walk_message_pages[7-3-3] PASSED [ 69%]
|
||||
student-tests/test_pagination.py::test_walk_message_pages[6-3-2] PASSED [ 70%]
|
||||
student-tests/test_pagination.py::test_walk_message_pages[5-1-5] PASSED [ 71%]
|
||||
student-tests/test_pagination.py::test_walk_message_pages[3-100-1] PASSED [ 72%]
|
||||
student-tests/test_pagination.py::test_limit_can_change_between_pages PASSED [ 73%]
|
||||
student-tests/test_pagination.py::test_new_message_appears_after_cursor PASSED [ 74%]
|
||||
student-tests/test_pagination.py::test_cursor_is_bound_to_chat PASSED [ 75%]
|
||||
student-tests/test_pagination.py::test_invalid_limit_or_cursor[params0] PASSED [ 76%]
|
||||
student-tests/test_pagination.py::test_invalid_limit_or_cursor[params1] PASSED [ 76%]
|
||||
student-tests/test_pagination.py::test_invalid_limit_or_cursor[params2] PASSED [ 77%]
|
||||
student-tests/test_pagination.py::test_invalid_limit_or_cursor[params3] PASSED [ 78%]
|
||||
student-tests/test_pagination.py::test_invalid_limit_or_cursor[params4] PASSED [ 79%]
|
||||
student-tests/test_pagination.py::test_invalid_limit_or_cursor[params5] PASSED [ 80%]
|
||||
student-tests/test_pagination.py::test_walk_chat_list_pages PASSED [ 81%]
|
||||
student-tests/test_pagination.py::test_chat_cursor_is_bound_to_user PASSED [ 82%]
|
||||
student-tests/test_transactions.py::test_same_username_race_creates_exactly_one_user PASSED [ 83%]
|
||||
student-tests/test_transactions.py::test_failed_chat_creation_is_rolled_back PASSED [ 84%]
|
||||
student-tests/test_transactions.py::test_successful_chat_creation_commits_all_members PASSED [ 84%]
|
||||
student-tests/test_users.py::test_duplicate_username_is_conflict PASSED [ 85%]
|
||||
student-tests/test_users.py::test_registration_never_returns_password PASSED [ 86%]
|
||||
student-tests/test_users.py::test_invalid_password_is_rejected[None] PASSED [ 87%]
|
||||
student-tests/test_users.py::test_invalid_password_is_rejected[] PASSED [ 88%]
|
||||
student-tests/test_users.py::test_invalid_password_is_rejected[\u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0439] PASSED [ 89%]
|
||||
student-tests/test_users.py::test_invalid_password_is_rejected[xxxxxxxxxxx] PASSED [ 90%]
|
||||
student-tests/test_users.py::test_invalid_password_is_rejected[xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] PASSED [ 91%]
|
||||
student-tests/test_users.py::test_invalid_password_is_rejected[123456789012] PASSED [ 92%]
|
||||
student-tests/test_users.py::test_password_length_is_counted_in_code_points PASSED [ 92%]
|
||||
student-tests/test_users.py::test_missing_or_unknown_token_is_unauthorized[headers0] PASSED [ 93%]
|
||||
student-tests/test_users.py::test_missing_or_unknown_token_is_unauthorized[headers1] PASSED [ 94%]
|
||||
student-tests/test_users.py::test_missing_or_unknown_token_is_unauthorized[headers2] PASSED [ 95%]
|
||||
student-tests/test_users.py::test_missing_or_unknown_token_is_unauthorized[headers3] PASSED [ 96%]
|
||||
student-tests/test_users.py::test_missing_or_unknown_token_is_unauthorized[headers4] PASSED [ 97%]
|
||||
student-tests/test_users.py::test_missing_or_unknown_token_is_unauthorized[headers5] PASSED [ 98%]
|
||||
student-tests/test_users.py::test_x_user_id_gives_no_access PASSED [ 99%]
|
||||
student-tests/test_users.py::test_current_user PASSED [100%]
|
||||
|
||||
======================= 107 passed, 6 skipped in 14.98s ========================
|
||||
@@ -0,0 +1,50 @@
|
||||
$ BASE_URL=http://localhost:8080 poetry run pytest student-tests/test_balancing.py student-tests/test_idempotency.py student-tests/test_concurrency.py -s
|
||||
/Users/enterxx/Library/Python/3.9/lib/python/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020
|
||||
warnings.warn(
|
||||
============================= test session starts ==============================
|
||||
platform darwin -- Python 3.12.12, pytest-9.1.1, pluggy-1.6.0 -- /Users/enterxx/Library/Caches/pypoetry/virtualenvs/max2-jSniokBX-py3.12/bin/python
|
||||
cachedir: .pytest_cache
|
||||
rootdir: /Users/enterxx/Desktop/university/4course/mtusi2026lab3
|
||||
configfile: pyproject.toml
|
||||
plugins: anyio-4.15.1
|
||||
collecting ... collected 21 items
|
||||
|
||||
student-tests/test_balancing.py::test_one_session_is_served_by_several_replicas
|
||||
INSTANCES FOR ONE SESSION: {'389b271f592b': 10, '978f2777b379': 10}
|
||||
PASSED
|
||||
student-tests/test_balancing.py::test_write_is_visible_through_every_replica PASSED
|
||||
student-tests/test_balancing.py::test_logout_applies_to_all_replicas PASSED
|
||||
student-tests/test_balancing.py::test_every_api_response_has_instance_id PASSED
|
||||
student-tests/test_idempotency.py::test_replay_returns_same_message PASSED
|
||||
student-tests/test_idempotency.py::test_replay_ignores_json_formatting PASSED
|
||||
student-tests/test_idempotency.py::test_same_key_with_other_body_is_conflict PASSED
|
||||
student-tests/test_idempotency.py::test_missing_or_invalid_key_is_rejected[headers0] PASSED
|
||||
student-tests/test_idempotency.py::test_missing_or_invalid_key_is_rejected[headers1] PASSED
|
||||
student-tests/test_idempotency.py::test_missing_or_invalid_key_is_rejected[headers2] PASSED
|
||||
student-tests/test_idempotency.py::test_missing_or_invalid_key_is_rejected[headers3] PASSED
|
||||
student-tests/test_idempotency.py::test_missing_or_invalid_key_is_rejected[headers4] PASSED
|
||||
student-tests/test_idempotency.py::test_missing_or_invalid_key_is_rejected[headers5] PASSED
|
||||
student-tests/test_idempotency.py::test_key_boundaries_are_accepted[xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] PASSED
|
||||
student-tests/test_idempotency.py::test_key_boundaries_are_accepted[Az09._:-] PASSED
|
||||
student-tests/test_idempotency.py::test_key_scope_is_user_and_chat PASSED
|
||||
student-tests/test_idempotency.py::test_replay_rechecks_session_and_access PASSED
|
||||
student-tests/test_idempotency.py::test_expired_key_can_be_reused PASSED
|
||||
student-tests/test_idempotency.py::test_parallel_replays_through_replicas_create_one_message
|
||||
PARALLEL SAME KEY: {"requests": 20, "statuses": {"201": 20}, "instances": {"389b271f592b": 10, "978f2777b379": 10}, "distinct_bodies": 1, "messages_in_history": 1, "rows_in_db": 1}
|
||||
PASSED
|
||||
student-tests/test_idempotency.py::test_parallel_same_key_with_different_bodies PASSED
|
||||
student-tests/test_concurrency.py::test_parallel_messages_are_not_lost_or_duplicated
|
||||
CONCURRENCY RESULT: {"clients": 10, "messages_sent": 100, "status_201": 100, "messages_read": 100, "unique_ids": 100, "unique_texts": 100, "pages_read": 4, "send_seconds": 0.227}
|
||||
PASSED
|
||||
|
||||
============================== 21 passed in 3.79s ==============================
|
||||
$ docker compose logs --no-log-prefix nginx | grep "\"uri\":\"/api/v1/users/me\",\"status\":200" | tail -6
|
||||
{"time":"2026-10-19T17:01:21+00:00","request_id":"d754e7abc1b3b0e09ab4bf0d9fd50270","method":"GET","uri":"/api/v1/users/me","status":200,"request_time":0.001,"upstream_addr":"192.168.48.3:8080","upstream_status":"200","upstream_response_time":"0.002","instance":"389b271f592b"}
|
||||
{"time":"2026-10-19T17:01:21+00:00","request_id":"aeaf60de5c114ed306169370ede170b6","method":"GET","uri":"/api/v1/users/me","status":200,"request_time":0.002,"upstream_addr":"192.168.48.2:8080","upstream_status":"200","upstream_response_time":"0.001","instance":"978f2777b379"}
|
||||
{"time":"2026-10-19T17:01:21+00:00","request_id":"7caafb063b53d8985d17356cd62d812d","method":"GET","uri":"/api/v1/users/me","status":200,"request_time":0.002,"upstream_addr":"192.168.48.3:8080","upstream_status":"200","upstream_response_time":"0.001","instance":"389b271f592b"}
|
||||
{"time":"2026-10-19T17:01:21+00:00","request_id":"a8e4fb898cd5ff5e56bdd1669067a60f","method":"GET","uri":"/api/v1/users/me","status":200,"request_time":0.001,"upstream_addr":"192.168.48.2:8080","upstream_status":"200","upstream_response_time":"0.001","instance":"978f2777b379"}
|
||||
{"time":"2026-10-19T17:01:21+00:00","request_id":"fb8e6ba34f06d739933b9872b9aadd89","method":"GET","uri":"/api/v1/users/me","status":200,"request_time":0.001,"upstream_addr":"192.168.48.3:8080","upstream_status":"200","upstream_response_time":"0.000","instance":"389b271f592b"}
|
||||
{"time":"2026-10-19T17:01:21+00:00","request_id":"e166ba8766667300b8bcc65996ab6827","method":"GET","uri":"/api/v1/users/me","status":200,"request_time":0.001,"upstream_addr":"192.168.48.2:8080","upstream_status":"200","upstream_response_time":"0.001","instance":"978f2777b379"}
|
||||
$ rid=$(docker compose logs --no-log-prefix nginx | grep "\"uri\":\"/api/v1/users/me\",\"status\":200" | tail -1 | sed -E "s/.*\"request_id\":\"([0-9a-f]+)\".*/\1/"); echo "request_id=$rid"; docker compose logs api | grep "$rid"
|
||||
request_id=e166ba8766667300b8bcc65996ab6827
|
||||
api-1 | 2026-10-19 17:01:21.285 | INFO | application.api.middlewares.request_context:_log_request:70 - GET /api/v1/users/me -> 200 | 0.6 ms | request_id=e166ba8766667300b8bcc65996ab6827
|
||||
@@ -0,0 +1,41 @@
|
||||
$ make instances
|
||||
./scripts/instances.sh
|
||||
CONTAINER STATE INSTANCE PID REVISION
|
||||
messenger-lab3-api-1 running/healthy 978f2777b379 95632 dev
|
||||
messenger-lab3-api-2 running/healthy 389b271f592b 95685 dev
|
||||
$ make test-failover ACTION_SCRIPT=scripts/stop-one.sh
|
||||
python3 tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://localhost:8080"
|
||||
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
|
||||
{"observed_instances_before": 2, "successful_writes": 129, "failed_attempts": 0, "first_recovery_seconds": 2.693}
|
||||
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||
$ make instances
|
||||
./scripts/instances.sh
|
||||
CONTAINER STATE INSTANCE PID REVISION
|
||||
messenger-lab3-api-1 exited/unhealthy - 0 dev
|
||||
messenger-lab3-api-2 running/healthy 389b271f592b 95685 dev
|
||||
$ docker inspect -f "{{.Name}} status={{.State.Status}} exit={{.State.ExitCode}} finished={{.State.FinishedAt}}" $(docker compose ps -aq api)
|
||||
/messenger-lab3-api-1 status=exited exit=0 finished=2026-10-19T17:01:38.389851544Z
|
||||
/messenger-lab3-api-2 status=running exit=0 finished=0001-01-01T00:00:00Z
|
||||
$ docker compose logs api | grep -E "SIGTERM|Application stopped|Finished server process"
|
||||
api-1 | 2026-10-19 17:01:37.194 | INFO | __main__:shutdown:91 - SIGTERM: drain 1.0 с, instance=978f2777b379: порт закрыт, keep-alive соединения закрываются после ответа
|
||||
api-1 | 2026-10-19 17:01:38.301 | INFO | __main__:lifespan:54 - Application stopped, instance=978f2777b379, connections closed
|
||||
api-1 | INFO: Finished server process [1]
|
||||
$ docker compose logs --no-log-prefix nginx | grep -E "\[(warn|error)\]" | tail -4
|
||||
2026/10/19 17:00:46 [warn] 30#30: *43 a client request body is buffered to a temporary file /var/cache/nginx/client_temp/0000000001, client: 192.168.48.1, server: , request: "POST /api/v1/chats/01a1551b-c8fe-7b27-bef6-22c34e5e5367/messages HTTP/1.1", host: "localhost:8080"
|
||||
2026/10/19 17:00:46 [warn] 30#30: *43 a client request body is buffered to a temporary file /var/cache/nginx/client_temp/0000000002, client: 192.168.48.1, server: , request: "POST /api/v1/chats/01a1551b-c8fe-7b27-bef6-22c34e5e5367/messages HTTP/1.1", host: "localhost:8080"
|
||||
2026/10/19 17:01:38 [error] 30#30: *803 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.48.1, server: , request: "POST /api/v1/chats/01a1551c-8845-7e45-aa74-7d88537eac05/messages HTTP/1.1", upstream: "http://192.168.48.2:8080/api/v1/chats/01a1551c-8845-7e45-aa74-7d88537eac05/messages", host: "localhost:8080"
|
||||
2026/10/19 17:01:38 [warn] 30#30: *803 upstream server temporarily disabled while connecting to upstream, client: 192.168.48.1, server: , request: "POST /api/v1/chats/01a1551c-8845-7e45-aa74-7d88537eac05/messages HTTP/1.1", upstream: "http://192.168.48.2:8080/api/v1/chats/01a1551c-8845-7e45-aa74-7d88537eac05/messages", host: "localhost:8080"
|
||||
$ make restore
|
||||
./scripts/restore.sh
|
||||
Container messenger-lab3-api-2 Running
|
||||
Container messenger-lab3-api-1 Starting
|
||||
Container messenger-lab3-api-1 Started
|
||||
Container messenger-lab3-api-1 Waiting
|
||||
Container messenger-lab3-api-2 Waiting
|
||||
Container messenger-lab3-api-1 Healthy
|
||||
Container messenger-lab3-api-2 Healthy
|
||||
$ make instances
|
||||
./scripts/instances.sh
|
||||
CONTAINER STATE INSTANCE PID REVISION
|
||||
messenger-lab3-api-1 running/healthy b0b074af6d9c 18674 dev
|
||||
messenger-lab3-api-2 running/healthy 389b271f592b 95685 dev
|
||||
@@ -0,0 +1,69 @@
|
||||
$ make instances
|
||||
./scripts/instances.sh
|
||||
CONTAINER STATE INSTANCE PID REVISION
|
||||
messenger-lab3-api-1 running/healthy b0b074af6d9c 18674 dev
|
||||
messenger-lab3-api-2 running/healthy 389b271f592b 95685 dev
|
||||
$ make test-failover ACTION_SCRIPT=scripts/stop-one.sh
|
||||
python3 tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://localhost:8080"
|
||||
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
|
||||
{"observed_instances_before": 2, "successful_writes": 128, "failed_attempts": 0, "first_recovery_seconds": 1.616}
|
||||
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||
$ make instances
|
||||
./scripts/instances.sh
|
||||
CONTAINER STATE INSTANCE PID REVISION
|
||||
messenger-lab3-api-1 exited/unhealthy - 0 dev
|
||||
messenger-lab3-api-2 running/healthy 389b271f592b 95685 dev
|
||||
$ docker inspect -f "{{.Name}} status={{.State.Status}} exit={{.State.ExitCode}} finished={{.State.FinishedAt}}" $(docker compose ps -aq api)
|
||||
/messenger-lab3-api-1 status=exited exit=0 finished=2026-10-19T17:02:10.511955087Z
|
||||
/messenger-lab3-api-2 status=running exit=0 finished=0001-01-01T00:00:00Z
|
||||
$ make restore
|
||||
./scripts/restore.sh
|
||||
Container messenger-lab3-api-2 Running
|
||||
Container messenger-lab3-api-1 Starting
|
||||
Container messenger-lab3-api-1 Started
|
||||
Container messenger-lab3-api-1 Waiting
|
||||
Container messenger-lab3-api-2 Waiting
|
||||
Container messenger-lab3-api-1 Healthy
|
||||
Container messenger-lab3-api-2 Healthy
|
||||
$ make instances
|
||||
./scripts/instances.sh
|
||||
CONTAINER STATE INSTANCE PID REVISION
|
||||
messenger-lab3-api-1 running/healthy e06c3c21d163 21196 dev
|
||||
messenger-lab3-api-2 running/healthy 389b271f592b 95685 dev
|
||||
$ curl -s -o /dev/null -w "shift round-robin: %{http_code}\n" http://localhost:8080/health/live
|
||||
shift round-robin: 200
|
||||
$ make test-failover ACTION_SCRIPT=scripts/stop-one.sh
|
||||
python3 tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://localhost:8080"
|
||||
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
|
||||
{"observed_instances_before": 2, "successful_writes": 129, "failed_attempts": 0, "first_recovery_seconds": 1.618}
|
||||
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||
$ make instances
|
||||
./scripts/instances.sh
|
||||
CONTAINER STATE INSTANCE PID REVISION
|
||||
messenger-lab3-api-1 running/healthy e06c3c21d163 21196 dev
|
||||
messenger-lab3-api-2 exited/unhealthy - 0 dev
|
||||
$ docker inspect -f "{{.Name}} status={{.State.Status}} exit={{.State.ExitCode}} finished={{.State.FinishedAt}}" $(docker compose ps -aq api)
|
||||
/messenger-lab3-api-1 status=running exit=0 finished=2026-10-19T17:02:10.511955087Z
|
||||
/messenger-lab3-api-2 status=exited exit=0 finished=2026-10-19T17:02:43.134886046Z
|
||||
$ docker compose logs api-2 2>/dev/null | grep -E "SIGTERM|Application stopped|Finished server process" || docker compose logs api | grep -E "SIGTERM|Application stopped" | tail -2
|
||||
api-1 | 2026-10-19 17:02:09.336 | INFO | __main__:shutdown:91 - SIGTERM: drain 1.0 с, instance=b0b074af6d9c: порт закрыт, keep-alive соединения закрываются после ответа
|
||||
api-1 | 2026-10-19 17:02:10.443 | INFO | __main__:lifespan:54 - Application stopped, instance=b0b074af6d9c, connections closed
|
||||
$ make restore
|
||||
./scripts/restore.sh
|
||||
Container messenger-lab3-api-1 Running
|
||||
Container messenger-lab3-api-2 Starting
|
||||
Container messenger-lab3-api-2 Started
|
||||
Container messenger-lab3-api-1 Waiting
|
||||
Container messenger-lab3-api-2 Waiting
|
||||
Container messenger-lab3-api-1 Healthy
|
||||
Container messenger-lab3-api-2 Healthy
|
||||
$ make instances
|
||||
./scripts/instances.sh
|
||||
CONTAINER STATE INSTANCE PID REVISION
|
||||
messenger-lab3-api-1 running/healthy e06c3c21d163 21196 dev
|
||||
messenger-lab3-api-2 running/healthy 774df521af21 22387 dev
|
||||
$ docker logs messenger-lab3-api-2 2>&1 | grep -E "instance=389b271f592b|Finished server process"
|
||||
2026-10-19 17:00:05.226 | INFO | __main__:lifespan:45 - Application started, env=lab, revision=dev, instance=389b271f592b, session ttl=3600 s
|
||||
2026-10-19 17:02:41.948 | INFO | __main__:shutdown:91 - SIGTERM: drain 1.0 с, instance=389b271f592b: порт закрыт, keep-alive соединения закрываются после ответа
|
||||
2026-10-19 17:02:43.068 | INFO | __main__:lifespan:54 - Application stopped, instance=389b271f592b, connections closed
|
||||
INFO: Finished server process [1]
|
||||
@@ -0,0 +1,37 @@
|
||||
$ cat scripts/restart.sh
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# Перезапуск всего стенда с сохранением volumes: хранилища (PostgreSQL, Valkey),
|
||||
# затем все API-реплики и балансировщик. Никаких down -v, flush или повторного
|
||||
# seed; migrate не запускается — схема уже в volume. Готовность ждёт тест.
|
||||
cd "$(dirname "$0")/.."
|
||||
ENV_FILE="${ENV_FILE:-.env}"
|
||||
|
||||
docker compose --env-file "$ENV_FILE" restart --no-deps postgres valkey
|
||||
docker compose --env-file "$ENV_FILE" restart --no-deps api
|
||||
docker compose --env-file "$ENV_FILE" restart --no-deps nginx
|
||||
$ docker inspect -f "{{.Name}} started={{.State.StartedAt}}" $(docker compose ps -q)
|
||||
/messenger-lab3-api-1 started=2026-10-19T17:02:39.217903586Z
|
||||
/messenger-lab3-api-2 started=2026-10-19T17:02:57.444596886Z
|
||||
/messenger-lab3-nginx-1 started=2026-10-19T17:00:06.004414293Z
|
||||
/messenger-lab3-postgres-1 started=2026-10-19T16:59:57.066060636Z
|
||||
/messenger-lab3-valkey-1 started=2026-10-19T16:59:57.064188719Z
|
||||
$ docker volume inspect -f "{{.Name}} created={{.CreatedAt}}" messenger-lab3_postgres_data
|
||||
messenger-lab3_postgres_data created=2026-10-19T16:10:27Z
|
||||
$ make test-persistence ACTION_SCRIPT=scripts/restart.sh
|
||||
python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost:8080"
|
||||
Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.
|
||||
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||
$ docker inspect -f "{{.Name}} started={{.State.StartedAt}}" $(docker compose ps -q)
|
||||
/messenger-lab3-api-1 started=2026-10-19T17:03:17.125014673Z
|
||||
/messenger-lab3-api-2 started=2026-10-19T17:03:17.151381881Z
|
||||
/messenger-lab3-nginx-1 started=2026-10-19T17:03:17.57774484Z
|
||||
/messenger-lab3-postgres-1 started=2026-10-19T17:03:15.504400631Z
|
||||
/messenger-lab3-valkey-1 started=2026-10-19T17:03:15.535674256Z
|
||||
$ docker volume inspect -f "{{.Name}} created={{.CreatedAt}}" messenger-lab3_postgres_data
|
||||
messenger-lab3_postgres_data created=2026-10-19T16:10:27Z
|
||||
$ make instances
|
||||
./scripts/instances.sh
|
||||
CONTAINER STATE INSTANCE PID REVISION
|
||||
messenger-lab3-api-1 running/healthy bfb3f7bbb85d 23878 dev
|
||||
messenger-lab3-api-2 running/healthy 6d2f6cd2b04a 23926 dev
|
||||
@@ -0,0 +1,108 @@
|
||||
$ make test-expiry
|
||||
docker compose --profile expiry up -d --build --wait api-short-ttl
|
||||
#1 [internal] load local bake definitions
|
||||
#1 reading from stdin 1.20kB done
|
||||
#1 DONE 0.0s
|
||||
|
||||
#2 [api-short-ttl internal] load build definition from Dockerfile
|
||||
#2 transferring dockerfile: 914B done
|
||||
#2 DONE 0.0s
|
||||
|
||||
#3 [api-short-ttl internal] load metadata for docker.io/library/python:3.12.12-slim-bookworm
|
||||
#3 DONE 0.1s
|
||||
|
||||
#4 [api-short-ttl internal] load .dockerignore
|
||||
#4 transferring context: 358B done
|
||||
#4 DONE 0.0s
|
||||
|
||||
#5 [api-short-ttl 1/7] FROM docker.io/library/python:3.12.12-slim-bookworm@sha256:593bd06efe90efa80dc4eee3948be7c0fde4134606dd40d8dd8dbcade98e669c
|
||||
#5 resolve docker.io/library/python:3.12.12-slim-bookworm@sha256:593bd06efe90efa80dc4eee3948be7c0fde4134606dd40d8dd8dbcade98e669c done
|
||||
#5 DONE 0.0s
|
||||
|
||||
#6 [api-short-ttl internal] load build context
|
||||
#6 transferring context: 7.39kB done
|
||||
#6 DONE 0.0s
|
||||
|
||||
#7 [migrate 5/7] COPY migrations ./migrations
|
||||
#7 CACHED
|
||||
|
||||
#8 [migrate 4/7] RUN pip install --no-cache-dir --timeout 60 --retries 10 "poetry==2.2.1" && poetry install --only main --no-root --no-interaction
|
||||
#8 CACHED
|
||||
|
||||
#9 [migrate 2/7] WORKDIR /app
|
||||
#9 CACHED
|
||||
|
||||
#10 [migrate 6/7] COPY src ./src
|
||||
#10 CACHED
|
||||
|
||||
#11 [migrate 3/7] COPY poetry.lock pyproject.toml ./
|
||||
#11 CACHED
|
||||
|
||||
#12 [api-short-ttl 7/7] RUN useradd --system --uid 10001 --no-create-home app
|
||||
#12 CACHED
|
||||
|
||||
#13 [migrate] exporting to image
|
||||
#13 exporting layers done
|
||||
#13 exporting manifest sha256:9802819ef7b235f44ad03fed838cf83fcd4573a342405936538c7c969adfd573 done
|
||||
#13 exporting config sha256:e2b0c133c10bc42d53160db701c32694ed2cbac54f0522999289f4cf7cef21e1 done
|
||||
#13 naming to docker.io/library/mtusi-messenger:lab3 done
|
||||
#13 unpacking to docker.io/library/mtusi-messenger:lab3 done
|
||||
#13 DONE 0.0s
|
||||
|
||||
#14 [api-short-ttl] exporting to image
|
||||
#14 exporting layers done
|
||||
#14 exporting manifest sha256:036b7dde37a28a3eeec751ce6ada2034e1a9a1d76cd9310dbd3cd157824ea087 done
|
||||
#14 exporting config sha256:26fb210928fa7201fedcce1e64d506079d863ddc42532dc4d25f1f77d673e3b3 done
|
||||
#14 naming to docker.io/library/mtusi-messenger:lab3 done
|
||||
#14 unpacking to docker.io/library/mtusi-messenger:lab3 done
|
||||
#14 DONE 0.0s
|
||||
|
||||
#15 [migrate] resolving provenance for metadata file
|
||||
#15 DONE 0.0s
|
||||
|
||||
#16 [api-short-ttl] resolving provenance for metadata file
|
||||
#16 DONE 0.0s
|
||||
mtusi-messenger:lab3 Built
|
||||
mtusi-messenger:lab3 Built
|
||||
Container messenger-lab3-valkey-1 Running
|
||||
Container messenger-lab3-postgres-1 Running
|
||||
Container messenger-lab3-migrate-1 Recreate
|
||||
Container messenger-lab3-migrate-1 Recreated
|
||||
Container messenger-lab3-api-short-ttl-1 Creating
|
||||
Container messenger-lab3-api-short-ttl-1 Created
|
||||
Container messenger-lab3-postgres-1 Waiting
|
||||
Container messenger-lab3-postgres-1 Healthy
|
||||
Container messenger-lab3-migrate-1 Starting
|
||||
Container messenger-lab3-migrate-1 Started
|
||||
Container messenger-lab3-migrate-1 Waiting
|
||||
Container messenger-lab3-postgres-1 Waiting
|
||||
Container messenger-lab3-postgres-1 Healthy
|
||||
Container messenger-lab3-migrate-1 Exited
|
||||
Container messenger-lab3-api-short-ttl-1 Starting
|
||||
Container messenger-lab3-api-short-ttl-1 Started
|
||||
Container messenger-lab3-api-short-ttl-1 Waiting
|
||||
Container messenger-lab3-migrate-1 Waiting
|
||||
Container messenger-lab3-postgres-1 Waiting
|
||||
Container messenger-lab3-valkey-1 Waiting
|
||||
Container messenger-lab3-migrate-1 Exited
|
||||
Container messenger-lab3-valkey-1 Healthy
|
||||
Container messenger-lab3-postgres-1 Healthy
|
||||
Container messenger-lab3-api-short-ttl-1 Healthy
|
||||
EXPIRY_BASE_URL=http://localhost:8081 poetry run pytest student-tests/test_expiry.py -s; \
|
||||
status=$?; docker compose --profile expiry stop api-short-ttl; exit $status
|
||||
/Users/enterxx/Library/Python/3.9/lib/python/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020
|
||||
warnings.warn(
|
||||
============================= test session starts ==============================
|
||||
platform darwin -- Python 3.12.12, pytest-9.1.1, pluggy-1.6.0 -- /Users/enterxx/Library/Caches/pypoetry/virtualenvs/max2-jSniokBX-py3.12/bin/python
|
||||
cachedir: .pytest_cache
|
||||
rootdir: /Users/enterxx/Desktop/university/4course/mtusi2026lab3
|
||||
configfile: pyproject.toml
|
||||
plugins: anyio-4.15.1
|
||||
collecting ... collected 2 items
|
||||
|
||||
student-tests/test_expiry.py::test_session_expires_after_short_ttl PASSED
|
||||
student-tests/test_expiry.py::test_expires_at_matches_short_ttl PASSED
|
||||
|
||||
============================== 2 passed in 3.54s ===============================
|
||||
Container messenger-lab3-api-short-ttl-1 Stopping
|
||||
Container messenger-lab3-api-short-ttl-1 Stopped
|
||||
@@ -0,0 +1,24 @@
|
||||
$ make test-failures
|
||||
BASE_URL="http://localhost:8080" RUN_FAILURE_TESTS=1 poetry run pytest student-tests/test_dependency_failures.py -s
|
||||
/Users/enterxx/Library/Python/3.9/lib/python/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020
|
||||
warnings.warn(
|
||||
============================= test session starts ==============================
|
||||
platform darwin -- Python 3.12.12, pytest-9.1.1, pluggy-1.6.0 -- /Users/enterxx/Library/Caches/pypoetry/virtualenvs/max2-jSniokBX-py3.12/bin/python
|
||||
cachedir: .pytest_cache
|
||||
rootdir: /Users/enterxx/Desktop/university/4course/mtusi2026lab3
|
||||
configfile: pyproject.toml
|
||||
plugins: anyio-4.15.1
|
||||
collecting ... collected 4 items
|
||||
|
||||
student-tests/test_dependency_failures.py::test_valkey_down_cache_is_bypassed PASSED
|
||||
student-tests/test_dependency_failures.py::test_logout_during_valkey_outage_is_not_resurrected PASSED
|
||||
student-tests/test_dependency_failures.py::test_postgres_down_returns_bounded_503_and_no_anonymous_access PASSED
|
||||
student-tests/test_dependency_failures.py::test_session_survives_postgres_restart PASSED
|
||||
|
||||
============================== 4 passed in 11.79s ==============================
|
||||
$ docker compose ps --format "{{.Name}}\t{{.Status}}"
|
||||
messenger-lab3-api-1 Up 12 hours (healthy)
|
||||
messenger-lab3-api-2 Up 12 hours (healthy)
|
||||
messenger-lab3-nginx-1 Up 12 hours (healthy)
|
||||
messenger-lab3-postgres-1 Up Less than a second (health: starting)
|
||||
messenger-lab3-valkey-1 Up 8 seconds (healthy)
|
||||
@@ -0,0 +1,22 @@
|
||||
$ make test-migrations
|
||||
./scripts/check_migrations.sh
|
||||
2026-10-20 04:55:31.041 | INFO | infrastructure.dependencies.database:create_pool:69 - Установка соединения с базой данных psql
|
||||
2026-10-20 04:55:31.118 | INFO | infrastructure.dependencies.database:create_pool:99 - Соединение с базой данных psql установлено. DB_NAME: test, pool: 2..10
|
||||
пустой volume: таблиц в public = 0
|
||||
API до migrate: В БД не применены миграции [1, 2]: сначала запустите migrate
|
||||
2026-10-20 04:55:31.132 | INFO | infrastructure.dependencies.database:apply_migrations:152 - Применена миграция 0001_init.sql
|
||||
2026-10-20 04:55:31.134 | INFO | infrastructure.dependencies.database:apply_migrations:152 - Применена миграция 0002_message_idempotency.sql
|
||||
2026-10-20 04:55:31.134 | INFO | infrastructure.dependencies.database:apply_migrations:157 - Схема БД в актуальной версии
|
||||
запуск 1: schema_migrations = [(1, '0001_init'), (2, '0002_message_idempotency')], таблиц = 7
|
||||
2026-10-20 04:55:31.135 | INFO | infrastructure.dependencies.database:apply_migrations:157 - Схема БД в актуальной версии
|
||||
запуск 2: schema_migrations = [(1, '0001_init'), (2, '0002_message_idempotency')], таблиц = 7
|
||||
2026-10-20 04:55:31.136 | INFO | infrastructure.dependencies.database:apply_migrations:157 - Схема БД в актуальной версии
|
||||
запуск 3: schema_migrations = [(1, '0001_init'), (2, '0002_message_idempotency')], таблиц = 7
|
||||
2026-10-20 04:55:31.136 | INFO | infrastructure.dependencies.database:check_migrations:183 - Схема БД совместима: версия 2
|
||||
API после migrate: схема совместима
|
||||
$ BUILDX_NO_DEFAULT_ATTESTATIONS=1 ./scripts/check_previous_version.sh
|
||||
previous version image: mtusi-messenger:compat-aa971fb (aa971fb feat: перенос core-логики 2 лабы)
|
||||
schema on stand: 0001_init, 0002_message_idempotency
|
||||
{"ready": true, "register": 201, "login": 201, "create_chat": 201, "send_message_without_key": 201, "read_history": 200, "history_matches": true}
|
||||
startup log: ['Схема БД в актуальной версии']
|
||||
PASS: previous API version works on the current schema (expand-only migration)
|
||||
@@ -0,0 +1,54 @@
|
||||
$ make instances
|
||||
./scripts/instances.sh
|
||||
CONTAINER STATE INSTANCE PID REVISION
|
||||
messenger-lab3-api-1 running/healthy bfb3f7bbb85d 23878 dev
|
||||
messenger-lab3-api-2 running/healthy 6d2f6cd2b04a 23926 dev
|
||||
$ make sigterm-check
|
||||
python3 scripts/sigterm_check.py --base-url "http://localhost:8080"
|
||||
Журнал реплики после SIGTERM:
|
||||
2026-10-20 04:55:46.580 | INFO | __main__:shutdown:91 - SIGTERM: drain 1.0 с, instance=bfb3f7bbb85d: порт закрыт, keep-alive соединения закрываются после ответа
|
||||
INFO: Shutting down
|
||||
INFO: Waiting for application shutdown.
|
||||
2026-10-20 04:55:47.701 | INFO | __main__:lifespan:54 - Application stopped, instance=bfb3f7bbb85d, connections closed
|
||||
INFO: Application shutdown complete.
|
||||
INFO: Finished server process [1]
|
||||
{
|
||||
"container": "messenger-lab3-api-1",
|
||||
"instance": "bfb3f7bbb85d",
|
||||
"exit_code": 0,
|
||||
"shutdown_seconds": 1.53,
|
||||
"limit_seconds": 10,
|
||||
"instances_before": [
|
||||
"6d2f6cd2b04a",
|
||||
"bfb3f7bbb85d"
|
||||
],
|
||||
"probes_ok_after_sigterm": 354,
|
||||
"instances_after_sigterm": [
|
||||
"6d2f6cd2b04a",
|
||||
"bfb3f7bbb85d"
|
||||
],
|
||||
"last_target_response_after_sigterm_seconds": 0.133,
|
||||
"target_responses_after_exit": 0,
|
||||
"errors_after_sigterm": 0,
|
||||
"error_kinds": []
|
||||
}
|
||||
PASS: SIGTERM завершил messenger-lab3-api-1 за 1.53 с, LB обслужил 354 запросов без ошибок
|
||||
$ make instances
|
||||
./scripts/instances.sh
|
||||
CONTAINER STATE INSTANCE PID REVISION
|
||||
messenger-lab3-api-1 exited/unhealthy - 0 dev
|
||||
messenger-lab3-api-2 running/healthy 6d2f6cd2b04a 23926 dev
|
||||
$ make restore
|
||||
./scripts/restore.sh
|
||||
Container messenger-lab3-api-2 Running
|
||||
Container messenger-lab3-api-1 Starting
|
||||
Container messenger-lab3-api-1 Started
|
||||
Container messenger-lab3-api-1 Waiting
|
||||
Container messenger-lab3-api-2 Waiting
|
||||
Container messenger-lab3-api-1 Healthy
|
||||
Container messenger-lab3-api-2 Healthy
|
||||
$ make instances
|
||||
./scripts/instances.sh
|
||||
CONTAINER STATE INSTANCE PID REVISION
|
||||
messenger-lab3-api-1 running/healthy 7535b2ed4434 69183 dev
|
||||
messenger-lab3-api-2 running/healthy 6d2f6cd2b04a 23926 dev
|
||||
@@ -0,0 +1,33 @@
|
||||
$ docker inspect -f "{{.Name}} cpus={{.HostConfig.NanoCpus}} memory={{.HostConfig.Memory}} restart={{.HostConfig.RestartPolicy.Name}} ports={{json .HostConfig.PortBindings}}" $(docker compose ps -aq)
|
||||
/messenger-lab3-api-1 cpus=1000000000 memory=536870912 restart=unless-stopped ports={}
|
||||
/messenger-lab3-api-2 cpus=1000000000 memory=536870912 restart=unless-stopped ports={}
|
||||
/messenger-lab3-api-short-ttl-1 cpus=1000000000 memory=536870912 restart=unless-stopped ports={}
|
||||
/messenger-lab3-migrate-1 cpus=500000000 memory=268435456 restart=no ports={}
|
||||
/messenger-lab3-nginx-1 cpus=500000000 memory=67108864 restart=unless-stopped ports={"8080/tcp":[{"HostIp":"127.0.0.1","HostPort":"8080"}],"8081/tcp":[{"HostIp":"127.0.0.1","HostPort":"8081"}]}
|
||||
/messenger-lab3-postgres-1 cpus=1500000000 memory=536870912 restart=unless-stopped ports={}
|
||||
/messenger-lab3-valkey-1 cpus=250000000 memory=100663296 restart=unless-stopped ports={}
|
||||
$ for n in messenger-lab3_edge messenger-lab3_backend; do docker network inspect -f "{{.Name}} internal={{.Internal}} containers={{range .Containers}}{{.Name}} {{end}}" $n; done
|
||||
messenger-lab3_edge internal=false containers=messenger-lab3-api-2 messenger-lab3-api-1 messenger-lab3-nginx-1
|
||||
messenger-lab3_backend internal=true containers=messenger-lab3-api-2 messenger-lab3-valkey-1 messenger-lab3-postgres-1 messenger-lab3-api-1
|
||||
$ grep -n "container_name" compose.yaml || echo "container_name: not used"
|
||||
container_name: not used
|
||||
$ docker compose config | grep -E "^\s+(published|target):" | sort | uniq -c
|
||||
1 published: "8080"
|
||||
1 published: "8081"
|
||||
1 target: /etc/nginx/errors.conf
|
||||
1 target: /etc/nginx/nginx.conf
|
||||
1 target: /var/lib/postgresql
|
||||
1 target: 8080
|
||||
1 target: 8081
|
||||
$ docker compose exec -T nginx sh -c "wget -q -O - --timeout 2 http://postgres:5432 2>&1 | head -1; nslookup postgres 127.0.0.11 2>&1 | tail -2"
|
||||
wget: bad address 'postgres:5432'
|
||||
** server can't find postgres: NXDOMAIN
|
||||
|
||||
$ curl -s --max-time 2 http://localhost:5432 >/dev/null 2>&1 && echo "5432 open" || echo "host 127.0.0.1:5432: closed"
|
||||
host 127.0.0.1:5432: closed
|
||||
$ docker stats --no-stream --format "{{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}" $(docker compose ps -q)
|
||||
messenger-lab3-api-1 0.25% 45.73MiB / 512MiB
|
||||
messenger-lab3-api-2 0.21% 45.42MiB / 512MiB
|
||||
messenger-lab3-nginx-1 0.03% 2.59MiB / 64MiB
|
||||
messenger-lab3-postgres-1 1.15% 30.5MiB / 512MiB
|
||||
messenger-lab3-valkey-1 0.22% 11.04MiB / 96MiB
|
||||
@@ -0,0 +1,18 @@
|
||||
$ python3 tests/load.py --duration 30 --concurrency 4 --output evidence/load.json
|
||||
{
|
||||
"lab": 3,
|
||||
"workload": "closed-loop POST messages; no automatic retries",
|
||||
"requested_duration_seconds": 30.0,
|
||||
"elapsed_seconds": 30.024,
|
||||
"concurrency": 4,
|
||||
"successful_requests": 21418,
|
||||
"failed_requests": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 713.369,
|
||||
"successful_latency_ms": {
|
||||
"p50": 4.748,
|
||||
"p95": 8.395,
|
||||
"p99": 22.531
|
||||
},
|
||||
"note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead."
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
$ make instances
|
||||
./scripts/instances.sh
|
||||
CONTAINER STATE INSTANCE PID REVISION
|
||||
messenger-lab3-api-1 running/healthy 7535b2ed4434 69183 dev
|
||||
messenger-lab3-api-2 running/healthy 6d2f6cd2b04a 23926 dev
|
||||
$ make experiment-failover
|
||||
python3 scripts/experiment.py failover --base-url "http://localhost:8080" --output evidence/experiment-failover.json
|
||||
== SIGTERM messenger-lab3-api-1 (instance 7535b2ed4434)
|
||||
{"scenario": "SIGTERM messenger-lab3-api-1", "rps_ok_write": {"before": 381.6, "during": 276.9, "after": 398.9}, "rps_ok_read": {"before": 381.6, "during": 276.8, "after": 399.0}, "errors": {"before": 0, "during": 0, "after": 0}, "recovery": {"write_errors": 0, "error_kinds": {}, "write_recovery_seconds": 0.0, "max_write_gap_seconds": 0.063}, "target_responses_while_stopped": 0, "restored_replica_in_rotation_seconds": 0.971, "instances_seen_from_action": {"6d2f6cd2b04a": [-10.128, 21.291], "7535b2ed4434": [-10.128, 0.032], "eb1dace786c6": [14.257, 21.289]}, "integrity": {"messages_in_history": 10740, "confirmed_unique": 10740, "confirmed_missing_or_changed": 0, "duplicate_ids": 0, "duplicate_texts": 0, "retried_writes_confirmed_as_replay": 0}}
|
||||
== SIGTERM messenger-lab3-api-2 (instance 6d2f6cd2b04a)
|
||||
{"scenario": "SIGTERM messenger-lab3-api-2", "rps_ok_write": {"before": 401.3, "during": 282.3, "after": 362.4}, "rps_ok_read": {"before": 401.1, "during": 282.4, "after": 362.5}, "errors": {"before": 0, "during": 0, "after": 0}, "recovery": {"write_errors": 0, "error_kinds": {}, "write_recovery_seconds": 0.0, "max_write_gap_seconds": 0.071}, "target_responses_while_stopped": 0, "restored_replica_in_rotation_seconds": 3.736, "instances_seen_from_action": {"6d2f6cd2b04a": [-10.122, 0.079], "eb1dace786c6": [-10.121, 21.19], "577316c523cb": [16.921, 21.191]}, "integrity": {"messages_in_history": 10687, "confirmed_unique": 10687, "confirmed_missing_or_changed": 0, "duplicate_ids": 0, "duplicate_texts": 0, "retried_writes_confirmed_as_replay": 0}}
|
||||
== SIGKILL messenger-lab3-api-1 (instance eb1dace786c6)
|
||||
{"scenario": "SIGKILL messenger-lab3-api-1", "rps_ok_write": {"before": 427.4, "during": 281.4, "after": 408.2}, "rps_ok_read": {"before": 427.3, "during": 281.3, "after": 408.5}, "errors": {"before": 0, "during": 1, "after": 0}, "recovery": {"write_errors": 1, "error_kinds": {"HTTP 503": 1}, "write_recovery_seconds": 0.0, "max_write_gap_seconds": 0.07}, "target_responses_while_stopped": 0, "restored_replica_in_rotation_seconds": 0.79, "instances_seen_from_action": {"eb1dace786c6": [-9.73, 0.024], "577316c523cb": [-9.73, 20.11], "506b547ba744": [12.893, 20.111]}, "integrity": {"messages_in_history": 10837, "confirmed_unique": 10837, "confirmed_missing_or_changed": 0, "duplicate_ids": 0, "duplicate_texts": 0, "retried_writes_confirmed_as_replay": 0}}
|
||||
== SIGKILL messenger-lab3-api-2 (instance 577316c523cb)
|
||||
{"scenario": "SIGKILL messenger-lab3-api-2", "rps_ok_write": {"before": 387.4, "during": 276.7, "after": 373.2}, "rps_ok_read": {"before": 387.1, "during": 276.8, "after": 373.3}, "errors": {"before": 0, "during": 3, "after": 0}, "recovery": {"write_errors": 3, "error_kinds": {"HTTP 503": 3}, "write_recovery_seconds": 0.006, "max_write_gap_seconds": 0.061}, "target_responses_while_stopped": 0, "restored_replica_in_rotation_seconds": 0.0, "instances_seen_from_action": {"506b547ba744": [-9.94, 20.189], "577316c523cb": [-9.939, 0.028], "1c20e7f6dbde": [11.911, 20.19]}, "integrity": {"messages_in_history": 10212, "confirmed_unique": 10212, "confirmed_missing_or_changed": 0, "duplicate_ids": 0, "duplicate_texts": 0, "retried_writes_confirmed_as_replay": 0}}
|
||||
saved: evidence/experiment-failover.json
|
||||
PASS: прогонов 4, подтверждённые сообщения целы, дублей нет
|
||||
$ make instances
|
||||
./scripts/instances.sh
|
||||
CONTAINER STATE INSTANCE PID REVISION
|
||||
messenger-lab3-api-1 running/healthy 506b547ba744 76140 dev
|
||||
messenger-lab3-api-2 running/healthy 1c20e7f6dbde 77892 dev
|
||||
@@ -0,0 +1,18 @@
|
||||
$ make instances
|
||||
./scripts/instances.sh
|
||||
CONTAINER STATE INSTANCE PID REVISION
|
||||
messenger-lab3-api-1 running/healthy 506b547ba744 76140 dev
|
||||
messenger-lab3-api-2 running/healthy 1c20e7f6dbde 77892 dev
|
||||
$ make experiment-rolling
|
||||
python3 scripts/experiment.py rolling --base-url "http://localhost:8080" --output evidence/experiment-rolling.json
|
||||
== rolling update to revision exp-1792472327
|
||||
{"scenario": "rolling update", "rps_ok_write": {"before": 420.1, "during": 308.0, "after": 345.4}, "rps_ok_read": {"before": 419.9, "during": 307.9, "after": 345.5}, "errors": {"before": 0, "during": 0, "after": 0}, "recovery": {"write_errors": 0, "error_kinds": {}, "write_recovery_seconds": 0.0, "max_write_gap_seconds": 0.098}, "target_responses_while_stopped": null, "restored_replica_in_rotation_seconds": null, "instances_seen_from_action": {"506b547ba744": [-9.228, 8.14], "1c20e7f6dbde": [-9.227, 14.911], "27601bb40a21": [4.875, 24.355], "e7e986ee1b58": [13.885, 24.355]}, "integrity": {"messages_in_history": 11682, "confirmed_unique": 11682, "confirmed_missing_or_changed": 0, "duplicate_ids": 0, "duplicate_texts": 0, "retried_writes_confirmed_as_replay": 0}}
|
||||
saved: evidence/experiment-rolling.json
|
||||
PASS: прогонов 1, подтверждённые сообщения целы, дублей нет
|
||||
$ make instances
|
||||
./scripts/instances.sh
|
||||
CONTAINER STATE INSTANCE PID REVISION
|
||||
messenger-lab3-api-3 running/healthy 27601bb40a21 79887 exp-1792472327
|
||||
messenger-lab3-api-4 running/healthy e7e986ee1b58 80346 exp-1792472327
|
||||
$ docker compose logs --no-log-prefix nginx --since 3m | grep -cE "prematurely closed" || true
|
||||
4
|
||||
@@ -0,0 +1,192 @@
|
||||
$ echo "COMPOSE_PROJECT_NAME=$COMPOSE_PROJECT_NAME (отдельный проект: рабочий стенд не затрагивается)"
|
||||
COMPOSE_PROJECT_NAME=lab3-clean (отдельный проект: рабочий стенд не затрагивается)
|
||||
$ cp .env.example .env && sed -i "" "s/^DB_PASS=.*/DB_PASS=$(openssl rand -hex 16)/; s/^VALKEY_PASS=.*/VALKEY_PASS=$(openssl rand -hex 16)/; s/^LB_PORT=.*/LB_PORT=18080/" .env && echo "LB_EXPIRY_PORT=18081" >> .env && echo ".env created from .env.example with random synthetic secrets"
|
||||
.env created from .env.example with random synthetic secrets
|
||||
$ docker compose --env-file .env config --quiet && echo "compose config: OK"
|
||||
compose config: OK
|
||||
$ make up
|
||||
docker compose up -d --build --wait
|
||||
#1 [internal] load local bake definitions
|
||||
#1 reading from stdin 1.47kB done
|
||||
#1 DONE 0.0s
|
||||
|
||||
#2 [api internal] load build definition from Dockerfile
|
||||
#2 transferring dockerfile: 914B done
|
||||
#2 DONE 0.0s
|
||||
|
||||
#3 [api internal] load metadata for docker.io/library/python:3.12.12-slim-bookworm
|
||||
#3 DONE 0.0s
|
||||
|
||||
#4 [api internal] load .dockerignore
|
||||
#4 transferring context: 358B done
|
||||
#4 DONE 0.0s
|
||||
|
||||
#5 [api 1/7] FROM docker.io/library/python:3.12.12-slim-bookworm@sha256:593bd06efe90efa80dc4eee3948be7c0fde4134606dd40d8dd8dbcade98e669c
|
||||
#5 resolve docker.io/library/python:3.12.12-slim-bookworm@sha256:593bd06efe90efa80dc4eee3948be7c0fde4134606dd40d8dd8dbcade98e669c done
|
||||
#5 DONE 0.0s
|
||||
|
||||
#6 [api internal] load build context
|
||||
#6 transferring context: 184.06kB 0.0s done
|
||||
#6 DONE 0.0s
|
||||
|
||||
#7 [api 2/7] WORKDIR /app
|
||||
#7 CACHED
|
||||
|
||||
#8 [api 4/7] RUN pip install --no-cache-dir --timeout 60 --retries 10 "poetry==2.2.1" && poetry install --only main --no-root --no-interaction
|
||||
#8 CACHED
|
||||
|
||||
#9 [api 3/7] COPY poetry.lock pyproject.toml ./
|
||||
#9 CACHED
|
||||
|
||||
#10 [migrate 5/7] COPY migrations ./migrations
|
||||
#10 CACHED
|
||||
|
||||
#11 [migrate 6/7] COPY src ./src
|
||||
#11 DONE 0.0s
|
||||
|
||||
#12 [api 7/7] RUN useradd --system --uid 10001 --no-create-home app
|
||||
#12 0.118 useradd warning: app's uid 10001 is greater than SYS_UID_MAX 999
|
||||
#12 DONE 0.1s
|
||||
|
||||
#13 [migrate] exporting to image
|
||||
#13 exporting layers 0.0s done
|
||||
#13 exporting manifest sha256:a99a66062da552c44bf2543747fc3ac215605d7487b93882f0248f46ae71dbf5 done
|
||||
#13 exporting config sha256:53e2ac5d5830524705f0737d78b69d4f9c1ca9670b8a24788c8717a7d772898f done
|
||||
#13 naming to docker.io/library/mtusi-messenger:lab3 done
|
||||
#13 unpacking to docker.io/library/mtusi-messenger:lab3 0.0s done
|
||||
#13 DONE 0.1s
|
||||
|
||||
#14 [api] exporting to image
|
||||
#14 exporting layers 0.0s done
|
||||
#14 exporting manifest sha256:47e4d0732ca8d624529f0f775abf4cd8457445931de74053dec5bbab49de873b done
|
||||
#14 exporting config sha256:d6557e51b469086507b892558ee5960f31b77943d8c8fddc381e955e4eb3718a done
|
||||
#14 naming to docker.io/library/mtusi-messenger:lab3 done
|
||||
#14 unpacking to docker.io/library/mtusi-messenger:lab3 0.0s done
|
||||
#14 DONE 0.1s
|
||||
|
||||
#15 [migrate] resolving provenance for metadata file
|
||||
#15 DONE 0.0s
|
||||
|
||||
#16 [api] resolving provenance for metadata file
|
||||
#16 DONE 0.0s
|
||||
mtusi-messenger:lab3 Built
|
||||
mtusi-messenger:lab3 Built
|
||||
Network lab3-clean_backend Creating
|
||||
Network lab3-clean_backend Created
|
||||
Network lab3-clean_edge Creating
|
||||
Network lab3-clean_edge Created
|
||||
Volume lab3-clean_postgres_data Creating
|
||||
Volume lab3-clean_postgres_data Created
|
||||
Container lab3-clean-valkey-1 Creating
|
||||
Container lab3-clean-postgres-1 Creating
|
||||
Container lab3-clean-valkey-1 Created
|
||||
Container lab3-clean-postgres-1 Created
|
||||
Container lab3-clean-migrate-1 Creating
|
||||
Container lab3-clean-migrate-1 Created
|
||||
Container lab3-clean-api-2 Creating
|
||||
Container lab3-clean-api-1 Creating
|
||||
Container lab3-clean-api-2 Created
|
||||
Container lab3-clean-api-1 Created
|
||||
Container lab3-clean-nginx-1 Creating
|
||||
Container lab3-clean-nginx-1 Created
|
||||
Container lab3-clean-valkey-1 Starting
|
||||
Container lab3-clean-postgres-1 Starting
|
||||
Container lab3-clean-postgres-1 Started
|
||||
Container lab3-clean-postgres-1 Waiting
|
||||
Container lab3-clean-valkey-1 Started
|
||||
Container lab3-clean-postgres-1 Healthy
|
||||
Container lab3-clean-migrate-1 Starting
|
||||
Container lab3-clean-migrate-1 Started
|
||||
Container lab3-clean-migrate-1 Waiting
|
||||
Container lab3-clean-postgres-1 Waiting
|
||||
Container lab3-clean-postgres-1 Healthy
|
||||
Container lab3-clean-migrate-1 Exited
|
||||
Container lab3-clean-api-1 Starting
|
||||
Container lab3-clean-api-1 Started
|
||||
Container lab3-clean-api-2 Starting
|
||||
Container lab3-clean-api-2 Started
|
||||
Container lab3-clean-api-1 Waiting
|
||||
Container lab3-clean-api-2 Waiting
|
||||
Container lab3-clean-api-1 Healthy
|
||||
Container lab3-clean-api-2 Healthy
|
||||
Container lab3-clean-nginx-1 Starting
|
||||
Container lab3-clean-nginx-1 Started
|
||||
Container lab3-clean-postgres-1 Waiting
|
||||
Container lab3-clean-valkey-1 Waiting
|
||||
Container lab3-clean-nginx-1 Waiting
|
||||
Container lab3-clean-api-1 Waiting
|
||||
Container lab3-clean-api-2 Waiting
|
||||
Container lab3-clean-migrate-1 Waiting
|
||||
Container lab3-clean-valkey-1 Healthy
|
||||
Container lab3-clean-postgres-1 Healthy
|
||||
Container lab3-clean-migrate-1 Exited
|
||||
Container lab3-clean-api-1 Healthy
|
||||
Container lab3-clean-api-2 Healthy
|
||||
Container lab3-clean-nginx-1 Healthy
|
||||
$ make check test BASE_URL=http://localhost:18080
|
||||
python3 scripts/check_contract.py
|
||||
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
|
||||
python3 tests/smoke.py --base-url "http://localhost:18080"
|
||||
PASS: lab3 public API scenario; infrastructure and grading evidence remain separate.
|
||||
$ make test-failover ACTION_SCRIPT=scripts/stop-one.sh BASE_URL=http://localhost:18080
|
||||
python3 tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://localhost:18080"
|
||||
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
|
||||
{"observed_instances_before": 2, "successful_writes": 131, "failed_attempts": 0, "first_recovery_seconds": 2.118}
|
||||
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||
$ make instances
|
||||
./scripts/instances.sh
|
||||
CONTAINER STATE INSTANCE PID REVISION
|
||||
lab3-clean-api-1 exited/unhealthy - 0 dev
|
||||
lab3-clean-api-2 running/healthy 4a226e142873 83992 dev
|
||||
$ make restore
|
||||
./scripts/restore.sh
|
||||
Container lab3-clean-api-2 Running
|
||||
Container lab3-clean-api-1 Starting
|
||||
Container lab3-clean-api-1 Started
|
||||
Container lab3-clean-api-1 Waiting
|
||||
Container lab3-clean-api-2 Waiting
|
||||
Container lab3-clean-api-1 Healthy
|
||||
Container lab3-clean-api-2 Healthy
|
||||
$ make test-persistence ACTION_SCRIPT=scripts/restart.sh BASE_URL=http://localhost:18080
|
||||
python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost:18080"
|
||||
Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.
|
||||
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||
$ docker compose ps -a
|
||||
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
|
||||
lab3-clean-api-1 mtusi-messenger:lab3 "python src/gateway.…" api 43 seconds ago Up 6 seconds (healthy) 8080/tcp
|
||||
lab3-clean-api-2 mtusi-messenger:lab3 "python src/gateway.…" api 43 seconds ago Up 6 seconds (healthy) 8080/tcp
|
||||
lab3-clean-migrate-1 mtusi-messenger:lab3 "python src/migrate.…" migrate 43 seconds ago Exited (0) 35 seconds ago
|
||||
lab3-clean-nginx-1 nginx:1.28.0-alpine3.21 "/docker-entrypoint.…" nginx 43 seconds ago Up 5 seconds (healthy) 127.0.0.1:18080->8080/tcp, 127.0.0.1:18081->8081/tcp
|
||||
lab3-clean-postgres-1 postgres:18.2-alpine3.22 "docker-entrypoint.s…" postgres 44 seconds ago Up 8 seconds (healthy) 5432/tcp
|
||||
lab3-clean-valkey-1 valkey/valkey:8.1.4-alpine3.22 "docker-entrypoint.s…" valkey 44 seconds ago Up 8 seconds (healthy) 6379/tcp
|
||||
$ docker compose --profile expiry down -v
|
||||
Container lab3-clean-nginx-1 Stopping
|
||||
Container lab3-clean-nginx-1 Stopped
|
||||
Container lab3-clean-nginx-1 Removing
|
||||
Container lab3-clean-nginx-1 Removed
|
||||
Container lab3-clean-api-2 Stopping
|
||||
Container lab3-clean-api-1 Stopping
|
||||
Container lab3-clean-api-1 Stopped
|
||||
Container lab3-clean-api-1 Removing
|
||||
Container lab3-clean-api-1 Removed
|
||||
Container lab3-clean-api-2 Stopped
|
||||
Container lab3-clean-api-2 Removing
|
||||
Container lab3-clean-api-2 Removed
|
||||
Container lab3-clean-valkey-1 Stopping
|
||||
Container lab3-clean-migrate-1 Stopping
|
||||
Container lab3-clean-migrate-1 Stopped
|
||||
Container lab3-clean-migrate-1 Removing
|
||||
Container lab3-clean-migrate-1 Removed
|
||||
Container lab3-clean-postgres-1 Stopping
|
||||
Container lab3-clean-postgres-1 Stopped
|
||||
Container lab3-clean-postgres-1 Removing
|
||||
Container lab3-clean-postgres-1 Removed
|
||||
Container lab3-clean-valkey-1 Stopped
|
||||
Container lab3-clean-valkey-1 Removing
|
||||
Container lab3-clean-valkey-1 Removed
|
||||
Volume lab3-clean_postgres_data Removing
|
||||
Network lab3-clean_edge Removing
|
||||
Network lab3-clean_backend Removing
|
||||
Volume lab3-clean_postgres_data Removed
|
||||
Network lab3-clean_edge Removed
|
||||
Network lab3-clean_backend Removed
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,482 @@
|
||||
{
|
||||
"mode": "rolling",
|
||||
"started": "2026-10-20T09:50:22+0300",
|
||||
"profile": {
|
||||
"workers": 4,
|
||||
"loop": "closed: POST message (retry same key until 201) + GET limit=1",
|
||||
"client_timeout_seconds": 5,
|
||||
"phases_seconds": {
|
||||
"before": 8,
|
||||
"outage": 10,
|
||||
"after": 8
|
||||
}
|
||||
},
|
||||
"runs": [
|
||||
{
|
||||
"scenario": "rolling update",
|
||||
"target_instance": null,
|
||||
"workers": 4,
|
||||
"events_seconds": {
|
||||
"action_start": 10.323,
|
||||
"action_end": 26.786,
|
||||
"end": 34.791
|
||||
},
|
||||
"instances_before": [
|
||||
"109172df7747",
|
||||
"f6e244c650a2"
|
||||
],
|
||||
"instances_after": [
|
||||
"500528cb388e",
|
||||
"de52b5578b6b"
|
||||
],
|
||||
"target_responses_while_stopped": null,
|
||||
"restored_replica_in_rotation_seconds": null,
|
||||
"instances_seen_from_action": {
|
||||
"109172df7747": [
|
||||
-10.3,
|
||||
14.813
|
||||
],
|
||||
"f6e244c650a2": [
|
||||
-10.296,
|
||||
8.226
|
||||
],
|
||||
"500528cb388e": [
|
||||
6.241,
|
||||
24.466
|
||||
],
|
||||
"de52b5578b6b": [
|
||||
13.791,
|
||||
24.468
|
||||
]
|
||||
},
|
||||
"phases": {
|
||||
"before": {
|
||||
"seconds": 10.323,
|
||||
"write": {
|
||||
"ok": 3687,
|
||||
"errors": 0,
|
||||
"ok_rps": 357.2,
|
||||
"latency_ms": {
|
||||
"p50": 5.4,
|
||||
"p95": 10.3,
|
||||
"p99": 28.9
|
||||
}
|
||||
},
|
||||
"read": {
|
||||
"ok": 3684,
|
||||
"errors": 0,
|
||||
"ok_rps": 356.9,
|
||||
"latency_ms": {
|
||||
"p50": 4.2,
|
||||
"p95": 7.8,
|
||||
"p99": 19.9
|
||||
}
|
||||
}
|
||||
},
|
||||
"during": {
|
||||
"seconds": 16.463,
|
||||
"write": {
|
||||
"ok": 5071,
|
||||
"errors": 0,
|
||||
"ok_rps": 308.0,
|
||||
"latency_ms": {
|
||||
"p50": 5.4,
|
||||
"p95": 16.9,
|
||||
"p99": 38.6
|
||||
}
|
||||
},
|
||||
"read": {
|
||||
"ok": 5073,
|
||||
"errors": 0,
|
||||
"ok_rps": 308.1,
|
||||
"latency_ms": {
|
||||
"p50": 4.2,
|
||||
"p95": 12.3,
|
||||
"p99": 31.4
|
||||
}
|
||||
}
|
||||
},
|
||||
"after": {
|
||||
"seconds": 8.005,
|
||||
"write": {
|
||||
"ok": 2800,
|
||||
"errors": 0,
|
||||
"ok_rps": 349.8,
|
||||
"latency_ms": {
|
||||
"p50": 4.8,
|
||||
"p95": 10.9,
|
||||
"p99": 48.3
|
||||
}
|
||||
},
|
||||
"read": {
|
||||
"ok": 2801,
|
||||
"errors": 0,
|
||||
"ok_rps": 349.9,
|
||||
"latency_ms": {
|
||||
"p50": 3.7,
|
||||
"p95": 8.0,
|
||||
"p99": 47.3
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"recovery": {
|
||||
"write_errors": 0,
|
||||
"error_kinds": {},
|
||||
"write_recovery_seconds": 0.0,
|
||||
"max_write_gap_seconds": 0.299
|
||||
},
|
||||
"integrity": {
|
||||
"messages_in_history": 11558,
|
||||
"confirmed_unique": 11558,
|
||||
"confirmed_missing_or_changed": 0,
|
||||
"duplicate_ids": 0,
|
||||
"duplicate_texts": 0,
|
||||
"retried_writes_confirmed_as_replay": 0
|
||||
},
|
||||
"container_stats_before_action": [
|
||||
"messenger-lab3-nginx-1\t10.92%\t3.227MiB / 64MiB",
|
||||
"messenger-lab3-postgres-1\t30.04%\t54.82MiB / 512MiB",
|
||||
"messenger-lab3-api-1\t85.53%\t52.1MiB / 512MiB",
|
||||
"messenger-lab3-api-2\t84.90%\t54.01MiB / 512MiB"
|
||||
],
|
||||
"action_output": [
|
||||
"[09:50:33] build revision exp-1792479022",
|
||||
"[09:50:34] migrate",
|
||||
"[09:50:36] scale api to 3, wait healthy",
|
||||
"[09:50:41] graceful stop messenger-lab3-api-1",
|
||||
"[09:50:42] scale api to 3, wait healthy",
|
||||
"[09:50:47] graceful stop messenger-lab3-api-2",
|
||||
"[09:50:49] done: all API replicas run revision exp-1792479022"
|
||||
],
|
||||
"restore_output_tail": [],
|
||||
"timeline": [
|
||||
{
|
||||
"t": 0,
|
||||
"ok": 705,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"109172df7747": 354,
|
||||
"f6e244c650a2": 351
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 1,
|
||||
"ok": 522,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"f6e244c650a2": 263,
|
||||
"109172df7747": 259
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 2,
|
||||
"ok": 733,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"109172df7747": 367,
|
||||
"f6e244c650a2": 366
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 3,
|
||||
"ok": 771,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"109172df7747": 385,
|
||||
"f6e244c650a2": 386
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 4,
|
||||
"ok": 725,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"109172df7747": 363,
|
||||
"f6e244c650a2": 362
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 5,
|
||||
"ok": 803,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"109172df7747": 401,
|
||||
"f6e244c650a2": 402
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 6,
|
||||
"ok": 449,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"109172df7747": 224,
|
||||
"f6e244c650a2": 225
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 7,
|
||||
"ok": 777,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"109172df7747": 390,
|
||||
"f6e244c650a2": 387
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 8,
|
||||
"ok": 803,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"f6e244c650a2": 402,
|
||||
"109172df7747": 401
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 9,
|
||||
"ok": 811,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"109172df7747": 405,
|
||||
"f6e244c650a2": 406
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 10,
|
||||
"ok": 677,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"109172df7747": 339,
|
||||
"f6e244c650a2": 338
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 11,
|
||||
"ok": 271,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"f6e244c650a2": 135,
|
||||
"109172df7747": 136
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 12,
|
||||
"ok": 432,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"f6e244c650a2": 217,
|
||||
"109172df7747": 215
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 13,
|
||||
"ok": 422,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"109172df7747": 211,
|
||||
"f6e244c650a2": 211
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 14,
|
||||
"ok": 533,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"109172df7747": 267,
|
||||
"f6e244c650a2": 266
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 15,
|
||||
"ok": 737,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"f6e244c650a2": 369,
|
||||
"109172df7747": 368
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 16,
|
||||
"ok": 683,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"109172df7747": 276,
|
||||
"f6e244c650a2": 277,
|
||||
"500528cb388e": 130
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 17,
|
||||
"ok": 635,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"500528cb388e": 212,
|
||||
"109172df7747": 212,
|
||||
"f6e244c650a2": 211
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 18,
|
||||
"ok": 841,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"f6e244c650a2": 153,
|
||||
"500528cb388e": 345,
|
||||
"109172df7747": 343
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 19,
|
||||
"ok": 782,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"109172df7747": 391,
|
||||
"500528cb388e": 391
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 20,
|
||||
"ok": 254,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"109172df7747": 128,
|
||||
"500528cb388e": 126
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 21,
|
||||
"ok": 767,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"500528cb388e": 384,
|
||||
"109172df7747": 383
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 22,
|
||||
"ok": 761,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"109172df7747": 380,
|
||||
"500528cb388e": 381
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 23,
|
||||
"ok": 795,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"500528cb388e": 397,
|
||||
"109172df7747": 398
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 24,
|
||||
"ok": 924,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"109172df7747": 322,
|
||||
"500528cb388e": 321,
|
||||
"de52b5578b6b": 281
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 25,
|
||||
"ok": 681,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"500528cb388e": 320,
|
||||
"109172df7747": 41,
|
||||
"de52b5578b6b": 320
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 26,
|
||||
"ok": 232,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"de52b5578b6b": 115,
|
||||
"500528cb388e": 117
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 27,
|
||||
"ok": 252,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"de52b5578b6b": 126,
|
||||
"500528cb388e": 126
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 28,
|
||||
"ok": 633,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"de52b5578b6b": 317,
|
||||
"500528cb388e": 316
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 29,
|
||||
"ok": 940,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"500528cb388e": 470,
|
||||
"de52b5578b6b": 470
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 30,
|
||||
"ok": 814,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"de52b5578b6b": 407,
|
||||
"500528cb388e": 407
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 31,
|
||||
"ok": 626,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"de52b5578b6b": 313,
|
||||
"500528cb388e": 313
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 32,
|
||||
"ok": 878,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"de52b5578b6b": 439,
|
||||
"500528cb388e": 439
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 33,
|
||||
"ok": 740,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"500528cb388e": 370,
|
||||
"de52b5578b6b": 370
|
||||
}
|
||||
},
|
||||
{
|
||||
"t": 34,
|
||||
"ok": 707,
|
||||
"errors": 0,
|
||||
"instances": {
|
||||
"500528cb388e": 353,
|
||||
"de52b5578b6b": 354
|
||||
}
|
||||
}
|
||||
],
|
||||
"revisions_after": [
|
||||
"CONTAINER STATE INSTANCE PID REVISION",
|
||||
"messenger-lab3-api-3 running/healthy 500528cb388e 60335 exp-1792479022",
|
||||
"messenger-lab3-api-4 running/healthy de52b5578b6b 60779 exp-1792479022"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"lab": 3,
|
||||
"workload": "closed-loop POST messages; no automatic retries",
|
||||
"requested_duration_seconds": 30.0,
|
||||
"elapsed_seconds": 30.024,
|
||||
"concurrency": 4,
|
||||
"successful_requests": 21418,
|
||||
"failed_requests": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 713.369,
|
||||
"successful_latency_ms": {
|
||||
"p50": 4.748,
|
||||
"p95": 8.395,
|
||||
"p99": 22.531
|
||||
},
|
||||
"note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead."
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
# Ошибки, созданные самим балансировщиком, — в формате ошибок контракта.
|
||||
# Ответы реплик (включая их 503) проходят без изменений: proxy_intercept_errors off.
|
||||
# X-Instance-Id здесь нет: ответ создан LB, а не API-репликой.
|
||||
|
||||
location @unavailable {
|
||||
default_type application/json;
|
||||
add_header X-Request-Id $request_id always;
|
||||
return 503 '{"error":{"code":"unavailable","message":"Нет доступной API-реплики","request_id":"$request_id"}}';
|
||||
}
|
||||
|
||||
location @too_large {
|
||||
default_type application/json;
|
||||
add_header X-Request-Id $request_id always;
|
||||
return 413 '{"error":{"code":"payload_too_large","message":"Тело запроса слишком большое","request_id":"$request_id"}}';
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
worker_processes 1;
|
||||
pid /tmp/nginx.pid;
|
||||
error_log /dev/stderr warn;
|
||||
|
||||
events {
|
||||
worker_connections 1024;
|
||||
}
|
||||
|
||||
http {
|
||||
resolver 127.0.0.11 valid=2s ipv6=off;
|
||||
resolver_timeout 1s;
|
||||
|
||||
log_format upstream_json escape=json
|
||||
'{"time":"$time_iso8601","request_id":"$request_id",'
|
||||
'"method":"$request_method","uri":"$uri","status":$status,'
|
||||
'"request_time":$request_time,"upstream_addr":"$upstream_addr",'
|
||||
'"upstream_status":"$upstream_status",'
|
||||
'"upstream_response_time":"$upstream_response_time",'
|
||||
'"instance":"$upstream_http_x_instance_id"}';
|
||||
access_log /dev/stdout upstream_json;
|
||||
|
||||
server_tokens off;
|
||||
client_max_body_size 1m;
|
||||
client_body_buffer_size 64k;
|
||||
client_header_timeout 10s;
|
||||
client_body_timeout 10s;
|
||||
send_timeout 10s;
|
||||
keepalive_timeout 65s;
|
||||
|
||||
upstream api {
|
||||
zone api 64k;
|
||||
server api:8080 resolve max_fails=1 fail_timeout=5s;
|
||||
keepalive 32;
|
||||
# Меньше APP_KEEPALIVE_TIMEOUT_SEC (75 с): idle-соединение первым
|
||||
# закрывает nginx, а не реплика, — нет гонки «запрос в закрытое соединение»
|
||||
keepalive_timeout 60s;
|
||||
}
|
||||
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Connection "";
|
||||
proxy_set_header Host $host;
|
||||
# Один ID на LB и реплику: API принимает $request_id и возвращает его
|
||||
proxy_set_header X-Request-Id $request_id;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
|
||||
# connect внутри bridge-сети завершает ядро за доли мс даже у занятой реплики.
|
||||
# Самый долгий запрос API: ожидание пула 3 с + statement_timeout 5 с
|
||||
proxy_connect_timeout 500ms;
|
||||
proxy_send_timeout 10s;
|
||||
proxy_read_timeout 10s;
|
||||
|
||||
# Повтор на другой реплике только при ошибке соединения/таймауте.
|
||||
# POST, уже отправленный реплике, nginx не повторяет (non_idempotent выключен):
|
||||
# повтор сообщения делает клиент с тем же Idempotency-Key.
|
||||
# 503 приложения (общая зависимость недоступна) не повторяется и не исключает
|
||||
# реплику: другая реплика зависит от той же БД.
|
||||
proxy_next_upstream error timeout;
|
||||
proxy_next_upstream_tries 3;
|
||||
proxy_next_upstream_timeout 5s;
|
||||
|
||||
server {
|
||||
listen 8080;
|
||||
|
||||
location / {
|
||||
proxy_pass http://api;
|
||||
}
|
||||
|
||||
error_page 502 503 504 = @unavailable;
|
||||
error_page 413 = @too_large;
|
||||
|
||||
include /etc/nginx/errors.conf;
|
||||
}
|
||||
|
||||
server {
|
||||
listen 8081;
|
||||
|
||||
# Реплика api-short-ttl только для make test-expiry (профиль expiry):
|
||||
# адрес резолвится на каждый запрос, без профиля — 503 от LB
|
||||
location / {
|
||||
set $api_short_ttl http://api-short-ttl:8080;
|
||||
proxy_pass $api_short_ttl;
|
||||
}
|
||||
|
||||
error_page 502 503 504 = @unavailable;
|
||||
error_page 413 = @too_large;
|
||||
|
||||
include /etc/nginx/errors.conf;
|
||||
}
|
||||
|
||||
# Liveness самого балансировщика для healthcheck контейнера; наружу не публикуется
|
||||
server {
|
||||
listen 127.0.0.1:8090;
|
||||
access_log off;
|
||||
|
||||
location = /healthz {
|
||||
default_type text/plain;
|
||||
return 200 "ok\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
CREATE TABLE users (
|
||||
id uuid PRIMARY KEY DEFAULT uuidv7(),
|
||||
username text NOT NULL,
|
||||
display_name text NOT NULL,
|
||||
password_hash text NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
CONSTRAINT users_username_key UNIQUE (username),
|
||||
CONSTRAINT users_username_format CHECK (username ~ '^[a-z0-9_]{3,32}$'),
|
||||
CONSTRAINT users_display_name_len CHECK (char_length(display_name) BETWEEN 1 AND 100)
|
||||
);
|
||||
|
||||
-- Источник истины для сессий; токен хранится только как SHA-256
|
||||
CREATE TABLE sessions (
|
||||
token_hash bytea PRIMARY KEY,
|
||||
user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
expires_at timestamptz NOT NULL,
|
||||
revoked_at timestamptz,
|
||||
CONSTRAINT sessions_token_hash_len CHECK (octet_length(token_hash) = 32),
|
||||
CONSTRAINT sessions_expiry_order CHECK (expires_at > created_at)
|
||||
);
|
||||
|
||||
CREATE INDEX sessions_user_id_idx ON sessions (user_id);
|
||||
|
||||
CREATE TABLE chats (
|
||||
id uuid PRIMARY KEY DEFAULT uuidv7(),
|
||||
title text NOT NULL,
|
||||
created_by uuid NOT NULL REFERENCES users (id),
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
CONSTRAINT chats_title_len CHECK (char_length(title) BETWEEN 1 AND 100)
|
||||
);
|
||||
|
||||
-- position сохраняет порядок member_ids из запроса создания
|
||||
CREATE TABLE chat_members (
|
||||
chat_id uuid NOT NULL REFERENCES chats (id) ON DELETE CASCADE,
|
||||
user_id uuid NOT NULL REFERENCES users (id),
|
||||
position smallint NOT NULL,
|
||||
PRIMARY KEY (chat_id, user_id),
|
||||
CONSTRAINT chat_members_position_key UNIQUE (chat_id, position)
|
||||
);
|
||||
|
||||
-- Список чатов пользователя: WHERE user_id = $1
|
||||
CREATE INDEX chat_members_user_id_chat_id_idx ON chat_members (user_id, chat_id);
|
||||
|
||||
-- created_at назначает приложение под блокировкой чата (строго возрастает в чате)
|
||||
CREATE TABLE messages (
|
||||
id uuid PRIMARY KEY DEFAULT uuidv7(),
|
||||
chat_id uuid NOT NULL,
|
||||
sender_id uuid NOT NULL,
|
||||
text text NOT NULL,
|
||||
created_at timestamptz NOT NULL,
|
||||
CONSTRAINT messages_text_len CHECK (char_length(text) BETWEEN 1 AND 4000),
|
||||
-- отправитель обязан быть участником этого чата
|
||||
CONSTRAINT messages_sender_member_fkey FOREIGN KEY (chat_id, sender_id)
|
||||
REFERENCES chat_members (chat_id, user_id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- Keyset-пагинация истории: WHERE chat_id = $1 AND (created_at, id) > ($2, $3)
|
||||
-- ORDER BY created_at, id LIMIT $4 — Index Scan без сортировки и полного прохода
|
||||
CREATE INDEX messages_chat_id_created_at_id_idx ON messages (chat_id, created_at, id);
|
||||
@@ -0,0 +1,18 @@
|
||||
-- Idempotency-Key отправки сообщения (lab3). Область по контракту:
|
||||
-- пользователь + метод + путь + ключ. Метод и путь фиксированы
|
||||
-- (POST /api/v1/chats/{chat_id}/messages), поэтому ключ — (sender_id, chat_id, key).
|
||||
-- Только добавление таблицы (expand): предыдущая версия API работает на новой схеме.
|
||||
CREATE TABLE message_idempotency_keys (
|
||||
sender_id uuid NOT NULL,
|
||||
chat_id uuid NOT NULL,
|
||||
idempotency_key text NOT NULL,
|
||||
request_hash bytea NOT NULL,
|
||||
message_id uuid NOT NULL REFERENCES messages (id) ON DELETE CASCADE,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
expires_at timestamptz NOT NULL,
|
||||
PRIMARY KEY (sender_id, chat_id, idempotency_key),
|
||||
CONSTRAINT message_idempotency_keys_key_format
|
||||
CHECK (idempotency_key ~ '^[A-Za-z0-9._:-]{1,128}$'),
|
||||
CONSTRAINT message_idempotency_keys_hash_len CHECK (octet_length(request_hash) = 32),
|
||||
CONSTRAINT message_idempotency_keys_expiry_order CHECK (expires_at > created_at)
|
||||
);
|
||||
Generated
+829
@@ -0,0 +1,829 @@
|
||||
# This file is automatically @generated by Poetry 2.2.1 and should not be changed by hand.
|
||||
|
||||
[[package]]
|
||||
name = "annotated-doc"
|
||||
version = "0.0.5"
|
||||
description = "Document parameters, class attributes, return types, and variables inline, with Annotated."
|
||||
optional = false
|
||||
python-versions = ">=3.9"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "annotated_doc-0.0.5-py3-none-any.whl", hash = "sha256:117bac03a25ede5df5440e855b32d556049ca169ead221505badf432fed4b101"},
|
||||
{file = "annotated_doc-0.0.5.tar.gz", hash = "sha256:c7e58ce09192557605d8bbd92836d7e1d520ac9580096042c0bfd197efacf1bb"},
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "annotated-types"
|
||||
version = "0.8.0"
|
||||
description = "Reusable constraint types to use with typing.Annotated"
|
||||
optional = false
|
||||
python-versions = ">=3.10"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "annotated_types-0.8.0-py3-none-any.whl", hash = "sha256:f072f4d804ea359e4eaf198b1af7a8b0943881a87f31bb764f8bf219bb9419e0"},
|
||||
{file = "annotated_types-0.8.0.tar.gz", hash = "sha256:13b2beaad985e05e2d6407ee4c4f35590b11f8d693a258a561055cac8f64cab7"},
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anyio"
|
||||
version = "4.15.1"
|
||||
description = "High-level concurrency and networking framework on top of asyncio or Trio"
|
||||
optional = false
|
||||
python-versions = ">=3.10"
|
||||
groups = ["main", "dev"]
|
||||
files = [
|
||||
{file = "anyio-4.15.1-py3-none-any.whl", hash = "sha256:6152fdbbf9a77fdec97731721bebf7c4c44f7c29b424b0065826173efc7ed101"},
|
||||
{file = "anyio-4.15.1.tar.gz", hash = "sha256:9f28306018cbd6d329e64a36d58256edff76dd996fe423bc957326e578b82a94"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
idna = ">=2.8"
|
||||
typing_extensions = {version = ">=4.16.0", markers = "python_version < \"3.15\""}
|
||||
|
||||
[package.extras]
|
||||
trio = ["trio (>=0.32.0)"]
|
||||
|
||||
[[package]]
|
||||
name = "argon2-cffi"
|
||||
version = "25.1.0"
|
||||
description = "Argon2 for Python"
|
||||
optional = false
|
||||
python-versions = ">=3.8"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "argon2_cffi-25.1.0-py3-none-any.whl", hash = "sha256:fdc8b074db390fccb6eb4a3604ae7231f219aa669a2652e0f20e16ba513d5741"},
|
||||
{file = "argon2_cffi-25.1.0.tar.gz", hash = "sha256:694ae5cc8a42f4c4e2bf2ca0e64e51e23a040c6a517a85074683d3959e1346c1"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
argon2-cffi-bindings = "*"
|
||||
|
||||
[[package]]
|
||||
name = "argon2-cffi-bindings"
|
||||
version = "26.1.0"
|
||||
description = "Low-level CFFI bindings for Argon2"
|
||||
optional = false
|
||||
python-versions = ">=3.10"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "argon2_cffi_bindings-26.1.0-cp310-abi3-macosx_11_0_arm64.whl", hash = "sha256:21ca0396fe5ec995dd54431c32698189666f9224810acfa752e50d2bd94d9df2"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-cp310-abi3-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:78de2d65e0b9ea7ce9d1b1c3e87297b2d7305a02c266ee2a2d6910daddd7ee69"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-cp310-abi3-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:27f1821903e2ceadcb88ec2b45ef190897b7682449c772f4d9b53e42c520cf29"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-cp310-abi3-manylinux_2_34_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:d88e5f7e60f28ae0b0cc6b2f16c43e87cd642a196a86f85e0d8bb6fe016fc16d"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-cp310-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:34b7d9c24a4165a2c61cc8ae11d44d48c9ce2830fb536cb7914e11fdd9962728"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-cp310-abi3-musllinux_1_2_riscv64.whl", hash = "sha256:224865cbbcb7a2bd1356741dff12b0134df726b6d44bb7b500df8e303cbd9e81"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-cp310-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:ffff613aaa9ce6236766e2fc6dc560bb5abde7a2e2416e3db1f9ae395a2b4dd4"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-cp310-abi3-win32.whl", hash = "sha256:a86c069c91a747a2c4e5c51473590aeb48172fff9b2130d23729a42d98665ecb"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-cp310-abi3-win_amd64.whl", hash = "sha256:2c36ff87b5dfaa477d0bd51e9d7f6abdae7c8955d2983c97419085d842154b3e"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-cp310-abi3-win_arm64.whl", hash = "sha256:f9c4420a7a864fe1b86ce35befc95b8e39fb852493b81cf798671ddc265de638"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-cp313-cp313-pyemscripten_2025_0_wasm32.whl", hash = "sha256:af11ac37a7c53dc16cb7950a6190851b0870fe218b6c60c0bb7ac355234e3083"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-cp314-cp314-pyemscripten_2026_0_wasm32.whl", hash = "sha256:db0fcd827ca61622a01b220aadfbece01939acf53888f2cb98cd93e9b1e2c97e"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:28524438cd3e723f25412f63d4fd516ff5bae9ae5aa56acbe2a1404398a0cf31"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-cp314-cp314t-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:ac82fc756a446b6ccd7139ce70efa9d8bbe541e7ad579a12dcb52764b7175c5f"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-cp314-cp314t-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:6a4e68eed961a8de6928d1c17ff3dc2a547e0e923c17f8f1cd79fb7bc9502f98"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-cp314-cp314t-manylinux_2_34_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:151dfaad9de753f4af2a7854e707e4784f2acc434340ade64239c5b104b2d605"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:061a6919145bbf282ebf1f9c59d3135d4833c25313c8595c0d68cf7712ddfce2"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-cp314-cp314t-musllinux_1_2_riscv64.whl", hash = "sha256:62ff20cd130c956c7c9144d5fe35228f98b51c579b2439e988b27ef93e16c02a"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:19423e5d7ac1cc354baab59eaabf18db2ec04ef6593b5abe5a34f323c4a8f87a"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-cp314-cp314t-win32.whl", hash = "sha256:4f84cdd868978d7b7350a566c254042d44216d9e37f241f3a6d3b1dfebeede35"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-cp314-cp314t-win_amd64.whl", hash = "sha256:2b741888c93147444fdfc851abd81cc207f37f7f7da42062a00deb3888e57da8"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-cp314-cp314t-win_arm64.whl", hash = "sha256:6ab674f668d5962a3a4136ae0812519b0f1586874263723a32181d60d64137e1"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-cp315-cp315t-macosx_11_0_arm64.whl", hash = "sha256:1d98e33bd8bd67d7206c124e200bf2229c4cfa8c9c19f7b44a897f0fc71837eb"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-cp315-cp315t-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:ccaf0a46cbb380f1fd102a874e32aa629fd3cb0c0e94f4943fa1f6d5edc5dac6"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-cp315-cp315t-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:f0c3103fcff20183e593459cfea6e012281c0e76ae3ed8b5565ad1b92eac3990"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-cp315-cp315t-manylinux_2_34_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:c49e853a3bef9dd10329f31f702e7fa9b5c58229ff9c2ff6d069efaf09177c08"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-cp315-cp315t-musllinux_1_2_aarch64.whl", hash = "sha256:6376d4b3aca039375ca8bf92f770da0ec424a1ce3a37077a8d3c557411aa56ca"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-cp315-cp315t-musllinux_1_2_riscv64.whl", hash = "sha256:9bacedc04b0402837586a17f0919e3dfdd95291f441f1f56bd80ec274c2840a1"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-cp315-cp315t-musllinux_1_2_x86_64.whl", hash = "sha256:76ae29acace5d33355344612844d588e19deaaba4639d8bb01601e4b1418ef36"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-cp315-cp315t-win32.whl", hash = "sha256:df612391feca41c44d20118f3b88d1b86419465cd1f5496859f715ca60ec2210"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-cp315-cp315t-win_amd64.whl", hash = "sha256:1a0a29ed86960e44eaace7e081bdfab4f08b012fd96ec8edba71e2ad020939e4"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-cp315-cp315t-win_arm64.whl", hash = "sha256:d157ddfab1e8b21f2f1dedda9c09645d98b5ed0b667b0626be600a345d426440"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-pp310-pypy310_pp73-macosx_11_0_arm64.whl", hash = "sha256:7014ab7e6f5d8511af92544667a0346ea6dfc314ea9a7cad1dba9fdb5c9a6e33"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-pp310-pypy310_pp73-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:242bb0cda2ae3650764fc194593d9ea45fc9e72729acd89778c7cfe184cec2a5"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-pp310-pypy310_pp73-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:b70225b5fd1e0d2ef4f7fd30d24658454535f0924dff0caca5dc08efbbbadfbb"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-pp310-pypy310_pp73-win_amd64.whl", hash = "sha256:1af817e84578ef8b7295ad17de0f9896e4c8520dbf2233c7aa5aa3d487256fc4"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-pp311-pypy311_pp73-macosx_11_0_arm64.whl", hash = "sha256:19b562b1de4b9052ef1214a2821c44b6e6f22945daa102c32ae4eff929d8b6d8"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-pp311-pypy311_pp73-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:49d525938467d52c923a890153c99087c9d5a937d1f6b585dbdba34ec82e397a"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-pp311-pypy311_pp73-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:1b0bcac4d490a237e18cf91f57352920c29f77f2fa39efd0813fb81298bf17ba"},
|
||||
{file = "argon2_cffi_bindings-26.1.0-pp311-pypy311_pp73-win_amd64.whl", hash = "sha256:0cc40f7b4050bb93eb67de95d2d759322fc7ce4930b9d645581ecf4913ec651e"},
|
||||
{file = "argon2_cffi_bindings-26.1.0.tar.gz", hash = "sha256:63505c71542a44b68b1e38060450fb006404170da375feb31af153e7f9c6205d"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
cffi = {version = ">=1.0.1", markers = "python_version < \"3.14\""}
|
||||
|
||||
[[package]]
|
||||
name = "asyncpg"
|
||||
version = "0.31.0"
|
||||
description = "An asyncio PostgreSQL driver"
|
||||
optional = false
|
||||
python-versions = ">=3.9.0"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "asyncpg-0.31.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:831712dd3cf117eec68575a9b50da711893fd63ebe277fc155ecae1c6c9f0f61"},
|
||||
{file = "asyncpg-0.31.0-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:0b17c89312c2f4ccea222a3a6571f7df65d4ba2c0e803339bfc7bed46a96d3be"},
|
||||
{file = "asyncpg-0.31.0-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:3faa62f997db0c9add34504a68ac2c342cfee4d57a0c3062fcf0d86c7f9cb1e8"},
|
||||
{file = "asyncpg-0.31.0-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:8ea599d45c361dfbf398cb67da7fd052affa556a401482d3ff1ee99bd68808a1"},
|
||||
{file = "asyncpg-0.31.0-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:795416369c3d284e1837461909f58418ad22b305f955e625a4b3a2521d80a5f3"},
|
||||
{file = "asyncpg-0.31.0-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:a8d758dac9d2e723e173d286ef5e574f0b350ec00e9186fce84d0fc5f6a8e6b8"},
|
||||
{file = "asyncpg-0.31.0-cp310-cp310-win32.whl", hash = "sha256:2d076d42eb583601179efa246c5d7ae44614b4144bc1c7a683ad1222814ed095"},
|
||||
{file = "asyncpg-0.31.0-cp310-cp310-win_amd64.whl", hash = "sha256:9ea33213ac044171f4cac23740bed9a3805abae10e7025314cfbd725ec670540"},
|
||||
{file = "asyncpg-0.31.0-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:eee690960e8ab85063ba93af2ce128c0f52fd655fdff9fdb1a28df01329f031d"},
|
||||
{file = "asyncpg-0.31.0-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:2657204552b75f8288de08ca60faf4a99a65deef3a71d1467454123205a88fab"},
|
||||
{file = "asyncpg-0.31.0-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:a429e842a3a4b4ea240ea52d7fe3f82d5149853249306f7ff166cb9948faa46c"},
|
||||
{file = "asyncpg-0.31.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:c0807be46c32c963ae40d329b3a686356e417f674c976c07fa49f1b30303f109"},
|
||||
{file = "asyncpg-0.31.0-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:e5d5098f63beeae93512ee513d4c0c53dc12e9aa2b7a1af5a81cddf93fe4e4da"},
|
||||
{file = "asyncpg-0.31.0-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:37fc6c00a814e18eef51833545d1891cac9aa69140598bb076b4cd29b3e010b9"},
|
||||
{file = "asyncpg-0.31.0-cp311-cp311-win32.whl", hash = "sha256:5a4af56edf82a701aece93190cc4e094d2df7d33f6e915c222fb09efbb5afc24"},
|
||||
{file = "asyncpg-0.31.0-cp311-cp311-win_amd64.whl", hash = "sha256:480c4befbdf079c14c9ca43c8c5e1fe8b6296c96f1f927158d4f1e750aacc047"},
|
||||
{file = "asyncpg-0.31.0-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:b44c31e1efc1c15188ef183f287c728e2046abb1d26af4d20858215d50d91fad"},
|
||||
{file = "asyncpg-0.31.0-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:0c89ccf741c067614c9b5fc7f1fc6f3b61ab05ae4aaa966e6fd6b93097c7d20d"},
|
||||
{file = "asyncpg-0.31.0-cp312-cp312-manylinux_2_28_aarch64.whl", hash = "sha256:12b3b2e39dc5470abd5e98c8d3373e4b1d1234d9fbdedf538798b2c13c64460a"},
|
||||
{file = "asyncpg-0.31.0-cp312-cp312-manylinux_2_28_x86_64.whl", hash = "sha256:aad7a33913fb8bcb5454313377cc330fbb19a0cd5faa7272407d8a0c4257b671"},
|
||||
{file = "asyncpg-0.31.0-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:3df118d94f46d85b2e434fd62c84cb66d5834d5a890725fe625f498e72e4d5ec"},
|
||||
{file = "asyncpg-0.31.0-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:bd5b6efff3c17c3202d4b37189969acf8927438a238c6257f66be3c426beba20"},
|
||||
{file = "asyncpg-0.31.0-cp312-cp312-win32.whl", hash = "sha256:027eaa61361ec735926566f995d959ade4796f6a49d3bde17e5134b9964f9ba8"},
|
||||
{file = "asyncpg-0.31.0-cp312-cp312-win_amd64.whl", hash = "sha256:72d6bdcbc93d608a1158f17932de2321f68b1a967a13e014998db87a72ed3186"},
|
||||
{file = "asyncpg-0.31.0-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:c204fab1b91e08b0f47e90a75d1b3c62174dab21f670ad6c5d0f243a228f015b"},
|
||||
{file = "asyncpg-0.31.0-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:54a64f91839ba59008eccf7aad2e93d6e3de688d796f35803235ea1c4898ae1e"},
|
||||
{file = "asyncpg-0.31.0-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:c0e0822b1038dc7253b337b0f3f676cadc4ac31b126c5d42691c39691962e403"},
|
||||
{file = "asyncpg-0.31.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:bef056aa502ee34204c161c72ca1f3c274917596877f825968368b2c33f585f4"},
|
||||
{file = "asyncpg-0.31.0-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:0bfbcc5b7ffcd9b75ab1558f00db2ae07db9c80637ad1b2469c43df79d7a5ae2"},
|
||||
{file = "asyncpg-0.31.0-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:22bc525ebbdc24d1261ecbf6f504998244d4e3be1721784b5f64664d61fbe602"},
|
||||
{file = "asyncpg-0.31.0-cp313-cp313-win32.whl", hash = "sha256:f890de5e1e4f7e14023619399a471ce4b71f5418cd67a51853b9910fdfa73696"},
|
||||
{file = "asyncpg-0.31.0-cp313-cp313-win_amd64.whl", hash = "sha256:dc5f2fa9916f292e5c5c8b2ac2813763bcd7f58e130055b4ad8a0531314201ab"},
|
||||
{file = "asyncpg-0.31.0-cp314-cp314-macosx_10_15_x86_64.whl", hash = "sha256:f6b56b91bb0ffc328c4e3ed113136cddd9deefdf5f79ab448598b9772831df44"},
|
||||
{file = "asyncpg-0.31.0-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:334dec28cf20d7f5bb9e45b39546ddf247f8042a690bff9b9573d00086e69cb5"},
|
||||
{file = "asyncpg-0.31.0-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:98cc158c53f46de7bb677fd20c417e264fc02b36d901cc2a43bd6cb0dc6dbfd2"},
|
||||
{file = "asyncpg-0.31.0-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:9322b563e2661a52e3cdbc93eed3be7748b289f792e0011cb2720d278b366ce2"},
|
||||
{file = "asyncpg-0.31.0-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:19857a358fc811d82227449b7ca40afb46e75b33eb8897240c3839dd8b744218"},
|
||||
{file = "asyncpg-0.31.0-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:ba5f8886e850882ff2c2ace5732300e99193823e8107e2c53ef01c1ebfa1e85d"},
|
||||
{file = "asyncpg-0.31.0-cp314-cp314-win32.whl", hash = "sha256:cea3a0b2a14f95834cee29432e4ddc399b95700eb1d51bbc5bfee8f31fa07b2b"},
|
||||
{file = "asyncpg-0.31.0-cp314-cp314-win_amd64.whl", hash = "sha256:04d19392716af6b029411a0264d92093b6e5e8285ae97a39957b9a9c14ea72be"},
|
||||
{file = "asyncpg-0.31.0-cp314-cp314t-macosx_10_15_x86_64.whl", hash = "sha256:bdb957706da132e982cc6856bb2f7b740603472b54c3ebc77fe60ea3e57e1bd2"},
|
||||
{file = "asyncpg-0.31.0-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:6d11b198111a72f47154fa03b85799f9be63701e068b43f84ac25da0bda9cb31"},
|
||||
{file = "asyncpg-0.31.0-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:18c83b03bc0d1b23e6230f5bf8d4f217dc9bc08644ce0502a9d91dc9e634a9c7"},
|
||||
{file = "asyncpg-0.31.0-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:e009abc333464ff18b8f6fd146addffd9aaf63e79aa3bb40ab7a4c332d0c5e9e"},
|
||||
{file = "asyncpg-0.31.0-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:3b1fbcb0e396a5ca435a8826a87e5c2c2cc0c8c68eb6fadf82168056b0e53a8c"},
|
||||
{file = "asyncpg-0.31.0-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:8df714dba348efcc162d2adf02d213e5fab1bd9f557e1305633e851a61814a7a"},
|
||||
{file = "asyncpg-0.31.0-cp314-cp314t-win32.whl", hash = "sha256:1b41f1afb1033f2b44f3234993b15096ddc9cd71b21a42dbd87fc6a57b43d65d"},
|
||||
{file = "asyncpg-0.31.0-cp314-cp314t-win_amd64.whl", hash = "sha256:bd4107bb7cdd0e9e65fae66a62afd3a249663b844fa34d479f6d5b3bef9c04c3"},
|
||||
{file = "asyncpg-0.31.0-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:ebb3cde58321a1f89ce41812be3f2a98dddedc1e76d0838aba1d724f1e4e1a95"},
|
||||
{file = "asyncpg-0.31.0-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:e6974f36eb9a224d8fb428bcf66bd411aa12cf57c2967463178149e73d4de366"},
|
||||
{file = "asyncpg-0.31.0-cp39-cp39-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:bc2b685f400ceae428f79f78b58110470d7b4466929a7f78d455964b17ad1008"},
|
||||
{file = "asyncpg-0.31.0-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:bb223567dea5f47c45d347f2bde5486be8d9f40339f27217adb3fb1c3be51298"},
|
||||
{file = "asyncpg-0.31.0-cp39-cp39-musllinux_1_2_aarch64.whl", hash = "sha256:22be6e02381bab3101cd502d9297ac71e2f966c86e20e78caead9934c98a8af6"},
|
||||
{file = "asyncpg-0.31.0-cp39-cp39-musllinux_1_2_x86_64.whl", hash = "sha256:37a58919cfef2448a920df00d1b2f821762d17194d0dbf355d6dde8d952c04f9"},
|
||||
{file = "asyncpg-0.31.0-cp39-cp39-win32.whl", hash = "sha256:c1a9c5b71d2371a2290bc93336cd05ba4ec781683cab292adbddc084f89443c6"},
|
||||
{file = "asyncpg-0.31.0-cp39-cp39-win_amd64.whl", hash = "sha256:c1e1ab5bc65373d92dd749d7308c5b26fb2dc0fbe5d3bf68a32b676aa3bcd24a"},
|
||||
{file = "asyncpg-0.31.0.tar.gz", hash = "sha256:c989386c83940bfbd787180f2b1519415e2d3d6277a70d9d0f0145ac73500735"},
|
||||
]
|
||||
|
||||
[package.extras]
|
||||
gssauth = ["gssapi ; platform_system != \"Windows\"", "sspilib ; platform_system == \"Windows\""]
|
||||
|
||||
[[package]]
|
||||
name = "certifi"
|
||||
version = "2026.7.22"
|
||||
description = "Python package for providing Mozilla's CA Bundle."
|
||||
optional = false
|
||||
python-versions = ">=3.7"
|
||||
groups = ["dev"]
|
||||
files = [
|
||||
{file = "certifi-2026.7.22-py3-none-any.whl", hash = "sha256:62f22742b58a1a33014a2b6b706588a8d7e2a88ae7bd1a6ebe8c992928483775"},
|
||||
{file = "certifi-2026.7.22.tar.gz", hash = "sha256:741e2c3b351ddf169a738da9f2c048608ff7f2c5cc02f1ebc6b118bb090d5d55"},
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cffi"
|
||||
version = "2.1.1"
|
||||
description = "Foreign Function Interface for Python calling C code."
|
||||
optional = false
|
||||
python-versions = ">=3.10"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "cffi-2.1.1-cp310-cp310-macosx_10_15_x86_64.whl", hash = "sha256:baed1e86cc735622097354b9d1281406caf42ff42a886d29faa8e8d1630333be"},
|
||||
{file = "cffi-2.1.1-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:ca82be1a1d406ecfe1d25dc16cb33488e5a16bf4438c9fb590484ea29d92478b"},
|
||||
{file = "cffi-2.1.1-cp310-cp310-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl", hash = "sha256:42e2f76b9455f5a9a844f770bf3e200ed3da0e15f5df3db9c31fe80b04b3d004"},
|
||||
{file = "cffi-2.1.1-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:5a59cc1c4442bc3d5c703bf720b51138d0bfc173618807c9ee2490a7541dd3d9"},
|
||||
{file = "cffi-2.1.1-cp310-cp310-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:9f8d177621de5cb38ee3e731eda45d421db093ec0739f46a5594babda7987a98"},
|
||||
{file = "cffi-2.1.1-cp310-cp310-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:75f80557d1389eddbd0de2681f6a390a0c5338c31ddaa821381c203fc3fd50d9"},
|
||||
{file = "cffi-2.1.1-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:194cffa889098ced9976c3fc6340305e43f6303657d298da55366907c05c22d6"},
|
||||
{file = "cffi-2.1.1-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:5bb4e7ea95dcd6a014a6fef62e62467d67d8e582326443f3d68e71d6320a9fcf"},
|
||||
{file = "cffi-2.1.1-cp310-cp310-musllinux_1_2_i686.whl", hash = "sha256:3d22a20b1fb1632cc72c22f95f7b0d2961c3e1c235f245ba4c606c4771035659"},
|
||||
{file = "cffi-2.1.1-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:1dea0e4d7d4f11f619fe8c1d76caf49e24405b4b5743c0e3be16a500ecd930c9"},
|
||||
{file = "cffi-2.1.1-cp310-cp310-win32.whl", hash = "sha256:7ce713ace7c0e4520535b42b77eaa742c16dab813978064913e5a3cf82973b41"},
|
||||
{file = "cffi-2.1.1-cp310-cp310-win_amd64.whl", hash = "sha256:a48d62ab9d6f4f98c983223a547af44be6ca3691074c31cecced6facd3ba2dc1"},
|
||||
{file = "cffi-2.1.1-cp311-cp311-macosx_10_15_x86_64.whl", hash = "sha256:c8d2c9fd1f2d16f780d15127abb050d13d1a76c03a4bd87d7e4980e45e511e12"},
|
||||
{file = "cffi-2.1.1-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:398aff33cee2767e3e781d2554c54bd0dff386bb437581e0d8011fde1a942ec1"},
|
||||
{file = "cffi-2.1.1-cp311-cp311-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl", hash = "sha256:154852545011f779917b11c78db2358d095da62a9a172b78ad0a583ee5adc0d0"},
|
||||
{file = "cffi-2.1.1-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:3311ed60d36f83378794e1009ac6258bafbf81f7888b4caa7b35a521e3f95813"},
|
||||
{file = "cffi-2.1.1-cp311-cp311-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:6e192623c49c94421616a5778fba35cf0d5a8d000650c1967ef4448ee5cdd990"},
|
||||
{file = "cffi-2.1.1-cp311-cp311-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:a6e721d4b0e45d5b65e87534470e67b18dcd092c83f68fba09f152b9cbc061af"},
|
||||
{file = "cffi-2.1.1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:34e261f78cb6ceaaa36f42f2613f4380d94d9c759a9c73c769ee6e0247364632"},
|
||||
{file = "cffi-2.1.1-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:7225e4514edb64eb6740324353e0da0711954fd8d7da4576755b1c6e09b697cd"},
|
||||
{file = "cffi-2.1.1-cp311-cp311-musllinux_1_2_i686.whl", hash = "sha256:df913725b79db7bcf03448f36b7bf8815363417d5b58deecf9305e3e30f0f21a"},
|
||||
{file = "cffi-2.1.1-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:f5cfbc5fe74540d335175b656c725d74d90e3730c626d92575eea35029d9afaa"},
|
||||
{file = "cffi-2.1.1-cp311-cp311-win32.whl", hash = "sha256:f8ec5e643a9a937f64e1999eb9f75d072263751912dc5cd06d3c85f8f44be7c3"},
|
||||
{file = "cffi-2.1.1-cp311-cp311-win_amd64.whl", hash = "sha256:42f6930c31dc7f50732c9ae793c2786c7b6b044195967bbdde40bb9be81c4cc0"},
|
||||
{file = "cffi-2.1.1-cp311-cp311-win_arm64.whl", hash = "sha256:c7659f22557c5a0bc4855cd635f55edec690cc008a40768527762cb9fb263455"},
|
||||
{file = "cffi-2.1.1-cp312-cp312-macosx_10_15_x86_64.whl", hash = "sha256:c8c69575568085ba0b1b10c0249d779a214aea6f6522e949a0fc9fb0fcb449d0"},
|
||||
{file = "cffi-2.1.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:f81b3b8f3d4e343550fa4baa0e479bba9f2d29ce9c2e9b51d1ce1718d7442fcf"},
|
||||
{file = "cffi-2.1.1-cp312-cp312-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl", hash = "sha256:811bd1e21d32de12efca32393a0ab3f5133b54fce9bd44b8bd77ab07da14bf6a"},
|
||||
{file = "cffi-2.1.1-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:68e62fe11f30d5ca8289242866f0a5291402d8529ca2178ab8afc5c9694ae890"},
|
||||
{file = "cffi-2.1.1-cp312-cp312-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:4a7c934f7360e8cd64fe9efadcbd10c7c6364f531e432b9a4bf5ccbc9e0e8b50"},
|
||||
{file = "cffi-2.1.1-cp312-cp312-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:3143d81e29e1e20a9ce10901ec369012947876596f75a222235965f2b7ae832e"},
|
||||
{file = "cffi-2.1.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:c1453022f490d2459a11819d83ad1d586e9ff65a12ac3e705ffebd46d3685dcf"},
|
||||
{file = "cffi-2.1.1-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:208f941bb9d18e768138677f0a6d2ce01f590df56043dda1df1535ac57c88517"},
|
||||
{file = "cffi-2.1.1-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:210019b6c7cf07f081b4c54635c8cf744377001350e29cc0f81c4377b4797735"},
|
||||
{file = "cffi-2.1.1-cp312-cp312-win32.whl", hash = "sha256:046bfc24911b37851ee1b51aab8bffe713d89c68c6a057b09484ce9fd5f69b4e"},
|
||||
{file = "cffi-2.1.1-cp312-cp312-win_amd64.whl", hash = "sha256:f53e442b08449d42821fa4a4fba000095af9f62742a500f978a9f557ec44339a"},
|
||||
{file = "cffi-2.1.1-cp312-cp312-win_arm64.whl", hash = "sha256:7bde5e4cc5c10140859842b9d383af292b22639a4dffb725314baf45968cef80"},
|
||||
{file = "cffi-2.1.1-cp313-cp313-ios_13_0_arm64_iphoneos.whl", hash = "sha256:b5bdfd1c873d4e093aabc0ca84c4ca6dbc4f752afb5c86f146d9742580c9da2e"},
|
||||
{file = "cffi-2.1.1-cp313-cp313-ios_13_0_arm64_iphonesimulator.whl", hash = "sha256:31348097ff5bbe827ccc41795d4dd099d9f0625e7def00ee653c137a490c2a6c"},
|
||||
{file = "cffi-2.1.1-cp313-cp313-macosx_10_15_x86_64.whl", hash = "sha256:9d2055050ea716bd38b7f7f1579c275386646b4894c155a3e2f3cd62ed41b7c6"},
|
||||
{file = "cffi-2.1.1-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:19ee6127ee34de7d83ce3d371ebc5ed91addbdcc39f9ab15ce4eb35a4e534971"},
|
||||
{file = "cffi-2.1.1-cp313-cp313-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl", hash = "sha256:6a8dddef476fab96d066d578fc88526767b836ab5ab21754e1d5bf3879c31c7c"},
|
||||
{file = "cffi-2.1.1-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:f16c709686a78c727bbbf059f92b0bf41c6fc60deec706d2dc19f529175a6125"},
|
||||
{file = "cffi-2.1.1-cp313-cp313-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:fcd22650c908d7b7da162bbfaab594a1227a15d1643a98c68b122ac642fa2264"},
|
||||
{file = "cffi-2.1.1-cp313-cp313-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:aa9511c62d14da7aacc9b4bf51f3f697a621e83b2d6919008243c3aad168eea3"},
|
||||
{file = "cffi-2.1.1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:a931079504ecc49efed7744c476a5c343a92fabf66dec2db95edb1b2fdc770e2"},
|
||||
{file = "cffi-2.1.1-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:a2d7755bef5a12ed488f4ef1f1b69ee9191d7396083b755a5d2295f6edb4768b"},
|
||||
{file = "cffi-2.1.1-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:e0bcb7e0f677f543555d2adff3bf19c05f66cdb4796e5ff602442ab2fe3c4ef7"},
|
||||
{file = "cffi-2.1.1-cp313-cp313-win32.whl", hash = "sha256:334644fbac4eff73d985a17a91226df55d0f394160c4cfb880e084c8f7161cac"},
|
||||
{file = "cffi-2.1.1-cp313-cp313-win_amd64.whl", hash = "sha256:1aa5645c30469b09530c4ebca77ebf8f17618293c58f8549cb1a543a50236e7d"},
|
||||
{file = "cffi-2.1.1-cp313-cp313-win_arm64.whl", hash = "sha256:63bbfd5ded17c4840ac07cd8f1c21ba9d9708141f840b324f422f41b207e3973"},
|
||||
{file = "cffi-2.1.1-cp314-cp314-ios_13_0_arm64_iphoneos.whl", hash = "sha256:7dbb61fe3a7699468030f71bbe5f8a0e326a151daa91beb11a6fc1f980c55e1c"},
|
||||
{file = "cffi-2.1.1-cp314-cp314-ios_13_0_arm64_iphonesimulator.whl", hash = "sha256:f24fb43132a4c6b4cb4eb029492919b2db645be6808d738f244fd146c03c32cb"},
|
||||
{file = "cffi-2.1.1-cp314-cp314-macosx_10_15_x86_64.whl", hash = "sha256:d28630f5854ab07ab1fd4aba756de52326c82e6be15d414b12793f1975048b54"},
|
||||
{file = "cffi-2.1.1-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:661c298b4821edebead0c91edd2b00374d67ad7c5a1f7a91d4442633b79d6a72"},
|
||||
{file = "cffi-2.1.1-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:58acb8ab8e295e6c5ea12f888cbb13cf21511ef2a3303a23f4325c29d17fe5c1"},
|
||||
{file = "cffi-2.1.1-cp314-cp314-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:456a61fa52d579ebf9df2e9552ead5129855dbaff6c1e5a9b1bc408809bdc062"},
|
||||
{file = "cffi-2.1.1-cp314-cp314-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:a4f00aa42f75d6e4595e8866e748cc1705adc0cddfeb2ca86d0d03993d63ba03"},
|
||||
{file = "cffi-2.1.1-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:b0431303acaea1089ad4b3e9ce4e6518193def1118d4073ca848635ee4ea2e96"},
|
||||
{file = "cffi-2.1.1-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:64faea20f4e2613363a1a9b9c7dd73058f3ecd00133a511e72ad7c511658f527"},
|
||||
{file = "cffi-2.1.1-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:5c58fe613dc5e5336357eff555824a314d8e43282600435c8d1cb6a7a2fedd13"},
|
||||
{file = "cffi-2.1.1-cp314-cp314-win32.whl", hash = "sha256:1a18a57b58cfb21fc28d72e876acf10eaed67a1ed96226f92af4df681d571c4c"},
|
||||
{file = "cffi-2.1.1-cp314-cp314-win_amd64.whl", hash = "sha256:3222ba5d678f80a030e6afbcc33dc1ae5cb45facabb61cee2c7016b8432fde48"},
|
||||
{file = "cffi-2.1.1-cp314-cp314-win_arm64.whl", hash = "sha256:ab36d55f9ed2d067327667c2fea18dda018eb628dd6347aa01dda6cf1f5d3836"},
|
||||
{file = "cffi-2.1.1-cp314-cp314t-macosx_10_15_x86_64.whl", hash = "sha256:7750c6449dff7864bb9bb27ddfb0267756189201a3afc911d82b3caacd70dfc3"},
|
||||
{file = "cffi-2.1.1-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:0beceaabe56af686895136a2de78db54ecd8e4046b236b8fd6d6cb61389e9bf2"},
|
||||
{file = "cffi-2.1.1-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:49cbc70e6542d4ccccb936558d1064a8012541e78f821f955cff24e357776c94"},
|
||||
{file = "cffi-2.1.1-cp314-cp314t-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:e2d65b31f36619cda3999b78b2aa9632e76b78448e7a56fc4240824200e7c4fc"},
|
||||
{file = "cffi-2.1.1-cp314-cp314t-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:28907ab9bfb6aa13184cfc17c6b8e1023c5ab6fd7076d8c20a35e59fe04f8f29"},
|
||||
{file = "cffi-2.1.1-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:51b31d1c98274844cfd7838ce00bfc27c7423a4dc00fc0772fc3331c2cc90676"},
|
||||
{file = "cffi-2.1.1-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:5e7cecbaadb83884793e05828cee59b210b24583b9c7425d0ba6a754fe22eb4e"},
|
||||
{file = "cffi-2.1.1-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:25792eac27877609e7bb06d42ff88278a6624fff2ba9bbb523c09616b117e80f"},
|
||||
{file = "cffi-2.1.1-cp314-cp314t-win32.whl", hash = "sha256:8ef53b2de9bcb9197d31854256575d59dbac0cba72ac627bb291ef5eceb74be4"},
|
||||
{file = "cffi-2.1.1-cp314-cp314t-win_amd64.whl", hash = "sha256:616f097f2fe415bc92a247f02e11f634e1f9e9a83d327e3c915c15089c87869e"},
|
||||
{file = "cffi-2.1.1-cp314-cp314t-win_arm64.whl", hash = "sha256:ad2c86c495b899d862ea0f4b42891b8713a3bd45dd4105c7fd51c2a72f39f3a5"},
|
||||
{file = "cffi-2.1.1-cp315-cp315-ios_13_0_arm64_iphoneos.whl", hash = "sha256:dddad92b554513a31f272570678ba307fb9f618f05e3d4a5eacafff9eae03e1d"},
|
||||
{file = "cffi-2.1.1-cp315-cp315-ios_13_0_arm64_iphonesimulator.whl", hash = "sha256:da0e573f9f97159390c89d9f1a9e41908b66d408cc5b58d08cf3847d844c531b"},
|
||||
{file = "cffi-2.1.1-cp315-cp315-macosx_10_15_x86_64.whl", hash = "sha256:fb92203a88b3d3053034db775110081c49d28be6551923805e039924093761e4"},
|
||||
{file = "cffi-2.1.1-cp315-cp315-macosx_11_0_arm64.whl", hash = "sha256:2ae64be792b8966f2c69538199728b290e34726562896df1e5dc8ffd8d8188e8"},
|
||||
{file = "cffi-2.1.1-cp315-cp315-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:507a24c282e0f42f8ed737cf048572cbf580468da5555764a8331735e9c736b6"},
|
||||
{file = "cffi-2.1.1-cp315-cp315-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:246fa40ce8645a614ff682e0b70f37134e460eaf93a775e0cbe3cca585a67a80"},
|
||||
{file = "cffi-2.1.1-cp315-cp315-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:471cee653ae88de62096552e6d24ccb4a5adb8c8c9f10b5054d0122c15bf2779"},
|
||||
{file = "cffi-2.1.1-cp315-cp315-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:aeae0e330c9f6acd681f647d46cefd30c29f93e3392882e792e82080c9691399"},
|
||||
{file = "cffi-2.1.1-cp315-cp315-musllinux_1_2_aarch64.whl", hash = "sha256:42a494cee34437f05546455144f2b5d9ac09b1face62bcfce597d2e521066688"},
|
||||
{file = "cffi-2.1.1-cp315-cp315-musllinux_1_2_x86_64.whl", hash = "sha256:cc572dace3f60ef98d7b12ff411d20f5362feb31a0439eab0085bbfd349982d7"},
|
||||
{file = "cffi-2.1.1-cp315-cp315-win32.whl", hash = "sha256:4f42141fc14250de6dde5ee7ea4432be017252d91f19c5ad043c084cea629cac"},
|
||||
{file = "cffi-2.1.1-cp315-cp315-win_amd64.whl", hash = "sha256:e6e8cff14d6fb0be70a09c0bdc58096f501952d04624ebf867e0e56da2df8960"},
|
||||
{file = "cffi-2.1.1-cp315-cp315-win_arm64.whl", hash = "sha256:27350daa11d4f10c540e6e89dada4c54feb7256ad03e9a4dc075ebad7ba360d1"},
|
||||
{file = "cffi-2.1.1-cp315-cp315t-macosx_10_15_x86_64.whl", hash = "sha256:c26608d2222fb1e94487e4a387d85f13eb55d5ed725cb25a0c589ac4ee60e7bc"},
|
||||
{file = "cffi-2.1.1-cp315-cp315t-macosx_11_0_arm64.whl", hash = "sha256:4be96343e422f2dfcd12ab5c9f5aebe03f82f737c6bffeca6830b3875cb44aab"},
|
||||
{file = "cffi-2.1.1-cp315-cp315t-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:937c0052c05a31ca1daf18de3158eed4dbfcb9cc107adbea227728d647be701e"},
|
||||
{file = "cffi-2.1.1-cp315-cp315t-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:df423d40ee8654634421812bc3b196da3f9bd7d32929da813f8394c4348a5358"},
|
||||
{file = "cffi-2.1.1-cp315-cp315t-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:a730a083190634c65cca36ba5f489531576ebd79bcd5c8e172130f6453127231"},
|
||||
{file = "cffi-2.1.1-cp315-cp315t-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:363e05fa78e15116c3c32c210ee36884fd6b9afa6d440e47112c3bd511d64cb6"},
|
||||
{file = "cffi-2.1.1-cp315-cp315t-musllinux_1_2_aarch64.whl", hash = "sha256:770de9db11e84213beec501cfcaa013b019820ca881e03344dea5844f7876d94"},
|
||||
{file = "cffi-2.1.1-cp315-cp315t-musllinux_1_2_x86_64.whl", hash = "sha256:7da0c5eff80f0197f3b3d1232ec5a682a9325f4ae9016a78f5f5ca35f9ced1f5"},
|
||||
{file = "cffi-2.1.1-cp315-cp315t-win32.whl", hash = "sha256:06c72bb76605a4b0cd0aad6930b69d4baf7dd5d806cfc409b824191099700e66"},
|
||||
{file = "cffi-2.1.1-cp315-cp315t-win_amd64.whl", hash = "sha256:d9c275eaacd24aa73f94ffd6de08fc3f932424d8b6c376f4bed7cde376fe7bc3"},
|
||||
{file = "cffi-2.1.1-cp315-cp315t-win_arm64.whl", hash = "sha256:d18e5ac0f2f03f4f518d3e23db0f0cad7faa1da8620e9c09461d443bbf6e6692"},
|
||||
{file = "cffi-2.1.1.tar.gz", hash = "sha256:dd31f52ea1086513bb9df30f8fcee9b8918323ae067a3d5b78bc826a000712be"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
pycparser = {version = "*", markers = "implementation_name != \"PyPy\""}
|
||||
|
||||
[[package]]
|
||||
name = "click"
|
||||
version = "8.5.0"
|
||||
description = "Composable command line interface toolkit"
|
||||
optional = false
|
||||
python-versions = ">=3.10"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "click-8.5.0-py3-none-any.whl", hash = "sha256:255bc9599cf7748b4b1a446ccc735421bd08a2ae529a8b88597d3de5664ee360"},
|
||||
{file = "click-8.5.0.tar.gz", hash = "sha256:ba0d2089de75ea0310e2dde03160e6ca10009947fb95a182f9b54021bb272e34"},
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "colorama"
|
||||
version = "0.4.6"
|
||||
description = "Cross-platform colored terminal text."
|
||||
optional = false
|
||||
python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7"
|
||||
groups = ["main", "dev"]
|
||||
markers = "sys_platform == \"win32\""
|
||||
files = [
|
||||
{file = "colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"},
|
||||
{file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"},
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "envparse"
|
||||
version = "0.2.0"
|
||||
description = "Simple environment variable parsing"
|
||||
optional = false
|
||||
python-versions = "*"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "envparse-0.2.0.tar.gz", hash = "sha256:4f3b9a27bb55d27f124eb4adf006fec05e4588891c9a054a183a112645056eb7"},
|
||||
]
|
||||
|
||||
[package.extras]
|
||||
testing = ["pytest"]
|
||||
|
||||
[[package]]
|
||||
name = "fastapi"
|
||||
version = "0.142.2"
|
||||
description = "FastAPI framework, high performance, easy to learn, fast to code, ready for production"
|
||||
optional = false
|
||||
python-versions = ">=3.10"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "fastapi-0.142.2-py3-none-any.whl", hash = "sha256:bd5f4d81f1e93a88bcd77caf4dfe3c2dbffc3805407a0007e9a114c18b3a670b"},
|
||||
{file = "fastapi-0.142.2.tar.gz", hash = "sha256:06366626f2e70576367714d9ab2fe8472e6c8456dba69b399f9f797ab5e92570"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
annotated-doc = ">=0.0.2"
|
||||
opentelemetry-api = ">=1.44.0"
|
||||
pydantic = ">=2.9.0"
|
||||
starlette = ">=0.46.0"
|
||||
typing-extensions = ">=4.8.0"
|
||||
typing-inspection = ">=0.4.2"
|
||||
|
||||
[package.extras]
|
||||
all = ["email-validator (>=2.0.0)", "fastapi-cli[standard] (>=0.0.32)", "httpx (>=0.23.0,<1.0.0)", "itsdangerous (>=1.1.0)", "jinja2 (>=3.1.5)", "opentelemetry-exporter-otlp-proto-http (>=1.44.0)", "opentelemetry-sdk (>=1.44.0)", "pydantic-extra-types (>=2.0.0)", "pydantic-settings (>=2.0.0)", "python-multipart (>=0.0.18)", "pyyaml (>=5.3.1)", "uvicorn[standard] (>=0.12.0)"]
|
||||
opentelemetry = ["opentelemetry-exporter-otlp-proto-http (>=1.44.0)", "opentelemetry-sdk (>=1.44.0)"]
|
||||
standard = ["email-validator (>=2.0.0)", "fastapi-cli[standard] (>=0.0.32)", "fastar (>=0.9.0)", "httpx (>=0.23.0,<1.0.0)", "jinja2 (>=3.1.5)", "opentelemetry-exporter-otlp-proto-http (>=1.44.0)", "opentelemetry-sdk (>=1.44.0)", "pydantic-extra-types (>=2.0.0)", "pydantic-settings (>=2.0.0)", "python-multipart (>=0.0.18)", "uvicorn[standard] (>=0.12.0)"]
|
||||
standard-no-fastapi-cloud-cli = ["email-validator (>=2.0.0)", "fastapi-cli[standard-no-fastapi-cloud-cli] (>=0.0.32)", "httpx (>=0.23.0,<1.0.0)", "jinja2 (>=3.1.5)", "opentelemetry-exporter-otlp-proto-http (>=1.44.0)", "opentelemetry-sdk (>=1.44.0)", "pydantic-extra-types (>=2.0.0)", "pydantic-settings (>=2.0.0)", "python-multipart (>=0.0.18)", "uvicorn[standard] (>=0.12.0)"]
|
||||
|
||||
[[package]]
|
||||
name = "h11"
|
||||
version = "0.16.0"
|
||||
description = "A pure-Python, bring-your-own-I/O implementation of HTTP/1.1"
|
||||
optional = false
|
||||
python-versions = ">=3.8"
|
||||
groups = ["main", "dev"]
|
||||
files = [
|
||||
{file = "h11-0.16.0-py3-none-any.whl", hash = "sha256:63cf8bbe7522de3bf65932fda1d9c2772064ffb3dae62d55932da54b31cb6c86"},
|
||||
{file = "h11-0.16.0.tar.gz", hash = "sha256:4e35b956cf45792e4caa5885e69fba00bdbc6ffafbfa020300e549b208ee5ff1"},
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "httpcore"
|
||||
version = "1.0.9"
|
||||
description = "A minimal low-level HTTP client."
|
||||
optional = false
|
||||
python-versions = ">=3.8"
|
||||
groups = ["dev"]
|
||||
files = [
|
||||
{file = "httpcore-1.0.9-py3-none-any.whl", hash = "sha256:2d400746a40668fc9dec9810239072b40b4484b640a8c38fd654a024c7a1bf55"},
|
||||
{file = "httpcore-1.0.9.tar.gz", hash = "sha256:6e34463af53fd2ab5d807f399a9b45ea31c3dfa2276f15a2c3f00afff6e176e8"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
certifi = "*"
|
||||
h11 = ">=0.16"
|
||||
|
||||
[package.extras]
|
||||
asyncio = ["anyio (>=4.0,<5.0)"]
|
||||
http2 = ["h2 (>=3,<5)"]
|
||||
socks = ["socksio (==1.*)"]
|
||||
trio = ["trio (>=0.22.0,<1.0)"]
|
||||
|
||||
[[package]]
|
||||
name = "httpx"
|
||||
version = "0.28.1"
|
||||
description = "The next generation HTTP client."
|
||||
optional = false
|
||||
python-versions = ">=3.8"
|
||||
groups = ["dev"]
|
||||
files = [
|
||||
{file = "httpx-0.28.1-py3-none-any.whl", hash = "sha256:d909fcccc110f8c7faf814ca82a9a4d816bc5a6dbfea25d6591d6985b8ba59ad"},
|
||||
{file = "httpx-0.28.1.tar.gz", hash = "sha256:75e98c5f16b0f35b567856f597f06ff2270a374470a5c2392242528e3e3e42fc"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
anyio = "*"
|
||||
certifi = "*"
|
||||
httpcore = "==1.*"
|
||||
idna = "*"
|
||||
|
||||
[package.extras]
|
||||
brotli = ["brotli ; platform_python_implementation == \"CPython\"", "brotlicffi ; platform_python_implementation != \"CPython\""]
|
||||
cli = ["click (==8.*)", "pygments (==2.*)", "rich (>=10,<14)"]
|
||||
http2 = ["h2 (>=3,<5)"]
|
||||
socks = ["socksio (==1.*)"]
|
||||
zstd = ["zstandard (>=0.18.0)"]
|
||||
|
||||
[[package]]
|
||||
name = "idna"
|
||||
version = "3.20"
|
||||
description = "Internationalized Domain Names in Applications (IDNA)"
|
||||
optional = false
|
||||
python-versions = ">=3.9"
|
||||
groups = ["main", "dev"]
|
||||
files = [
|
||||
{file = "idna-3.20-py3-none-any.whl", hash = "sha256:ab7ae7122974553370f0bdb919e1a960b2cd1bc1ef0276416d896db81c14582c"},
|
||||
{file = "idna-3.20.tar.gz", hash = "sha256:a7db850025b95ded1eae8a46181a1a6c56c92c96f0e2b005d9ff8dc0210cab44"},
|
||||
]
|
||||
|
||||
[package.extras]
|
||||
all = ["coverage (>=7.10.0)", "hypothesis (>=6.141.1)", "mypy (>=1.11.2)", "pytest (>=8.3.2)", "ruff (>=0.16.0)", "ty (>=0.0.37)"]
|
||||
|
||||
[[package]]
|
||||
name = "iniconfig"
|
||||
version = "2.3.0"
|
||||
description = "brain-dead simple config-ini parsing"
|
||||
optional = false
|
||||
python-versions = ">=3.10"
|
||||
groups = ["dev"]
|
||||
files = [
|
||||
{file = "iniconfig-2.3.0-py3-none-any.whl", hash = "sha256:f631c04d2c48c52b84d0d0549c99ff3859c98df65b3101406327ecc7d53fbf12"},
|
||||
{file = "iniconfig-2.3.0.tar.gz", hash = "sha256:c76315c77db068650d49c5b56314774a7804df16fee4402c1f19d6d15d8c4730"},
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "loguru"
|
||||
version = "0.7.3"
|
||||
description = "Python logging made (stupidly) simple"
|
||||
optional = false
|
||||
python-versions = "<4.0,>=3.5"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "loguru-0.7.3-py3-none-any.whl", hash = "sha256:31a33c10c8e1e10422bfd431aeb5d351c7cf7fa671e3c4df004162264b28220c"},
|
||||
{file = "loguru-0.7.3.tar.gz", hash = "sha256:19480589e77d47b8d85b2c827ad95d49bf31b0dcde16593892eb51dd18706eb6"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
colorama = {version = ">=0.3.4", markers = "sys_platform == \"win32\""}
|
||||
win32-setctime = {version = ">=1.0.0", markers = "sys_platform == \"win32\""}
|
||||
|
||||
[package.extras]
|
||||
dev = ["Sphinx (==8.1.3) ; python_version >= \"3.11\"", "build (==1.2.2) ; python_version >= \"3.11\"", "colorama (==0.4.5) ; python_version < \"3.8\"", "colorama (==0.4.6) ; python_version >= \"3.8\"", "exceptiongroup (==1.1.3) ; python_version >= \"3.7\" and python_version < \"3.11\"", "freezegun (==1.1.0) ; python_version < \"3.8\"", "freezegun (==1.5.0) ; python_version >= \"3.8\"", "mypy (==v0.910) ; python_version < \"3.6\"", "mypy (==v0.971) ; python_version == \"3.6\"", "mypy (==v1.13.0) ; python_version >= \"3.8\"", "mypy (==v1.4.1) ; python_version == \"3.7\"", "myst-parser (==4.0.0) ; python_version >= \"3.11\"", "pre-commit (==4.0.1) ; python_version >= \"3.9\"", "pytest (==6.1.2) ; python_version < \"3.8\"", "pytest (==8.3.2) ; python_version >= \"3.8\"", "pytest-cov (==2.12.1) ; python_version < \"3.8\"", "pytest-cov (==5.0.0) ; python_version == \"3.8\"", "pytest-cov (==6.0.0) ; python_version >= \"3.9\"", "pytest-mypy-plugins (==1.9.3) ; python_version >= \"3.6\" and python_version < \"3.8\"", "pytest-mypy-plugins (==3.1.0) ; python_version >= \"3.8\"", "sphinx-rtd-theme (==3.0.2) ; python_version >= \"3.11\"", "tox (==3.27.1) ; python_version < \"3.8\"", "tox (==4.23.2) ; python_version >= \"3.8\"", "twine (==6.0.1) ; python_version >= \"3.11\""]
|
||||
|
||||
[[package]]
|
||||
name = "opentelemetry-api"
|
||||
version = "1.45.0"
|
||||
description = "OpenTelemetry Python API"
|
||||
optional = false
|
||||
python-versions = ">=3.10"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "opentelemetry_api-1.45.0-py3-none-any.whl", hash = "sha256:80e068aba7cd56c8b58512d6a36f8d25cb1dfaa0c0a4cc1c938ccf9f362d9cb3"},
|
||||
{file = "opentelemetry_api-1.45.0.tar.gz", hash = "sha256:711ede81773c8025c2c03dac0450bc89f3d30aea6eabcc815c570d4e35a963f7"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
typing-extensions = ">=4.5.0"
|
||||
|
||||
[[package]]
|
||||
name = "packaging"
|
||||
version = "26.3"
|
||||
description = "Core utilities for Python packages"
|
||||
optional = false
|
||||
python-versions = ">=3.9"
|
||||
groups = ["dev"]
|
||||
files = [
|
||||
{file = "packaging-26.3-py3-none-any.whl", hash = "sha256:d7193f7c8e4e93f444fde0262bf90af30e16fa0ad0ad44cb553c87339b23cd1c"},
|
||||
{file = "packaging-26.3.tar.gz", hash = "sha256:94edc256424af38762eb31306eed28beb9f0efc50a8837492c9d6fd6004aed79"},
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pluggy"
|
||||
version = "1.6.0"
|
||||
description = "plugin and hook calling mechanisms for python"
|
||||
optional = false
|
||||
python-versions = ">=3.9"
|
||||
groups = ["dev"]
|
||||
files = [
|
||||
{file = "pluggy-1.6.0-py3-none-any.whl", hash = "sha256:e920276dd6813095e9377c0bc5566d94c932c33b27a3e3945d8389c374dd4746"},
|
||||
{file = "pluggy-1.6.0.tar.gz", hash = "sha256:7dcc130b76258d33b90f61b658791dede3486c3e6bfb003ee5c9bfb396dd22f3"},
|
||||
]
|
||||
|
||||
[package.extras]
|
||||
dev = ["pre-commit", "tox"]
|
||||
testing = ["coverage", "pytest", "pytest-benchmark"]
|
||||
|
||||
[[package]]
|
||||
name = "pycparser"
|
||||
version = "3.0"
|
||||
description = "C parser in Python"
|
||||
optional = false
|
||||
python-versions = ">=3.10"
|
||||
groups = ["main"]
|
||||
markers = "implementation_name != \"PyPy\""
|
||||
files = [
|
||||
{file = "pycparser-3.0-py3-none-any.whl", hash = "sha256:b727414169a36b7d524c1c3e31839a521725078d7b2ff038656844266160a992"},
|
||||
{file = "pycparser-3.0.tar.gz", hash = "sha256:600f49d217304a5902ac3c37e1281c9fe94e4d0489de643a9504c5cdfdfc6b29"},
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pydantic"
|
||||
version = "2.13.5"
|
||||
description = "Data validation using Python type hints"
|
||||
optional = false
|
||||
python-versions = ">=3.9"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "pydantic-2.13.5-py3-none-any.whl", hash = "sha256:346a034f080da3755d8e9cb5e00e8b07de1d39e4f6e2c87d8ab7cafa0b269a73"},
|
||||
{file = "pydantic-2.13.5.tar.gz", hash = "sha256:51a9c5f7b2f8e636f04c6cada605d9b6a3bf1348fdf945a3d8869b19bba0ee08"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
annotated-types = ">=0.6.0"
|
||||
pydantic-core = "2.46.5"
|
||||
typing-extensions = ">=4.14.1"
|
||||
typing-inspection = ">=0.4.2"
|
||||
|
||||
[package.extras]
|
||||
email = ["email-validator (>=2.0.0)"]
|
||||
timezone = ["tzdata ; python_version >= \"3.9\" and platform_system == \"Windows\""]
|
||||
|
||||
[[package]]
|
||||
name = "pydantic-core"
|
||||
version = "2.46.5"
|
||||
description = "Core functionality for Pydantic validation and serialization"
|
||||
optional = false
|
||||
python-versions = ">=3.9"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "pydantic_core-2.46.5-cp310-cp310-macosx_10_12_x86_64.whl", hash = "sha256:657b40d6240c0a7b6a64b30f22d1e3aa631c7e846c621b0c0f6d1d75e2e15ea6"},
|
||||
{file = "pydantic_core-2.46.5-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:ecb42011e12ee19cafbc312887cbf3546959fe02fbad44f272d4be5baa997615"},
|
||||
{file = "pydantic_core-2.46.5-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4dedce55295becb61921e386b99d4f2706045306e7fa52249a33004c837379fb"},
|
||||
{file = "pydantic_core-2.46.5-cp310-cp310-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:9f47b8a949e60f027f0aa0a6f6c7b7e9c55cbf4380d10b344e282fa4e7ab1e1b"},
|
||||
{file = "pydantic_core-2.46.5-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:200aa3dc9f8d54f0754f43247c0bad0999fdcfbfd2488384dd44f37279271fe6"},
|
||||
{file = "pydantic_core-2.46.5-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:6d30e1a4f138b8951063e9a394752a9179b51da288ffa507b1e659222f4c1793"},
|
||||
{file = "pydantic_core-2.46.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:850a08d167dde16db8702c274f320c7be9d7da6f6dff2b58b18f9e815bd94f5b"},
|
||||
{file = "pydantic_core-2.46.5-cp310-cp310-manylinux_2_31_riscv64.whl", hash = "sha256:c3471e5c4a949c26ec00a77f01df59096aa9495877de76fd60a980f8ee6be461"},
|
||||
{file = "pydantic_core-2.46.5-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:3a3e26b6a8274211bddee2d0e4d0d42778f17a34510f49d2ec44b58abfc41736"},
|
||||
{file = "pydantic_core-2.46.5-cp310-cp310-musllinux_1_1_aarch64.whl", hash = "sha256:fc5d783bd4a2387e97b8a2d5ec781cfb92b3d893bf82370548e99db5915935d3"},
|
||||
{file = "pydantic_core-2.46.5-cp310-cp310-musllinux_1_1_armv7l.whl", hash = "sha256:356c8368cbc321050b169595683a2e1d63413b1e0e2868b330af9fc14c616d3f"},
|
||||
{file = "pydantic_core-2.46.5-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:eb7d8d0e5886a89a55d2eef490e272fa965a9d57c6b29a5b5088a7997ec2cad1"},
|
||||
{file = "pydantic_core-2.46.5-cp310-cp310-win32.whl", hash = "sha256:4d44cf99ddebf875f9b68cc267aa684c99b7b44fe63ee1cac4ec163807290069"},
|
||||
{file = "pydantic_core-2.46.5-cp310-cp310-win_amd64.whl", hash = "sha256:1e5aad1220a1192c42341c8fd4a8686657e73ab2a920c970bdc4de334fe3193d"},
|
||||
{file = "pydantic_core-2.46.5-cp311-cp311-macosx_10_12_x86_64.whl", hash = "sha256:a1dee1b804ff4d11c663636cf15d2ea47e9f79cd56c033fb1cbf08924842a48f"},
|
||||
{file = "pydantic_core-2.46.5-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:d625a186a65201c23a9e3b8ed9c47e90a026e03256608cc91851c6709096844f"},
|
||||
{file = "pydantic_core-2.46.5-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4f8507560a9284e1370bb048ed4282012fbef4e8d109875b95e884d228552061"},
|
||||
{file = "pydantic_core-2.46.5-cp311-cp311-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:5f93c5fe914d75fbec9a49209b00da5f08e9e467d69da2b1510c81940cfd10be"},
|
||||
{file = "pydantic_core-2.46.5-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:aca6c767f552b21b10f774aeac128e828eafb796adfa1b666a18bf6321453c3a"},
|
||||
{file = "pydantic_core-2.46.5-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:701b2e04b560eeb4bddf7a25ab8ca476176e34fdbd9a0e18196f0d12d4685f0b"},
|
||||
{file = "pydantic_core-2.46.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:49776eab08766a08dfff7012f8b422dcd7e25e43b316eedf0477c24fcfa84b7c"},
|
||||
{file = "pydantic_core-2.46.5-cp311-cp311-manylinux_2_31_riscv64.whl", hash = "sha256:a2468d93d181667a7abd66e1b64bb9f76f361b0fef8faddf687456453576f5ee"},
|
||||
{file = "pydantic_core-2.46.5-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:53feb344243bb9510a9dec7bf3cf1b64d88a98af5dc7872a5160465f8b198c8e"},
|
||||
{file = "pydantic_core-2.46.5-cp311-cp311-musllinux_1_1_aarch64.whl", hash = "sha256:cd5214352ae68f3b5e9af7768bdc5253695ee069675db3480518420b3be881f2"},
|
||||
{file = "pydantic_core-2.46.5-cp311-cp311-musllinux_1_1_armv7l.whl", hash = "sha256:9432f3598db432cb51c5b37fdbf29a60fcccc79e30d37a05022776a6bc4ab689"},
|
||||
{file = "pydantic_core-2.46.5-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:8feeac04b5794e513e710af2f9c87d49f31a6dc47967bb264a1fed61a8989bec"},
|
||||
{file = "pydantic_core-2.46.5-cp311-cp311-win32.whl", hash = "sha256:892a881d5f68c2b9ea304b7a6c2c60d9343df578a311b0f86b94bc8f1ffe8129"},
|
||||
{file = "pydantic_core-2.46.5-cp311-cp311-win_amd64.whl", hash = "sha256:40375c2d05acec10323e45dfe2077ac44bc74659008614af5069034e2cfc781c"},
|
||||
{file = "pydantic_core-2.46.5-cp311-cp311-win_arm64.whl", hash = "sha256:28a6a556cd3b6066bea827857f9d9cce027c96f776e512f544a581f9e42161f8"},
|
||||
{file = "pydantic_core-2.46.5-cp312-cp312-macosx_10_12_x86_64.whl", hash = "sha256:b9fe6fb92520e3fd61f2e49000b6911b188824f089b75973ea06d6267f0b476d"},
|
||||
{file = "pydantic_core-2.46.5-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:a39ac25a9a2fa4072efdb429833c4a4c8009a51ff9eea3eeae131713cd27991e"},
|
||||
{file = "pydantic_core-2.46.5-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4fdc8b93a41521988916eeaa271173fcca7fa0803d62f87675aac8dcec1c8e29"},
|
||||
{file = "pydantic_core-2.46.5-cp312-cp312-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:b98134087d9de723658d17a42c7d0da8d6e2ef08015dee7dc93889047315f5e4"},
|
||||
{file = "pydantic_core-2.46.5-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:e652ab17569c94bff5475520f907b7148b8c24036a8ebbe5cf7cf7493d28579a"},
|
||||
{file = "pydantic_core-2.46.5-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:d925f3d9afd05a8c0fb3a1031463a8d59ebe5e2afad297e29c78be19e13b4e62"},
|
||||
{file = "pydantic_core-2.46.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:0fc5be0abd4a407e200d844b404e33639a554e7bd0d448e7b9ae181be4789ac2"},
|
||||
{file = "pydantic_core-2.46.5-cp312-cp312-manylinux_2_31_riscv64.whl", hash = "sha256:816ff0a6550ffc06c098ccd2e0698600f9aa7da192a79eaa6f9af504a35db869"},
|
||||
{file = "pydantic_core-2.46.5-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:c7ea57fc63aa7da93a1bd2d644e6577befae10c52c4e36377635eea1056a74f5"},
|
||||
{file = "pydantic_core-2.46.5-cp312-cp312-musllinux_1_1_aarch64.whl", hash = "sha256:efd62a42486f1bda5d24cb4f63d15a3c7768375fe83d36f9417b4ad7a2fb20b3"},
|
||||
{file = "pydantic_core-2.46.5-cp312-cp312-musllinux_1_1_armv7l.whl", hash = "sha256:2bc9419666990c06d7397831f2126a1ecc3594aaa3ff7de5bf2d066802f4e07b"},
|
||||
{file = "pydantic_core-2.46.5-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:18a09e1e1011b462f2e32774f25859ef1223d5c2b0546a633cf56654710721e0"},
|
||||
{file = "pydantic_core-2.46.5-cp312-cp312-win32.whl", hash = "sha256:5cb482e9e84c851f4e623fe4acc1ced89168cf1fe18f7089db4548c8f5bbb65b"},
|
||||
{file = "pydantic_core-2.46.5-cp312-cp312-win_amd64.whl", hash = "sha256:5e81740c09e310f5aa5cbd3e434a01c154d4bef93241c7877b39f211d2b78ba8"},
|
||||
{file = "pydantic_core-2.46.5-cp312-cp312-win_arm64.whl", hash = "sha256:f7b0ec93a2893de856652154d73b7ba622f26fa97726487dcac373de5f4c6084"},
|
||||
{file = "pydantic_core-2.46.5-cp313-cp313-macosx_10_12_x86_64.whl", hash = "sha256:b7ca9034437b6022f941f4857459562ee00a560b97e7cce8a0ec5a74fc6766e0"},
|
||||
{file = "pydantic_core-2.46.5-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:f332f0e72a5a0400141f830744e141bf9f97917878dbe968669e8a7fefea78ff"},
|
||||
{file = "pydantic_core-2.46.5-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:193375f3548919d3f0b60936ca113ada3e38f264f91b9b8e0508efaad57be931"},
|
||||
{file = "pydantic_core-2.46.5-cp313-cp313-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:79bdfa52f843137045b2d081cc05c120ba6665d29b7559c2c47690906f39279f"},
|
||||
{file = "pydantic_core-2.46.5-cp313-cp313-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:24922243639cbdac66c75fcb6fd6495a9cb52b213d62f9a0d16f0310b1ff8038"},
|
||||
{file = "pydantic_core-2.46.5-cp313-cp313-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:c76fe65e607be28c7fd4d56fc3c42b1583aa058ce3408b7ad0fd540171d31f9f"},
|
||||
{file = "pydantic_core-2.46.5-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6f7b393a8b3da82f5c1fc0751e6d01ac6c55b93c18226a60bdfba4a724efafd1"},
|
||||
{file = "pydantic_core-2.46.5-cp313-cp313-manylinux_2_31_riscv64.whl", hash = "sha256:7ac031912d54f3d83ef3b3eb98dfabc1608802e2202263d25957eeed40b94761"},
|
||||
{file = "pydantic_core-2.46.5-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:837b396ca3d7b74091ca623f6cbd8351bd42d670a79c2683e79fb089f06a2de5"},
|
||||
{file = "pydantic_core-2.46.5-cp313-cp313-musllinux_1_1_aarch64.whl", hash = "sha256:5ee239d575f80b08eca11f6e20f90c4c695de7825c67eefe6091fbf20dda648e"},
|
||||
{file = "pydantic_core-2.46.5-cp313-cp313-musllinux_1_1_armv7l.whl", hash = "sha256:e80675d75ae2cd14372cb65cad5400d9347a3d3f6c13000183f22dfd027283ed"},
|
||||
{file = "pydantic_core-2.46.5-cp313-cp313-musllinux_1_1_x86_64.whl", hash = "sha256:9c4b71f10dd532fb7a5cbc8f58707779e64f03a258c2bf8bfbaecfcd9970b519"},
|
||||
{file = "pydantic_core-2.46.5-cp313-cp313-win32.whl", hash = "sha256:97bf8de4d541598c94a59344eeb988a94c08ff76b5723c41f6567ec18c7892ea"},
|
||||
{file = "pydantic_core-2.46.5-cp313-cp313-win_amd64.whl", hash = "sha256:15f4a94963c95accac15b7b657bb177d3ad82bb90b0d0526d9a9b85079925db5"},
|
||||
{file = "pydantic_core-2.46.5-cp313-cp313-win_arm64.whl", hash = "sha256:d22a945598fb91236b4dd793a6e42e4f3dd7740bb5aace5ebd7d4c08d13bb575"},
|
||||
{file = "pydantic_core-2.46.5-cp314-cp314-macosx_10_12_x86_64.whl", hash = "sha256:c1c43ad4339643d70ebb8124e1305a7dab423001eff58bb41a0f731adbc98355"},
|
||||
{file = "pydantic_core-2.46.5-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:1a353f84de772f423b5ffb11d7ae352fbbef0f446f3c0b0af0f8236d7233606e"},
|
||||
{file = "pydantic_core-2.46.5-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:5086029a57366b8cf81b130a43908738095c270c21a8d7f0e8bdfdb89718e2f3"},
|
||||
{file = "pydantic_core-2.46.5-cp314-cp314-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:46c25dda9d092a06c08db76ffe0a197107904d0dfac653f7d5306bbcd6d6119c"},
|
||||
{file = "pydantic_core-2.46.5-cp314-cp314-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:37ea7b83c935e5b0d68c9449b82651accf78a10828b2c02b2f2d9e9496446c21"},
|
||||
{file = "pydantic_core-2.46.5-cp314-cp314-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:e64e88d5585bea9ce95861079de72006c7fa6d3df4e3a3b65ba31eb979c15c9f"},
|
||||
{file = "pydantic_core-2.46.5-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:54d510bac3ee52247af28ed4bb18a1e799f040ac60fd2bf5ccd4c92f1fbe786f"},
|
||||
{file = "pydantic_core-2.46.5-cp314-cp314-manylinux_2_31_riscv64.whl", hash = "sha256:a2a5e1d0ff29adddc9f6d6821a66302e4493f8ca898b715b6b1182c2c201ea0a"},
|
||||
{file = "pydantic_core-2.46.5-cp314-cp314-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:03b9666e41e35d8909852ba191a0607520f81b74eaf12ccf8737005dbb313821"},
|
||||
{file = "pydantic_core-2.46.5-cp314-cp314-musllinux_1_1_aarch64.whl", hash = "sha256:a91c17edf6eea2402cb5457b4c89e99bc5ed1004aa34c4adf1d4258c1a5c22c2"},
|
||||
{file = "pydantic_core-2.46.5-cp314-cp314-musllinux_1_1_armv7l.whl", hash = "sha256:b49924c73a235e969511bf2aabdff3beebf9820931f646c80274d5d780010c47"},
|
||||
{file = "pydantic_core-2.46.5-cp314-cp314-musllinux_1_1_x86_64.whl", hash = "sha256:2cbd9a5eff05e51c447c34dfa4632145b26b09120cf04bd0c871e44c1a5e1c9a"},
|
||||
{file = "pydantic_core-2.46.5-cp314-cp314-win32.whl", hash = "sha256:2d5d76654becf5efd62c9e51c3756c67b49498b0c9a40884934c40807adbd074"},
|
||||
{file = "pydantic_core-2.46.5-cp314-cp314-win_amd64.whl", hash = "sha256:fa10ef4112775900e7a0661068635eb67b2ab824fbde764de6e0e21982a93db0"},
|
||||
{file = "pydantic_core-2.46.5-cp314-cp314-win_arm64.whl", hash = "sha256:045ab3b6d308439e32b81cc173bba5b9018bc6ed896afd0c65b3b009b1699af5"},
|
||||
{file = "pydantic_core-2.46.5-cp314-cp314t-macosx_10_12_x86_64.whl", hash = "sha256:8816f3d218beb4b787de5c9759c259b8fa61f9dec42dc7811f320a33771778b7"},
|
||||
{file = "pydantic_core-2.46.5-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:bce57638e08ac148e5778cce7feb968307a727d66f8e2274a543d0cf0c9ad6a3"},
|
||||
{file = "pydantic_core-2.46.5-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:976e1128455aa595ea04c79ccfedff1aaeab96ee013fcc916bed120c4f0ad94f"},
|
||||
{file = "pydantic_core-2.46.5-cp314-cp314t-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:e7b891faeedeafba41b2983e5001a81b6a915b69544c7e7570d1989ce1c36ac7"},
|
||||
{file = "pydantic_core-2.46.5-cp314-cp314t-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:5f194189415698233dd1114a093a9b56e61e2c57e11b469be3b0506f46f0771c"},
|
||||
{file = "pydantic_core-2.46.5-cp314-cp314t-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:82a36973cf8a2ef5406f4fe2edbf8ed0c99629535d959e0b100c76a32535a111"},
|
||||
{file = "pydantic_core-2.46.5-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:cdbb78909f52b981d3b2d56b97328d71eb0b974c36bd77c920123a7ebb192829"},
|
||||
{file = "pydantic_core-2.46.5-cp314-cp314t-manylinux_2_31_riscv64.whl", hash = "sha256:52e24eacdb536cade636aa90fb851835222becff8484b7001fdc78cb0290f2aa"},
|
||||
{file = "pydantic_core-2.46.5-cp314-cp314t-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:37ae34309d7bd8c0d61ab839668058f2a7962ea1fc51d105d2db228fe0618034"},
|
||||
{file = "pydantic_core-2.46.5-cp314-cp314t-musllinux_1_1_aarch64.whl", hash = "sha256:0cdbada856a1c69a7624a64d3d9aefe79300bd6ef827b43a4f265010b9b55184"},
|
||||
{file = "pydantic_core-2.46.5-cp314-cp314t-musllinux_1_1_armv7l.whl", hash = "sha256:545f26c504b27c3758439a5e6d9349931f0a04f855668d5fe323c89e82300a38"},
|
||||
{file = "pydantic_core-2.46.5-cp314-cp314t-musllinux_1_1_x86_64.whl", hash = "sha256:ff218293c9c806138dca139765e3b067621be52bcd93cdc14c7711be7ddc90a9"},
|
||||
{file = "pydantic_core-2.46.5-cp314-cp314t-win32.whl", hash = "sha256:97cf3eb53a8cccacf9d46686a0926186c9bfb5574f2ed66d3639d5fe117cd3a9"},
|
||||
{file = "pydantic_core-2.46.5-cp314-cp314t-win_amd64.whl", hash = "sha256:d2f9fc07a8042a8f95925b35c4f04f469707c981fc33245b6ca187cf5d2dd290"},
|
||||
{file = "pydantic_core-2.46.5-cp314-cp314t-win_arm64.whl", hash = "sha256:acf8a67ba51f4ca9ddbd0e6b3000a65ac51ab734661778b3e7ba64d99a710f2f"},
|
||||
{file = "pydantic_core-2.46.5-cp39-cp39-macosx_10_12_x86_64.whl", hash = "sha256:c583b927a8838dab890706a6fa7573fbb8b70e24000ef9f7238e2d6f6435a5ed"},
|
||||
{file = "pydantic_core-2.46.5-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:cdc8b74ecc48c0cb1e9607a05ec4e9e88db60a19ffcc9a1d5f9088ede40c8dc0"},
|
||||
{file = "pydantic_core-2.46.5-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:8b10e3e8fd7ddc2bd915848a2768e44c15b22936f1cc54c462ad1164deb02655"},
|
||||
{file = "pydantic_core-2.46.5-cp39-cp39-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:f077d0b97ab11fa7dcc633fca53515f290bca8a8a633e966d5b6d1879d9ed01a"},
|
||||
{file = "pydantic_core-2.46.5-cp39-cp39-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:7b0fc826b16c55e561e5d2a0c5c77b051ba1d92808118c4e4b5390f5e0cf191d"},
|
||||
{file = "pydantic_core-2.46.5-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:ef3fbbf161dc9351a2fe0422e51b129f9e97e42385bd0320b309c15f7d287dd8"},
|
||||
{file = "pydantic_core-2.46.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:978e7b97d4824b5be09c69fb70507cbde3b0323fc147332ca40a94d9a6a0ebbf"},
|
||||
{file = "pydantic_core-2.46.5-cp39-cp39-manylinux_2_31_riscv64.whl", hash = "sha256:9b68938dd5b0c783d88ff8e2dcc69451b5eb936fe212d516b21b9d5567f6d464"},
|
||||
{file = "pydantic_core-2.46.5-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:771cf63ae0b1b50dd22e5f3e3549fab5f3f4ff1635d352a9e1a97fe01c7b2e64"},
|
||||
{file = "pydantic_core-2.46.5-cp39-cp39-musllinux_1_1_aarch64.whl", hash = "sha256:7c6be839a5a8312626b32029a415644a0846b420bc8b52b95b28cd92da162168"},
|
||||
{file = "pydantic_core-2.46.5-cp39-cp39-musllinux_1_1_armv7l.whl", hash = "sha256:895395f8918627b04efb1ad2a4cf605387143300ba03304cd1dfa6d03f5e095e"},
|
||||
{file = "pydantic_core-2.46.5-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:fc8515076c11f3cfdf4fb142dcca0fe384b1230a3b5415458ac84f3e0903ec13"},
|
||||
{file = "pydantic_core-2.46.5-cp39-cp39-win32.whl", hash = "sha256:3d2652072b2d774947ba5cf78a9e59644ac62ee572daf6dd2e1dfe905e15b2b7"},
|
||||
{file = "pydantic_core-2.46.5-cp39-cp39-win_amd64.whl", hash = "sha256:3aa166e99c4f2985407fb8714aebede877ecb5455cf321b606adca926d30d5a0"},
|
||||
{file = "pydantic_core-2.46.5-graalpy311-graalpy242_311_native-macosx_10_12_x86_64.whl", hash = "sha256:c14ad3bdc85ee7f318742c457ca3968a92126d144b15721c759033bfb06296c2"},
|
||||
{file = "pydantic_core-2.46.5-graalpy311-graalpy242_311_native-macosx_11_0_arm64.whl", hash = "sha256:0bddb4020d8f04175865ccd17eff3040874fc11fb593f424edb452653b4b947c"},
|
||||
{file = "pydantic_core-2.46.5-graalpy311-graalpy242_311_native-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:2471fd51c61c610e1dcf7de44d7299283661654d11264ab4802b303368d69c47"},
|
||||
{file = "pydantic_core-2.46.5-graalpy311-graalpy242_311_native-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:b10ec717381bdbfafef34607824db4c91de69ff085e4fca3b2af91b4fa17e68a"},
|
||||
{file = "pydantic_core-2.46.5-graalpy312-graalpy250_312_native-macosx_10_12_x86_64.whl", hash = "sha256:013d6f3483d81e02e7c328831808f336c8596ee33b4bd4026b9ffb1e960b8942"},
|
||||
{file = "pydantic_core-2.46.5-graalpy312-graalpy250_312_native-macosx_11_0_arm64.whl", hash = "sha256:e9c134bb666dd54b778b9fc0d2b50cbb7f979b9e3716f26a88c9ab3b6fc1dd0f"},
|
||||
{file = "pydantic_core-2.46.5-graalpy312-graalpy250_312_native-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:347ec774390c87326a2e4929d58d3f7e8763a104d5d35f4cd595a4c952366433"},
|
||||
{file = "pydantic_core-2.46.5-graalpy312-graalpy250_312_native-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:8e24d8f05fa2d28513d94e877e9c75ad66175376209b3977f916e240e623193c"},
|
||||
{file = "pydantic_core-2.46.5-pp311-pypy311_pp73-macosx_10_12_x86_64.whl", hash = "sha256:ab4b66edffb32d9e951efb3814bd104b8367a7501b81b955cacb5726d897389f"},
|
||||
{file = "pydantic_core-2.46.5-pp311-pypy311_pp73-macosx_11_0_arm64.whl", hash = "sha256:337639ba62a11acde6ef3aeb08c8ea755f8ef1fe5e513356c0f36a2b0d7568b0"},
|
||||
{file = "pydantic_core-2.46.5-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:413a717a410d0c817ef5b786a059415550b3794e1d0c2abffd9efb93a3d9f7b4"},
|
||||
{file = "pydantic_core-2.46.5-pp311-pypy311_pp73-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:1e449def1945a462c464331254e5a44fca7c3b4f9aedf59ec2f50f8066dd8e25"},
|
||||
{file = "pydantic_core-2.46.5-pp311-pypy311_pp73-musllinux_1_1_aarch64.whl", hash = "sha256:a445486499897b88a7d6c310c88ed64dd37b1b59bfd7ae9107490bbb362f47d6"},
|
||||
{file = "pydantic_core-2.46.5-pp311-pypy311_pp73-musllinux_1_1_armv7l.whl", hash = "sha256:2d330aaba8621b1edcec8ae2c4050f63b84ccf6d98723a8f212e9684713abf0e"},
|
||||
{file = "pydantic_core-2.46.5-pp311-pypy311_pp73-musllinux_1_1_x86_64.whl", hash = "sha256:b6acfb46a814762367fb7ba0828b0a17d441b92ce249a0e007474c9072662dda"},
|
||||
{file = "pydantic_core-2.46.5-pp311-pypy311_pp73-win_amd64.whl", hash = "sha256:d0a24b40877af2de4950252be9d21eaf7fb07660f3c2cae1f56c6b599ada5266"},
|
||||
{file = "pydantic_core-2.46.5.tar.gz", hash = "sha256:10416c15b8839ecc4ef4d0885da76da6fd0f67333a0eb8aff6d93c4b8f2910fc"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
typing-extensions = ">=4.14.1"
|
||||
|
||||
[[package]]
|
||||
name = "pygments"
|
||||
version = "2.21.0"
|
||||
description = "Pygments is a syntax highlighting package written in Python."
|
||||
optional = false
|
||||
python-versions = ">=3.9"
|
||||
groups = ["dev"]
|
||||
files = [
|
||||
{file = "pygments-2.21.0-py3-none-any.whl", hash = "sha256:2363c69b61c4a97c838da3b130dcd6468f4848992b21a82f2a63ec34377137d9"},
|
||||
{file = "pygments-2.21.0.tar.gz", hash = "sha256:610ca751c9bc2492b38eb9a38a7fbc93edbbb2d7182edaf34e66ae493dee5c8c"},
|
||||
]
|
||||
|
||||
[package.extras]
|
||||
windows-terminal = ["colorama (>=0.4.6)"]
|
||||
|
||||
[[package]]
|
||||
name = "pytest"
|
||||
version = "9.1.1"
|
||||
description = "pytest: simple powerful testing with Python"
|
||||
optional = false
|
||||
python-versions = ">=3.10"
|
||||
groups = ["dev"]
|
||||
files = [
|
||||
{file = "pytest-9.1.1-py3-none-any.whl", hash = "sha256:37a86b45efb9a47a61a36449063e8e18d0cab3161329fc099eb21783169c4f0c"},
|
||||
{file = "pytest-9.1.1.tar.gz", hash = "sha256:1088fbde8f2b49d95a549a195707afa7a76a3ce9bcadc26b6d71f0ffda5fe313"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
colorama = {version = ">=0.4", markers = "sys_platform == \"win32\""}
|
||||
iniconfig = ">=1.0.1"
|
||||
packaging = ">=22"
|
||||
pluggy = ">=1.5,<2"
|
||||
pygments = ">=2.7.2"
|
||||
|
||||
[package.extras]
|
||||
dev = ["argcomplete", "attrs (>=19.2)", "hypothesis (>=3.56)", "mock", "requests", "setuptools", "xmlschema"]
|
||||
|
||||
[[package]]
|
||||
name = "redis"
|
||||
version = "6.4.0"
|
||||
description = "Python client for Redis database and key-value store"
|
||||
optional = false
|
||||
python-versions = ">=3.9"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "redis-6.4.0-py3-none-any.whl", hash = "sha256:f0544fa9604264e9464cdf4814e7d4830f74b165d52f2a330a760a88dd248b7f"},
|
||||
{file = "redis-6.4.0.tar.gz", hash = "sha256:b01bc7282b8444e28ec36b261df5375183bb47a07eb9c603f284e89cbc5ef010"},
|
||||
]
|
||||
|
||||
[package.extras]
|
||||
hiredis = ["hiredis (>=3.2.0)"]
|
||||
jwt = ["pyjwt (>=2.9.0)"]
|
||||
ocsp = ["cryptography (>=36.0.1)", "pyopenssl (>=20.0.1)", "requests (>=2.31.0)"]
|
||||
|
||||
[[package]]
|
||||
name = "starlette"
|
||||
version = "1.7.0"
|
||||
description = "The little ASGI library that shines."
|
||||
optional = false
|
||||
python-versions = ">=3.10"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "starlette-1.7.0-py3-none-any.whl", hash = "sha256:67f8e99895493dd2911a03f11314af6ceebeae4e704bb9f43dfc6a9db151c93e"},
|
||||
{file = "starlette-1.7.0.tar.gz", hash = "sha256:c79f74ea63cff761804fbbfb182f1e0b440c2d07b164d24700c5a1bab5d6ff5d"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
anyio = ">=4.0.0,<5"
|
||||
typing-extensions = {version = ">=4.10.0", markers = "python_version < \"3.13\""}
|
||||
|
||||
[package.extras]
|
||||
full = ["httpx (>=0.27.0,<0.29.0)", "httpx2 (>=2.0.0)", "itsdangerous", "jinja2", "opentelemetry-api", "python-multipart (>=0.0.18)", "pyyaml"]
|
||||
|
||||
[[package]]
|
||||
name = "typing-extensions"
|
||||
version = "4.16.0"
|
||||
description = "Backported and Experimental Type Hints for Python 3.9+"
|
||||
optional = false
|
||||
python-versions = ">=3.9"
|
||||
groups = ["main", "dev"]
|
||||
files = [
|
||||
{file = "typing_extensions-4.16.0-py3-none-any.whl", hash = "sha256:481caa481374e813c1b176ada14e97f1f67a4539ce9cfeb3f350d78d6370c2e8"},
|
||||
{file = "typing_extensions-4.16.0.tar.gz", hash = "sha256:dc983d19a509c94dba722ee6abd33940f7c05a89e243c47e907eb4db6f1a43e5"},
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "typing-inspection"
|
||||
version = "0.4.4"
|
||||
description = "Runtime typing introspection tools"
|
||||
optional = false
|
||||
python-versions = ">=3.10"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "typing_inspection-0.4.4-py3-none-any.whl", hash = "sha256:65b8397ba37ccbce054456aaccddfc91e6e3083c92824df348d96ca832f3f147"},
|
||||
{file = "typing_inspection-0.4.4.tar.gz", hash = "sha256:547274fa6b0a561ccf549cc9524b999a578e737d015d8709d021f9d0d13bea47"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
typing-extensions = ">=4.15.0"
|
||||
|
||||
[[package]]
|
||||
name = "uvicorn"
|
||||
version = "0.54.0"
|
||||
description = "The lightning-fast ASGI server."
|
||||
optional = false
|
||||
python-versions = ">=3.10"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "uvicorn-0.54.0-py3-none-any.whl", hash = "sha256:505bdb0f318731d45f1f712071fc781a8981f6847a31c902c9f5e652d4f67faf"},
|
||||
{file = "uvicorn-0.54.0.tar.gz", hash = "sha256:a2e33cbfaa0306f8e6b0c13e0cb89d7d7a2da3e62b90c66e18c33d9807b28620"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
click = ">=7.0"
|
||||
h11 = ">=0.8"
|
||||
|
||||
[package.extras]
|
||||
standard = ["httptools (>=0.8.0)", "python-dotenv (>=0.13)", "pyyaml (>=5.1)", "uvloop (>=0.15.1) ; sys_platform != \"win32\" and sys_platform != \"cygwin\" and platform_python_implementation != \"PyPy\"", "watchfiles (>=0.20)", "websockets (>=13.0)"]
|
||||
|
||||
[[package]]
|
||||
name = "win32-setctime"
|
||||
version = "1.2.0"
|
||||
description = "A small Python utility to set file creation time on Windows"
|
||||
optional = false
|
||||
python-versions = ">=3.5"
|
||||
groups = ["main"]
|
||||
markers = "sys_platform == \"win32\""
|
||||
files = [
|
||||
{file = "win32_setctime-1.2.0-py3-none-any.whl", hash = "sha256:95d644c4e708aba81dc3704a116d8cbc974d70b3bdb8be1d150e36be6e9d1390"},
|
||||
{file = "win32_setctime-1.2.0.tar.gz", hash = "sha256:ae1fdf948f5640aae05c511ade119313fb6a30d7eabe25fef9764dca5873c4c0"},
|
||||
]
|
||||
|
||||
[package.extras]
|
||||
dev = ["black (>=19.3b0) ; python_version >= \"3.6\"", "pytest (>=4.6.2)"]
|
||||
|
||||
[metadata]
|
||||
lock-version = "2.1"
|
||||
python-versions = ">=3.12,<3.13"
|
||||
content-hash = "69e25ec2435541d7cdd8ffcea503313332dda8958ff07fcbe81fe125f1521346"
|
||||
@@ -0,0 +1,29 @@
|
||||
[tool.poetry]
|
||||
name = "max2"
|
||||
version = "1.2.0"
|
||||
description = "Мессенджер на PostgreSQL и Valkey, лабораторная 3"
|
||||
authors = ["Саватеев Артём"]
|
||||
package-mode = false
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = ">=3.12,<3.13"
|
||||
fastapi = "0.142.2"
|
||||
uvicorn = "0.54.0"
|
||||
pydantic = "2.13.5"
|
||||
envparse = "0.2.0"
|
||||
loguru = "0.7.3"
|
||||
asyncpg = "0.31.0"
|
||||
redis = "6.4.0"
|
||||
argon2-cffi = "25.1.0"
|
||||
|
||||
[tool.poetry.group.dev.dependencies]
|
||||
pytest = "9.1.1"
|
||||
httpx = "0.28.1"
|
||||
|
||||
[build-system]
|
||||
requires = ["poetry-core>=2.0.0,<3.0.0"]
|
||||
build-backend = "poetry.core.masonry.api"
|
||||
|
||||
[tool.pytest.ini_options]
|
||||
testpaths = ["student-tests"]
|
||||
addopts = "-v"
|
||||
Executable
+58
@@ -0,0 +1,58 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# Миграции с нуля и повторно, отказ API на непримененной схеме: отдельный
|
||||
# compose-проект с собственным пустым volume. Рабочий стенд и его данные
|
||||
# не затрагиваются; временный volume этого проекта удаляется в конце.
|
||||
cd "$(dirname "$0")/.."
|
||||
PROJECT=messenger-lab3-migrations-check
|
||||
# Публикуется только порт LB: свои порты, чтобы не конфликтовать с основным стендом
|
||||
export LB_PORT="${MIGRATIONS_CHECK_LB_PORT:-58080}"
|
||||
export LB_EXPIRY_PORT="${MIGRATIONS_CHECK_LB_EXPIRY_PORT:-58081}"
|
||||
COMPOSE="docker compose --env-file ${ENV_FILE:-.env} -p $PROJECT"
|
||||
trap '$COMPOSE down -v --remove-orphans >/dev/null 2>&1' EXIT
|
||||
|
||||
$COMPOSE up -d --wait postgres >/dev/null 2>&1
|
||||
# Тот же образ и сеть, что у сервиса migrate; apply_migrations трижды подряд
|
||||
$COMPOSE run --rm --no-deps -T migrate python - <<'EOF'
|
||||
import asyncio
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, "src")
|
||||
|
||||
from infrastructure.dependencies.database import database_pool_manager as db
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
await db.create_pool(30)
|
||||
async with db.acquire() as conn:
|
||||
tables = await conn.fetchval(
|
||||
"SELECT count(*) FROM pg_tables WHERE schemaname = 'public';"
|
||||
)
|
||||
print(f"пустой volume: таблиц в public = {tables}")
|
||||
# Так стартует API-реплика: без migrate она отказывается работать
|
||||
try:
|
||||
await db.check_migrations()
|
||||
except RuntimeError as error:
|
||||
print(f"API до migrate: {error}")
|
||||
else:
|
||||
raise SystemExit("FAIL: API стартовал бы на пустой схеме")
|
||||
for attempt in (1, 2, 3):
|
||||
await db.apply_migrations()
|
||||
async with db.acquire() as conn:
|
||||
versions = await conn.fetch(
|
||||
"SELECT version, name FROM schema_migrations ORDER BY version;"
|
||||
)
|
||||
tables = await conn.fetchval(
|
||||
"SELECT count(*) FROM pg_tables WHERE schemaname = 'public';"
|
||||
)
|
||||
print(
|
||||
f"запуск {attempt}: schema_migrations = "
|
||||
f"{[(r['version'], r['name']) for r in versions]}, таблиц = {tables}"
|
||||
)
|
||||
await db.check_migrations()
|
||||
print("API после migrate: схема совместима")
|
||||
await db.close()
|
||||
|
||||
|
||||
asyncio.run(main())
|
||||
EOF
|
||||
Executable
+89
@@ -0,0 +1,89 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# Совместимость текущей схемы с предыдущей версией API — условие rolling update:
|
||||
# миграции применяются до замены реплик, и старые реплики продолжают работать.
|
||||
# Образ собирается из коммита PREVIOUS_REVISION (по умолчанию lab2-код aa971fb),
|
||||
# запускается одним процессом в сети backend рабочего стенда (наружу порт
|
||||
# не публикуется) и выполняет вход, создание чата, запись и чтение сообщения.
|
||||
# Создаются только синтетические данные; volumes не затрагиваются.
|
||||
cd "$(dirname "$0")/.."
|
||||
ENV_FILE="${ENV_FILE:-.env}"
|
||||
REVISION="${PREVIOUS_REVISION:-aa971fb}"
|
||||
IMAGE="mtusi-messenger:compat-$REVISION"
|
||||
NETWORK=messenger-lab3_backend
|
||||
|
||||
git archive "$REVISION" | docker build --quiet --tag "$IMAGE" - >/dev/null
|
||||
echo "previous version image: $IMAGE ($(git log -1 --format='%h %s' "$REVISION"))"
|
||||
echo "schema on stand: $(docker compose --env-file "$ENV_FILE" exec -T postgres sh -c \
|
||||
'psql -X -q -tA -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "SELECT string_agg(name, \$\$, \$\$ ORDER BY version) FROM schema_migrations;"')"
|
||||
|
||||
docker run --rm -i --network "$NETWORK" --env-file "$ENV_FILE" \
|
||||
-e DB_HOST=postgres -e DB_PORT=5432 -e VALKEY_HOST=valkey -e VALKEY_PORT=6379 \
|
||||
"$IMAGE" sh -c 'python src/gateway.py >/tmp/api.log 2>&1 & exec python -' <<'EOF'
|
||||
import base64
|
||||
import json
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import uuid
|
||||
|
||||
BASE = "http://127.0.0.1:8080"
|
||||
|
||||
|
||||
def call(method, path, body=None, headers=None):
|
||||
data = None if body is None else json.dumps(body).encode()
|
||||
request = urllib.request.Request(BASE + path, data=data, method=method)
|
||||
request.add_header("Content-Type", "application/json")
|
||||
for name, value in (headers or {}).items():
|
||||
request.add_header(name, value)
|
||||
try:
|
||||
with urllib.request.urlopen(request, timeout=5) as response:
|
||||
return response.status, json.loads(response.read() or b"null")
|
||||
except urllib.error.HTTPError as error:
|
||||
return error.code, None
|
||||
|
||||
|
||||
for _ in range(60):
|
||||
try:
|
||||
if call("GET", "/health/ready")[0] == 200:
|
||||
break
|
||||
except OSError:
|
||||
pass
|
||||
time.sleep(0.5)
|
||||
else:
|
||||
print(open("/tmp/api.log").read()[-2000:])
|
||||
raise SystemExit("FAIL: previous version did not become ready on current schema")
|
||||
|
||||
username = "compat_" + uuid.uuid4().hex[:12]
|
||||
password = "Compat-" + uuid.uuid4().hex
|
||||
status_user, user = call(
|
||||
"POST", "/api/v1/users",
|
||||
{"username": username, "display_name": "Compat", "password": password},
|
||||
)
|
||||
credentials = base64.b64encode(f"{username}:{password}".encode()).decode()
|
||||
status_login, session = call(
|
||||
"POST", "/api/v1/auth/sessions", headers={"Authorization": "Basic " + credentials}
|
||||
)
|
||||
bearer = {"Authorization": "Bearer " + session["token"]}
|
||||
status_chat, chat = call(
|
||||
"POST", "/api/v1/chats", {"title": "compat", "member_ids": [user["id"]]}, bearer
|
||||
)
|
||||
path = f"/api/v1/chats/{chat['id']}/messages"
|
||||
status_send, message = call("POST", path, {"text": "старая версия пишет"}, bearer)
|
||||
status_read, page = call("GET", path, headers=bearer)
|
||||
result = {
|
||||
"ready": True,
|
||||
"register": status_user,
|
||||
"login": status_login,
|
||||
"create_chat": status_chat,
|
||||
"send_message_without_key": status_send,
|
||||
"read_history": status_read,
|
||||
"history_matches": page["items"] == [message],
|
||||
}
|
||||
print(json.dumps(result, ensure_ascii=False))
|
||||
log = open("/tmp/api.log").read()
|
||||
print("startup log:", [line.split(" - ", 1)[-1] for line in log.splitlines() if "миграц" in line.lower() or "Схема" in line])
|
||||
if any(code >= 400 for code in result.values() if isinstance(code, int)) or not result["history_matches"]:
|
||||
raise SystemExit("FAIL: previous version is not compatible with current schema")
|
||||
print("PASS: previous API version works on the current schema (expand-only migration)")
|
||||
EOF
|
||||
@@ -0,0 +1,480 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Нагрузка через LB во время отказа/замены API-реплик; метрики и проверка данных.
|
||||
|
||||
failover — для каждой API-реплики по очереди: нагрузка → остановка этой реплики
|
||||
(docker stop = SIGTERM, затем docker kill = SIGKILL) → восстановление
|
||||
(scripts/restore.sh) → нагрузка после восстановления.
|
||||
rolling — нагрузка во время scripts/rolling-update.sh (поочерёдная замена реплик).
|
||||
|
||||
Клиент ведёт себя как настоящий: неподтверждённый POST повторяет с тем же
|
||||
Idempotency-Key и телом. После прогона вся история читается заново: каждое
|
||||
подтверждённое сообщение на месте и не изменено, дублей нет.
|
||||
Python 3.9+, стандартная библиотека; tokens не печатаются и не сохраняются.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import uuid
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from urllib.parse import quote
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "tests"))
|
||||
|
||||
from client import Client # noqa: E402
|
||||
|
||||
REQUEST_TIMEOUT_SEC = 5
|
||||
RETRY_PAUSE_SEC = 0.05
|
||||
STATS_FORMAT = "{{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}"
|
||||
|
||||
|
||||
@dataclass
|
||||
class Attempt:
|
||||
"""Одна HTTP-попытка клиента; время — секунды от начала прогона"""
|
||||
|
||||
op: str
|
||||
started: float
|
||||
finished: float
|
||||
status: int | None
|
||||
error: str | None
|
||||
instance: str | None
|
||||
retry: bool
|
||||
|
||||
@property
|
||||
def ok(self) -> bool:
|
||||
return self.status in (200, 201)
|
||||
|
||||
|
||||
class Recorder:
|
||||
def __init__(self) -> None:
|
||||
self.started = time.monotonic()
|
||||
self.lock = threading.Lock()
|
||||
self.attempts: list[Attempt] = []
|
||||
self.confirmed: list[dict] = []
|
||||
self.events: dict[str, float] = {}
|
||||
|
||||
def now(self) -> float:
|
||||
return time.monotonic() - self.started
|
||||
|
||||
def mark(self, name: str) -> None:
|
||||
self.events[name] = round(self.now(), 3)
|
||||
|
||||
def add(self, attempt: Attempt, message: dict | None = None) -> None:
|
||||
with self.lock:
|
||||
self.attempts.append(attempt)
|
||||
if message is not None:
|
||||
self.confirmed.append(message)
|
||||
|
||||
|
||||
def request(
|
||||
base: str, method: str, path: str, token: str, body: dict | None, key: str | None
|
||||
) -> tuple[int | None, str | None, str | None, dict | None]:
|
||||
headers = {"Authorization": "Bearer " + token, "Accept": "application/json"}
|
||||
data = None
|
||||
if body is not None:
|
||||
data = json.dumps(body, ensure_ascii=False).encode()
|
||||
headers["Content-Type"] = "application/json"
|
||||
if key is not None:
|
||||
headers["Idempotency-Key"] = key
|
||||
req = urllib.request.Request(base + path, data=data, headers=headers, method=method)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=REQUEST_TIMEOUT_SEC) as response:
|
||||
payload = json.loads(response.read())
|
||||
return response.status, None, response.headers.get("X-Instance-Id"), payload
|
||||
except urllib.error.HTTPError as error:
|
||||
error.read()
|
||||
return error.code, None, error.headers.get("X-Instance-Id"), None
|
||||
except OSError as error:
|
||||
return None, type(error).__name__, None, None
|
||||
|
||||
|
||||
def worker(
|
||||
base: str,
|
||||
token: str,
|
||||
chat_id: str,
|
||||
worker_id: int,
|
||||
recorder: Recorder,
|
||||
stop: threading.Event,
|
||||
) -> None:
|
||||
"""Запись с повтором по тому же ключу до подтверждения, затем чтение"""
|
||||
path = f"/api/v1/chats/{chat_id}/messages"
|
||||
index = 0
|
||||
while not stop.is_set():
|
||||
key = uuid.uuid4().hex
|
||||
body = {"text": f"exp-{worker_id}-{index}-{key[:8]}"}
|
||||
retry = False
|
||||
while not stop.is_set():
|
||||
started = recorder.now()
|
||||
status, error, instance, payload = request(
|
||||
base, "POST", path, token, body, key
|
||||
)
|
||||
attempt = Attempt(
|
||||
"write", started, recorder.now(), status, error, instance, retry
|
||||
)
|
||||
recorder.add(attempt, payload if status == 201 else None)
|
||||
if status == 201:
|
||||
break
|
||||
retry = True
|
||||
time.sleep(RETRY_PAUSE_SEC)
|
||||
started = recorder.now()
|
||||
status, error, instance, _ = request(
|
||||
base, "GET", path + "?limit=1", token, None, None
|
||||
)
|
||||
recorder.add(
|
||||
Attempt("read", started, recorder.now(), status, error, instance, False)
|
||||
)
|
||||
index += 1
|
||||
|
||||
|
||||
def percentile(values: list[float], p: float) -> float | None:
|
||||
if not values:
|
||||
return None
|
||||
ordered = sorted(values)
|
||||
return round(1000 * ordered[max(0, math.ceil(p * len(ordered)) - 1)], 1)
|
||||
|
||||
|
||||
def phase_stats(attempts: list[Attempt], start: float, end: float) -> dict:
|
||||
inside = [a for a in attempts if start <= a.finished < end]
|
||||
duration = max(end - start, 1e-9)
|
||||
result: dict = {"seconds": round(duration, 3)}
|
||||
for op in ("write", "read"):
|
||||
ops = [a for a in inside if a.op == op]
|
||||
ok = [a for a in ops if a.ok]
|
||||
result[op] = {
|
||||
"ok": len(ok),
|
||||
"errors": len(ops) - len(ok),
|
||||
"ok_rps": round(len(ok) / duration, 1),
|
||||
"latency_ms": {
|
||||
"p50": percentile([a.finished - a.started for a in ok], 0.50),
|
||||
"p95": percentile([a.finished - a.started for a in ok], 0.95),
|
||||
"p99": percentile([a.finished - a.started for a in ok], 0.99),
|
||||
},
|
||||
}
|
||||
return result
|
||||
|
||||
|
||||
def recovery_stats(attempts: list[Attempt], action_start: float, end: float) -> dict:
|
||||
"""
|
||||
Время восстановления записи: от первой ошибки после воздействия до первой
|
||||
успешной записи после последней ошибки; без ошибок — 0. Отдельно —
|
||||
наибольший разрыв между подтверждениями записи (задержка без ошибок).
|
||||
"""
|
||||
window = sorted(
|
||||
(a for a in attempts if a.op == "write" and a.finished >= action_start - 1),
|
||||
key=lambda a: a.finished,
|
||||
)
|
||||
errors = [a for a in window if not a.ok and a.finished >= action_start]
|
||||
kinds: dict[str, int] = {}
|
||||
for a in errors:
|
||||
kind = a.error or f"HTTP {a.status}"
|
||||
kinds[kind] = kinds.get(kind, 0) + 1
|
||||
if errors:
|
||||
last_error = errors[-1].finished
|
||||
first_ok_after = next(
|
||||
(a.finished for a in window if a.ok and a.finished > last_error), end
|
||||
)
|
||||
recovery = first_ok_after - errors[0].finished
|
||||
else:
|
||||
recovery = 0.0
|
||||
oks = [a.finished for a in window if a.ok and a.finished <= end]
|
||||
gap = max((b - a for a, b in zip(oks, oks[1:])), default=None)
|
||||
return {
|
||||
"write_errors": len(errors),
|
||||
"error_kinds": kinds,
|
||||
"write_recovery_seconds": round(recovery, 3),
|
||||
"max_write_gap_seconds": None if gap is None else round(gap, 3),
|
||||
}
|
||||
|
||||
|
||||
def timeline(attempts: list[Attempt], end: float) -> list[dict]:
|
||||
rows = []
|
||||
for second in range(int(end) + 1):
|
||||
inside = [a for a in attempts if second <= a.finished < second + 1]
|
||||
instances: dict[str, int] = {}
|
||||
for a in inside:
|
||||
if a.ok and a.instance:
|
||||
instances[a.instance] = instances.get(a.instance, 0) + 1
|
||||
rows.append(
|
||||
{
|
||||
"t": second,
|
||||
"ok": sum(a.ok for a in inside),
|
||||
"errors": sum(not a.ok for a in inside),
|
||||
"instances": instances,
|
||||
}
|
||||
)
|
||||
return rows
|
||||
|
||||
|
||||
def read_history(client: Client, actor: dict, chat_id: str) -> list[dict]:
|
||||
path = f"/api/v1/chats/{chat_id}/messages?limit=100"
|
||||
items: list[dict] = []
|
||||
while True:
|
||||
page = client.call("GET", path, actor=actor, schema="MessagePage")["json"]
|
||||
items.extend(page["items"])
|
||||
if page["next_cursor"] is None:
|
||||
return items
|
||||
path = f"/api/v1/chats/{chat_id}/messages?limit=100&cursor=" + quote(
|
||||
page["next_cursor"], safe=""
|
||||
)
|
||||
|
||||
|
||||
def integrity(client: Client, actor: dict, chat_id: str, recorder: Recorder) -> dict:
|
||||
history = read_history(client, actor, chat_id)
|
||||
by_id = {m["id"]: m for m in history}
|
||||
texts = [m["text"] for m in history]
|
||||
confirmed = {m["id"]: m for m in recorder.confirmed}
|
||||
return {
|
||||
"messages_in_history": len(history),
|
||||
"confirmed_unique": len(confirmed),
|
||||
"confirmed_missing_or_changed": sum(
|
||||
by_id.get(i) != m for i, m in confirmed.items()
|
||||
),
|
||||
"duplicate_ids": len(history) - len(by_id),
|
||||
"duplicate_texts": len(texts) - len(set(texts)),
|
||||
"retried_writes_confirmed_as_replay": len(recorder.confirmed) - len(confirmed),
|
||||
}
|
||||
|
||||
|
||||
def run(command: list[str], env: dict | None = None) -> str:
|
||||
result = subprocess.run(
|
||||
command, cwd=ROOT, env=env, check=True, capture_output=True, text=True
|
||||
)
|
||||
return result.stdout.strip()
|
||||
|
||||
|
||||
def api_containers() -> list[str]:
|
||||
ids = run(["docker", "compose", "--env-file", ".env", "ps", "-q", "api"]).split()
|
||||
names = [run(["docker", "inspect", "-f", "{{.Name}}", i]).lstrip("/") for i in ids]
|
||||
return sorted(names)
|
||||
|
||||
|
||||
def instance_of(container: str) -> str:
|
||||
return run(["docker", "exec", container, "cat", "/tmp/instance-id"])
|
||||
|
||||
|
||||
def container_stats() -> list[str]:
|
||||
names = ["messenger-lab3-nginx-1", "messenger-lab3-postgres-1", *api_containers()]
|
||||
output = run(["docker", "stats", "--no-stream", "--format", STATS_FORMAT, *names])
|
||||
return output.splitlines()
|
||||
|
||||
|
||||
def scenario(
|
||||
args: argparse.Namespace,
|
||||
label: str,
|
||||
action: list[str],
|
||||
restore: list[str] | None,
|
||||
target: str | None = None,
|
||||
env: dict | None = None,
|
||||
) -> dict:
|
||||
client = Client(args.base_url, timeout=REQUEST_TIMEOUT_SEC)
|
||||
alice, _, _, chat = client.bootstrap()
|
||||
base = client.base
|
||||
recorder = Recorder()
|
||||
stop = threading.Event()
|
||||
threads = [
|
||||
threading.Thread(
|
||||
target=worker,
|
||||
args=(base, alice["token"], chat["id"], n, recorder, stop),
|
||||
daemon=True,
|
||||
)
|
||||
for n in range(args.workers)
|
||||
]
|
||||
for thread in threads:
|
||||
thread.start()
|
||||
time.sleep(args.before / 2)
|
||||
stats = container_stats()
|
||||
time.sleep(args.before / 2)
|
||||
|
||||
instances_before = sorted(
|
||||
{a.instance for a in recorder.attempts if a.ok and a.instance}
|
||||
)
|
||||
recorder.mark("action_start")
|
||||
action_output = run(action, env)
|
||||
recorder.mark("action_end")
|
||||
restore_output = ""
|
||||
if restore is not None:
|
||||
time.sleep(args.outage)
|
||||
recorder.mark("restore_start")
|
||||
restore_output = run(restore)
|
||||
recorder.mark("restore_end")
|
||||
time.sleep(args.after)
|
||||
stop.set()
|
||||
for thread in threads:
|
||||
thread.join(timeout=REQUEST_TIMEOUT_SEC + 1)
|
||||
recorder.mark("end")
|
||||
|
||||
events = recorder.events
|
||||
end = events["end"]
|
||||
after_start = events.get("restore_end", events["action_end"])
|
||||
attempts = recorder.attempts
|
||||
seen_after = [
|
||||
a for a in attempts if a.ok and a.finished >= after_start and a.instance
|
||||
]
|
||||
back_in_rotation = next(
|
||||
(
|
||||
round(a.finished - after_start, 3)
|
||||
for a in sorted(seen_after, key=lambda a: a.finished)
|
||||
if a.instance not in instances_before
|
||||
),
|
||||
None,
|
||||
)
|
||||
served = sorted(
|
||||
(a for a in attempts if a.ok and a.instance), key=lambda a: a.finished
|
||||
)
|
||||
instances_seen: dict[str, list[float]] = {}
|
||||
for a in served:
|
||||
offset = round(a.finished - events["action_start"], 3)
|
||||
instances_seen.setdefault(a.instance, [offset, offset])[1] = offset
|
||||
phases = {
|
||||
"before": phase_stats(attempts, 0, events["action_start"]),
|
||||
"during": phase_stats(attempts, events["action_start"], after_start),
|
||||
"after": phase_stats(attempts, after_start, end),
|
||||
}
|
||||
target_after_stop = None
|
||||
if target is not None:
|
||||
target_after_stop = sum(
|
||||
1
|
||||
for a in attempts
|
||||
if a.ok
|
||||
and a.instance == target
|
||||
and a.finished > events["action_end"]
|
||||
and a.finished < events.get("restore_start", end)
|
||||
)
|
||||
return {
|
||||
"scenario": label,
|
||||
"target_instance": target,
|
||||
"workers": args.workers,
|
||||
"events_seconds": events,
|
||||
"instances_before": instances_before,
|
||||
"instances_after": sorted({a.instance for a in seen_after}),
|
||||
"target_responses_while_stopped": target_after_stop,
|
||||
"restored_replica_in_rotation_seconds": back_in_rotation
|
||||
if restore is not None
|
||||
else None,
|
||||
# alias → [первый, последний] успешный ответ, секунды от начала воздействия
|
||||
"instances_seen_from_action": instances_seen,
|
||||
"phases": phases,
|
||||
"recovery": recovery_stats(attempts, events["action_start"], end),
|
||||
"integrity": integrity(client, alice, chat["id"], recorder),
|
||||
"container_stats_before_action": stats,
|
||||
"action_output": action_output.splitlines(),
|
||||
"restore_output_tail": restore_output.splitlines()[-2:],
|
||||
"timeline": timeline(attempts, end),
|
||||
}
|
||||
|
||||
|
||||
def failover(args: argparse.Namespace) -> list[dict]:
|
||||
results = []
|
||||
for signal in args.signals:
|
||||
for container in api_containers():
|
||||
target = instance_of(container)
|
||||
if signal == "SIGTERM":
|
||||
action = ["docker", "stop", "--timeout", "10", container]
|
||||
else:
|
||||
action = ["docker", "kill", "--signal", "SIGKILL", container]
|
||||
label = f"{signal} {container}"
|
||||
print(f"== {label} (instance {target})", flush=True)
|
||||
result = scenario(
|
||||
args, label, action, ["./scripts/restore.sh"], target=target
|
||||
)
|
||||
print(json.dumps(brief(result), ensure_ascii=False), flush=True)
|
||||
results.append(result)
|
||||
return results
|
||||
|
||||
|
||||
def rolling(args: argparse.Namespace) -> list[dict]:
|
||||
env = dict(os.environ)
|
||||
env.setdefault("APP_REVISION", f"exp-{int(time.time())}")
|
||||
print(f"== rolling update to revision {env['APP_REVISION']}", flush=True)
|
||||
result = scenario(
|
||||
args, "rolling update", ["./scripts/rolling-update.sh"], None, env=env
|
||||
)
|
||||
result["revisions_after"] = run(["./scripts/instances.sh"]).splitlines()
|
||||
print(json.dumps(brief(result), ensure_ascii=False), flush=True)
|
||||
return [result]
|
||||
|
||||
|
||||
def brief(result: dict) -> dict:
|
||||
"""Краткая строка прогона для терминала; полная версия — в --output"""
|
||||
return {
|
||||
"scenario": result["scenario"],
|
||||
"rps_ok_write": {k: v["write"]["ok_rps"] for k, v in result["phases"].items()},
|
||||
"rps_ok_read": {k: v["read"]["ok_rps"] for k, v in result["phases"].items()},
|
||||
"errors": {
|
||||
k: v["write"]["errors"] + v["read"]["errors"]
|
||||
for k, v in result["phases"].items()
|
||||
},
|
||||
"recovery": result["recovery"],
|
||||
"target_responses_while_stopped": result["target_responses_while_stopped"],
|
||||
"restored_replica_in_rotation_seconds": result[
|
||||
"restored_replica_in_rotation_seconds"
|
||||
],
|
||||
"instances_seen_from_action": result["instances_seen_from_action"],
|
||||
"integrity": result["integrity"],
|
||||
}
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("mode", choices=["failover", "rolling"])
|
||||
parser.add_argument("--base-url", default=None)
|
||||
parser.add_argument("--workers", type=int, default=4)
|
||||
parser.add_argument("--before", type=float, default=8, help="нагрузка до отказа")
|
||||
parser.add_argument("--outage", type=float, default=10, help="реплика остановлена")
|
||||
parser.add_argument("--after", type=float, default=8, help="после восстановления")
|
||||
parser.add_argument(
|
||||
"--signals",
|
||||
nargs="+",
|
||||
default=["SIGTERM", "SIGKILL"],
|
||||
choices=["SIGTERM", "SIGKILL"],
|
||||
)
|
||||
parser.add_argument("--output", default=None, help="JSON с полными результатами")
|
||||
args = parser.parse_args()
|
||||
started = time.strftime("%Y-%m-%dT%H:%M:%S%z")
|
||||
results = failover(args) if args.mode == "failover" else rolling(args)
|
||||
report = {
|
||||
"mode": args.mode,
|
||||
"started": started,
|
||||
"profile": {
|
||||
"workers": args.workers,
|
||||
"loop": "closed: POST message (retry same key until 201) + GET limit=1",
|
||||
"client_timeout_seconds": REQUEST_TIMEOUT_SEC,
|
||||
"phases_seconds": {
|
||||
"before": args.before,
|
||||
"outage": args.outage,
|
||||
"after": args.after,
|
||||
},
|
||||
},
|
||||
"runs": results,
|
||||
}
|
||||
if args.output:
|
||||
destination = ROOT / args.output
|
||||
destination.parent.mkdir(parents=True, exist_ok=True)
|
||||
destination.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n")
|
||||
print(f"saved: {args.output}")
|
||||
failed = [
|
||||
r["scenario"]
|
||||
for r in results
|
||||
if r["integrity"]["confirmed_missing_or_changed"]
|
||||
or r["integrity"]["duplicate_ids"]
|
||||
or r["integrity"]["duplicate_texts"]
|
||||
or r["target_responses_while_stopped"]
|
||||
]
|
||||
if failed:
|
||||
raise SystemExit(f"FAIL: потеря/дубль или отказ не изолирован: {failed}")
|
||||
print(f"PASS: прогонов {len(results)}, подтверждённые сообщения целы, дублей нет")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,111 @@
|
||||
-- Обоснование индекса истории: EXPLAIN (ANALYZE, BUFFERS) на ≥10 000 сообщений.
|
||||
-- Запуск: make explain → evidence/explain_history.txt
|
||||
-- Сид синтетический и идемпотентный: повторный запуск данных не дублирует.
|
||||
-- Пользователь explain_seed не может войти: password_hash не является hash Argon2.
|
||||
|
||||
\pset pager off
|
||||
\timing off
|
||||
|
||||
DO $$
|
||||
DECLARE
|
||||
seed_user uuid;
|
||||
big_chat uuid;
|
||||
noise uuid;
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM chats WHERE title = 'explain-big') THEN
|
||||
RETURN;
|
||||
END IF;
|
||||
|
||||
INSERT INTO users (username, display_name, password_hash)
|
||||
VALUES ('explain_seed', 'EXPLAIN seed', 'not-an-argon2-hash')
|
||||
ON CONFLICT (username) DO UPDATE SET display_name = EXCLUDED.display_name
|
||||
RETURNING id INTO seed_user;
|
||||
|
||||
-- Большой чат: 20 000 сообщений
|
||||
INSERT INTO chats (title, created_by) VALUES ('explain-big', seed_user)
|
||||
RETURNING id INTO big_chat;
|
||||
INSERT INTO chat_members (chat_id, user_id, position) VALUES (big_chat, seed_user, 0);
|
||||
INSERT INTO messages (chat_id, sender_id, text, created_at)
|
||||
SELECT big_chat, seed_user, 'synthetic message #' || n,
|
||||
timestamptz '2026-01-01 00:00:00+00' + n * interval '1 second'
|
||||
FROM generate_series(1, 20000) AS n;
|
||||
|
||||
-- Шум: 50 чатов по 1 000 сообщений, чтобы таблица была больше одного чата
|
||||
FOR i IN 1..50 LOOP
|
||||
INSERT INTO chats (title, created_by) VALUES ('explain-noise-' || i, seed_user)
|
||||
RETURNING id INTO noise;
|
||||
INSERT INTO chat_members (chat_id, user_id, position) VALUES (noise, seed_user, 0);
|
||||
INSERT INTO messages (chat_id, sender_id, text, created_at)
|
||||
SELECT noise, seed_user, 'noise #' || n,
|
||||
timestamptz '2026-01-01 00:00:00+00' + n * interval '1 second'
|
||||
FROM generate_series(1, 1000) AS n;
|
||||
END LOOP;
|
||||
END $$;
|
||||
|
||||
ANALYZE users, chats, chat_members, messages;
|
||||
|
||||
SELECT id AS big_chat FROM chats WHERE title = 'explain-big' \gset
|
||||
|
||||
-- Cursor из середины истории: ключ 10 000-го сообщения
|
||||
SELECT created_at AS cursor_created_at, id AS cursor_id
|
||||
FROM messages WHERE chat_id = :'big_chat'
|
||||
ORDER BY created_at, id OFFSET 9999 LIMIT 1 \gset
|
||||
|
||||
\echo '== Объём данных'
|
||||
SELECT
|
||||
(SELECT count(*) FROM messages) AS messages_total,
|
||||
(SELECT count(*) FROM messages WHERE chat_id = :'big_chat') AS messages_in_big_chat,
|
||||
pg_size_pretty(pg_relation_size('messages')) AS messages_heap,
|
||||
pg_size_pretty(pg_relation_size('messages_chat_id_created_at_id_idx')) AS history_index;
|
||||
|
||||
\echo ''
|
||||
\echo '== 1. Первая страница истории (limit 50 + 1), как list_for_chat'
|
||||
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF)
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = :'big_chat'
|
||||
ORDER BY created_at, id
|
||||
LIMIT 51;
|
||||
|
||||
\echo ''
|
||||
\echo '== 2. Страница из середины по cursor (после 10 000-го), как list_for_chat_after'
|
||||
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF)
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = :'big_chat'
|
||||
AND (created_at, id) > (:'cursor_created_at'::timestamptz, :'cursor_id'::uuid)
|
||||
ORDER BY created_at, id
|
||||
LIMIT 51;
|
||||
|
||||
\echo ''
|
||||
\echo '== 3. Тот же запрос как generic plan prepared statement (так его исполняет asyncpg)'
|
||||
SET plan_cache_mode = force_generic_plan;
|
||||
PREPARE history_page(uuid, timestamptz, uuid, int) AS
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = $1 AND (created_at, id) > ($2, $3)
|
||||
ORDER BY created_at, id
|
||||
LIMIT $4;
|
||||
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF)
|
||||
EXECUTE history_page(:'big_chat', :'cursor_created_at', :'cursor_id', 51);
|
||||
DEALLOCATE history_page;
|
||||
RESET plan_cache_mode;
|
||||
|
||||
\echo ''
|
||||
\echo '== 4. MAX(created_at) при вставке сообщения (монотонное время чата)'
|
||||
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF)
|
||||
SELECT max(created_at) FROM messages WHERE chat_id = :'big_chat';
|
||||
|
||||
\echo ''
|
||||
\echo '== 5. Для сравнения: тот же запрос из середины БЕЗ индекса истории'
|
||||
\echo ' (DROP INDEX внутри транзакции с ROLLBACK — индекс не теряется)'
|
||||
BEGIN;
|
||||
DROP INDEX messages_chat_id_created_at_id_idx;
|
||||
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF)
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = :'big_chat'
|
||||
AND (created_at, id) > (:'cursor_created_at'::timestamptz, :'cursor_id'::uuid)
|
||||
ORDER BY created_at, id
|
||||
LIMIT 51;
|
||||
ROLLBACK;
|
||||
Executable
+17
@@ -0,0 +1,17 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# Операторский вид API-реплик: контейнер ↔ alias X-Instance-Id ↔ ревизия образа.
|
||||
# Нужен, чтобы показать, какой реальный контейнер обслужил запрос или остановлен:
|
||||
# заголовок сам по себе не доказывает разные процессы.
|
||||
cd "$(dirname "$0")/.."
|
||||
ENV_FILE="${ENV_FILE:-.env}"
|
||||
|
||||
printf '%-24s %-28s %-14s %-8s %s\n' CONTAINER STATE INSTANCE PID REVISION
|
||||
for container in $(docker compose --env-file "$ENV_FILE" ps -a -q api); do
|
||||
name=$(docker inspect -f '{{.Name}}' "$container")
|
||||
state=$(docker inspect -f '{{.State.Status}}{{if .State.Health}}/{{.State.Health.Status}}{{end}}' "$container")
|
||||
pid=$(docker inspect -f '{{.State.Pid}}' "$container")
|
||||
revision=$(docker inspect -f '{{index .Config.Labels "org.opencontainers.image.revision"}}' "$container")
|
||||
alias=$(docker exec "$container" cat /tmp/instance-id 2>/dev/null || echo -)
|
||||
printf '%-24s %-28s %-14s %-8s %s\n' "${name#/}" "$state" "$alias" "$pid" "$revision"
|
||||
done
|
||||
Executable
+11
@@ -0,0 +1,11 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# Перезапуск всего стенда с сохранением volumes: хранилища (PostgreSQL, Valkey),
|
||||
# затем все API-реплики и балансировщик. Никаких down -v, flush или повторного
|
||||
# seed; migrate не запускается — схема уже в volume. Готовность ждёт тест.
|
||||
cd "$(dirname "$0")/.."
|
||||
ENV_FILE="${ENV_FILE:-.env}"
|
||||
|
||||
docker compose --env-file "$ENV_FILE" restart --no-deps postgres valkey
|
||||
docker compose --env-file "$ENV_FILE" restart --no-deps api
|
||||
docker compose --env-file "$ENV_FILE" restart --no-deps nginx
|
||||
Executable
+9
@@ -0,0 +1,9 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# Возврат API-реплик к числу API_REPLICAS после stop-one.sh: запуск остановленных
|
||||
# контейнеров и ожидание healthy. Работающие реплики не пересоздаются
|
||||
# (--no-recreate), хранилища, migrate и LB не перезапускаются.
|
||||
cd "$(dirname "$0")/.."
|
||||
ENV_FILE="${ENV_FILE:-.env}"
|
||||
|
||||
docker compose --env-file "$ENV_FILE" up -d --no-deps --no-recreate --wait --wait-timeout 60 api
|
||||
Executable
+42
@@ -0,0 +1,42 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# Поочерёдная замена API-реплик новой сборкой без остановки обслуживания.
|
||||
# 1. build нового образа (ревизия видна в метке образа и журнале старта);
|
||||
# 2. migrate однократно: миграции только расширяют схему, старые реплики
|
||||
# продолжают работать на новой схеме;
|
||||
# 3. для каждой старой реплики: +1 новая → ждать healthy → пауза на обновление
|
||||
# DNS в nginx → graceful stop старой (SIGTERM, LB уводит трафик) → удалить.
|
||||
# Во время замены реплик на одну больше, чем API_REPLICAS: ресурс заложен заранее.
|
||||
cd "$(dirname "$0")/.."
|
||||
ENV_FILE="${ENV_FILE:-.env}"
|
||||
COMPOSE="docker compose --env-file $ENV_FILE"
|
||||
APP_REVISION="${APP_REVISION:-$(git rev-parse --short HEAD 2>/dev/null || echo dev)-$(date +%s)}"
|
||||
export APP_REVISION
|
||||
# ID образа зависит только от содержимого (без provenance-attestation)
|
||||
export BUILDX_NO_DEFAULT_ATTESTATIONS=1
|
||||
# Больше resolver valid=2s в nginx: новая реплика уже в upstream до остановки старой
|
||||
LB_DNS_REFRESH_SEC=3
|
||||
STOP_TIMEOUT_SEC=10
|
||||
|
||||
log() {
|
||||
echo "[$(date +%H:%M:%S)] $*"
|
||||
}
|
||||
|
||||
log "build revision $APP_REVISION"
|
||||
$COMPOSE build --quiet api
|
||||
log "migrate"
|
||||
$COMPOSE run --rm --no-deps migrate >/dev/null
|
||||
|
||||
old_containers=$($COMPOSE ps -q api)
|
||||
for container in $old_containers; do
|
||||
name=$(docker inspect -f '{{.Name}}' "$container")
|
||||
running=$($COMPOSE ps -q api | wc -l | tr -d ' ')
|
||||
log "scale api to $((running + 1)), wait healthy"
|
||||
$COMPOSE up -d --no-deps --no-recreate --wait --wait-timeout 60 \
|
||||
--scale "api=$((running + 1))" api
|
||||
sleep "$LB_DNS_REFRESH_SEC"
|
||||
log "graceful stop ${name#/}"
|
||||
docker stop --timeout "$STOP_TIMEOUT_SEC" "$container" >/dev/null
|
||||
docker rm "$container" >/dev/null
|
||||
done
|
||||
log "done: all API replicas run revision $APP_REVISION"
|
||||
@@ -0,0 +1,148 @@
|
||||
#!/usr/bin/env python3
|
||||
"""SIGTERM одной API-реплики за балансировщиком.
|
||||
|
||||
Реплика перестаёт принимать соединения, дожидается текущих запросов и
|
||||
завершается не дольше чем за 10 секунд; LB уводит трафик на другие реплики.
|
||||
Во время проверки фоновые клиенты непрерывно шлют GET /health/live через LB:
|
||||
ошибок (5xx/обрыв) быть не должно, после выхода реплики её X-Instance-Id
|
||||
в ответах не появляется.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import threading
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
DEFAULT_BASE_URL = json.loads((ROOT / "lab.json").read_text())["default_base_url"]
|
||||
|
||||
SHUTDOWN_LIMIT_SEC = 10
|
||||
PROBE_INTERVAL_SEC = 0.02
|
||||
PROBERS = 4
|
||||
|
||||
|
||||
class Prober(threading.Thread):
|
||||
"""Фоновый клиент через LB: время ответа и обслужившая реплика"""
|
||||
|
||||
def __init__(self, url: str) -> None:
|
||||
super().__init__(daemon=True)
|
||||
self.url = url
|
||||
self.stop = threading.Event()
|
||||
self.ok: list[tuple[float, str]] = []
|
||||
self.errors: list[tuple[float, str]] = []
|
||||
|
||||
def run(self) -> None:
|
||||
while not self.stop.is_set():
|
||||
try:
|
||||
with urllib.request.urlopen(self.url, timeout=2) as response:
|
||||
self.ok.append(
|
||||
(time.monotonic(), response.headers.get("X-Instance-Id", ""))
|
||||
)
|
||||
except urllib.error.HTTPError as error:
|
||||
self.errors.append((time.monotonic(), f"HTTP {error.code}"))
|
||||
except OSError as error:
|
||||
self.errors.append((time.monotonic(), type(error).__name__))
|
||||
time.sleep(PROBE_INTERVAL_SEC)
|
||||
|
||||
|
||||
def docker(*args: str) -> str:
|
||||
result = subprocess.run(
|
||||
["docker", *args], check=True, capture_output=True, text=True
|
||||
)
|
||||
return result.stdout.strip()
|
||||
|
||||
|
||||
def first_api_container() -> str:
|
||||
ids = docker("compose", "--env-file", ".env", "ps", "-q", "api").split()
|
||||
if not ids:
|
||||
raise SystemExit("FAIL: нет запущенных API-реплик")
|
||||
return docker("inspect", "-f", "{{.Name}}", ids[0]).lstrip("/")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument(
|
||||
"--base-url", default=os.environ.get("BASE_URL") or DEFAULT_BASE_URL
|
||||
)
|
||||
parser.add_argument("--container", default=os.environ.get("CONTAINER"))
|
||||
args = parser.parse_args()
|
||||
os.chdir(ROOT)
|
||||
container = args.container or first_api_container()
|
||||
instance = docker("exec", container, "cat", "/tmp/instance-id")
|
||||
|
||||
logs = subprocess.Popen(
|
||||
["docker", "logs", "--follow", "--since", "0s", container],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT,
|
||||
text=True,
|
||||
)
|
||||
waiter = subprocess.Popen(
|
||||
["docker", "wait", container], stdout=subprocess.PIPE, text=True
|
||||
)
|
||||
probers = [
|
||||
Prober(args.base_url.rstrip("/") + "/health/live") for _ in range(PROBERS)
|
||||
]
|
||||
for prober in probers:
|
||||
prober.start()
|
||||
time.sleep(1)
|
||||
before = {alias for prober in probers for _, alias in prober.ok}
|
||||
if instance not in before:
|
||||
raise SystemExit("FAIL: целевая реплика не обслуживает запросы до SIGTERM")
|
||||
|
||||
sent_at = time.monotonic()
|
||||
docker("kill", "--signal", "SIGTERM", container)
|
||||
exit_code = int(waiter.communicate(timeout=SHUTDOWN_LIMIT_SEC + 5)[0].strip())
|
||||
exited_at = time.monotonic()
|
||||
time.sleep(1)
|
||||
for prober in probers:
|
||||
prober.stop.set()
|
||||
prober.join()
|
||||
logs.terminate()
|
||||
shutdown_log = logs.communicate(timeout=5)[0]
|
||||
|
||||
ok_after = [(t, a) for p in probers for t, a in p.ok if t > sent_at]
|
||||
errors_after = [(t, e) for p in probers for t, e in p.errors if t > sent_at]
|
||||
target_after_exit = [t for t, a in ok_after if a == instance and t > exited_at]
|
||||
last_target = max((t for t, a in ok_after if a == instance), default=None)
|
||||
print("Журнал реплики после SIGTERM:")
|
||||
print(
|
||||
"\n".join(line for line in shutdown_log.splitlines() if "/health/" not in line)
|
||||
)
|
||||
summary = {
|
||||
"container": container,
|
||||
"instance": instance,
|
||||
"exit_code": exit_code,
|
||||
"shutdown_seconds": round(exited_at - sent_at, 3),
|
||||
"limit_seconds": SHUTDOWN_LIMIT_SEC,
|
||||
"instances_before": sorted(before),
|
||||
"probes_ok_after_sigterm": len(ok_after),
|
||||
"instances_after_sigterm": sorted({a for _, a in ok_after}),
|
||||
"last_target_response_after_sigterm_seconds": None
|
||||
if last_target is None
|
||||
else round(last_target - sent_at, 3),
|
||||
"target_responses_after_exit": len(target_after_exit),
|
||||
"errors_after_sigterm": len(errors_after),
|
||||
"error_kinds": sorted({e for _, e in errors_after}),
|
||||
}
|
||||
print(json.dumps(summary, indent=2, ensure_ascii=False))
|
||||
if (
|
||||
exit_code != 0
|
||||
or exited_at - sent_at > SHUTDOWN_LIMIT_SEC
|
||||
or errors_after
|
||||
or target_after_exit
|
||||
or not ok_after
|
||||
):
|
||||
raise SystemExit("FAIL: завершение по SIGTERM не соответствует требованиям")
|
||||
print(
|
||||
"PASS: SIGTERM завершил %s за %.2f с, LB обслужил %d запросов без ошибок"
|
||||
% (container, exited_at - sent_at, len(ok_after))
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Executable
+24
@@ -0,0 +1,24 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# Остановка ОДНОЙ API-реплики, обслужившей тест: TARGET_INSTANCE_ID → контейнер.
|
||||
# Каждый процесс API пишет свой alias (X-Instance-Id) в /tmp/instance-id.
|
||||
# docker stop: SIGTERM → graceful shutdown; restart-политика unless-stopped
|
||||
# не поднимает вручную остановленный контейнер, он остаётся остановленным.
|
||||
# БД, LB и volumes не затрагиваются. Восстановление — scripts/restore.sh.
|
||||
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
|
||||
cd "$(dirname "$0")/.."
|
||||
ENV_FILE="${ENV_FILE:-.env}"
|
||||
# Меньше 10 с лимита hook; больше drain + graceful shutdown (1 + 5 с)
|
||||
STOP_TIMEOUT_SEC=8
|
||||
|
||||
for container in $(docker compose --env-file "$ENV_FILE" ps -q api); do
|
||||
alias=$(docker exec "$container" cat /tmp/instance-id 2>/dev/null || true)
|
||||
if [ "$alias" = "$TARGET_INSTANCE_ID" ]; then
|
||||
name=$(docker inspect -f '{{.Name}}' "$container")
|
||||
docker stop --timeout "$STOP_TIMEOUT_SEC" "$container" >/dev/null
|
||||
echo "stopped ${name#/} (instance $alias)" >&2
|
||||
exit 0
|
||||
fi
|
||||
done
|
||||
echo "no running API replica with instance $TARGET_INSTANCE_ID" >&2
|
||||
exit 1
|
||||
@@ -0,0 +1,4 @@
|
||||
from .api import app as api_endpoints
|
||||
from .api import health_router as health_endpoints
|
||||
|
||||
__all__ = ["api_endpoints", "health_endpoints"]
|
||||
@@ -0,0 +1,10 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
from .health import health_router
|
||||
from .routers import app as v1_endpoints
|
||||
|
||||
app = APIRouter(prefix="/api")
|
||||
|
||||
app.include_router(v1_endpoints)
|
||||
|
||||
__all__ = ["app", "health_router"]
|
||||
@@ -0,0 +1,4 @@
|
||||
from .auth import get_current_user
|
||||
from .security import basic_credentials, bearer_token
|
||||
|
||||
__all__ = ["get_current_user", "basic_credentials", "bearer_token"]
|
||||
@@ -0,0 +1,15 @@
|
||||
from fastapi import Depends
|
||||
|
||||
from domain import UserSchema
|
||||
from infrastructure.dependencies import ServiceCreator
|
||||
from infrastructure.services import AuthService
|
||||
|
||||
from .security import bearer_token
|
||||
|
||||
|
||||
async def get_current_user(
|
||||
token: str | None = Depends(bearer_token),
|
||||
service: AuthService = Depends(ServiceCreator.get_auth_service),
|
||||
) -> UserSchema:
|
||||
"""Владелец действующей Bearer-сессии; X-User-Id не учитывается; иначе 401"""
|
||||
return await service.authenticate(token)
|
||||
@@ -0,0 +1,52 @@
|
||||
import base64
|
||||
import binascii
|
||||
|
||||
from fastapi import Depends
|
||||
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
||||
from fastapi.security.http import HTTPBase
|
||||
|
||||
from domain import BasicCredentialsSchema
|
||||
from domain.errors import InvalidCredentialsError
|
||||
|
||||
BASIC_SCHEME = "basic"
|
||||
|
||||
basic_scheme = HTTPBase(
|
||||
scheme=BASIC_SCHEME,
|
||||
scheme_name="basicAuth",
|
||||
description="Только для POST /auth/sessions",
|
||||
auto_error=False,
|
||||
)
|
||||
|
||||
bearer_scheme = HTTPBearer(
|
||||
scheme_name="bearerAuth",
|
||||
bearerFormat="opaque session token",
|
||||
auto_error=False,
|
||||
)
|
||||
|
||||
|
||||
async def basic_credentials(
|
||||
authorization: HTTPAuthorizationCredentials | None = Depends(basic_scheme),
|
||||
) -> BasicCredentialsSchema:
|
||||
"""
|
||||
Authorization: Basic base64(username:password)
|
||||
|
||||
username — до первого двоеточия, остальное — пароль
|
||||
Любая ошибка формата — тот же 401, что и неверный пароль.
|
||||
"""
|
||||
if authorization is None or authorization.scheme.lower() != BASIC_SCHEME:
|
||||
raise InvalidCredentialsError()
|
||||
try:
|
||||
decoded = base64.b64decode(authorization.credentials, validate=True)
|
||||
username, separator, password = decoded.decode("utf-8").partition(":")
|
||||
except (binascii.Error, UnicodeDecodeError):
|
||||
raise InvalidCredentialsError() from None
|
||||
if not separator or not username.isascii():
|
||||
raise InvalidCredentialsError()
|
||||
return BasicCredentialsSchema(username=username, password=password)
|
||||
|
||||
|
||||
async def bearer_token(
|
||||
authorization: HTTPAuthorizationCredentials | None = Depends(bearer_scheme),
|
||||
) -> str | None:
|
||||
"""Токен из Authorization: Bearer; другие схемы (Basic) — как отсутствие"""
|
||||
return authorization.credentials if authorization is not None else None
|
||||
@@ -0,0 +1,3 @@
|
||||
from .exceptions import register_exception_handlers
|
||||
|
||||
__all__ = ["register_exception_handlers"]
|
||||
@@ -0,0 +1,61 @@
|
||||
from http import HTTPStatus
|
||||
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.responses import JSONResponse
|
||||
from starlette.exceptions import HTTPException as StarletteHTTPException
|
||||
|
||||
from application.api.utils import error_response, get_request_id
|
||||
from domain.errors import AppError, ErrorCode, InvalidRequestError, NotFoundError
|
||||
|
||||
|
||||
async def app_error_handler(request: Request, error: Exception) -> JSONResponse:
|
||||
"""Доменные ошибки сервисов и репозиториев"""
|
||||
assert isinstance(error, AppError)
|
||||
return error_response(
|
||||
get_request_id(request),
|
||||
error.status_code,
|
||||
error.code,
|
||||
error.message,
|
||||
error.headers,
|
||||
)
|
||||
|
||||
|
||||
async def validation_error_handler(request: Request, error: Exception) -> JSONResponse:
|
||||
"""Malformed JSON, лишние поля, неверные типы, uuid и limit — 400"""
|
||||
assert isinstance(error, RequestValidationError)
|
||||
return error_response(
|
||||
get_request_id(request),
|
||||
InvalidRequestError.status_code,
|
||||
InvalidRequestError.code,
|
||||
_describe_validation_error(error),
|
||||
)
|
||||
|
||||
|
||||
async def http_error_handler(request: Request, error: Exception) -> JSONResponse:
|
||||
"""Ошибки маршрутизации неизвестный путь, неподдерживаемый метод"""
|
||||
assert isinstance(error, StarletteHTTPException)
|
||||
if error.status_code == HTTPStatus.NOT_FOUND:
|
||||
code, message = NotFoundError.code, NotFoundError.default_message
|
||||
elif error.status_code >= HTTPStatus.INTERNAL_SERVER_ERROR:
|
||||
code, message = AppError.code, AppError.default_message
|
||||
else:
|
||||
code, message = ErrorCode.INVALID_REQUEST, HTTPStatus(error.status_code).phrase
|
||||
return error_response(
|
||||
get_request_id(request), error.status_code, code, message, error.headers
|
||||
)
|
||||
|
||||
|
||||
def _describe_validation_error(error: RequestValidationError) -> str:
|
||||
"""Безопасное описание: только тип и путь поля, без присланного значения"""
|
||||
first = error.errors()[0]
|
||||
if first.get("type") == "json_invalid":
|
||||
return "Некорректный JSON в теле запроса"
|
||||
location = ".".join(str(part) for part in first.get("loc", ()))
|
||||
return f"Некорректное значение: {location}"
|
||||
|
||||
|
||||
def register_exception_handlers(app: FastAPI) -> None:
|
||||
app.add_exception_handler(AppError, app_error_handler)
|
||||
app.add_exception_handler(RequestValidationError, validation_error_handler)
|
||||
app.add_exception_handler(StarletteHTTPException, http_error_handler)
|
||||
@@ -0,0 +1,3 @@
|
||||
from .endpoints import health_router
|
||||
|
||||
__all__ = ["health_router"]
|
||||
@@ -0,0 +1,36 @@
|
||||
from fastapi import APIRouter, Depends, Request, status
|
||||
|
||||
from domain import HealthSchema
|
||||
from domain.errors import UnavailableError
|
||||
from infrastructure.dependencies import DatabasePoolManager, get_database
|
||||
|
||||
health_router = APIRouter(prefix="/health", tags=["Health"])
|
||||
|
||||
|
||||
@health_router.get(
|
||||
"/live",
|
||||
status_code=status.HTTP_200_OK,
|
||||
response_model=HealthSchema,
|
||||
)
|
||||
async def liveness() -> HealthSchema:
|
||||
"""Процесс жив и обрабатывает хттп"""
|
||||
return HealthSchema()
|
||||
|
||||
|
||||
@health_router.get(
|
||||
"/ready",
|
||||
status_code=status.HTTP_200_OK,
|
||||
response_model=HealthSchema,
|
||||
)
|
||||
async def readiness(
|
||||
request: Request, database: DatabasePoolManager = Depends(get_database)
|
||||
) -> HealthSchema:
|
||||
"""
|
||||
Приложение не завершается и PostgreSQL отвечает. Valkey не критичен:
|
||||
без него сессии читаются из PostgreSQL, поэтому он не влияет на готовность.
|
||||
"""
|
||||
if not getattr(request.app.state, "is_ready", False):
|
||||
raise UnavailableError()
|
||||
if not await database.check():
|
||||
raise UnavailableError("бд недоступна")
|
||||
return HealthSchema()
|
||||
@@ -0,0 +1,3 @@
|
||||
from .request_context import RequestContextMiddleware
|
||||
|
||||
__all__ = ["RequestContextMiddleware"]
|
||||
@@ -0,0 +1,71 @@
|
||||
import re
|
||||
import time
|
||||
import uuid
|
||||
|
||||
from fastapi import Request, Response
|
||||
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
|
||||
|
||||
from application.api.utils import INSTANCE_ID_HEADER, REQUEST_ID_HEADER, error_response
|
||||
from config import INSTANCE_ID, LOGGER
|
||||
from domain.errors import AppError
|
||||
|
||||
HEALTH_PATH_PREFIX = "/health/"
|
||||
|
||||
INCOMING_REQUEST_ID = re.compile(r"^[0-9a-f]{32}$")
|
||||
|
||||
|
||||
class RequestContextMiddleware(BaseHTTPMiddleware):
|
||||
"""
|
||||
X-Request-Id и X-Instance-Id в каждом ответе, журнал запросов и защита
|
||||
от необработанных ошибок.
|
||||
|
||||
X-Request-Id берётся у балансировщика (nginx выставляет $request_id),
|
||||
поэтому одна строка access-лога LB и журнал реплики связаны одним ID.
|
||||
В лог попадают метод, путь, статус и длительность — без тел запросов,
|
||||
query-параметров и текста сообщений.
|
||||
"""
|
||||
|
||||
async def dispatch(
|
||||
self, request: Request, call_next: RequestResponseEndpoint
|
||||
) -> Response:
|
||||
request_id = self._request_id(request)
|
||||
request.state.request_id = request_id
|
||||
started = time.perf_counter()
|
||||
try:
|
||||
response = await call_next(request)
|
||||
except Exception:
|
||||
LOGGER.exception(f"Необработанная ошибка | request_id={request_id}")
|
||||
error = AppError()
|
||||
response = error_response(
|
||||
request_id, error.status_code, error.code, error.message
|
||||
)
|
||||
response.headers[REQUEST_ID_HEADER] = request_id
|
||||
response.headers[INSTANCE_ID_HEADER] = INSTANCE_ID
|
||||
if getattr(request.app.state, "is_draining", False):
|
||||
response.headers["Connection"] = "close"
|
||||
self._log_request(request, response, request_id, started)
|
||||
return response
|
||||
|
||||
@staticmethod
|
||||
def _request_id(request: Request) -> str:
|
||||
incoming = request.headers.get(REQUEST_ID_HEADER, "")
|
||||
if INCOMING_REQUEST_ID.fullmatch(incoming):
|
||||
return incoming
|
||||
return uuid.uuid4().hex
|
||||
|
||||
@staticmethod
|
||||
def _log_request(
|
||||
request: Request, response: Response, request_id: str, started: float
|
||||
) -> None:
|
||||
duration_ms = (time.perf_counter() - started) * 1000
|
||||
if response.status_code >= 500:
|
||||
level = "ERROR"
|
||||
elif request.url.path.startswith(HEALTH_PATH_PREFIX):
|
||||
level = "DEBUG"
|
||||
else:
|
||||
level = "INFO"
|
||||
LOGGER.log(
|
||||
level,
|
||||
f"{request.method} {request.url.path} -> {response.status_code} "
|
||||
f"| {duration_ms:.1f} ms | request_id={request_id}",
|
||||
)
|
||||
@@ -0,0 +1,15 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
from .auth import auth_router
|
||||
from .chats import chats_router
|
||||
from .messages import messages_router
|
||||
from .users import users_router
|
||||
|
||||
app = APIRouter(prefix="/v1")
|
||||
|
||||
app.include_router(users_router)
|
||||
app.include_router(auth_router)
|
||||
app.include_router(chats_router)
|
||||
app.include_router(messages_router)
|
||||
|
||||
__all__ = ["app"]
|
||||
@@ -0,0 +1,3 @@
|
||||
from .endpoints import auth_router
|
||||
|
||||
__all__ = ["auth_router"]
|
||||
@@ -0,0 +1,35 @@
|
||||
from fastapi import APIRouter, Depends, Response, status
|
||||
|
||||
from application.api.dependencies import basic_credentials, bearer_token
|
||||
from domain import BasicCredentialsSchema, SessionSchema
|
||||
from infrastructure.dependencies import ServiceCreator
|
||||
from infrastructure.services import AuthService
|
||||
|
||||
auth_router = APIRouter(prefix="/auth/sessions", tags=["Auth"])
|
||||
|
||||
|
||||
@auth_router.post(
|
||||
"",
|
||||
status_code=status.HTTP_201_CREATED,
|
||||
response_model=SessionSchema,
|
||||
)
|
||||
async def create_session(
|
||||
credentials: BasicCredentialsSchema = Depends(basic_credentials),
|
||||
service: AuthService = Depends(ServiceCreator.get_auth_service),
|
||||
) -> SessionSchema:
|
||||
"""Вход по Basic, без JSON body; возвращает Bearer-токен"""
|
||||
return await service.login(credentials)
|
||||
|
||||
|
||||
@auth_router.delete(
|
||||
"/current",
|
||||
status_code=status.HTTP_204_NO_CONTENT,
|
||||
response_class=Response,
|
||||
)
|
||||
async def revoke_session(
|
||||
token: str | None = Depends(bearer_token),
|
||||
service: AuthService = Depends(ServiceCreator.get_auth_service),
|
||||
) -> Response:
|
||||
"""Logout: текущий токен перестаёт действовать; повтор — 401"""
|
||||
await service.logout(token)
|
||||
return Response(status_code=status.HTTP_204_NO_CONTENT)
|
||||
@@ -0,0 +1,3 @@
|
||||
from .endpoints import chats_router
|
||||
|
||||
__all__ = ["chats_router"]
|
||||
@@ -0,0 +1,58 @@
|
||||
from typing import Annotated
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Body, Depends, Query, status
|
||||
|
||||
from application.api.dependencies import get_current_user
|
||||
from domain import (
|
||||
ChatPageSchema,
|
||||
ChatSchema,
|
||||
CreateChatSchema,
|
||||
UserSchema,
|
||||
)
|
||||
from infrastructure.dependencies import ServiceCreator
|
||||
from infrastructure.services import ChatService
|
||||
|
||||
chats_router = APIRouter(prefix="/chats", tags=["Chats"])
|
||||
|
||||
|
||||
@chats_router.post(
|
||||
"",
|
||||
status_code=status.HTTP_201_CREATED,
|
||||
response_model=ChatSchema,
|
||||
)
|
||||
async def create_chat(
|
||||
body: Annotated[CreateChatSchema, Body(...)],
|
||||
current_user: UserSchema = Depends(get_current_user),
|
||||
service: ChatService = Depends(ServiceCreator.get_chat_service),
|
||||
) -> ChatSchema:
|
||||
"""Вызывающий должен входить в member_ids"""
|
||||
return await service.create_chat(current_user, body)
|
||||
|
||||
|
||||
@chats_router.get(
|
||||
"",
|
||||
status_code=status.HTTP_200_OK,
|
||||
response_model=ChatPageSchema,
|
||||
)
|
||||
async def list_chats(
|
||||
limit: Annotated[int, Query(ge=1, le=100)] = 50,
|
||||
cursor: Annotated[str | None, Query(min_length=1)] = None,
|
||||
current_user: UserSchema = Depends(get_current_user),
|
||||
service: ChatService = Depends(ServiceCreator.get_chat_service),
|
||||
) -> ChatPageSchema:
|
||||
"""Чаты текущего пользователя по (created_at, id) по возрастанию"""
|
||||
return await service.list_chats(current_user, limit, cursor)
|
||||
|
||||
|
||||
@chats_router.get(
|
||||
"/{chat_id}",
|
||||
status_code=status.HTTP_200_OK,
|
||||
response_model=ChatSchema,
|
||||
)
|
||||
async def get_chat(
|
||||
chat_id: UUID,
|
||||
current_user: UserSchema = Depends(get_current_user),
|
||||
service: ChatService = Depends(ServiceCreator.get_chat_service),
|
||||
) -> ChatSchema:
|
||||
return await service.get_chat(current_user, chat_id)
|
||||
@@ -0,0 +1,3 @@
|
||||
from .endpoints import messages_router
|
||||
|
||||
__all__ = ["messages_router"]
|
||||
@@ -0,0 +1,63 @@
|
||||
from typing import Annotated
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Body, Depends, Header, Query, status
|
||||
|
||||
from application.api.dependencies import get_current_user
|
||||
from domain import (
|
||||
IDEMPOTENCY_KEY_MAX_LENGTH,
|
||||
IDEMPOTENCY_KEY_PATTERN,
|
||||
CreateMessageSchema,
|
||||
MessagePageSchema,
|
||||
MessageSchema,
|
||||
UserSchema,
|
||||
)
|
||||
from infrastructure.dependencies import ServiceCreator
|
||||
from infrastructure.services import MessageService
|
||||
|
||||
messages_router = APIRouter(
|
||||
prefix="/chats/{chat_id}/messages", tags=["Messages"]
|
||||
)
|
||||
|
||||
|
||||
@messages_router.post(
|
||||
"",
|
||||
status_code=status.HTTP_201_CREATED,
|
||||
response_model=MessageSchema,
|
||||
)
|
||||
async def send_message(
|
||||
chat_id: UUID,
|
||||
body: Annotated[CreateMessageSchema, Body(...)],
|
||||
idempotency_key: Annotated[
|
||||
str,
|
||||
Header(
|
||||
alias="Idempotency-Key",
|
||||
min_length=1,
|
||||
max_length=IDEMPOTENCY_KEY_MAX_LENGTH,
|
||||
pattern=IDEMPOTENCY_KEY_PATTERN,
|
||||
),
|
||||
],
|
||||
current_user: UserSchema = Depends(get_current_user),
|
||||
service: MessageService = Depends(ServiceCreator.get_message_service),
|
||||
) -> MessageSchema:
|
||||
"""
|
||||
Idempotency-Key обязателен: повтор того же тела возвращает тот же 201
|
||||
и то же сообщение, другое тело с тем же ключом — 409. Нет/неверный ключ — 400.
|
||||
"""
|
||||
return await service.send_message(current_user, chat_id, body, idempotency_key)
|
||||
|
||||
|
||||
@messages_router.get(
|
||||
"",
|
||||
status_code=status.HTTP_200_OK,
|
||||
response_model=MessagePageSchema,
|
||||
)
|
||||
async def list_messages(
|
||||
chat_id: UUID,
|
||||
limit: Annotated[int, Query(ge=1, le=100)] = 50,
|
||||
cursor: Annotated[str | None, Query(min_length=1)] = None,
|
||||
current_user: UserSchema = Depends(get_current_user),
|
||||
service: MessageService = Depends(ServiceCreator.get_message_service),
|
||||
) -> MessagePageSchema:
|
||||
"""История чата: keyset cursor по (created_at, id) по возрастанию"""
|
||||
return await service.list_messages(current_user, chat_id, limit, cursor)
|
||||
@@ -0,0 +1,3 @@
|
||||
from .endpoints import users_router
|
||||
|
||||
__all__ = ["users_router"]
|
||||
@@ -0,0 +1,33 @@
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, Body, Depends, status
|
||||
|
||||
from application.api.dependencies import get_current_user
|
||||
from domain import CreateUserSchema, UserSchema
|
||||
from infrastructure.dependencies import ServiceCreator
|
||||
from infrastructure.services import UserService
|
||||
|
||||
users_router = APIRouter(prefix="/users", tags=["Users"])
|
||||
|
||||
|
||||
@users_router.post(
|
||||
"",
|
||||
status_code=status.HTTP_201_CREATED,
|
||||
response_model=UserSchema,
|
||||
openapi_extra={"security": []},
|
||||
)
|
||||
async def register_user(
|
||||
body: Annotated[CreateUserSchema, Body(...)],
|
||||
service: UserService = Depends(ServiceCreator.get_user_service),
|
||||
) -> UserSchema:
|
||||
"""Публичная регистрация; username уникален"""
|
||||
return await service.register(body)
|
||||
|
||||
|
||||
@users_router.get(
|
||||
"/me",
|
||||
status_code=status.HTTP_200_OK,
|
||||
response_model=UserSchema,
|
||||
)
|
||||
async def get_me(current_user: UserSchema = Depends(get_current_user)) -> UserSchema:
|
||||
return current_user
|
||||
@@ -0,0 +1,13 @@
|
||||
from .errors import (
|
||||
INSTANCE_ID_HEADER,
|
||||
REQUEST_ID_HEADER,
|
||||
error_response,
|
||||
get_request_id,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"REQUEST_ID_HEADER",
|
||||
"INSTANCE_ID_HEADER",
|
||||
"error_response",
|
||||
"get_request_id",
|
||||
]
|
||||
@@ -0,0 +1,33 @@
|
||||
from collections.abc import Mapping
|
||||
|
||||
from fastapi import Request
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from domain.errors import ErrorBodySchema, ErrorCode, ErrorSchema
|
||||
|
||||
REQUEST_ID_HEADER = "X-Request-Id"
|
||||
INSTANCE_ID_HEADER = "X-Instance-Id"
|
||||
|
||||
|
||||
def get_request_id(request: Request) -> str:
|
||||
"""ID запроса, назначенный RequestContextMiddleware"""
|
||||
request_id: str = request.state.request_id
|
||||
return request_id
|
||||
|
||||
|
||||
def error_response(
|
||||
request_id: str,
|
||||
status_code: int,
|
||||
code: ErrorCode,
|
||||
message: str,
|
||||
headers: Mapping[str, str] | None = None,
|
||||
) -> JSONResponse:
|
||||
"""Ответ в едином формате ошибки контракта"""
|
||||
body = ErrorSchema(
|
||||
error=ErrorBodySchema(code=code, message=message, request_id=request_id)
|
||||
)
|
||||
return JSONResponse(
|
||||
status_code=status_code,
|
||||
content=body.model_dump(mode="json"),
|
||||
headers={**(headers or {}), REQUEST_ID_HEADER: request_id},
|
||||
)
|
||||
@@ -0,0 +1,67 @@
|
||||
from .log_config import LOGGER
|
||||
from .settings import (
|
||||
APP_ENV,
|
||||
APP_HOST,
|
||||
APP_KEEPALIVE_TIMEOUT_SEC,
|
||||
APP_PORT,
|
||||
APP_REVISION,
|
||||
DB_COMMAND_TIMEOUT_SEC,
|
||||
DB_CONNECT_TIMEOUT_SEC,
|
||||
DB_HOST,
|
||||
DB_NAME,
|
||||
DB_PASS,
|
||||
DB_POOL_MAX_SIZE,
|
||||
DB_POOL_MIN_SIZE,
|
||||
DB_PORT,
|
||||
DB_STARTUP_TIMEOUT_SEC,
|
||||
DB_USER,
|
||||
IDEMPOTENCY_TTL_SECONDS,
|
||||
INSTANCE_ID,
|
||||
INSTANCE_ID_FILE,
|
||||
LOG_LEVEL,
|
||||
PASSWORD_HASH_WORKERS,
|
||||
SESSION_CACHE_TTL_SECONDS,
|
||||
SESSION_TTL_SECONDS,
|
||||
SHUTDOWN_DRAIN_SEC,
|
||||
SHUTDOWN_TIMEOUT_SEC,
|
||||
VALKEY_HOST,
|
||||
VALKEY_PASS,
|
||||
VALKEY_POOL_MAX_SIZE,
|
||||
VALKEY_PORT,
|
||||
VALKEY_RETRY_AFTER_SEC,
|
||||
VALKEY_TIMEOUT_SEC,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"LOGGER",
|
||||
"APP_HOST",
|
||||
"APP_PORT",
|
||||
"APP_ENV",
|
||||
"APP_REVISION",
|
||||
"APP_KEEPALIVE_TIMEOUT_SEC",
|
||||
"LOG_LEVEL",
|
||||
"SHUTDOWN_TIMEOUT_SEC",
|
||||
"SHUTDOWN_DRAIN_SEC",
|
||||
"DB_USER",
|
||||
"DB_PASS",
|
||||
"DB_NAME",
|
||||
"DB_HOST",
|
||||
"DB_PORT",
|
||||
"DB_POOL_MIN_SIZE",
|
||||
"DB_POOL_MAX_SIZE",
|
||||
"DB_CONNECT_TIMEOUT_SEC",
|
||||
"DB_COMMAND_TIMEOUT_SEC",
|
||||
"DB_STARTUP_TIMEOUT_SEC",
|
||||
"VALKEY_HOST",
|
||||
"VALKEY_PORT",
|
||||
"VALKEY_PASS",
|
||||
"VALKEY_POOL_MAX_SIZE",
|
||||
"VALKEY_TIMEOUT_SEC",
|
||||
"VALKEY_RETRY_AFTER_SEC",
|
||||
"SESSION_TTL_SECONDS",
|
||||
"SESSION_CACHE_TTL_SECONDS",
|
||||
"PASSWORD_HASH_WORKERS",
|
||||
"IDEMPOTENCY_TTL_SECONDS",
|
||||
"INSTANCE_ID",
|
||||
"INSTANCE_ID_FILE",
|
||||
]
|
||||
@@ -0,0 +1,12 @@
|
||||
"""Сборщик логов"""
|
||||
|
||||
import sys
|
||||
|
||||
from loguru import logger
|
||||
|
||||
from .settings import LOG_LEVEL
|
||||
|
||||
LOGGER = logger
|
||||
|
||||
LOGGER.remove()
|
||||
LOGGER.add(sys.stdout, level=LOG_LEVEL, diagnose=False)
|
||||
@@ -0,0 +1,49 @@
|
||||
import os
|
||||
import secrets
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
from envparse import Env
|
||||
|
||||
env = Env()
|
||||
|
||||
ENV_FILE = Path(os.getenv("ENV_FILE", ".env"))
|
||||
if ENV_FILE.is_file():
|
||||
env.read_envfile(path=str(ENV_FILE))
|
||||
|
||||
APP_HOST = env.str("APP_HOST")
|
||||
APP_PORT = env.int("APP_PORT")
|
||||
APP_ENV = env.str("APP_ENV")
|
||||
APP_REVISION = env.str("APP_REVISION", default="dev")
|
||||
# Больше keepalive_timeout балансировщика: idle-соединение закрывает LB, а не API
|
||||
APP_KEEPALIVE_TIMEOUT_SEC = env.int("APP_KEEPALIVE_TIMEOUT_SEC")
|
||||
|
||||
LOG_LEVEL = env.str("LOG_LEVEL")
|
||||
SHUTDOWN_TIMEOUT_SEC = env.int("SHUTDOWN_TIMEOUT_SEC")
|
||||
SHUTDOWN_DRAIN_SEC = env.float("SHUTDOWN_DRAIN_SEC")
|
||||
|
||||
DB_USER = env.str("DB_USER")
|
||||
DB_PASS = env.str("DB_PASS")
|
||||
DB_NAME = env.str("DB_NAME")
|
||||
DB_HOST = env.str("DB_HOST")
|
||||
DB_PORT = env.int("DB_PORT")
|
||||
DB_POOL_MIN_SIZE = env.int("DB_POOL_MIN_SIZE")
|
||||
DB_POOL_MAX_SIZE = env.int("DB_POOL_MAX_SIZE")
|
||||
DB_CONNECT_TIMEOUT_SEC = env.float("DB_CONNECT_TIMEOUT_SEC")
|
||||
DB_COMMAND_TIMEOUT_SEC = env.float("DB_COMMAND_TIMEOUT_SEC")
|
||||
DB_STARTUP_TIMEOUT_SEC = env.float("DB_STARTUP_TIMEOUT_SEC")
|
||||
|
||||
VALKEY_HOST = env.str("VALKEY_HOST")
|
||||
VALKEY_PORT = env.int("VALKEY_PORT")
|
||||
VALKEY_PASS = env.str("VALKEY_PASS")
|
||||
VALKEY_POOL_MAX_SIZE = env.int("VALKEY_POOL_MAX_SIZE")
|
||||
VALKEY_TIMEOUT_SEC = env.float("VALKEY_TIMEOUT_SEC")
|
||||
VALKEY_RETRY_AFTER_SEC = env.float("VALKEY_RETRY_AFTER_SEC")
|
||||
|
||||
SESSION_TTL_SECONDS = env.int("SESSION_TTL_SECONDS")
|
||||
SESSION_CACHE_TTL_SECONDS = env.int("SESSION_CACHE_TTL_SECONDS")
|
||||
PASSWORD_HASH_WORKERS = env.int("PASSWORD_HASH_WORKERS")
|
||||
IDEMPOTENCY_TTL_SECONDS = env.int("IDEMPOTENCY_TTL_SECONDS")
|
||||
|
||||
INSTANCE_ID = secrets.token_hex(6)
|
||||
INSTANCE_ID_FILE = Path(tempfile.gettempdir()) / "instance-id"
|
||||
@@ -0,0 +1,34 @@
|
||||
from .auth import ActiveSessionSchema, BasicCredentialsSchema, SessionSchema
|
||||
from .chats import ChatPageSchema, ChatSchema, CreateChatSchema
|
||||
from .health import HealthSchema
|
||||
from .messages import (
|
||||
IDEMPOTENCY_KEY_MAX_LENGTH,
|
||||
IDEMPOTENCY_KEY_PATTERN,
|
||||
CreateMessageSchema,
|
||||
IdempotencyKeySchema,
|
||||
MessagePageSchema,
|
||||
MessageSchema,
|
||||
)
|
||||
from .pagination import PageCursorSchema, PageKey
|
||||
from .users import CreateUserSchema, UserCredentialsSchema, UserSchema
|
||||
|
||||
__all__ = [
|
||||
"CreateUserSchema",
|
||||
"UserSchema",
|
||||
"UserCredentialsSchema",
|
||||
"BasicCredentialsSchema",
|
||||
"SessionSchema",
|
||||
"ActiveSessionSchema",
|
||||
"CreateChatSchema",
|
||||
"ChatSchema",
|
||||
"ChatPageSchema",
|
||||
"CreateMessageSchema",
|
||||
"IdempotencyKeySchema",
|
||||
"IDEMPOTENCY_KEY_PATTERN",
|
||||
"IDEMPOTENCY_KEY_MAX_LENGTH",
|
||||
"MessageSchema",
|
||||
"MessagePageSchema",
|
||||
"HealthSchema",
|
||||
"PageKey",
|
||||
"PageCursorSchema",
|
||||
]
|
||||
@@ -0,0 +1,7 @@
|
||||
from .schemas import ActiveSessionSchema, BasicCredentialsSchema, SessionSchema
|
||||
|
||||
__all__ = [
|
||||
"BasicCredentialsSchema",
|
||||
"SessionSchema",
|
||||
"ActiveSessionSchema",
|
||||
]
|
||||
@@ -0,0 +1,35 @@
|
||||
from typing import Literal
|
||||
|
||||
from pydantic import BaseModel, ConfigDict
|
||||
|
||||
from domain.users import UserSchema
|
||||
from domain.utils import UtcDateTime
|
||||
|
||||
|
||||
class BasicCredentialsSchema(BaseModel):
|
||||
"""Разобранный заголовок Authorization: Basic"""
|
||||
|
||||
model_config = ConfigDict(frozen=True)
|
||||
|
||||
username: str
|
||||
password: str
|
||||
|
||||
|
||||
class SessionSchema(BaseModel):
|
||||
"""Ответ входа: непрозрачный токен показывается клиенту один раз"""
|
||||
|
||||
model_config = ConfigDict(frozen=True)
|
||||
|
||||
token: str
|
||||
token_type: Literal["Bearer"] = "Bearer"
|
||||
expires_at: UtcDateTime
|
||||
user: UserSchema
|
||||
|
||||
|
||||
class ActiveSessionSchema(BaseModel):
|
||||
"""Действующая серверная сессия: владелец и срок; так же лежит в кэше"""
|
||||
|
||||
model_config = ConfigDict(frozen=True)
|
||||
|
||||
user: UserSchema
|
||||
expires_at: UtcDateTime
|
||||
@@ -0,0 +1,7 @@
|
||||
from .schemas import ChatPageSchema, ChatSchema, CreateChatSchema
|
||||
|
||||
__all__ = [
|
||||
"CreateChatSchema",
|
||||
"ChatSchema",
|
||||
"ChatPageSchema",
|
||||
]
|
||||
@@ -0,0 +1,42 @@
|
||||
from typing import Annotated
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
||||
|
||||
from domain.utils import NotBlankStr, UtcDateTime
|
||||
|
||||
ChatTitle = Annotated[NotBlankStr, Field(min_length=1, max_length=100)]
|
||||
|
||||
|
||||
class CreateChatSchema(BaseModel):
|
||||
"""Запрос создания чата: участники задаются явно, включая вызывающего"""
|
||||
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
title: ChatTitle
|
||||
member_ids: list[UUID] = Field(min_length=1, max_length=100)
|
||||
|
||||
@field_validator("member_ids")
|
||||
@classmethod
|
||||
def validate_unique_members(cls, value: list[UUID]) -> list[UUID]:
|
||||
if len(set(value)) != len(value):
|
||||
raise ValueError("member_ids не должен содержать дубликаты")
|
||||
return value
|
||||
|
||||
|
||||
class ChatSchema(BaseModel):
|
||||
"""Чат"""
|
||||
|
||||
model_config = ConfigDict(frozen=True)
|
||||
|
||||
id: UUID
|
||||
title: str
|
||||
member_ids: list[UUID]
|
||||
created_at: UtcDateTime
|
||||
|
||||
|
||||
class ChatPageSchema(BaseModel):
|
||||
"""Страница чатов текущего пользователя"""
|
||||
|
||||
items: list[ChatSchema]
|
||||
next_cursor: str | None
|
||||
@@ -0,0 +1,24 @@
|
||||
from .codes import ErrorCode
|
||||
from .exceptions import (
|
||||
AppError,
|
||||
ConflictError,
|
||||
InvalidCredentialsError,
|
||||
InvalidRequestError,
|
||||
NotFoundError,
|
||||
UnauthorizedError,
|
||||
UnavailableError,
|
||||
)
|
||||
from .schemas import ErrorBodySchema, ErrorSchema
|
||||
|
||||
__all__ = [
|
||||
"ErrorCode",
|
||||
"AppError",
|
||||
"InvalidRequestError",
|
||||
"UnauthorizedError",
|
||||
"InvalidCredentialsError",
|
||||
"NotFoundError",
|
||||
"ConflictError",
|
||||
"UnavailableError",
|
||||
"ErrorBodySchema",
|
||||
"ErrorSchema",
|
||||
]
|
||||
@@ -0,0 +1,15 @@
|
||||
from enum import StrEnum
|
||||
|
||||
|
||||
class ErrorCode(StrEnum):
|
||||
"""коды ошибок"""
|
||||
|
||||
INVALID_REQUEST = "invalid_request"
|
||||
UNAUTHORIZED = "unauthorized"
|
||||
NOT_FOUND = "not_found"
|
||||
CONFLICT = "conflict"
|
||||
PAYLOAD_TOO_LARGE = "payload_too_large"
|
||||
UNSUPPORTED_MEDIA_TYPE = "unsupported_media_type"
|
||||
RATE_LIMITED = "rate_limited"
|
||||
UNAVAILABLE = "unavailable"
|
||||
INTERNAL_ERROR = "internal_error"
|
||||
@@ -0,0 +1,64 @@
|
||||
from http import HTTPStatus
|
||||
|
||||
from .codes import ErrorCode
|
||||
|
||||
|
||||
class AppError(Exception):
|
||||
"""Базовая ошибка приложения: статус, машинный код и безопасное сообщение"""
|
||||
|
||||
status_code: HTTPStatus = HTTPStatus.INTERNAL_SERVER_ERROR
|
||||
code: ErrorCode = ErrorCode.INTERNAL_ERROR
|
||||
default_message: str = "Внутренняя ошибка сервера"
|
||||
headers: dict[str, str] = {}
|
||||
|
||||
def __init__(self, message: str | None = None) -> None:
|
||||
self.message = message or self.default_message
|
||||
super().__init__(self.message)
|
||||
|
||||
|
||||
class InvalidRequestError(AppError):
|
||||
"""Неверные поля, UUID, cursor или limit"""
|
||||
|
||||
status_code = HTTPStatus.BAD_REQUEST
|
||||
code = ErrorCode.INVALID_REQUEST
|
||||
default_message = "Некорректный запрос"
|
||||
|
||||
|
||||
class UnauthorizedError(AppError):
|
||||
"""Нет действительной Bearer-сессии: токена нет, он истёк, отозван или неизвестен"""
|
||||
|
||||
status_code = HTTPStatus.UNAUTHORIZED
|
||||
code = ErrorCode.UNAUTHORIZED
|
||||
default_message = "Требуется действительная сессия: Authorization: Bearer <token>"
|
||||
headers = {"WWW-Authenticate": 'Bearer realm="messenger"'}
|
||||
|
||||
|
||||
class InvalidCredentialsError(UnauthorizedError):
|
||||
"""Неверные Basic-credentials: одинаковый ответ для неизвестного логина и пароля"""
|
||||
|
||||
default_message = "Неверное имя пользователя или пароль"
|
||||
headers = {"WWW-Authenticate": 'Basic realm="messenger", charset="UTF-8"'}
|
||||
|
||||
|
||||
class NotFoundError(AppError):
|
||||
"""Объекта нет или он чужой: существование чужого объекта не раскрывается"""
|
||||
|
||||
status_code = HTTPStatus.NOT_FOUND
|
||||
code = ErrorCode.NOT_FOUND
|
||||
default_message = "Объект не найден"
|
||||
|
||||
|
||||
class ConflictError(AppError):
|
||||
"""Конфликт состояния, например занятый username"""
|
||||
|
||||
status_code = HTTPStatus.CONFLICT
|
||||
code = ErrorCode.CONFLICT
|
||||
default_message = "Конфликт состояния"
|
||||
|
||||
|
||||
class UnavailableError(AppError):
|
||||
"""Приложение не готово обслуживать запросы"""
|
||||
|
||||
status_code = HTTPStatus.SERVICE_UNAVAILABLE
|
||||
code = ErrorCode.UNAVAILABLE
|
||||
default_message = "Сервис временно недоступен"
|
||||
@@ -0,0 +1,21 @@
|
||||
from pydantic import BaseModel, ConfigDict
|
||||
|
||||
from .codes import ErrorCode
|
||||
|
||||
|
||||
class ErrorBodySchema(BaseModel):
|
||||
"""Тело ошибки"""
|
||||
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
code: ErrorCode
|
||||
message: str
|
||||
request_id: str
|
||||
|
||||
|
||||
class ErrorSchema(BaseModel):
|
||||
"""Единый формат ошибки: {"error": {...}}"""
|
||||
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
error: ErrorBodySchema
|
||||
@@ -0,0 +1,3 @@
|
||||
from .schemas import HealthSchema
|
||||
|
||||
__all__ = ["HealthSchema"]
|
||||
@@ -0,0 +1,9 @@
|
||||
from typing import Literal
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class HealthSchema(BaseModel):
|
||||
"""Ответ health-проверок"""
|
||||
|
||||
status: Literal["ok"] = "ok"
|
||||
@@ -0,0 +1,17 @@
|
||||
from .schemas import (
|
||||
IDEMPOTENCY_KEY_MAX_LENGTH,
|
||||
IDEMPOTENCY_KEY_PATTERN,
|
||||
CreateMessageSchema,
|
||||
IdempotencyKeySchema,
|
||||
MessagePageSchema,
|
||||
MessageSchema,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"IDEMPOTENCY_KEY_PATTERN",
|
||||
"IDEMPOTENCY_KEY_MAX_LENGTH",
|
||||
"CreateMessageSchema",
|
||||
"IdempotencyKeySchema",
|
||||
"MessageSchema",
|
||||
"MessagePageSchema",
|
||||
]
|
||||
@@ -0,0 +1,53 @@
|
||||
from typing import Annotated
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
from domain.utils import NotBlankStr, UtcDateTime
|
||||
|
||||
MessageText = Annotated[NotBlankStr, Field(min_length=1, max_length=4000)]
|
||||
|
||||
IDEMPOTENCY_KEY_PATTERN = r"^[A-Za-z0-9._:-]{1,128}$"
|
||||
IDEMPOTENCY_KEY_MAX_LENGTH = 128
|
||||
|
||||
|
||||
class CreateMessageSchema(BaseModel):
|
||||
"""Запрос отправки сообщения: отправитель берётся из текущей сессии"""
|
||||
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
text: MessageText
|
||||
|
||||
|
||||
class MessageSchema(BaseModel):
|
||||
"""Сообщение"""
|
||||
|
||||
model_config = ConfigDict(frozen=True)
|
||||
|
||||
id: UUID
|
||||
chat_id: UUID
|
||||
sender_id: UUID
|
||||
text: str
|
||||
created_at: UtcDateTime
|
||||
|
||||
|
||||
class MessagePageSchema(BaseModel):
|
||||
"""Страница истории чата"""
|
||||
|
||||
items: list[MessageSchema]
|
||||
next_cursor: str | None
|
||||
|
||||
|
||||
class IdempotencyKeySchema(BaseModel):
|
||||
"""
|
||||
Ключ повторной отправки сообщения.
|
||||
|
||||
request_hash — SHA-256 канонического тела: повтор с тем же ключом и тем же
|
||||
телом возвращает исходное сообщение, с другим телом — конфликт.
|
||||
"""
|
||||
|
||||
model_config = ConfigDict(frozen=True)
|
||||
|
||||
key: str
|
||||
request_hash: bytes
|
||||
ttl_seconds: int
|
||||
@@ -0,0 +1,6 @@
|
||||
from .schemas import PageCursorSchema, PageKey
|
||||
|
||||
__all__ = [
|
||||
"PageKey",
|
||||
"PageCursorSchema",
|
||||
]
|
||||
@@ -0,0 +1,26 @@
|
||||
from datetime import datetime
|
||||
from typing import NamedTuple
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel, ConfigDict
|
||||
|
||||
|
||||
class PageKey(NamedTuple):
|
||||
"""Ключ keyset-пагинации: порядок (created_at, id) по возрастанию"""
|
||||
|
||||
created_at: datetime
|
||||
id: UUID
|
||||
|
||||
|
||||
class PageCursorSchema(BaseModel):
|
||||
"""Содержимое непрозрачного cursor: ключ последнего элемента и его ресурс"""
|
||||
|
||||
model_config = ConfigDict(extra="forbid", frozen=True)
|
||||
|
||||
scope: str
|
||||
created_at: datetime
|
||||
id: UUID
|
||||
|
||||
@property
|
||||
def key(self) -> PageKey:
|
||||
return PageKey(self.created_at, self.id)
|
||||
@@ -0,0 +1,7 @@
|
||||
from .schemas import CreateUserSchema, UserCredentialsSchema, UserSchema
|
||||
|
||||
__all__ = [
|
||||
"CreateUserSchema",
|
||||
"UserSchema",
|
||||
"UserCredentialsSchema",
|
||||
]
|
||||
@@ -0,0 +1,40 @@
|
||||
from typing import Annotated
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field, StrictStr
|
||||
|
||||
from domain.utils import NotBlankStr, UtcDateTime
|
||||
|
||||
Username = Annotated[StrictStr, Field(pattern=r"^[a-z0-9_]{3,32}$")]
|
||||
DisplayName = Annotated[NotBlankStr, Field(min_length=1, max_length=100)]
|
||||
Password = Annotated[StrictStr, Field(min_length=12, max_length=128)]
|
||||
|
||||
|
||||
class CreateUserSchema(BaseModel):
|
||||
"""Запрос регистрации пользователя; длина пароля — в Unicode code points"""
|
||||
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
username: Username
|
||||
display_name: DisplayName
|
||||
password: Password = Field(json_schema_extra={"writeOnly": True})
|
||||
|
||||
|
||||
class UserSchema(BaseModel):
|
||||
"""Пользователь: пароль и его hash сюда никогда не попадают"""
|
||||
|
||||
model_config = ConfigDict(frozen=True)
|
||||
|
||||
id: UUID
|
||||
username: str
|
||||
display_name: str
|
||||
created_at: UtcDateTime
|
||||
|
||||
|
||||
class UserCredentialsSchema(BaseModel):
|
||||
"""Пользователь вместе с hash пароля — только для проверки входа"""
|
||||
|
||||
model_config = ConfigDict(frozen=True)
|
||||
|
||||
user: UserSchema
|
||||
password_hash: str
|
||||
@@ -0,0 +1,15 @@
|
||||
from .types import (
|
||||
NotBlankStr,
|
||||
UtcDateTime,
|
||||
ensure_not_blank,
|
||||
ensure_storable,
|
||||
to_rfc3339_utc,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"NotBlankStr",
|
||||
"UtcDateTime",
|
||||
"ensure_not_blank",
|
||||
"ensure_storable",
|
||||
"to_rfc3339_utc",
|
||||
]
|
||||
@@ -0,0 +1,36 @@
|
||||
from datetime import UTC, datetime
|
||||
from typing import Annotated
|
||||
|
||||
from pydantic import AfterValidator, PlainSerializer, StrictStr
|
||||
|
||||
NUL_CHARACTER = "\x00"
|
||||
|
||||
|
||||
def ensure_not_blank(value: str) -> str:
|
||||
"""Обязательная строка не может состоять только из пробельных символов"""
|
||||
if not value.strip():
|
||||
raise ValueError("Строка не может быть пустой или пробельной")
|
||||
return value
|
||||
|
||||
|
||||
def ensure_storable(value: str) -> str:
|
||||
"""Тип text в потсгре"""
|
||||
if NUL_CHARACTER in value:
|
||||
raise ValueError("Строка не может содержать символ NUL")
|
||||
return value
|
||||
|
||||
|
||||
def to_rfc3339_utc(value: datetime) -> str:
|
||||
"""RFC 3339 в UTC с суффиксом Z и микросекундами"""
|
||||
return (
|
||||
value.astimezone(UTC).isoformat(timespec="microseconds").replace("+00:00", "Z")
|
||||
)
|
||||
|
||||
|
||||
NotBlankStr = Annotated[
|
||||
StrictStr, AfterValidator(ensure_not_blank), AfterValidator(ensure_storable)
|
||||
]
|
||||
|
||||
UtcDateTime = Annotated[
|
||||
datetime, PlainSerializer(to_rfc3339_utc, return_type=str, when_used="json")
|
||||
]
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
import asyncio
|
||||
import socket
|
||||
from collections.abc import AsyncIterator
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
import uvicorn
|
||||
from fastapi import FastAPI
|
||||
|
||||
from application import api_endpoints, health_endpoints
|
||||
from application.api.handlers import register_exception_handlers
|
||||
from application.api.middlewares import RequestContextMiddleware
|
||||
from config import (
|
||||
APP_ENV,
|
||||
APP_HOST,
|
||||
APP_KEEPALIVE_TIMEOUT_SEC,
|
||||
APP_PORT,
|
||||
APP_REVISION,
|
||||
DB_STARTUP_TIMEOUT_SEC,
|
||||
INSTANCE_ID,
|
||||
INSTANCE_ID_FILE,
|
||||
LOGGER,
|
||||
PASSWORD_HASH_WORKERS,
|
||||
SESSION_TTL_SECONDS,
|
||||
SHUTDOWN_DRAIN_SEC,
|
||||
SHUTDOWN_TIMEOUT_SEC,
|
||||
)
|
||||
from infrastructure import PasswordHasher, cache_manager, database_pool_manager
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI) -> AsyncIterator[None]:
|
||||
"""
|
||||
uvicorn открывает порт только после startup: реплика без БД или со старой
|
||||
схемой не принимает соединений, и LB отправляет запросы другим репликам.
|
||||
"""
|
||||
await database_pool_manager.create_pool(DB_STARTUP_TIMEOUT_SEC)
|
||||
await database_pool_manager.check_migrations()
|
||||
cache_manager.create_client()
|
||||
password_hasher = PasswordHasher(PASSWORD_HASH_WORKERS)
|
||||
app.state.database = database_pool_manager
|
||||
app.state.cache = cache_manager
|
||||
app.state.password_hasher = password_hasher
|
||||
INSTANCE_ID_FILE.write_text(INSTANCE_ID)
|
||||
app.state.is_ready = True
|
||||
LOGGER.info(
|
||||
f"Application started, env={APP_ENV}, revision={APP_REVISION}, "
|
||||
f"instance={INSTANCE_ID}, session ttl={SESSION_TTL_SECONDS} s"
|
||||
)
|
||||
yield
|
||||
app.state.is_ready = False
|
||||
await cache_manager.close()
|
||||
await database_pool_manager.close()
|
||||
password_hasher.close()
|
||||
LOGGER.info(f"Application stopped, instance={INSTANCE_ID}, connections closed")
|
||||
|
||||
|
||||
app = FastAPI(
|
||||
title="mtuci-labs-messanger",
|
||||
description="мессенджер-конкурент-максу",
|
||||
version="1.3.0",
|
||||
lifespan=lifespan,
|
||||
)
|
||||
|
||||
app.add_middleware(RequestContextMiddleware)
|
||||
register_exception_handlers(app)
|
||||
|
||||
app.include_router(health_endpoints)
|
||||
app.include_router(api_endpoints)
|
||||
|
||||
|
||||
class DrainingServer(uvicorn.Server):
|
||||
"""
|
||||
SIGTERM: сначала drain, затем штатный graceful shutdown uvicorn.
|
||||
"""
|
||||
|
||||
def __init__(self, config: uvicorn.Config, drain_seconds: float) -> None:
|
||||
super().__init__(config)
|
||||
self._drain_seconds = drain_seconds
|
||||
|
||||
async def shutdown(self, sockets: list[socket.socket] | None = None) -> None:
|
||||
for server in getattr(self, "servers", []):
|
||||
server.close()
|
||||
app.state.is_ready = False
|
||||
app.state.is_draining = True
|
||||
LOGGER.info(
|
||||
f"SIGTERM: drain {self._drain_seconds} с, instance={INSTANCE_ID}: "
|
||||
"порт закрыт, keep-alive соединения закрываются после ответа"
|
||||
)
|
||||
await asyncio.sleep(self._drain_seconds)
|
||||
await super().shutdown(sockets)
|
||||
|
||||
|
||||
def main(
|
||||
host: str,
|
||||
port: int,
|
||||
shutdown_timeout: int,
|
||||
keepalive_timeout: int,
|
||||
drain_seconds: float,
|
||||
) -> None:
|
||||
"""
|
||||
Функция для запуска api.
|
||||
|
||||
SIGTERM: drain_seconds без приёма новых соединений, затем uvicorn ждёт
|
||||
текущих запросов не дольше shutdown_timeout, и lifespan закрывает пулы.
|
||||
Сумма меньше stop_grace_period контейнера (10 с).
|
||||
"""
|
||||
config = uvicorn.Config(
|
||||
app,
|
||||
host=host,
|
||||
port=port,
|
||||
timeout_graceful_shutdown=shutdown_timeout,
|
||||
timeout_keep_alive=keepalive_timeout,
|
||||
access_log=False,
|
||||
server_header=False,
|
||||
)
|
||||
DrainingServer(config, drain_seconds).run()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(
|
||||
host=APP_HOST,
|
||||
port=APP_PORT,
|
||||
shutdown_timeout=SHUTDOWN_TIMEOUT_SEC,
|
||||
keepalive_timeout=APP_KEEPALIVE_TIMEOUT_SEC,
|
||||
drain_seconds=SHUTDOWN_DRAIN_SEC,
|
||||
)
|
||||
@@ -0,0 +1,20 @@
|
||||
import sys
|
||||
import urllib.request
|
||||
|
||||
from config import APP_PORT
|
||||
|
||||
READY_URL = f"http://127.0.0.1:{APP_PORT}/health/ready"
|
||||
|
||||
REQUEST_TIMEOUT_SEC = 2
|
||||
|
||||
|
||||
def main() -> int:
|
||||
try:
|
||||
with urllib.request.urlopen(READY_URL, timeout=REQUEST_TIMEOUT_SEC) as response:
|
||||
return 0 if response.status == 200 else 1
|
||||
except OSError:
|
||||
return 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,4 @@
|
||||
from .dependencies import cache_manager, database_pool_manager
|
||||
from .security import PasswordHasher
|
||||
|
||||
__all__ = ["database_pool_manager", "cache_manager", "PasswordHasher"]
|
||||
@@ -0,0 +1,13 @@
|
||||
from .cache import CacheManager, cache_manager, get_cache
|
||||
from .database import DatabasePoolManager, database_pool_manager, get_database
|
||||
from .service_creator import ServiceCreator
|
||||
|
||||
__all__ = [
|
||||
"ServiceCreator",
|
||||
"DatabasePoolManager",
|
||||
"database_pool_manager",
|
||||
"get_database",
|
||||
"CacheManager",
|
||||
"cache_manager",
|
||||
"get_cache",
|
||||
]
|
||||
@@ -0,0 +1,94 @@
|
||||
import time
|
||||
|
||||
from redis.asyncio import BlockingConnectionPool, Redis
|
||||
from redis.asyncio.retry import Retry
|
||||
from redis.backoff import NoBackoff
|
||||
from starlette.requests import HTTPConnection
|
||||
|
||||
from config import (
|
||||
LOGGER,
|
||||
VALKEY_HOST,
|
||||
VALKEY_PASS,
|
||||
VALKEY_POOL_MAX_SIZE,
|
||||
VALKEY_PORT,
|
||||
VALKEY_RETRY_AFTER_SEC,
|
||||
VALKEY_TIMEOUT_SEC,
|
||||
)
|
||||
|
||||
|
||||
class CacheManager:
|
||||
"""
|
||||
Клиент Valkey: ограниченный пул, короткие таймауты, без встроенных повторов.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
host: str,
|
||||
port: int,
|
||||
password: str,
|
||||
max_size: int,
|
||||
timeout: float,
|
||||
retry_after: float,
|
||||
) -> None:
|
||||
self._host = host
|
||||
self._port = port
|
||||
self._password = password
|
||||
self.max_size = max_size
|
||||
self.timeout = timeout
|
||||
self.retry_after = retry_after
|
||||
self.client: Redis | None = None
|
||||
self._disabled_until = 0.0
|
||||
|
||||
def create_client(self) -> Redis:
|
||||
"""Создание клиента; соединения открываются лениво при первом запросе"""
|
||||
if self.client is None:
|
||||
pool = BlockingConnectionPool(
|
||||
host=self._host,
|
||||
port=self._port,
|
||||
password=self._password,
|
||||
max_connections=self.max_size,
|
||||
timeout=self.timeout,
|
||||
socket_timeout=self.timeout,
|
||||
socket_connect_timeout=self.timeout,
|
||||
retry=Retry(NoBackoff(), 0),
|
||||
)
|
||||
self.client = Redis(connection_pool=pool)
|
||||
LOGGER.info(
|
||||
f"Клиент Valkey создан: {self._host}:{self._port}, "
|
||||
f"pool: {self.max_size}, timeout: {self.timeout} с"
|
||||
)
|
||||
return self.client
|
||||
|
||||
@property
|
||||
def is_available(self) -> bool:
|
||||
return time.monotonic() >= self._disabled_until
|
||||
|
||||
def mark_failed(self, error: Exception) -> None:
|
||||
"""Обход кэша на retry_after секунд после ошибки Valkey"""
|
||||
if self.is_available:
|
||||
LOGGER.warning(
|
||||
f"Valkey недоступен ({type(error).__name__}): кэш сессий "
|
||||
f"обходится {self.retry_after} с, запросы идут в PostgreSQL"
|
||||
)
|
||||
self._disabled_until = time.monotonic() + self.retry_after
|
||||
|
||||
async def close(self) -> None:
|
||||
if self.client is not None:
|
||||
await self.client.aclose()
|
||||
self.client = None
|
||||
|
||||
|
||||
def get_cache(connection: HTTPConnection) -> CacheManager:
|
||||
"""Получение менеджера кэша, созданного в lifespan."""
|
||||
cache: CacheManager = connection.app.state.cache
|
||||
return cache
|
||||
|
||||
|
||||
cache_manager = CacheManager(
|
||||
host=VALKEY_HOST,
|
||||
port=VALKEY_PORT,
|
||||
password=VALKEY_PASS,
|
||||
max_size=VALKEY_POOL_MAX_SIZE,
|
||||
timeout=VALKEY_TIMEOUT_SEC,
|
||||
retry_after=VALKEY_RETRY_AFTER_SEC,
|
||||
)
|
||||
@@ -0,0 +1,236 @@
|
||||
import asyncio
|
||||
import time
|
||||
from collections.abc import AsyncIterator
|
||||
from contextlib import asynccontextmanager
|
||||
from pathlib import Path
|
||||
|
||||
import asyncpg
|
||||
from starlette.requests import HTTPConnection
|
||||
|
||||
from config import (
|
||||
DB_COMMAND_TIMEOUT_SEC,
|
||||
DB_CONNECT_TIMEOUT_SEC,
|
||||
DB_HOST,
|
||||
DB_NAME,
|
||||
DB_PASS,
|
||||
DB_POOL_MAX_SIZE,
|
||||
DB_POOL_MIN_SIZE,
|
||||
DB_PORT,
|
||||
DB_USER,
|
||||
LOGGER,
|
||||
)
|
||||
from infrastructure.utils import DATABASE_UNAVAILABLE_ERRORS
|
||||
|
||||
MIGRATIONS_DIR = Path(__file__).resolve().parents[3] / "migrations"
|
||||
|
||||
MIGRATIONS_LOCK_NAMESPACE = 1
|
||||
|
||||
STARTUP_RETRY_INTERVAL_SEC = 1
|
||||
|
||||
BROKEN_CONNECTION_ERRORS: tuple[type[Exception], ...] = (
|
||||
asyncpg.PostgresConnectionError,
|
||||
asyncpg.InterfaceError,
|
||||
asyncpg.exceptions.OperatorInterventionError,
|
||||
)
|
||||
|
||||
MIGRATIONS_TIMEOUT_SEC = 300
|
||||
|
||||
|
||||
class DatabasePoolManager:
|
||||
"""Класс для управления пулом соединений и версионируемыми миграциями."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
user: str,
|
||||
password: str,
|
||||
db: str,
|
||||
host: str,
|
||||
port: int,
|
||||
min_size: int,
|
||||
max_size: int,
|
||||
connect_timeout: float,
|
||||
command_timeout: float,
|
||||
) -> None:
|
||||
self._user = user
|
||||
self._password = password
|
||||
self._host = host
|
||||
self._port = port
|
||||
self._database = db
|
||||
self.min_size = min_size
|
||||
self.max_size = max_size
|
||||
self.connect_timeout = connect_timeout
|
||||
self.command_timeout = command_timeout
|
||||
self.pool: asyncpg.Pool | None = None
|
||||
|
||||
async def create_pool(self, startup_timeout: float) -> asyncpg.Pool:
|
||||
"""
|
||||
Создание ограниченного пула соединений.
|
||||
"""
|
||||
LOGGER.info("Установка соединения с базой данных psql")
|
||||
deadline = time.monotonic() + startup_timeout
|
||||
while self.pool is None:
|
||||
try:
|
||||
self.pool = await asyncpg.create_pool(
|
||||
user=self._user,
|
||||
password=self._password,
|
||||
host=self._host,
|
||||
port=self._port,
|
||||
database=self._database,
|
||||
min_size=self.min_size,
|
||||
max_size=self.max_size,
|
||||
timeout=self.connect_timeout,
|
||||
command_timeout=self.command_timeout,
|
||||
server_settings={
|
||||
"application_name": "messenger-api",
|
||||
"statement_timeout": str(int(self.command_timeout * 1000)),
|
||||
},
|
||||
)
|
||||
except (*DATABASE_UNAVAILABLE_ERRORS, asyncpg.PostgresError) as error:
|
||||
if time.monotonic() >= deadline:
|
||||
LOGGER.error(
|
||||
"Не удалось подключиться к psql за "
|
||||
f"{startup_timeout} с: {type(error).__name__}"
|
||||
)
|
||||
raise
|
||||
LOGGER.warning(
|
||||
f"PostgreSQL ещё недоступен: {type(error).__name__}, повтор"
|
||||
)
|
||||
await asyncio.sleep(STARTUP_RETRY_INTERVAL_SEC)
|
||||
LOGGER.info(
|
||||
f"Соединение с базой данных psql установлено. DB_NAME: "
|
||||
f"{self._database}, pool: {self.min_size}..{self.max_size}"
|
||||
)
|
||||
return self.pool
|
||||
|
||||
async def apply_migrations(self, directory: Path = MIGRATIONS_DIR) -> None:
|
||||
"""
|
||||
Применение миграций NNNN_name.sql по порядку версий.
|
||||
|
||||
Вызывается однократно отдельным процессом (src/migrate.py, сервис migrate)
|
||||
до запуска API-реплик, а не каждой репликой при старте.
|
||||
Каждая миграция и запись о ней в schema_migrations — одна транзакция.
|
||||
Advisory lock — страховка от двух одновременных запусков migrate;
|
||||
уже применённые версии пропускаются, поэтому повторный запуск безопасен.
|
||||
"""
|
||||
async with self.acquire() as conn:
|
||||
await conn.execute("SET statement_timeout = 0;")
|
||||
await conn.execute(
|
||||
"SELECT pg_advisory_lock($1, 0);",
|
||||
MIGRATIONS_LOCK_NAMESPACE,
|
||||
timeout=MIGRATIONS_TIMEOUT_SEC,
|
||||
)
|
||||
try:
|
||||
await conn.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS schema_migrations (
|
||||
version integer PRIMARY KEY,
|
||||
name text NOT NULL,
|
||||
applied_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
"""
|
||||
)
|
||||
applied = {
|
||||
record["version"]
|
||||
for record in await conn.fetch(
|
||||
"SELECT version FROM schema_migrations;"
|
||||
)
|
||||
}
|
||||
for version, path in self._migration_files(directory):
|
||||
if version in applied:
|
||||
continue
|
||||
async with conn.transaction():
|
||||
await conn.execute(
|
||||
path.read_text(encoding="utf-8"),
|
||||
timeout=MIGRATIONS_TIMEOUT_SEC,
|
||||
)
|
||||
await conn.execute(
|
||||
"INSERT INTO schema_migrations (version, name) "
|
||||
"VALUES ($1, $2);",
|
||||
version,
|
||||
path.stem,
|
||||
)
|
||||
LOGGER.info(f"Применена миграция {path.name}")
|
||||
finally:
|
||||
await conn.execute(
|
||||
"SELECT pg_advisory_unlock($1, 0);", MIGRATIONS_LOCK_NAMESPACE
|
||||
)
|
||||
LOGGER.info("Схема БД в актуальной версии")
|
||||
|
||||
async def check_migrations(self, directory: Path = MIGRATIONS_DIR) -> None:
|
||||
"""
|
||||
Схема БД не старше кода: все миграции из образа уже применены.
|
||||
|
||||
API миграции не применяет. Если migrate ещё не отработал, реплика не
|
||||
стартует и не начинает слушать порт, балансировщик её не использует
|
||||
|
||||
"""
|
||||
expected = {version for version, _ in self._migration_files(directory)}
|
||||
async with self.acquire() as conn:
|
||||
try:
|
||||
records = await conn.fetch("SELECT version FROM schema_migrations;")
|
||||
except asyncpg.UndefinedTableError:
|
||||
records = []
|
||||
applied = {record["version"] for record in records}
|
||||
missing = sorted(expected - applied)
|
||||
if missing:
|
||||
raise RuntimeError(
|
||||
f"В БД не применены миграции {missing}: сначала запустите migrate"
|
||||
)
|
||||
LOGGER.info(f"Схема БД совместима: версия {max(applied, default=0)}")
|
||||
|
||||
@staticmethod
|
||||
def _migration_files(directory: Path) -> list[tuple[int, Path]]:
|
||||
"""Файлы NNNN_name.sql по возрастанию версии"""
|
||||
return sorted(
|
||||
(int(path.name.split("_", 1)[0]), path) for path in directory.glob("*.sql")
|
||||
)
|
||||
|
||||
@asynccontextmanager
|
||||
async def acquire(self) -> AsyncIterator[asyncpg.Connection]:
|
||||
"""
|
||||
Получение соединения из пула с ограниченным ожиданием свободного.
|
||||
|
||||
После рестарта PostgreSQL все соединения пула мертвы. На первой же
|
||||
ошибке соединения пул помечает их на переподключение, чтобы 503
|
||||
получил один запрос, а не каждый из max_size следующих.
|
||||
"""
|
||||
assert self.pool is not None
|
||||
try:
|
||||
async with self.pool.acquire(timeout=self.connect_timeout) as connection:
|
||||
yield connection
|
||||
except BROKEN_CONNECTION_ERRORS:
|
||||
await self.pool.expire_connections()
|
||||
raise
|
||||
|
||||
async def check(self) -> bool:
|
||||
"""PostgreSQL отвечает на запрос — для readiness"""
|
||||
try:
|
||||
async with self.acquire() as conn:
|
||||
await conn.fetchval("SELECT 1;", timeout=self.connect_timeout)
|
||||
except DATABASE_UNAVAILABLE_ERRORS:
|
||||
return False
|
||||
return True
|
||||
|
||||
async def close(self) -> None:
|
||||
if self.pool:
|
||||
await self.pool.close()
|
||||
self.pool = None
|
||||
|
||||
|
||||
def get_database(connection: HTTPConnection) -> DatabasePoolManager:
|
||||
"""Получение менеджера пула соединений, созданного в lifespan."""
|
||||
database: DatabasePoolManager = connection.app.state.database
|
||||
return database
|
||||
|
||||
|
||||
database_pool_manager = DatabasePoolManager(
|
||||
user=DB_USER,
|
||||
password=DB_PASS,
|
||||
db=DB_NAME,
|
||||
host=DB_HOST,
|
||||
port=DB_PORT,
|
||||
min_size=DB_POOL_MIN_SIZE,
|
||||
max_size=DB_POOL_MAX_SIZE,
|
||||
connect_timeout=DB_CONNECT_TIMEOUT_SEC,
|
||||
command_timeout=DB_COMMAND_TIMEOUT_SEC,
|
||||
)
|
||||
@@ -0,0 +1,99 @@
|
||||
from fastapi import Depends
|
||||
|
||||
from config import (
|
||||
IDEMPOTENCY_TTL_SECONDS,
|
||||
SESSION_CACHE_TTL_SECONDS,
|
||||
SESSION_TTL_SECONDS,
|
||||
)
|
||||
from infrastructure.dependencies.cache import CacheManager, get_cache
|
||||
from infrastructure.dependencies.database import DatabasePoolManager, get_database
|
||||
from infrastructure.repositories import (
|
||||
PostgresChatRepository,
|
||||
PostgresMessageRepository,
|
||||
PostgresSessionRepository,
|
||||
PostgresUserRepository,
|
||||
ValkeySessionCache,
|
||||
)
|
||||
from infrastructure.security import PasswordHasher, get_password_hasher
|
||||
from infrastructure.services import (
|
||||
AuthService,
|
||||
ChatService,
|
||||
MessageService,
|
||||
UserService,
|
||||
)
|
||||
from interfaces import (
|
||||
ChatRepository,
|
||||
MessageRepository,
|
||||
SessionCache,
|
||||
SessionRepository,
|
||||
UserRepository,
|
||||
)
|
||||
|
||||
|
||||
class ServiceCreator:
|
||||
@staticmethod
|
||||
def _get_user_repository(
|
||||
database: DatabasePoolManager = Depends(get_database),
|
||||
) -> UserRepository:
|
||||
return PostgresUserRepository(database)
|
||||
|
||||
@staticmethod
|
||||
def _get_chat_repository(
|
||||
database: DatabasePoolManager = Depends(get_database),
|
||||
) -> ChatRepository:
|
||||
return PostgresChatRepository(database)
|
||||
|
||||
@staticmethod
|
||||
def _get_message_repository(
|
||||
database: DatabasePoolManager = Depends(get_database),
|
||||
) -> MessageRepository:
|
||||
return PostgresMessageRepository(database)
|
||||
|
||||
@staticmethod
|
||||
def _get_session_repository(
|
||||
database: DatabasePoolManager = Depends(get_database),
|
||||
) -> SessionRepository:
|
||||
return PostgresSessionRepository(database)
|
||||
|
||||
@staticmethod
|
||||
def _get_session_cache(
|
||||
cache: CacheManager = Depends(get_cache),
|
||||
) -> SessionCache:
|
||||
return ValkeySessionCache(cache, SESSION_CACHE_TTL_SECONDS)
|
||||
|
||||
@staticmethod
|
||||
def get_user_service(
|
||||
repository: UserRepository = Depends(_get_user_repository),
|
||||
hasher: PasswordHasher = Depends(get_password_hasher),
|
||||
) -> UserService:
|
||||
return UserService(repository, hasher)
|
||||
|
||||
@staticmethod
|
||||
def get_auth_service(
|
||||
user_repository: UserRepository = Depends(_get_user_repository),
|
||||
session_repository: SessionRepository = Depends(_get_session_repository),
|
||||
session_cache: SessionCache = Depends(_get_session_cache),
|
||||
hasher: PasswordHasher = Depends(get_password_hasher),
|
||||
) -> AuthService:
|
||||
return AuthService(
|
||||
user_repository,
|
||||
session_repository,
|
||||
session_cache,
|
||||
hasher,
|
||||
SESSION_TTL_SECONDS,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def get_chat_service(
|
||||
chat_repository: ChatRepository = Depends(_get_chat_repository),
|
||||
) -> ChatService:
|
||||
return ChatService(chat_repository)
|
||||
|
||||
@staticmethod
|
||||
def get_message_service(
|
||||
chat_repository: ChatRepository = Depends(_get_chat_repository),
|
||||
message_repository: MessageRepository = Depends(_get_message_repository),
|
||||
) -> MessageService:
|
||||
return MessageService(
|
||||
chat_repository, message_repository, IDEMPOTENCY_TTL_SECONDS
|
||||
)
|
||||
@@ -0,0 +1,13 @@
|
||||
from .chat_repository import PostgresChatRepository
|
||||
from .message_repository import PostgresMessageRepository
|
||||
from .session_cache import ValkeySessionCache
|
||||
from .session_repository import PostgresSessionRepository
|
||||
from .user_repository import PostgresUserRepository
|
||||
|
||||
__all__ = [
|
||||
"PostgresUserRepository",
|
||||
"PostgresChatRepository",
|
||||
"PostgresMessageRepository",
|
||||
"PostgresSessionRepository",
|
||||
"ValkeySessionCache",
|
||||
]
|
||||
@@ -0,0 +1,3 @@
|
||||
from .postgres import PostgresChatRepository
|
||||
|
||||
__all__ = ["PostgresChatRepository"]
|
||||
@@ -0,0 +1,150 @@
|
||||
from uuid import UUID
|
||||
|
||||
import asyncpg
|
||||
|
||||
from domain import ChatSchema, PageKey
|
||||
from domain.errors import InvalidRequestError
|
||||
from infrastructure.utils import Pool, repository_logs, transaction
|
||||
from interfaces import ChatRepository
|
||||
|
||||
|
||||
class PostgresChatRepository(ChatRepository):
|
||||
def __init__(self, pool: Pool) -> None:
|
||||
self._pool = pool
|
||||
|
||||
@repository_logs
|
||||
async def create(
|
||||
self, title: str, created_by: UUID, member_ids: list[UUID]
|
||||
) -> ChatSchema:
|
||||
"""
|
||||
Чат и участники — одна транзакция. Неизвестный участник нарушает FK
|
||||
на users, транзакция откатывается целиком, строка chats не остаётся.
|
||||
Участники вставляются одним запросом; position — порядок в member_ids.
|
||||
"""
|
||||
chat_query = """
|
||||
INSERT INTO chats
|
||||
(
|
||||
title,
|
||||
created_by
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
$1,
|
||||
$2
|
||||
)
|
||||
RETURNING id, title, created_at;
|
||||
"""
|
||||
members_query = """
|
||||
INSERT INTO chat_members
|
||||
(
|
||||
chat_id,
|
||||
user_id,
|
||||
position
|
||||
)
|
||||
SELECT
|
||||
$1,
|
||||
member.user_id,
|
||||
member.ordinality - 1
|
||||
FROM unnest($2::uuid[]) WITH ORDINALITY AS member(user_id, ordinality);
|
||||
"""
|
||||
try:
|
||||
async with transaction(self._pool) as conn:
|
||||
row = await conn.fetchrow(chat_query, title, created_by)
|
||||
assert row is not None
|
||||
await conn.execute(members_query, row["id"], member_ids)
|
||||
except asyncpg.ForeignKeyViolationError:
|
||||
raise InvalidRequestError(
|
||||
"member_ids содержит неизвестных пользователей"
|
||||
) from None
|
||||
return ChatSchema(
|
||||
id=row["id"],
|
||||
title=row["title"],
|
||||
member_ids=member_ids,
|
||||
created_at=row["created_at"],
|
||||
)
|
||||
|
||||
@repository_logs
|
||||
async def get_for_member(self, chat_id: UUID, member_id: UUID) -> ChatSchema | None:
|
||||
query = """
|
||||
SELECT
|
||||
chat.id,
|
||||
chat.title,
|
||||
chat.created_at,
|
||||
ARRAY(
|
||||
SELECT member.user_id
|
||||
FROM chat_members member
|
||||
WHERE member.chat_id = chat.id
|
||||
ORDER BY member.position
|
||||
) AS member_ids
|
||||
FROM chats chat
|
||||
WHERE
|
||||
chat.id = $1
|
||||
AND EXISTS
|
||||
(
|
||||
SELECT 1
|
||||
FROM chat_members caller
|
||||
WHERE caller.chat_id = chat.id AND caller.user_id = $2
|
||||
);
|
||||
"""
|
||||
async with self._pool.acquire() as conn:
|
||||
row = await conn.fetchrow(query, chat_id, member_id)
|
||||
return self._row_to_chat(row) if row is not None else None
|
||||
|
||||
@repository_logs
|
||||
async def list_for_member(
|
||||
self, member_id: UUID, after: PageKey | None, limit: int
|
||||
) -> list[ChatSchema]:
|
||||
"""Чаты пользователя по (created_at, id); со второй страницы — после ключа"""
|
||||
first_page_query = """
|
||||
SELECT
|
||||
chat.id,
|
||||
chat.title,
|
||||
chat.created_at,
|
||||
ARRAY(
|
||||
SELECT member.user_id
|
||||
FROM chat_members member
|
||||
WHERE member.chat_id = chat.id
|
||||
ORDER BY member.position
|
||||
) AS member_ids
|
||||
FROM chat_members caller
|
||||
JOIN chats chat ON chat.id = caller.chat_id
|
||||
WHERE caller.user_id = $1
|
||||
ORDER BY chat.created_at, chat.id
|
||||
LIMIT $2;
|
||||
"""
|
||||
next_page_query = """
|
||||
SELECT
|
||||
chat.id,
|
||||
chat.title,
|
||||
chat.created_at,
|
||||
ARRAY(
|
||||
SELECT member.user_id
|
||||
FROM chat_members member
|
||||
WHERE member.chat_id = chat.id
|
||||
ORDER BY member.position
|
||||
) AS member_ids
|
||||
FROM chat_members caller
|
||||
JOIN chats chat ON chat.id = caller.chat_id
|
||||
WHERE
|
||||
caller.user_id = $1
|
||||
AND (chat.created_at, chat.id) > ($2, $3)
|
||||
ORDER BY chat.created_at, chat.id
|
||||
LIMIT $4;
|
||||
"""
|
||||
async with self._pool.acquire() as conn:
|
||||
if after is None:
|
||||
rows = await conn.fetch(first_page_query, member_id, limit)
|
||||
else:
|
||||
rows = await conn.fetch(
|
||||
next_page_query, member_id, after.created_at, after.id, limit
|
||||
)
|
||||
return [self._row_to_chat(row) for row in rows]
|
||||
|
||||
@staticmethod
|
||||
def _row_to_chat(row: asyncpg.Record) -> ChatSchema:
|
||||
return ChatSchema(
|
||||
id=row["id"],
|
||||
title=row["title"],
|
||||
member_ids=row["member_ids"],
|
||||
created_at=row["created_at"],
|
||||
)
|
||||
@@ -0,0 +1,3 @@
|
||||
from .postgres import PostgresMessageRepository
|
||||
|
||||
__all__ = ["PostgresMessageRepository"]
|
||||
@@ -0,0 +1,179 @@
|
||||
from uuid import UUID
|
||||
|
||||
import asyncpg
|
||||
|
||||
from domain import IdempotencyKeySchema, MessageSchema, PageKey
|
||||
from domain.errors import ConflictError
|
||||
from infrastructure.utils import Pool, repository_logs, transaction
|
||||
from interfaces import MessageRepository
|
||||
|
||||
# Пространство ключей advisory lock: (namespace, hashtext(chat_id))
|
||||
CHAT_LOCK_NAMESPACE = 2
|
||||
|
||||
|
||||
class PostgresMessageRepository(MessageRepository):
|
||||
def __init__(self, pool: Pool) -> None:
|
||||
self._pool = pool
|
||||
|
||||
@repository_logs
|
||||
async def create(
|
||||
self,
|
||||
chat_id: UUID,
|
||||
sender_id: UUID,
|
||||
text: str,
|
||||
idempotency: IdempotencyKeySchema,
|
||||
) -> tuple[MessageSchema, bool]:
|
||||
"""
|
||||
Блокировка чата, проверка ключа, вставка сообщения и ключа — одна
|
||||
транзакция; lock снимается на COMMIT.
|
||||
|
||||
Запись в один чат сериализуется, записи в разные чаты не мешают друг другу.
|
||||
Ключ идемпотентности привязан к чату, поэтому та же блокировка
|
||||
сериализует и одновременные повторы с одним ключом с разных реплик:
|
||||
второй ждёт COMMIT первого и находит его ключ
|
||||
|
||||
created_at строго больше последнего в чате, даже если часы ушли назад:
|
||||
порядок коммитов совпадает с порядком (created_at, id), и читатель
|
||||
с cursor не пропустит сообщение. MAX берётся из индекса
|
||||
(chat_id, created_at, id) — Index Only Scan Backward, а не проход по истории.
|
||||
"""
|
||||
lock_query = """
|
||||
SELECT pg_advisory_xact_lock($1, hashtext($2::uuid::text));
|
||||
"""
|
||||
find_key_query = """
|
||||
SELECT
|
||||
idem.request_hash,
|
||||
message.id,
|
||||
message.chat_id,
|
||||
message.sender_id,
|
||||
message.text,
|
||||
message.created_at
|
||||
FROM message_idempotency_keys idem
|
||||
JOIN messages message ON message.id = idem.message_id
|
||||
WHERE
|
||||
idem.sender_id = $1
|
||||
AND idem.chat_id = $2
|
||||
AND idem.idempotency_key = $3
|
||||
AND idem.expires_at > clock_timestamp();
|
||||
"""
|
||||
insert_query = """
|
||||
INSERT INTO messages
|
||||
(
|
||||
chat_id,
|
||||
sender_id,
|
||||
text,
|
||||
created_at
|
||||
)
|
||||
SELECT
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
GREATEST(clock_timestamp(), MAX(created_at) + interval '1 microsecond')
|
||||
FROM messages
|
||||
WHERE chat_id = $1
|
||||
RETURNING id, chat_id, sender_id, text, created_at;
|
||||
"""
|
||||
|
||||
save_key_query = """
|
||||
INSERT INTO message_idempotency_keys
|
||||
(
|
||||
sender_id,
|
||||
chat_id,
|
||||
idempotency_key,
|
||||
request_hash,
|
||||
message_id,
|
||||
expires_at
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
$4,
|
||||
$5,
|
||||
clock_timestamp() + make_interval(secs => $6::integer)
|
||||
)
|
||||
ON CONFLICT (sender_id, chat_id, idempotency_key) DO UPDATE
|
||||
SET
|
||||
request_hash = EXCLUDED.request_hash,
|
||||
message_id = EXCLUDED.message_id,
|
||||
created_at = EXCLUDED.created_at,
|
||||
expires_at = EXCLUDED.expires_at;
|
||||
"""
|
||||
async with transaction(self._pool) as conn:
|
||||
await conn.execute(lock_query, CHAT_LOCK_NAMESPACE, chat_id)
|
||||
stored = await conn.fetchrow(
|
||||
find_key_query, sender_id, chat_id, idempotency.key
|
||||
)
|
||||
if stored is not None:
|
||||
if stored["request_hash"] != idempotency.request_hash:
|
||||
raise ConflictError(
|
||||
"Idempotency-Key уже использован с другим телом запроса"
|
||||
)
|
||||
return self._row_to_message(stored), False
|
||||
row = await conn.fetchrow(insert_query, chat_id, sender_id, text)
|
||||
assert row is not None
|
||||
await conn.execute(
|
||||
save_key_query,
|
||||
sender_id,
|
||||
chat_id,
|
||||
idempotency.key,
|
||||
idempotency.request_hash,
|
||||
row["id"],
|
||||
idempotency.ttl_seconds,
|
||||
)
|
||||
return self._row_to_message(row), True
|
||||
|
||||
@repository_logs
|
||||
async def list_for_chat(
|
||||
self, chat_id: UUID, after: PageKey | None, limit: int
|
||||
) -> list[MessageSchema]:
|
||||
"""
|
||||
История по (created_at, id). Со второй страницы Index Cond по
|
||||
(chat_id, created_at, id) начинает чтение сразу после cursor
|
||||
и останавливается после LIMIT строк.
|
||||
"""
|
||||
first_page_query = """
|
||||
SELECT
|
||||
id,
|
||||
chat_id,
|
||||
sender_id,
|
||||
text,
|
||||
created_at
|
||||
FROM messages
|
||||
WHERE chat_id = $1
|
||||
ORDER BY created_at, id
|
||||
LIMIT $2;
|
||||
"""
|
||||
next_page_query = """
|
||||
SELECT
|
||||
id,
|
||||
chat_id,
|
||||
sender_id,
|
||||
text,
|
||||
created_at
|
||||
FROM messages
|
||||
WHERE
|
||||
chat_id = $1
|
||||
AND (created_at, id) > ($2, $3)
|
||||
ORDER BY created_at, id
|
||||
LIMIT $4;
|
||||
"""
|
||||
async with self._pool.acquire() as conn:
|
||||
if after is None:
|
||||
rows = await conn.fetch(first_page_query, chat_id, limit)
|
||||
else:
|
||||
rows = await conn.fetch(
|
||||
next_page_query, chat_id, after.created_at, after.id, limit
|
||||
)
|
||||
return [self._row_to_message(row) for row in rows]
|
||||
|
||||
@staticmethod
|
||||
def _row_to_message(row: asyncpg.Record) -> MessageSchema:
|
||||
return MessageSchema(
|
||||
id=row["id"],
|
||||
chat_id=row["chat_id"],
|
||||
sender_id=row["sender_id"],
|
||||
text=row["text"],
|
||||
created_at=row["created_at"],
|
||||
)
|
||||
@@ -0,0 +1,3 @@
|
||||
from .valkey import ValkeySessionCache
|
||||
|
||||
__all__ = ["ValkeySessionCache"]
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user