feat: перенос логики 3 лабы

This commit is contained in:
Enterxx2
2026-10-20 11:14:26 +03:00
parent ec540a1a72
commit 445cfe4789
122 changed files with 6798 additions and 6 deletions
+17
View File
@@ -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
+31 -5
View File
@@ -3,14 +3,40 @@
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
S3_ENDPOINT=http://s3:9000
S3_REGION=us-east-1
S3_BUCKET=messenger-attachments
+4
View File
@@ -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
View File
@@ -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"]
+74 -1
View File
@@ -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
+169
View File
@@ -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:
+15
View File
@@ -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"}}';
}
+101
View File
@@ -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";
}
}
}
+60
View File
@@ -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);
+18
View File
@@ -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
View File
@@ -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"
+29
View File
@@ -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"
+58
View File
@@ -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
+89
View File
@@ -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
+480
View File
@@ -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()
+111
View File
@@ -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;
+17
View File
@@ -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
+11
View File
@@ -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
+9
View File
@@ -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
+42
View File
@@ -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"
+148
View File
@@ -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()
+24
View File
@@ -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
+4
View File
@@ -0,0 +1,4 @@
from .api import app as api_endpoints
from .api import health_router as health_endpoints
__all__ = ["api_endpoints", "health_endpoints"]
+10
View File
@@ -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"]
+15
View File
@@ -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
+3
View File
@@ -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)
+3
View File
@@ -0,0 +1,3 @@
from .endpoints import health_router
__all__ = ["health_router"]
+36
View File
@@ -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}",
)
+15
View File
@@ -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
+13
View File
@@ -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",
]
+33
View File
@@ -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},
)
+67
View File
@@ -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",
]
+12
View 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)
+49
View File
@@ -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"
+34
View File
@@ -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",
]
+7
View File
@@ -0,0 +1,7 @@
from .schemas import ActiveSessionSchema, BasicCredentialsSchema, SessionSchema
__all__ = [
"BasicCredentialsSchema",
"SessionSchema",
"ActiveSessionSchema",
]
+35
View File
@@ -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
+7
View File
@@ -0,0 +1,7 @@
from .schemas import ChatPageSchema, ChatSchema, CreateChatSchema
__all__ = [
"CreateChatSchema",
"ChatSchema",
"ChatPageSchema",
]
+42
View File
@@ -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
+24
View File
@@ -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",
]
+15
View File
@@ -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"
+64
View File
@@ -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 = "Сервис временно недоступен"
+21
View File
@@ -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
+3
View File
@@ -0,0 +1,3 @@
from .schemas import HealthSchema
__all__ = ["HealthSchema"]
+9
View File
@@ -0,0 +1,9 @@
from typing import Literal
from pydantic import BaseModel
class HealthSchema(BaseModel):
"""Ответ health-проверок"""
status: Literal["ok"] = "ok"
+17
View File
@@ -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",
]
+53
View File
@@ -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
+6
View File
@@ -0,0 +1,6 @@
from .schemas import PageCursorSchema, PageKey
__all__ = [
"PageKey",
"PageCursorSchema",
]
+26
View File
@@ -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)
+7
View File
@@ -0,0 +1,7 @@
from .schemas import CreateUserSchema, UserCredentialsSchema, UserSchema
__all__ = [
"CreateUserSchema",
"UserSchema",
"UserCredentialsSchema",
]
+40
View File
@@ -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
+15
View File
@@ -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",
]
+36
View File
@@ -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
View File
@@ -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,
)
+20
View File
@@ -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())
+4
View File
@@ -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",
]
+94
View File
@@ -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,
)
+236
View File
@@ -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"]
@@ -0,0 +1,87 @@
import math
from datetime import UTC, datetime
from typing import TYPE_CHECKING
from pydantic import ValidationError
from redis.exceptions import RedisError
from config import LOGGER
from domain import ActiveSessionSchema
from interfaces import SessionCache
if TYPE_CHECKING:
from infrastructure.dependencies.cache import CacheManager
KEY_PREFIX = "session:"
CACHE_UNAVAILABLE_ERRORS: tuple[type[Exception], ...] = (
RedisError,
OSError,
TimeoutError,
)
class ValkeySessionCache(SessionCache):
"""
Read-through кэш сессий в Valkey.
Ключ session:<sha256 токена hex>, значение — JSON ActiveSessionSchema.
TTL = min(остаток жизни сессии, max_ttl): запись исчезает не позже сессии,
а max_ttl ограничивает окно устаревания, если инвалидация не дошла.
"""
def __init__(self, cache: "CacheManager", max_ttl_seconds: int) -> None:
self._cache = cache
self._max_ttl_ms = max_ttl_seconds * 1000
async def get(self, token_hash: bytes) -> ActiveSessionSchema | None:
if not self._cache.is_available or self._cache.client is None:
return None
try:
raw = await self._cache.client.get(self._key(token_hash))
except CACHE_UNAVAILABLE_ERRORS as error:
self._cache.mark_failed(error)
return None
if raw is None:
return None
try:
return ActiveSessionSchema.model_validate_json(raw)
except ValidationError:
LOGGER.warning("Повреждённая запись кэша сессии проигнорирована")
return None
async def set(self, token_hash: bytes, session: ActiveSessionSchema) -> None:
if not self._cache.is_available or self._cache.client is None:
return
remaining = session.expires_at - datetime.now(UTC)
ttl_ms = min(self._max_ttl_ms, math.floor(remaining.total_seconds() * 1000))
if ttl_ms <= 0:
return
try:
await self._cache.client.set(
self._key(token_hash), session.model_dump_json(), px=ttl_ms
)
except CACHE_UNAVAILABLE_ERRORS as error:
self._cache.mark_failed(error)
async def delete(self, token_hash: bytes) -> bool:
if self._cache.client is None:
return False
try:
await self._cache.client.delete(self._key(token_hash))
except CACHE_UNAVAILABLE_ERRORS as error:
self._cache.mark_failed(error)
return False
return True
async def ping(self) -> bool:
if self._cache.client is None:
return False
try:
return bool(await self._cache.client.ping())
except CACHE_UNAVAILABLE_ERRORS:
return False
@staticmethod
def _key(token_hash: bytes) -> str:
return KEY_PREFIX + token_hash.hex()
@@ -0,0 +1,3 @@
from .postgres import PostgresSessionRepository
__all__ = ["PostgresSessionRepository"]
@@ -0,0 +1,85 @@
from datetime import datetime
from uuid import UUID
from domain import ActiveSessionSchema, UserSchema
from infrastructure.utils import Pool, repository_logs
from interfaces import SessionRepository
class PostgresSessionRepository(SessionRepository):
def __init__(self, pool: Pool) -> None:
self._pool = pool
@repository_logs
async def create(
self, token_hash: bytes, user_id: UUID, ttl_seconds: int
) -> datetime:
query = """
INSERT INTO sessions
(
token_hash,
user_id,
expires_at
)
VALUES
(
$1,
$2,
clock_timestamp() + make_interval(secs => $3::integer)
)
RETURNING expires_at;
"""
async with self._pool.acquire() as conn:
expires_at: datetime = await conn.fetchval(
query, token_hash, user_id, ttl_seconds
)
return expires_at
@repository_logs
async def get_active(self, token_hash: bytes) -> ActiveSessionSchema | None:
"""Поиск по PK; истёкшая или отозванная сессия не возвращается"""
query = """
SELECT
usr.id,
usr.username,
usr.display_name,
usr.created_at,
session.expires_at
FROM sessions session
JOIN users usr ON usr.id = session.user_id
WHERE
session.token_hash = $1
AND session.revoked_at IS NULL
AND session.expires_at > clock_timestamp();
"""
async with self._pool.acquire() as conn:
row = await conn.fetchrow(query, token_hash)
if row is None:
return None
return ActiveSessionSchema(
user=UserSchema(
id=row["id"],
username=row["username"],
display_name=row["display_name"],
created_at=row["created_at"],
),
expires_at=row["expires_at"],
)
@repository_logs
async def revoke(self, token_hash: bytes) -> bool:
query = """
UPDATE sessions
SET revoked_at = clock_timestamp()
WHERE
token_hash = $1
AND revoked_at IS NULL
AND expires_at > clock_timestamp();
"""
async with self._pool.acquire() as conn:
status = await conn.execute(query, token_hash)
return self._parse_affected(status) > 0
@staticmethod
def _parse_affected(status: str) -> int:
return int(status.split()[-1])
@@ -0,0 +1,3 @@
from .postgres import PostgresUserRepository
__all__ = ["PostgresUserRepository"]
@@ -0,0 +1,67 @@
import asyncpg
from domain import UserCredentialsSchema, UserSchema
from domain.errors import ConflictError
from infrastructure.utils import Pool, repository_logs
from interfaces import UserRepository
class PostgresUserRepository(UserRepository):
def __init__(self, pool: Pool) -> None:
self._pool = pool
@repository_logs
async def create(
self, username: str, display_name: str, password_hash: str
) -> UserSchema:
query = """
INSERT INTO users
(
username,
display_name,
password_hash
)
VALUES
(
$1,
$2,
$3
)
RETURNING id, username, display_name, created_at;
"""
async with self._pool.acquire() as conn:
try:
row = await conn.fetchrow(query, username, display_name, password_hash)
except asyncpg.UniqueViolationError:
raise ConflictError("Имя пользователя уже занято") from None
assert row is not None
return self._row_to_user(row)
@repository_logs
async def get_credentials(self, username: str) -> UserCredentialsSchema | None:
query = """
SELECT
id,
username,
display_name,
created_at,
password_hash
FROM users
WHERE username = $1;
"""
async with self._pool.acquire() as conn:
row = await conn.fetchrow(query, username)
if row is None:
return None
return UserCredentialsSchema(
user=self._row_to_user(row), password_hash=row["password_hash"]
)
@staticmethod
def _row_to_user(row: asyncpg.Record) -> UserSchema:
return UserSchema(
id=row["id"],
username=row["username"],
display_name=row["display_name"],
created_at=row["created_at"],
)
+4
View File
@@ -0,0 +1,4 @@
from .password_hasher import PasswordHasher, get_password_hasher
from .tokens import generate_token, hash_token
__all__ = ["PasswordHasher", "get_password_hasher", "generate_token", "hash_token"]
@@ -0,0 +1,50 @@
import asyncio
import secrets
from concurrent.futures import ThreadPoolExecutor
from argon2 import PasswordHasher as Argon2Hasher
from argon2.exceptions import InvalidHashError, VerificationError
from argon2.profiles import RFC_9106_LOW_MEMORY
from starlette.requests import HTTPConnection
class PasswordHasher:
"""
Хеширование паролей с использованием Argon2id.
"""
def __init__(self, workers: int) -> None:
self._hasher = Argon2Hasher.from_parameters(RFC_9106_LOW_MEMORY)
self._executor = ThreadPoolExecutor(
max_workers=workers, thread_name_prefix="argon2"
)
self._dummy_hash = self._hasher.hash(secrets.token_urlsafe())
async def hash(self, password: str) -> str:
loop = asyncio.get_running_loop()
return await loop.run_in_executor(self._executor, self._hasher.hash, password)
async def verify(self, password_hash: str, password: str) -> bool:
loop = asyncio.get_running_loop()
return await loop.run_in_executor(
self._executor, self._verify, password_hash, password
)
async def verify_dummy(self, password: str) -> None:
"""Та же работа, что у verify, для выравнивания времени ответа"""
await self.verify(self._dummy_hash, password)
def close(self) -> None:
self._executor.shutdown(wait=False, cancel_futures=True)
def _verify(self, password_hash: str, password: str) -> bool:
try:
return self._hasher.verify(password_hash, password)
except (VerificationError, InvalidHashError):
return False
def get_password_hasher(connection: HTTPConnection) -> PasswordHasher:
"""Получение hasher, созданного в lifespan."""
hasher: PasswordHasher = connection.app.state.password_hasher
return hasher
+17
View File
@@ -0,0 +1,17 @@
import hashlib
import secrets
def generate_token() -> str:
"""Непрозрачный токен сессии: 256 бит из CSPRNG, base64url"""
return secrets.token_urlsafe(32)
def hash_token(token: str) -> bytes:
"""
SHA-256 токена для хранения и поиска.
Токен уже имеет 256 бит энтропии, перебор невозможен, поэтому быстрый hash
достаточен; медленный KDF нужен паролям, у которых энтропия низкая.
"""
return hashlib.sha256(token.encode()).digest()
+6
View File
@@ -0,0 +1,6 @@
from .auth_service import AuthService
from .chat_service import ChatService
from .message_service import MessageService
from .user_service import UserService
__all__ = ["UserService", "AuthService", "ChatService", "MessageService"]
@@ -0,0 +1,3 @@
from .auth_service import AuthService
__all__ = ["AuthService"]
@@ -0,0 +1,92 @@
from datetime import UTC, datetime
from config import LOGGER
from domain import BasicCredentialsSchema, SessionSchema, UserSchema
from domain.errors import InvalidCredentialsError, UnauthorizedError
from infrastructure.security import PasswordHasher, generate_token, hash_token
from interfaces import SessionCache, SessionRepository, UserRepository
TOKEN_MAX_LENGTH = 256
class AuthService:
"""
Basic → opaque Bearer-сессия.
Источник истины — SessionRepository (PostgreSQL), SessionCache (Valkey) —
read-through кэш: при промахе или недоступности кэша сессия читается из БД.
"""
def __init__(
self,
user_repository: UserRepository,
session_repository: SessionRepository,
session_cache: SessionCache,
hasher: PasswordHasher,
session_ttl_seconds: int,
) -> None:
self._users = user_repository
self._sessions = session_repository
self._cache = session_cache
self._hasher = hasher
self._session_ttl = session_ttl_seconds
async def login(self, credentials: BasicCredentialsSchema) -> SessionSchema:
"""
Создание сессии по username и паролю.
Raises:
InvalidCredentialsError: неизвестный username или неверный пароль —
одинаковый ответ и сопоставимое время
"""
found = await self._users.get_credentials(credentials.username)
if found is None:
await self._hasher.verify_dummy(credentials.password)
raise InvalidCredentialsError()
if not await self._hasher.verify(found.password_hash, credentials.password):
raise InvalidCredentialsError()
token = generate_token()
expires_at = await self._sessions.create(
hash_token(token), found.user.id, self._session_ttl
)
LOGGER.info(f"Создана сессия пользователя {found.user.id}")
return SessionSchema(token=token, expires_at=expires_at, user=found.user)
async def authenticate(self, token: str | None) -> UserSchema:
"""
Владелец действующей сессии.
Raises:
UnauthorizedError: токена нет, он неизвестен, истёк или отозван
UnavailableError: кэш промахнулся, а PostgreSQL недоступен
"""
if not token or len(token) > TOKEN_MAX_LENGTH:
raise UnauthorizedError()
token_hash = hash_token(token)
session = await self._cache.get(token_hash)
if session is not None and session.expires_at > datetime.now(UTC):
return session.user
session = await self._sessions.get_active(token_hash)
if session is None:
raise UnauthorizedError()
await self._cache.set(token_hash, session)
return session.user
async def logout(self, token: str | None) -> None:
"""
Отзыв в источнике истины, затем инвалидация кэша.
Raises:
UnauthorizedError: действующей сессии с таким токеном нет
"""
if not token or len(token) > TOKEN_MAX_LENGTH:
raise UnauthorizedError()
token_hash = hash_token(token)
if not await self._sessions.revoke(token_hash):
raise UnauthorizedError()
if not await self._cache.delete(token_hash):
LOGGER.warning(
"Сессия отозвана в PostgreSQL, но запись кэша не удалена: "
"она истечёт по TTL кэша"
)
LOGGER.info("Сессия отозвана")
@@ -0,0 +1,3 @@
from .chat_service import ChatService
__all__ = ["ChatService"]
@@ -0,0 +1,46 @@
from uuid import UUID
from config import LOGGER
from domain import ChatPageSchema, ChatSchema, CreateChatSchema, UserSchema
from domain.errors import InvalidRequestError, NotFoundError
from infrastructure.utils import build_page, decode_cursor
from interfaces import ChatRepository
class ChatService:
def __init__(self, chat_repository: ChatRepository) -> None:
self._chats = chat_repository
async def create_chat(
self, current_user: UserSchema, data: CreateChatSchema
) -> ChatSchema:
"""
Чат создаётся из явного списка существующих участников, включая создателя.
Существование участников проверяет FK в транзакции создания: отдельная
предварительная проверка была бы гонкой и не нужна для отката.
"""
if current_user.id not in data.member_ids:
raise InvalidRequestError(
"member_ids должен содержать текущего пользователя"
)
chat = await self._chats.create(data.title, current_user.id, data.member_ids)
LOGGER.info(
f"Создан чат {chat.id} пользователем {current_user.id}, "
f"участников: {len(chat.member_ids)}"
)
return chat
async def get_chat(self, current_user: UserSchema, chat_id: UUID) -> ChatSchema:
chat = await self._chats.get_for_member(chat_id, current_user.id)
if chat is None:
raise NotFoundError()
return chat
async def list_chats(
self, current_user: UserSchema, limit: int, cursor: str | None
) -> ChatPageSchema:
scope = f"chats:{current_user.id}"
after = decode_cursor(cursor, scope)
fetched = await self._chats.list_for_member(current_user.id, after, limit + 1)
items, next_cursor = build_page(fetched, limit, scope)
return ChatPageSchema(items=items, next_cursor=next_cursor)
@@ -0,0 +1,3 @@
from .message_service import MessageService
__all__ = ["MessageService"]
@@ -0,0 +1,83 @@
import hashlib
from uuid import UUID
from config import LOGGER
from domain import (
CreateMessageSchema,
IdempotencyKeySchema,
MessagePageSchema,
MessageSchema,
UserSchema,
)
from domain.errors import NotFoundError
from infrastructure.utils import build_page, decode_cursor
from interfaces import ChatRepository, MessageRepository
class MessageService:
def __init__(
self,
chat_repository: ChatRepository,
message_repository: MessageRepository,
idempotency_ttl_seconds: int,
) -> None:
self._chats = chat_repository
self._messages = message_repository
self._idempotency_ttl = idempotency_ttl_seconds
async def send_message(
self,
current_user: UserSchema,
chat_id: UUID,
data: CreateMessageSchema,
idempotency_key: str,
) -> MessageSchema:
"""
Отправитель берётся из текущей идентификации; текст в логи не пишется.
Повтор с тем же Idempotency-Key заново проверяет доступ к чату
и возвращает исходное сообщение.
"""
await self._ensure_member(current_user, chat_id)
idempotency = IdempotencyKeySchema(
key=idempotency_key,
request_hash=self._request_hash(data),
ttl_seconds=self._idempotency_ttl,
)
message, created = await self._messages.create(
chat_id, current_user.id, data.text, idempotency
)
if created:
LOGGER.info(
f"Сообщение {message.id} отправлено в чат {chat_id} "
f"пользователем {current_user.id}"
)
else:
LOGGER.info(
f"Повтор по Idempotency-Key: возвращено сообщение {message.id} "
f"чата {chat_id}"
)
return message
async def list_messages(
self, current_user: UserSchema, chat_id: UUID, limit: int, cursor: str | None
) -> MessagePageSchema:
await self._ensure_member(current_user, chat_id)
scope = f"messages:{chat_id}"
after = decode_cursor(cursor, scope)
fetched = await self._messages.list_for_chat(chat_id, after, limit + 1)
items, next_cursor = build_page(fetched, limit, scope)
return MessagePageSchema(items=items, next_cursor=next_cursor)
async def _ensure_member(self, current_user: UserSchema, chat_id: UUID) -> None:
"""Посторонний получает 404: существование чужого чата не раскрывается"""
if await self._chats.get_for_member(chat_id, current_user.id) is None:
raise NotFoundError()
@staticmethod
def _request_hash(data: CreateMessageSchema) -> bytes:
"""
SHA-256 канонического тела. Порядок полей задаёт схема, а не клиент,
поэтому JSON с другим порядком ключей считается тем же запросом.
"""
return hashlib.sha256(data.model_dump_json().encode()).digest()
@@ -0,0 +1,3 @@
from .user_service import UserService
__all__ = ["UserService"]
@@ -0,0 +1,17 @@
from config import LOGGER
from domain import CreateUserSchema, UserSchema
from infrastructure.security import PasswordHasher
from interfaces import UserRepository
class UserService:
def __init__(self, repository: UserRepository, hasher: PasswordHasher) -> None:
self._repo = repository
self._hasher = hasher
async def register(self, data: CreateUserSchema) -> UserSchema:
"""Пароль сохраняется только как hash Argon2id"""
password_hash = await self._hasher.hash(data.password)
user = await self._repo.create(data.username, data.display_name, password_hash)
LOGGER.info(f"Зарегистрирован пользователь {user.id}")
return user
+15
View File
@@ -0,0 +1,15 @@
from .pagination import build_page, decode_cursor, encode_cursor, page_key
from .repository_utils import DATABASE_UNAVAILABLE_ERRORS, repository_logs
from .transaction import ConnectionPool, Pool, transaction
__all__ = [
"page_key",
"encode_cursor",
"decode_cursor",
"build_page",
"repository_logs",
"DATABASE_UNAVAILABLE_ERRORS",
"ConnectionPool",
"Pool",
"transaction",
]
+44
View File
@@ -0,0 +1,44 @@
import base64
import binascii
from pydantic import ValidationError
from domain import ChatSchema, MessageSchema, PageCursorSchema, PageKey
from domain.errors import InvalidRequestError
PageItem = ChatSchema | MessageSchema
def page_key(item: PageItem) -> PageKey:
return PageKey(item.created_at, item.id)
def encode_cursor(scope: str, key: PageKey) -> str:
"""Непрозрачный cursor: base64url от JSON с ресурсом и ключом последнего элемента"""
payload = PageCursorSchema(scope=scope, created_at=key.created_at, id=key.id)
encoded = base64.urlsafe_b64encode(payload.model_dump_json().encode())
return encoded.decode().rstrip("=")
def decode_cursor(raw: str | None, scope: str) -> PageKey | None:
"""Разбор cursor, привязанного к ресурсу."""
if raw is None:
return None
try:
decoded = base64.urlsafe_b64decode(raw + "=" * (-len(raw) % 4))
cursor = PageCursorSchema.model_validate_json(decoded)
except (binascii.Error, ValueError, ValidationError):
raise InvalidRequestError("Некорректный cursor") from None
if cursor.scope != scope:
raise InvalidRequestError("cursor выдан для другого ресурса")
return cursor.key
def build_page(
fetched: list[PageItem], limit: int, scope: str
) -> tuple[list[PageItem], str | None]:
"""Отрезает страницу из limit + 1 полученных элементов."""
items = fetched[:limit]
if len(fetched) <= limit:
return items, None
return items, encode_cursor(scope, page_key(items[-1]))
@@ -0,0 +1,39 @@
from collections.abc import Callable
from functools import wraps
from typing import Any
import asyncpg
from config import LOGGER
from domain.errors import UnavailableError
DATABASE_UNAVAILABLE_ERRORS: tuple[type[Exception], ...] = (
OSError,
TimeoutError,
asyncpg.PostgresConnectionError,
asyncpg.InterfaceError,
asyncpg.exceptions.OperatorInterventionError,
asyncpg.exceptions.InsufficientResourcesError,
)
def repository_logs(func: Callable) -> Callable:
"""
Decorator for logging database requests.
"""
@wraps(func)
async def wrapper(*args: Any, **kwargs: Any) -> Any:
log_text = (
f"Request to DATABASE || Object: {args[0].__class__.__name__} "
f"|| Function: {func.__name__} || "
)
try:
result = await func(*args, **kwargs)
except DATABASE_UNAVAILABLE_ERRORS as error:
LOGGER.warning(log_text + f"failed || Error: {type(error).__name__}")
raise UnavailableError() from None
LOGGER.debug(log_text + "completed")
return result
return wrapper
+29
View File
@@ -0,0 +1,29 @@
from collections.abc import AsyncIterator
from contextlib import AbstractAsyncContextManager, asynccontextmanager
from typing import Protocol
import asyncpg
class Pool(Protocol):
"""Источник соединений: DatabasePoolManager или одно соединение транзакции"""
def acquire(self) -> AbstractAsyncContextManager[asyncpg.Connection]: ...
class ConnectionPool:
"""Pool-подобная обёртка над одним соединением."""
def __init__(self, conn: asyncpg.Connection) -> None:
self.connection = conn
@asynccontextmanager
async def acquire(self) -> AsyncIterator[asyncpg.Connection]:
yield self.connection
@asynccontextmanager
async def transaction(pool: Pool) -> AsyncIterator[asyncpg.Connection]:
async with pool.acquire() as conn:
async with conn.transaction():
yield conn

Some files were not shown because too many files have changed in this diff Show More