forked from mtusi2026/mtusi2026lab4
Carry over the lab 3 solution
Bring the application, own tests, Dockerfile, toolchain, compose stack, HAProxy and k6 configuration, migrations, hooks, evidence and level 4 scripts and the CI job from the lab 3 solution commit 61c0c28, merge the lab 3 Makefile targets into the lab 4 skeleton Makefile without touching its targets, and append the lab 3 solution variables to the skeleton .env.example. The package and the application follow the contract version 1.4.0, and the image and the throwaway test containers are tagged lab4 so they do not collide with a lab 3 stand on the same host. This is the starting point of the lab 4 cycle: lint, own tests and the offline skeleton check pass before any lab 4 change. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
b0f463ac5c
commit
bf8e4e3cd1
@@ -14,7 +14,31 @@ backups
|
||||
*.pfx
|
||||
*.jks
|
||||
.venv
|
||||
venv
|
||||
__pycache__
|
||||
*.pyc
|
||||
*.pyo
|
||||
.pytest_cache
|
||||
.ruff_cache
|
||||
.mypy_cache
|
||||
.ty_cache
|
||||
node_modules
|
||||
evidence
|
||||
openspec
|
||||
student-tests
|
||||
tests
|
||||
scripts
|
||||
examples
|
||||
contracts
|
||||
infra
|
||||
*.md
|
||||
!README.md
|
||||
Makefile
|
||||
Dockerfile*
|
||||
compose*.yaml
|
||||
.dockerignore
|
||||
.gitignore
|
||||
.DS_Store
|
||||
lab.json
|
||||
requirements-dev.txt
|
||||
.claude
|
||||
|
||||
@@ -18,3 +18,21 @@ S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_access_key_id
|
||||
S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret_access_key
|
||||
MAX_UPLOAD_BYTES=10485760
|
||||
MAX_IMAGE_PIXELS=40000000
|
||||
# Solution-specific: published LB ports, replica limits, workers and pool per replica,
|
||||
# LB queue per replica, drain window before closing the listener on SIGTERM, PostgreSQL
|
||||
# limits, load generator limits (profile bench), cursor key file. API_PORT/SHORTTTL_PORT of lab2 are gone:
|
||||
# only the load balancer is published.
|
||||
LB_HTTP_PORT=8080
|
||||
LB_STATS_PORT=8404
|
||||
API_CPUS=1
|
||||
API_MEMORY=512m
|
||||
WEB_CONCURRENCY=2
|
||||
DB_POOL_MAX_SIZE=8
|
||||
API_SERVER_MAXCONN=64
|
||||
DRAIN_SECONDS=2
|
||||
PG_MAX_CONNECTIONS=200
|
||||
PG_CPUS=2
|
||||
PG_MEMORY=1g
|
||||
K6_CPUS=2
|
||||
K6_MEMORY=512m
|
||||
CURSOR_SECRET_FILE=/run/secrets/cursor_secret
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
name: Application
|
||||
|
||||
on: [push, pull_request]
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
quality:
|
||||
name: Линт, типы и собственные тесты
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: astral-sh/setup-uv@v5
|
||||
with:
|
||||
enable-cache: true
|
||||
- run: uv sync --locked --group dev
|
||||
- run: make lint VENV=.venv
|
||||
- run: make student-test VENV=.venv
|
||||
|
||||
acceptance:
|
||||
name: Стенд Compose за HAProxy, smoke, отказ реплики, рестарт и отказы зависимостей
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: astral-sh/setup-uv@v5
|
||||
with:
|
||||
enable-cache: true
|
||||
- run: uv sync --locked --group dev
|
||||
|
||||
- name: Поднять стенд из чистого clone
|
||||
run: make up
|
||||
|
||||
- name: Публичный smoke через балансировщик
|
||||
run: make test
|
||||
|
||||
- name: Несколько реплик на одном токене и гонка одного ключа
|
||||
run: make lb-check
|
||||
|
||||
- name: Остановка реплики под запросами, затем возврат
|
||||
run: |
|
||||
make test-failover ACTION_SCRIPT=scripts/stop-one.sh
|
||||
docker compose ps -a api
|
||||
make restore
|
||||
|
||||
- name: Рестарт LB, API и хранилищ с сохранением volumes
|
||||
run: make test-persistence ACTION_SCRIPT=scripts/restart.sh
|
||||
|
||||
- name: Отказы Valkey и PostgreSQL, гонка и rollback
|
||||
run: make test-faults
|
||||
|
||||
- name: Лимиты, порты, секреты, volumes и сети по политике
|
||||
run: make limits
|
||||
|
||||
- name: Время обнаружения отказа балансировщиком (SIGTERM, kill, pause, PostgreSQL)
|
||||
run: make detection
|
||||
|
||||
- name: Наружу опубликован только балансировщик
|
||||
run: |
|
||||
docker compose ps --format '{{.Service}} {{.Ports}}' | awk '$2 != "" && $1 != "lb" {print; bad=1} END {exit bad}'
|
||||
|
||||
- name: Процессы работают не от root
|
||||
run: |
|
||||
docker compose top | awk 'NR>1 && $3=="0" {print; bad=1} END {exit bad}'
|
||||
|
||||
- name: Корректная остановка реплик API по SIGTERM
|
||||
run: |
|
||||
start=$(date +%s)
|
||||
docker compose kill --signal SIGTERM api
|
||||
codes=$(docker wait $(docker compose ps -aq api) | sort -u)
|
||||
elapsed=$(( $(date +%s) - start ))
|
||||
echo "остановка за ${elapsed} с, коды завершения ${codes}"
|
||||
test "$elapsed" -le 10 && test "$codes" = "0"
|
||||
|
||||
- name: Журналы стенда
|
||||
if: always()
|
||||
run: docker compose logs --tail 80
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
FROM python:3.14-slim-trixie@sha256:51dafde81dbdb6ebde285137a295cf18a47ca95234fe388a343719cb97305b3d AS builder
|
||||
|
||||
COPY --from=ghcr.io/astral-sh/uv:0.9.7@sha256:ba4857bf2a068e9bc0e64eed8563b065908a4cd6bfb66b531a9c424c8e25e142 /uv /usr/local/bin/uv
|
||||
|
||||
ENV UV_COMPILE_BYTECODE=1 \
|
||||
UV_LINK_MODE=copy \
|
||||
UV_PYTHON_DOWNLOADS=never \
|
||||
UV_PROJECT_ENVIRONMENT=/opt/venv
|
||||
|
||||
WORKDIR /build
|
||||
|
||||
COPY pyproject.toml uv.lock README.md ./
|
||||
RUN uv sync --locked --no-dev --no-install-project --no-editable
|
||||
|
||||
COPY src ./src
|
||||
RUN uv sync --locked --no-dev --no-editable
|
||||
|
||||
|
||||
FROM python:3.14-slim-trixie@sha256:51dafde81dbdb6ebde285137a295cf18a47ca95234fe388a343719cb97305b3d AS runtime
|
||||
|
||||
ENV PYTHONUNBUFFERED=1 \
|
||||
PYTHONDONTWRITEBYTECODE=1 \
|
||||
PATH="/opt/venv/bin:$PATH" \
|
||||
HOST=0.0.0.0 \
|
||||
PORT=8080 \
|
||||
LOG_LEVEL=INFO \
|
||||
WEB_CONCURRENCY=2 \
|
||||
REQUEST_TIMEOUT_SECONDS=8 \
|
||||
DRAIN_SECONDS=2 \
|
||||
SHUTDOWN_TIMEOUT_SECONDS=5
|
||||
|
||||
RUN groupadd --system --gid 10001 messenger \
|
||||
&& useradd --system --uid 10001 --gid messenger --no-create-home --shell /usr/sbin/nologin messenger
|
||||
|
||||
COPY --from=builder --chown=root:root /opt/venv /opt/venv
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
USER 10001:10001
|
||||
|
||||
EXPOSE 8080
|
||||
|
||||
HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \
|
||||
CMD ["python", "-c", "import urllib.request,sys;sys.exit(0 if urllib.request.urlopen('http://127.0.0.1:8080/health/live',timeout=2).status==200 else 1)"]
|
||||
|
||||
CMD ["python", "-m", "gunicorn", \
|
||||
"--config", "python:messenger.gunicorn_conf", \
|
||||
"messenger.main:create_app()"]
|
||||
@@ -4,10 +4,27 @@ CA_FILE ?=
|
||||
ACTION_SCRIPT ?=
|
||||
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
|
||||
|
||||
.PHONY: help check validate test load test-persistence test-failover
|
||||
VENV ?= .venv
|
||||
UV ?= uv
|
||||
COMPOSE ?= docker compose
|
||||
RATE ?= 100
|
||||
API_IMAGE ?= mtusi-messenger:lab4
|
||||
FROM_REV ?=
|
||||
SCRIPTS_PY = scripts/probe.py scripts/limits.py
|
||||
PSQL = $(COMPOSE) exec -T postgres psql -U messenger -d messenger -X -q
|
||||
|
||||
.PHONY: help check validate test load test-persistence test-failover \
|
||||
install lint format typecheck student-test \
|
||||
secrets up down reset-data migrate ps logs restore lb-check failover-evidence stack-evidence \
|
||||
persistence-evidence storage-evidence cache-evidence test-faults seed explain \
|
||||
limits detection failover-each rolling-update level4
|
||||
|
||||
help:
|
||||
@echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes"
|
||||
@echo "install/lint/typecheck/student-test: студенческий тулчейн"
|
||||
@echo "secrets/up/down/migrate/ps/logs: стенд Docker Compose; reset-data: намеренное удаление volumes"
|
||||
@echo "restore: вернуть остановленные реплики; lb-check: реплики на одном токене и 20 гонок одного ключа"
|
||||
@echo "level4 = limits, lb-check, detection, failover-each, rolling-update [FROM_REV=<коммит>]; RATE=$(RATE) rps"
|
||||
|
||||
check:
|
||||
$(PYTHON) scripts/check_contract.py
|
||||
@@ -28,3 +45,100 @@ 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:
|
||||
$(UV) sync --group dev
|
||||
|
||||
lint:
|
||||
$(VENV)/bin/ruff check src student-tests $(SCRIPTS_PY)
|
||||
$(VENV)/bin/ruff format --check src student-tests $(SCRIPTS_PY)
|
||||
$(VENV)/bin/ty check src student-tests
|
||||
|
||||
format:
|
||||
$(VENV)/bin/ruff check --fix src student-tests $(SCRIPTS_PY)
|
||||
$(VENV)/bin/ruff format src student-tests $(SCRIPTS_PY)
|
||||
|
||||
typecheck:
|
||||
$(VENV)/bin/ty check src student-tests
|
||||
|
||||
student-test:
|
||||
$(VENV)/bin/pytest
|
||||
|
||||
secrets:
|
||||
sh scripts/secrets.sh
|
||||
|
||||
up: secrets
|
||||
$(COMPOSE) up -d --build --wait
|
||||
|
||||
down:
|
||||
$(COMPOSE) down
|
||||
|
||||
reset-data:
|
||||
@echo "Удаляются volumes с учебными данными стенда"
|
||||
$(COMPOSE) down -v --remove-orphans
|
||||
|
||||
migrate:
|
||||
$(COMPOSE) run --rm migrate
|
||||
|
||||
ps:
|
||||
$(COMPOSE) ps
|
||||
|
||||
logs:
|
||||
$(COMPOSE) logs --tail 200
|
||||
|
||||
restore:
|
||||
scripts/restore.sh
|
||||
|
||||
lb-check:
|
||||
$(PYTHON) scripts/probe.py check --rounds 20 --out evidence/lb-check.json
|
||||
|
||||
limits:
|
||||
$(PYTHON) scripts/limits.py --out evidence/limits.txt
|
||||
|
||||
detection:
|
||||
sh scripts/detection.sh evidence/detection
|
||||
|
||||
failover-each:
|
||||
RATE=$(RATE) sh scripts/failover-each.sh evidence/failover-each
|
||||
|
||||
rolling-update:
|
||||
ifneq ($(FROM_REV),)
|
||||
git archive "$(FROM_REV)" | docker build -q -t "$(API_IMAGE)" -
|
||||
$(COMPOSE) up -d --no-deps --force-recreate --wait api
|
||||
scripts/lb-wait.sh
|
||||
endif
|
||||
$(COMPOSE) build -q api
|
||||
RATE=$(RATE) $(PYTHON) scripts/probe.py load --expect replace --duration 90 \
|
||||
--out evidence/rolling$(if $(FROM_REV),-from-$(FROM_REV)) \
|
||||
-- scripts/rolling-update.sh
|
||||
|
||||
level4: limits lb-check detection failover-each rolling-update
|
||||
|
||||
failover-evidence:
|
||||
sh scripts/failover-evidence.sh "$(BASE_URL)"
|
||||
|
||||
stack-evidence:
|
||||
sh scripts/evidence-stack.sh
|
||||
|
||||
persistence-evidence:
|
||||
sh scripts/persistence-evidence.sh "$(BASE_URL)" > evidence/persistence.txt
|
||||
cat evidence/persistence.txt
|
||||
|
||||
storage-evidence:
|
||||
sh scripts/evidence-storage.sh
|
||||
|
||||
cache-evidence:
|
||||
sh scripts/evidence-cache.sh > evidence/cache.txt
|
||||
cat evidence/cache.txt
|
||||
|
||||
test-faults:
|
||||
BASE_URL="$(BASE_URL)" $(VENV)/bin/pytest -m stack -v -s student-tests/stack
|
||||
|
||||
seed:
|
||||
$(PSQL) -f - < scripts/sql/seed-history.sql
|
||||
|
||||
explain: seed
|
||||
{ echo "date: $$(date -u +%FT%TZ) commit: $$(git rev-parse --short HEAD)"; \
|
||||
echo "\$$ make explain # scripts/sql/explain-history.sql"; \
|
||||
$(PSQL) -f - < scripts/sql/explain-history.sql; } > evidence/explain-history.txt
|
||||
cat evidence/explain-history.txt
|
||||
|
||||
+185
@@ -0,0 +1,185 @@
|
||||
x-app: &app
|
||||
image: ${API_IMAGE:-mtusi-messenger:lab4}
|
||||
networks: [backend]
|
||||
|
||||
services:
|
||||
lb:
|
||||
image: haproxy:3.2.26-alpine@sha256:5893840576071e6217cd52ebdcc14f454c6f7a631056fa6f4e0f40d3cac4215b
|
||||
environment:
|
||||
API_SERVER_MAXCONN: ${API_SERVER_MAXCONN:-64}
|
||||
volumes: [./infra/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro]
|
||||
ports:
|
||||
- "127.0.0.1:${LB_HTTP_PORT:-8080}:8080"
|
||||
- "127.0.0.1:${LB_STATS_PORT:-8404}:8404"
|
||||
networks: [edge, backend]
|
||||
healthcheck:
|
||||
test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:8404/healthz"]
|
||||
interval: 2s
|
||||
timeout: 2s
|
||||
retries: 3
|
||||
start_period: 10s
|
||||
sysctls:
|
||||
net.ipv4.ip_local_port_range: "10240 65535"
|
||||
net.ipv4.tcp_tw_reuse: "1"
|
||||
stop_signal: SIGUSR1
|
||||
stop_grace_period: 8s
|
||||
restart: unless-stopped
|
||||
read_only: true
|
||||
cap_drop: [ALL]
|
||||
deploy:
|
||||
resources:
|
||||
limits: {cpus: "${LB_CPUS:-1}", memory: 128m}
|
||||
|
||||
api:
|
||||
<<: *app
|
||||
build: .
|
||||
environment:
|
||||
DATABASE_URL_FILE: /run/secrets/database_url
|
||||
CACHE_URL_FILE: /run/secrets/cache_url
|
||||
CURSOR_SECRET_FILE: /run/secrets/cursor_secret
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
|
||||
WEB_CONCURRENCY: ${WEB_CONCURRENCY:-2}
|
||||
DB_POOL_MAX_SIZE: ${DB_POOL_MAX_SIZE:-8}
|
||||
REQUEST_TIMEOUT_SECONDS: ${REQUEST_TIMEOUT_SECONDS:-8}
|
||||
DB_ACQUIRE_TIMEOUT_SECONDS: ${DB_ACQUIRE_TIMEOUT_SECONDS:-1}
|
||||
DB_COMMAND_TIMEOUT_SECONDS: ${DB_COMMAND_TIMEOUT_SECONDS:-2}
|
||||
DRAIN_SECONDS: ${DRAIN_SECONDS:-2}
|
||||
SHUTDOWN_TIMEOUT_SECONDS: ${SHUTDOWN_TIMEOUT_SECONDS:-5}
|
||||
LOG_LEVEL: ${LOG_LEVEL:-INFO}
|
||||
secrets: [database_url, cache_url, cursor_secret]
|
||||
healthcheck:
|
||||
test: ["CMD", "python", "-c", "import sys,urllib.request;sys.exit(urllib.request.urlopen('http://127.0.0.1:8080/health/ready',timeout=2).status!=200)"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 3
|
||||
start_period: 30s
|
||||
start_interval: 1s
|
||||
stop_grace_period: 8s
|
||||
restart: unless-stopped
|
||||
read_only: true
|
||||
tmpfs: [/tmp]
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
valkey:
|
||||
condition: service_started
|
||||
deploy:
|
||||
replicas: ${API_REPLICAS:-2}
|
||||
resources:
|
||||
limits: {cpus: "${API_CPUS:-1}", memory: "${API_MEMORY:-512m}"}
|
||||
|
||||
migrate:
|
||||
<<: *app
|
||||
pull_policy: never
|
||||
command: ["python", "-m", "messenger.migrate"]
|
||||
environment:
|
||||
DATABASE_URL_FILE: /run/secrets/database_url
|
||||
LOG_LEVEL: ${LOG_LEVEL:-INFO}
|
||||
secrets: [database_url]
|
||||
healthcheck:
|
||||
disable: true
|
||||
restart: "no"
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
deploy:
|
||||
resources:
|
||||
limits: {cpus: "0.5", memory: 256m}
|
||||
|
||||
postgres:
|
||||
image: postgres:17.11-alpine@sha256:266d3c257594e195b309ae9d1805e54c788f1d7b375f56bf6c367aac6c917e7b
|
||||
user: "70:70"
|
||||
command:
|
||||
- postgres
|
||||
- -c
|
||||
- max_connections=${PG_MAX_CONNECTIONS:-200}
|
||||
- -c
|
||||
- shared_buffers=${PG_SHARED_BUFFERS:-256MB}
|
||||
- -c
|
||||
- shared_preload_libraries=pg_stat_statements
|
||||
- -c
|
||||
- pg_stat_statements.track=top
|
||||
- -c
|
||||
- track_io_timing=on
|
||||
- -c
|
||||
- idle_in_transaction_session_timeout=10s
|
||||
environment:
|
||||
POSTGRES_DB: messenger
|
||||
POSTGRES_USER: messenger
|
||||
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
|
||||
secrets: [postgres_password]
|
||||
volumes: [pgdata:/var/lib/postgresql/data]
|
||||
shm_size: 256m
|
||||
healthcheck:
|
||||
test: ["CMD", "pg_isready", "-h", "127.0.0.1", "-U", "messenger", "-d", "messenger"]
|
||||
interval: 2s
|
||||
timeout: 3s
|
||||
retries: 30
|
||||
start_period: 10s
|
||||
stop_grace_period: 10s
|
||||
restart: unless-stopped
|
||||
networks: [backend]
|
||||
deploy:
|
||||
resources:
|
||||
limits: {cpus: "${PG_CPUS:-2}", memory: "${PG_MEMORY:-1g}"}
|
||||
|
||||
valkey:
|
||||
image: valkey/valkey:8.1.11-alpine@sha256:2602ace2be6569bc2bbb06f873776516ab9089821525be34f4305c67eb9db02a
|
||||
user: "999:1000"
|
||||
command:
|
||||
- valkey-server
|
||||
- /run/secrets/valkey.conf
|
||||
- --appendonly
|
||||
- "yes"
|
||||
- --appendfsync
|
||||
- always
|
||||
- --maxmemory
|
||||
- 64mb
|
||||
- --maxmemory-policy
|
||||
- volatile-lru
|
||||
secrets: [valkey.conf]
|
||||
volumes: [valkeydata:/data]
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "REDISCLI_AUTH=\"$$(sed -n 's/^requirepass //p' /run/secrets/valkey.conf)\" valkey-cli ping | grep -q PONG"]
|
||||
interval: 2s
|
||||
timeout: 3s
|
||||
retries: 30
|
||||
stop_grace_period: 10s
|
||||
restart: unless-stopped
|
||||
networks: [backend]
|
||||
deploy:
|
||||
resources:
|
||||
limits: {cpus: "0.5", memory: 128m}
|
||||
|
||||
k6:
|
||||
image: grafana/k6:2.3.0@sha256:2dd4756ec6e14e62778916e1eb850108e4b499304c02df56760fa0bdb0fba0b8
|
||||
profiles: [bench]
|
||||
environment:
|
||||
BASE_URL: http://lb:8080
|
||||
volumes: [./infra/k6:/scripts:ro]
|
||||
networks: [edge]
|
||||
deploy:
|
||||
resources:
|
||||
limits: {cpus: "${K6_CPUS:-2}", memory: "${K6_MEMORY:-512m}"}
|
||||
|
||||
secrets:
|
||||
postgres_password:
|
||||
file: ./secrets/postgres_password
|
||||
database_url:
|
||||
file: ./secrets/database_url
|
||||
valkey.conf:
|
||||
file: ./secrets/valkey.conf
|
||||
cache_url:
|
||||
file: ./secrets/cache_url
|
||||
cursor_secret:
|
||||
file: ./secrets/cursor_secret
|
||||
|
||||
volumes:
|
||||
pgdata:
|
||||
valkeydata:
|
||||
|
||||
networks:
|
||||
edge:
|
||||
backend:
|
||||
internal: true
|
||||
@@ -0,0 +1,67 @@
|
||||
global
|
||||
log stdout format raw local0 info
|
||||
maxconn 4096
|
||||
hard-stop-after 5s
|
||||
tune.bufsize 32768
|
||||
|
||||
resolvers docker
|
||||
nameserver dns 127.0.0.11:53
|
||||
accepted_payload_size 8192
|
||||
resolve_retries 3
|
||||
timeout resolve 1s
|
||||
timeout retry 1s
|
||||
hold nx 2s
|
||||
hold other 2s
|
||||
hold refused 2s
|
||||
hold timeout 2s
|
||||
|
||||
defaults
|
||||
mode http
|
||||
log global
|
||||
option dontlognull
|
||||
option dontlog-normal
|
||||
log-format "%ci [%tr] %ft %b/%s %TR/%Tw/%Tc/%Tr/%Ta %ST %B %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq rid=%[var(txn.request_id)] %HM %HPO"
|
||||
option http-server-close
|
||||
option redispatch 1
|
||||
retries 2
|
||||
retry-on conn-failure empty-response
|
||||
timeout connect 500ms
|
||||
timeout client 30s
|
||||
timeout server 10s
|
||||
timeout http-request 5s
|
||||
timeout http-keep-alive 5s
|
||||
timeout queue 3s
|
||||
timeout check 1s
|
||||
http-error status 502 content-type application/json hdr X-Request-Id "%[var(txn.request_id)]" lf-string '{"error":{"code":"unavailable","message":"Upstream replica failed","request_id":"%[var(txn.request_id)]"}}'
|
||||
http-error status 503 content-type application/json hdr X-Request-Id "%[var(txn.request_id)]" lf-string '{"error":{"code":"unavailable","message":"No ready API replica","request_id":"%[var(txn.request_id)]"}}'
|
||||
http-error status 504 content-type application/json hdr X-Request-Id "%[var(txn.request_id)]" lf-string '{"error":{"code":"unavailable","message":"Upstream timeout","request_id":"%[var(txn.request_id)]"}}'
|
||||
|
||||
frontend public
|
||||
bind :8080
|
||||
option forwardfor
|
||||
http-request set-var(txn.request_id) req.fhdr(X-Request-Id) if { req.fhdr(X-Request-Id) -m len 1:200 }
|
||||
http-request set-var(txn.request_id) uuid(4) unless { var(txn.request_id) -m found }
|
||||
http-request set-header X-Request-Id %[var(txn.request_id)]
|
||||
default_backend api
|
||||
|
||||
backend api
|
||||
balance roundrobin
|
||||
option httpchk
|
||||
http-check send meth GET uri /health/ready ver HTTP/1.1 hdr Host lb
|
||||
http-check expect status 200
|
||||
acl message_path path_reg ^/api/v1/chats/[^/]+/messages$
|
||||
acl has_key req.hdr(Idempotency-Key) -m found
|
||||
http-request set-var(txn.replay_safe) bool(1) if METH_GET
|
||||
http-request set-var(txn.replay_safe) bool(1) if METH_POST message_path has_key
|
||||
http-request disable-l7-retry unless { var(txn.replay_safe) -m bool }
|
||||
default-server check init-state fully-down inter 1s fastinter 500ms downinter 2s fall 2 rise 2 resolvers docker resolve-prefer ipv4 init-addr none maxconn "${API_SERVER_MAXCONN-64}"
|
||||
server-template api 8 api:8080
|
||||
|
||||
frontend stats
|
||||
bind :8404
|
||||
monitor-uri /healthz
|
||||
monitor fail if { nbsrv(api) lt 1 }
|
||||
http-request use-service prometheus-exporter if { path /metrics }
|
||||
stats enable
|
||||
stats uri /stats
|
||||
stats refresh 5s
|
||||
@@ -0,0 +1,106 @@
|
||||
import http from 'k6/http';
|
||||
import { check } from 'k6';
|
||||
import encoding from 'k6/encoding';
|
||||
import exec from 'k6/execution';
|
||||
|
||||
const BASE = __ENV.BASE_URL || 'http://lb:8080';
|
||||
const RATE = Number(__ENV.RATE || 50);
|
||||
const WARMUP = Number(__ENV.WARMUP || 10);
|
||||
const DURATION = Number(__ENV.DURATION || 60);
|
||||
const USERS = Math.max(2, Number(__ENV.USERS || 20));
|
||||
const READ_RATIO = Number(__ENV.READ_RATIO || 0.2);
|
||||
const VUS = Number(__ENV.VUS || Math.max(10, Math.ceil(RATE / 10)));
|
||||
const MAX_VUS = Number(__ENV.MAX_VUS || Math.max(VUS, Math.min(VUS * 4, 1000)));
|
||||
const RUN = __ENV.RUN_ID || Date.now().toString(36);
|
||||
const SUMMARY = __ENV.SUMMARY || '/out/summary.json';
|
||||
const JSON_HEADERS = { 'Content-Type': 'application/json' };
|
||||
|
||||
const phase = (startTime, duration) => ({
|
||||
executor: 'constant-arrival-rate',
|
||||
rate: RATE,
|
||||
timeUnit: '1s',
|
||||
startTime: `${startTime}s`,
|
||||
duration: `${duration}s`,
|
||||
preAllocatedVUs: VUS,
|
||||
maxVUs: MAX_VUS,
|
||||
gracefulStop: '5s',
|
||||
});
|
||||
|
||||
const measured = ['http_reqs', 'http_req_failed', 'http_req_duration', 'dropped_iterations', 'iterations'];
|
||||
|
||||
export const options = {
|
||||
setupTimeout: '180s',
|
||||
summaryTrendStats: ['avg', 'min', 'med', 'p(90)', 'p(95)', 'p(99)', 'max'],
|
||||
scenarios: Object.assign(WARMUP > 0 ? { warmup: phase(0, WARMUP) } : {}, { measure: phase(WARMUP, DURATION) }),
|
||||
thresholds: Object.fromEntries([
|
||||
...measured.map((metric) => [`${metric}{scenario:measure}`, []]),
|
||||
['http_req_duration{scenario:measure,name:post_message}', []],
|
||||
['http_req_duration{scenario:measure,name:get_history}', []],
|
||||
]),
|
||||
};
|
||||
|
||||
function created(response, what) {
|
||||
if (response.status !== 201) {
|
||||
throw new Error(`${what}: HTTP ${response.status}`);
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
const bearer = (token, extra) => Object.assign({ Authorization: `Bearer ${token}` }, extra || {});
|
||||
|
||||
export function setup() {
|
||||
const actors = [];
|
||||
for (let i = 0; i < USERS; i++) {
|
||||
const username = `k6_${RUN}_${i}`.slice(0, 32);
|
||||
const password = `K6-${RUN}-${i}-synthetic`;
|
||||
const user = created(
|
||||
http.post(`${BASE}/api/v1/users`, JSON.stringify({ username, display_name: `k6 ${i}`, password }), { headers: JSON_HEADERS }),
|
||||
'register',
|
||||
);
|
||||
const basic = encoding.b64encode(`${username}:${password}`);
|
||||
const session = created(
|
||||
http.post(`${BASE}/api/v1/auth/sessions`, null, { headers: { Authorization: `Basic ${basic}` } }),
|
||||
'login',
|
||||
);
|
||||
actors.push({ id: user.id, token: session.token });
|
||||
}
|
||||
const chats = [];
|
||||
for (let i = 0; i < USERS; i += 2) {
|
||||
const [a, b] = [actors[i], actors[(i + 1) % USERS]];
|
||||
const body = JSON.stringify({ title: `k6 ${RUN} ${i}`, member_ids: [a.id, b.id] });
|
||||
const chat = created(http.post(`${BASE}/api/v1/chats`, body, { headers: bearer(a.token, JSON_HEADERS) }), 'chat');
|
||||
chats.push({ id: chat.id, tokens: [a.token, b.token] });
|
||||
}
|
||||
console.log('LOAD-START');
|
||||
return { chats };
|
||||
}
|
||||
|
||||
export default function (data) {
|
||||
const n = exec.scenario.iterationInTest;
|
||||
const chat = data.chats[n % data.chats.length];
|
||||
const token = chat.tokens[Math.floor(n / data.chats.length) % 2];
|
||||
if (Math.random() < READ_RATIO) {
|
||||
const response = http.get(`${BASE}/api/v1/chats/${chat.id}/messages?limit=50`, {
|
||||
headers: bearer(token),
|
||||
tags: { name: 'get_history' },
|
||||
timeout: '10s',
|
||||
});
|
||||
check(response, { 'history 200': (r) => r.status === 200 });
|
||||
return;
|
||||
}
|
||||
const key = `${RUN}-${exec.scenario.name}-${n}`;
|
||||
const response = http.post(`${BASE}/api/v1/chats/${chat.id}/messages`, JSON.stringify({ text: `k6 ${key}` }), {
|
||||
headers: bearer(token, Object.assign({ 'Idempotency-Key': key }, JSON_HEADERS)),
|
||||
tags: { name: 'post_message' },
|
||||
timeout: '10s',
|
||||
});
|
||||
check(response, { 'message 201': (r) => r.status === 201 });
|
||||
}
|
||||
|
||||
export function handleSummary(data) {
|
||||
delete data.setup_data;
|
||||
const reqs = data.metrics['http_reqs{scenario:measure}'];
|
||||
const failed = data.metrics['http_req_failed{scenario:measure}'];
|
||||
const line = `measure: ${reqs ? reqs.values.count : 0} requests, failed ratio ${failed ? failed.values.rate : 'n/a'}\n`;
|
||||
return { [SUMMARY]: JSON.stringify(data, null, 1), stdout: line };
|
||||
}
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
[project]
|
||||
name = "mtusi-messenger"
|
||||
version = "1.4.0"
|
||||
description = "Лабораторная 4 «S3-хранилище и вложения», МТУСИ"
|
||||
requires-python = ">=3.14,<3.15"
|
||||
dependencies = [
|
||||
"fastapi>=0.118.0",
|
||||
"uvicorn[standard]>=0.37.0",
|
||||
"pydantic>=2.11.0",
|
||||
"pydantic-settings>=2.11.0",
|
||||
"dishka>=1.7.0",
|
||||
"structlog>=26.1.0",
|
||||
"gunicorn>=26.2.0",
|
||||
"orjson>=3.12.0",
|
||||
"asyncpg>=0.32.0",
|
||||
"argon2-cffi>=25.1.0",
|
||||
"uvicorn-worker>=0.4.0",
|
||||
"redis>=8.1.0",
|
||||
]
|
||||
|
||||
[dependency-groups]
|
||||
dev = [
|
||||
"pytest>=8.4.0",
|
||||
"pytest-asyncio>=1.2.0",
|
||||
"httpx>=0.28.0",
|
||||
"ruff>=0.14.0",
|
||||
"ty>=0.0.1a21",
|
||||
]
|
||||
|
||||
[build-system]
|
||||
requires = ["hatchling"]
|
||||
build-backend = "hatchling.build"
|
||||
|
||||
[tool.hatch.build.targets.wheel]
|
||||
packages = ["src/messenger"]
|
||||
|
||||
[tool.ruff]
|
||||
line-length = 100
|
||||
target-version = "py314"
|
||||
src = ["src", "student-tests"]
|
||||
extend-exclude = ["tests", "scripts/check_contract.py", "scripts/validate_openapi.py"]
|
||||
per-file-target-version = { "scripts/*.py" = "py39" }
|
||||
|
||||
[tool.ruff.format]
|
||||
indent-style = "space"
|
||||
quote-style = "double"
|
||||
|
||||
[tool.ruff.lint]
|
||||
select = [
|
||||
"E",
|
||||
"W",
|
||||
"F",
|
||||
"I",
|
||||
"N",
|
||||
"UP",
|
||||
"B",
|
||||
"A",
|
||||
"C4",
|
||||
"DTZ",
|
||||
"T20",
|
||||
"SIM",
|
||||
"TID",
|
||||
"PTH",
|
||||
"RUF",
|
||||
"ASYNC",
|
||||
"S",
|
||||
"ARG",
|
||||
"PL",
|
||||
]
|
||||
ignore = [
|
||||
"PLR0913",
|
||||
"RUF001",
|
||||
"RUF002",
|
||||
"RUF003",
|
||||
"S104",
|
||||
"BLE001",
|
||||
]
|
||||
|
||||
[tool.ruff.lint.per-file-ignores]
|
||||
"student-tests/**" = ["S101", "S105", "S106", "S603", "S607", "PLR2004", "ARG001", "T20", "TID251"]
|
||||
"scripts/*.py" = ["T20", "S603", "S607", "S310", "PLR2004", "TID251"]
|
||||
"src/messenger/*/infrastructure/**" = ["TID251", "ARG002"]
|
||||
"src/messenger/*/domain/errors.py" = ["N818"]
|
||||
"src/messenger/*/application/errors.py" = ["N818"]
|
||||
"src/messenger/*/entrypoints/**" = ["TID251", "N818", "PLR0917", "S105"]
|
||||
"src/messenger/shared/config.py" = ["ARG002", "ARG003"]
|
||||
"src/messenger/main.py" = ["TID251"]
|
||||
"src/messenger/migrate.py" = ["TID251"]
|
||||
"src/messenger/gunicorn_conf.py" = ["TID251", "ARG002", "RUF012"]
|
||||
|
||||
[tool.ruff.lint.flake8-tidy-imports.banned-api]
|
||||
"messenger.shared.infrastructure".msg = "Домен и слой приложения не импортируют инфраструктуру: зависимость идёт через порт."
|
||||
"messenger.identity.infrastructure".msg = "Домен и слой приложения не импортируют инфраструктуру: зависимость идёт через порт."
|
||||
"messenger.messaging.infrastructure".msg = "Домен и слой приложения не импортируют инфраструктуру: зависимость идёт через порт."
|
||||
|
||||
[tool.ruff.lint.isort]
|
||||
known-first-party = ["messenger"]
|
||||
|
||||
[tool.ty.src]
|
||||
include = ["src", "student-tests"]
|
||||
|
||||
[tool.ty.rules]
|
||||
unresolved-import = "error"
|
||||
invalid-argument-type = "error"
|
||||
possibly-missing-attribute = "warn"
|
||||
|
||||
[tool.pytest.ini_options]
|
||||
testpaths = ["student-tests"]
|
||||
asyncio_mode = "auto"
|
||||
asyncio_default_fixture_loop_scope = "function"
|
||||
addopts = "-q --strict-markers -m 'not stack'"
|
||||
markers = [
|
||||
"stack: black-box проверки против поднятого compose-стенда (make test-faults)",
|
||||
]
|
||||
Executable
+20
@@ -0,0 +1,20 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
out=${1:-evidence/detection}
|
||||
mkdir -p "$out"
|
||||
scripts/restore.sh
|
||||
replica() { docker compose ps -q "${API_SERVICE:-api}" | head -1; }
|
||||
detect() { python3 scripts/probe.py detect "$@"; }
|
||||
failed=0
|
||||
detect --max 1.5 --within-action --restore scripts/restore.sh --out "$out/sigterm.json" \
|
||||
-- docker stop -t "${STOP_TIMEOUT:-6}" "$(replica)" || failed=1
|
||||
detect --max 1.5 --restore scripts/restore.sh --out "$out/kill.json" \
|
||||
-- docker kill "$(replica)" || failed=1
|
||||
hung=$(replica)
|
||||
detect --max 3.5 --restore "docker unpause $hung && scripts/lb-wait.sh" --out "$out/pause.json" \
|
||||
-- docker pause "$hung" || failed=1
|
||||
db=$(docker compose ps -q postgres)
|
||||
detect --max 1.5 --until 0 --lb-status 503 --restore "docker start $db && scripts/lb-wait.sh" \
|
||||
--out "$out/postgres.json" -- docker stop "$db" || failed=1
|
||||
exit "$failed"
|
||||
Executable
+24
@@ -0,0 +1,24 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
cli() { docker compose exec -T valkey sh -c "REDISCLI_AUTH=\"\$(sed -n 's/^requirepass //p' /run/secrets/valkey.conf)\" valkey-cli $1"; }
|
||||
psql() { docker compose exec -T postgres psql -U messenger -d messenger -tA -c "$1"; }
|
||||
echo "date: $(date -u +%FT%TZ) commit: $(git rev-parse --short HEAD)"
|
||||
echo "\$ valkey-cli config get appendonly appendfsync maxmemory maxmemory-policy"
|
||||
cli "config get appendonly" ; cli "config get appendfsync"; cli "config get maxmemory"; cli "config get maxmemory-policy"
|
||||
echo "\$ valkey-cli info persistence | grep aof_enabled,aof_last_write_status"
|
||||
cli "info persistence" | grep -E '^aof_enabled|^aof_last_write_status'
|
||||
echo "\$ valkey-cli dbsize; info stats | grep keyspace"
|
||||
cli dbsize
|
||||
cli "info stats" | grep -E '^keyspace_(hits|misses)'
|
||||
key=$(cli "--scan --pattern session:* --count 1000" | head -1)
|
||||
hash=${key#session:}
|
||||
echo "\$ сравнение срока ключа кэша и срока сессии для одного ключа (значение и токен не печатаются)"
|
||||
echo "valkey pexpiretime, ms: $(cli "pexpiretime $key")"
|
||||
echo "postgres expires_at, ms: $(psql "SELECT (extract(epoch FROM expires_at) * 1000)::bigint FROM sessions WHERE token_hash = decode('$hash', 'hex')")"
|
||||
echo "\$ количество отметок отзыва и закэшированных сессий"
|
||||
revoked=0; cached=0
|
||||
for k in $(cli "--scan --pattern session:* --count 1000"); do
|
||||
if [ "$(cli "get $k")" = "-" ]; then revoked=$((revoked + 1)); else cached=$((cached + 1)); fi
|
||||
done
|
||||
echo "tombstones: $revoked, cached sessions: $cached"
|
||||
Executable
+48
@@ -0,0 +1,48 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
base=${1:-http://$(docker compose port lb 8080)}
|
||||
stats=http://$(docker compose port lb 8404)/stats\;csv
|
||||
header() { echo "date: $(date -u +%FT%TZ) commit: $(git rev-parse --short HEAD)"; }
|
||||
{
|
||||
header
|
||||
echo "\$ docker compose --env-file .env config --quiet && echo valid"
|
||||
docker compose --env-file .env config --quiet && echo valid
|
||||
echo "\$ docker compose ps --format '{{.Service}} {{.Name}} {{.Status}} {{.Ports}}'"
|
||||
docker compose ps --format '{{.Service}} {{.Name}} {{.Status}} {{.Ports}}'
|
||||
echo "\$ docker compose config | grep -c container_name"
|
||||
docker compose config | grep -c container_name || true
|
||||
echo "\$ docker port <каждая реплика api> # реплики не публикуют порт"
|
||||
for id in $(docker compose ps -q api); do echo "$(docker inspect -f '{{.Config.Hostname}}' "$id"): [$(docker port "$id")]"; done
|
||||
echo "\$ docker inspect: hostname = X-Instance-Id, сети, лимиты CPU (nano) и памяти"
|
||||
for id in $(docker compose ps -q); do
|
||||
docker inspect -f '{{index .Config.Labels "com.docker.compose.service"}} {{.Config.Hostname}} {{.Name}} nets={{range $k, $v := .NetworkSettings.Networks}}{{$k}},{{end}} cpus={{.HostConfig.NanoCpus}} mem={{.HostConfig.Memory}} user={{.Config.User}} ro={{.HostConfig.ReadonlyRootfs}}' "$id"
|
||||
done | sort
|
||||
echo "\$ curl LB stats: backend api, server status check"
|
||||
curl -fsS "$stats" | python3 -c 'import csv,sys
|
||||
for r in csv.DictReader(sys.stdin.read().lstrip("# ").splitlines()):
|
||||
if r["pxname"] == "api" and r["status"] != "MAINT (resolution)":
|
||||
print(r["svname"], r["status"], r["check_status"], r["check_code"])'
|
||||
echo "\$ for i in 1..6: curl -D - $base/health/live | X-Instance-Id"
|
||||
for _ in 1 2 3 4 5 6; do curl -fsS -D - -o /dev/null "$base/health/live" | tr -d '\r' | grep -i '^x-instance-id'; done
|
||||
} > evidence/stack.txt
|
||||
{
|
||||
header
|
||||
echo "host: $(sysctl -n machdep.cpu.brand_string 2>/dev/null || uname -m), $(sysctl -n hw.ncpu 2>/dev/null) CPU, $(($(sysctl -n hw.memsize 2>/dev/null || echo 0) / 1073741824)) GiB RAM, $(sw_vers -productName 2>/dev/null) $(sw_vers -productVersion 2>/dev/null)"
|
||||
echo "docker: $(docker version -f '{{.Server.Version}}'), VM: $(docker info -f '{{.NCPU}}') CPU, $(docker info -f '{{.MemTotal}}') bytes"
|
||||
echo "compose: $(docker compose version --short)"
|
||||
echo "images:"
|
||||
grep -h -E 'image:|@sha256' compose.yaml Dockerfile | sed 's/^ */ /'
|
||||
echo "python in image: $(docker exec "$(docker compose ps -q api | head -1)" python -V)"
|
||||
echo "haproxy: $(docker compose exec -T lb haproxy -v | head -1)"
|
||||
echo "packages (uv.lock):"
|
||||
for name in fastapi uvicorn uvicorn-worker gunicorn asyncpg argon2-cffi pydantic pydantic-settings dishka structlog redis; do
|
||||
printf ' %s %s\n' "$name" "$(sed -n "/^name = \"$name\"$/{n;s/version = \"\(.*\)\"/\1/p;}" uv.lock)"
|
||||
done
|
||||
echo "process users (service uid command):"
|
||||
for id in $(docker compose ps -q); do
|
||||
service=$(docker inspect -f '{{index .Config.Labels "com.docker.compose.service"}}' "$id")
|
||||
docker top "$id" -eo pid,uid,comm | awk -v s="$service" 'NR > 1 {print " " s, $2, $3}'
|
||||
done | sort -u
|
||||
} > evidence/environment.txt
|
||||
cat evidence/stack.txt evidence/environment.txt
|
||||
Executable
+44
@@ -0,0 +1,44 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
psql() { docker compose exec -T postgres psql -U messenger -d messenger -c "$1"; }
|
||||
header() { echo "date: $(date -u +%FT%TZ) commit: $(git rev-parse --short HEAD)"; }
|
||||
counts="SELECT (SELECT count(*) FROM users) users, (SELECT count(*) FROM sessions) sessions, (SELECT count(*) FROM chats) chats, (SELECT count(*) FROM chat_members) members, (SELECT count(*) FROM messages) messages, (SELECT count(*) FROM idempotency_keys) keys, (SELECT count(*) FROM schema_migrations) versions"
|
||||
kdf="SELECT substring(password_hash from '^[$]argon2id[$]v=[0-9]+[$]m=[0-9]+,t=[0-9]+,p=[0-9]+[$]') AS kdf_prefix, count(*) AS users, min(length(password_hash)) AS hash_length FROM users GROUP BY 1"
|
||||
tokens="SELECT octet_length(token_hash) AS stored_bytes, count(*) AS sessions FROM sessions GROUP BY 1"
|
||||
{
|
||||
header
|
||||
for table in users sessions chats chat_members messages idempotency_keys schema_migrations; do
|
||||
echo "\$ psql -c '\\d+ $table'"
|
||||
psql "\\d+ $table"
|
||||
done
|
||||
echo "\$ psql -c 'SELECT version, left(checksum, 12), applied_at FROM schema_migrations'"
|
||||
psql "SELECT version, left(checksum, 12) AS checksum, applied_at FROM schema_migrations"
|
||||
} > evidence/schema.txt
|
||||
{
|
||||
header
|
||||
echo "\$ psql -c \"$counts\""
|
||||
psql "$counts"
|
||||
echo "\$ make migrate"
|
||||
docker compose run --rm migrate 2>/dev/null
|
||||
echo "\$ psql -c \"$counts\""
|
||||
psql "$counts"
|
||||
} > evidence/migrations.txt
|
||||
{
|
||||
header
|
||||
echo "\$ psql -c \"$kdf\""
|
||||
psql "$kdf"
|
||||
echo "\$ psql -c \"$tokens\""
|
||||
psql "$tokens"
|
||||
} > evidence/kdf.txt
|
||||
{
|
||||
header
|
||||
echo "\$ git ls-files secrets .env backups volumes"
|
||||
git ls-files secrets .env backups volumes
|
||||
echo "(пусто: секретов, .env, дампов и volumes в Git нет)"
|
||||
echo "\$ git check-ignore -v secrets/database_url secrets/postgres_password .env backups/"
|
||||
git check-ignore -v secrets/database_url secrets/postgres_password .env backups/
|
||||
echo "\$ docker volume ls --filter name=mtusi2026lab4"
|
||||
docker volume ls --filter name=mtusi2026lab4
|
||||
} > evidence/secrets.txt
|
||||
echo "evidence/{schema,migrations,kdf,secrets}.txt обновлены"
|
||||
Executable
+19
@@ -0,0 +1,19 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
out=${1:-evidence/failover-each}
|
||||
rm -rf "$out"
|
||||
mkdir -p "$out"
|
||||
scripts/restore.sh
|
||||
hosts=$(docker inspect -f '{{.Config.Hostname}}' $(docker compose ps -q "${API_SERVICE:-api}"))
|
||||
failed=0
|
||||
set --
|
||||
for host in $hosts; do
|
||||
TARGET_INSTANCE_ID=$host python3 scripts/probe.py load --expect dip --out "$out/$host" \
|
||||
-- sh -c "scripts/stop-one.sh && sleep ${HOLD:-10} && scripts/restore.sh" || failed=1
|
||||
scripts/restore.sh
|
||||
set -- "$@" "$out/$host/result.json"
|
||||
done
|
||||
python3 -c 'import json,sys; print(json.dumps([json.load(open(p)) for p in sys.argv[1:]], ensure_ascii=False, indent=2))' "$@" > "$out/summary.tmp"
|
||||
mv "$out/summary.tmp" "$out/summary.json"
|
||||
exit "$failed"
|
||||
Executable
+28
@@ -0,0 +1,28 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
replicas() {
|
||||
docker inspect -f '{{.Config.Hostname}} {{.State.Status}} started={{.State.StartedAt}} finished={{.State.FinishedAt}}' $(docker compose ps -aq "${API_SERVICE:-api}")
|
||||
}
|
||||
tmp=evidence/failover.tmp
|
||||
since=$(date -u +%FT%TZ)
|
||||
{
|
||||
echo "date: $(date -u +%FT%TZ) commit: $(git rev-parse --short HEAD)"
|
||||
echo "== before: api replicas (hostname = X-Instance-Id), state, StartedAt, FinishedAt"
|
||||
replicas
|
||||
echo "\$ make test-failover ACTION_SCRIPT=scripts/stop-one.sh"
|
||||
make --no-print-directory test-failover ACTION_SCRIPT=scripts/stop-one.sh BASE_URL="${1:-http://localhost:8080}"
|
||||
echo "== after: api replicas"
|
||||
replicas
|
||||
} > "$tmp" 2>&1
|
||||
stopped=$(replicas | awk '$2 == "exited" {print $1}')
|
||||
{
|
||||
echo "== stopped replica: $stopped; shutdown: signal, drain window with readiness 503, close, exit"
|
||||
docker logs --since "$since" "$stopped" 2>&1 | grep -E 'Handling signal|Draining|"status":503|Shutting down|Waiting for conn|Finished server|service.stopped|SIGTERM' | tail -16
|
||||
echo "\$ scripts/restore.sh"
|
||||
scripts/restore.sh
|
||||
echo "== after restore"
|
||||
replicas
|
||||
} >> "$tmp" 2>&1
|
||||
mv "$tmp" "evidence/failover-$stopped.txt"
|
||||
cat "evidence/failover-$stopped.txt"
|
||||
Executable
+10
@@ -0,0 +1,10 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
out=${1:?usage: k6.sh <output dir> [k6 run args]}
|
||||
shift
|
||||
mkdir -p "$out"
|
||||
out=$(cd "$out" && pwd)
|
||||
cd "$(dirname "$0")/.."
|
||||
exec docker compose --profile bench run --rm --no-deps -T -u "$(id -u):$(id -g)" -v "$out:/out" \
|
||||
-e RATE -e WARMUP -e DURATION -e USERS -e READ_RATIO -e VUS -e MAX_VUS -e RUN_ID \
|
||||
k6 run --quiet "$@" "/scripts/${K6_SCRIPT:-messages.js}"
|
||||
Executable
+11
@@ -0,0 +1,11 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
service=${API_SERVICE:-api}
|
||||
want=${1:-$(docker compose ps -q "$service" | wc -l | tr -d ' ')}
|
||||
url=${LB_STATS_URL:-http://$(docker compose port lb 8404)/stats;csv}
|
||||
deadline=$(($(date +%s) + ${WAIT_TIMEOUT:-60}))
|
||||
until [ "$(curl -fsS "$url" | awk -F, -v b="$service" '$1 == b && $2 != "BACKEND" && $18 == "UP"' | wc -l)" -ge "$want" ]; do
|
||||
[ "$(date +%s)" -lt "$deadline" ] || { echo "LB does not see $want UP $service servers" >&2; exit 4; }
|
||||
sleep 0.5
|
||||
done
|
||||
@@ -0,0 +1,132 @@
|
||||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
POLICY = {
|
||||
"lb": {"secrets": [], "volumes": [], "networks": ["backend", "edge"], "ports": True},
|
||||
"api": {
|
||||
"secrets": ["cache_url", "cursor_secret", "database_url"],
|
||||
"volumes": [],
|
||||
"networks": ["backend"],
|
||||
},
|
||||
"migrate": {"secrets": ["database_url"], "volumes": [], "networks": ["backend"]},
|
||||
"postgres": {"secrets": ["postgres_password"], "volumes": ["pgdata"], "networks": ["backend"]},
|
||||
"valkey": {"secrets": ["valkey.conf"], "volumes": ["valkeydata"], "networks": ["backend"]},
|
||||
"k6": {"secrets": [], "volumes": [], "networks": ["edge"]},
|
||||
}
|
||||
|
||||
|
||||
def run(*command):
|
||||
done = subprocess.run(command, cwd=str(ROOT), capture_output=True, text=True, check=False)
|
||||
return done.returncode, done.stdout.strip()
|
||||
|
||||
|
||||
def declared(name, service, networks, problems):
|
||||
limits = service.get("deploy", {}).get("resources", {}).get("limits", {})
|
||||
secrets = sorted(s["source"] for s in service.get("secrets", []))
|
||||
volumes = sorted(v["source"] for v in service.get("volumes", []) if v["type"] == "volume")
|
||||
nets = sorted(service.get("networks", {}))
|
||||
ports = [
|
||||
f"{p.get('host_ip', '0.0.0.0')}:{p.get('published')}" for p in service.get("ports", [])
|
||||
]
|
||||
want = POLICY.get(name)
|
||||
if want is None:
|
||||
problems.append(f"{name}: service is not covered by the policy")
|
||||
want = {"secrets": [], "volumes": [], "networks": []}
|
||||
if not limits.get("cpus") or not limits.get("memory"):
|
||||
problems.append(f"{name}: no CPU or memory limit")
|
||||
if "container_name" in service:
|
||||
problems.append(f"{name}: container_name prevents scaling")
|
||||
for kind, actual in (("secrets", secrets), ("volumes", volumes), ("networks", nets)):
|
||||
if actual != want[kind]:
|
||||
problems.append(f"{name}: {kind} {actual}, policy {want[kind]}")
|
||||
if ports and not want.get("ports"):
|
||||
problems.append(f"{name}: publishes {ports} in bypass of the balancer")
|
||||
if any(not port.startswith("127.0.0.1:") for port in ports):
|
||||
problems.append(f"{name}: publishes {ports} beyond loopback")
|
||||
if name == "api" and not service.get("read_only"):
|
||||
problems.append("api: root filesystem is writable")
|
||||
internal = [n for n in nets if networks.get(n, {}).get("internal")]
|
||||
return [
|
||||
name,
|
||||
f"{limits.get('cpus', '-')}/{int(limits.get('memory') or 0) >> 20}MiB",
|
||||
",".join(ports) or "-",
|
||||
",".join(secrets) or "-",
|
||||
",".join(volumes) or "-",
|
||||
",".join(f"{n}{'(internal)' if n in internal else ''}" for n in nets),
|
||||
str(service.get("read_only", False)).lower(),
|
||||
]
|
||||
|
||||
|
||||
def running(problems):
|
||||
_, ids = run("docker", "compose", "ps", "-q")
|
||||
template = (
|
||||
'{{index .Config.Labels "com.docker.compose.service"}} {{.Config.Hostname}} '
|
||||
"{{.HostConfig.NanoCpus}} {{.HostConfig.Memory}} {{json .HostConfig.PortBindings}}"
|
||||
)
|
||||
_, found = run("docker", "inspect", "-f", template, *ids.split())
|
||||
rows = []
|
||||
for line in found.splitlines():
|
||||
service, host, cpus, memory, bindings = line.split(" ", 4)
|
||||
published = [
|
||||
f"{b['HostIp']}:{b['HostPort']}->{port}"
|
||||
for port, items in (json.loads(bindings) or {}).items()
|
||||
for b in items or []
|
||||
]
|
||||
if int(cpus) <= 0 or int(memory) <= 0:
|
||||
problems.append(f"container {service}/{host}: no CPU or memory limit applied")
|
||||
if published and service != "lb":
|
||||
problems.append(f"container {service}/{host}: publishes {published}")
|
||||
rows.append(
|
||||
[service, host, f"{int(cpus) / 1e9:g}", f"{int(memory) >> 20}MiB", ",".join(published)]
|
||||
)
|
||||
return sorted(rows)
|
||||
|
||||
|
||||
def table(header, rows):
|
||||
widths = [max(len(str(r[i])) for r in [header, *rows]) for i in range(len(header))]
|
||||
lines = [header, ["-" * w for w in widths], *rows]
|
||||
return [" ".join(str(c).ljust(w) for c, w in zip(r, widths)).rstrip() for r in lines]
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="limits, ports, secrets, volumes and networks")
|
||||
parser.add_argument("--out", help="text report, written only after a complete check")
|
||||
args = parser.parse_args()
|
||||
problems = []
|
||||
_, raw = run("docker", "compose", "--profile", "bench", "config", "--format", "json")
|
||||
config = json.loads(raw)
|
||||
services = [
|
||||
declared(name, service, config.get("networks", {}), problems)
|
||||
for name, service in sorted(config["services"].items())
|
||||
]
|
||||
containers = running(problems)
|
||||
if not any(row[0] == "api" for row in containers):
|
||||
problems.append("no running api replicas to inspect")
|
||||
lines = [
|
||||
f"date: {time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime())}",
|
||||
"$ docker compose --profile bench config --format json (declared)",
|
||||
*table(["service", "cpus/mem", "ports", "secrets", "volumes", "networks", "ro"], services),
|
||||
"",
|
||||
"$ docker inspect (running containers: hostname = X-Instance-Id, applied limits)",
|
||||
*table(["service", "hostname", "cpus", "memory", "published"], containers),
|
||||
"",
|
||||
*(f"FAIL: {p}" for p in problems),
|
||||
"PASS: limits, ports, secrets, volumes and networks match the policy"
|
||||
if not problems
|
||||
else f"FAIL: {len(problems)} violation(s)",
|
||||
]
|
||||
text = "\n".join(lines) + "\n"
|
||||
if args.out:
|
||||
Path(args.out).write_text(text)
|
||||
print(text, end="")
|
||||
return 1 if problems else 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Executable
+14
@@ -0,0 +1,14 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
started() {
|
||||
for id in $(docker compose ps -aq); do
|
||||
docker inspect --format '{{index .Config.Labels "com.docker.compose.service"}} {{.Config.Hostname}} {{.State.StartedAt}} {{.State.Status}}' "$id"
|
||||
done | sort
|
||||
}
|
||||
echo "date: $(date -u +%FT%TZ) commit: $(git rev-parse --short HEAD)"
|
||||
echo "== before: service hostname StartedAt status"
|
||||
started
|
||||
make --no-print-directory test-persistence ACTION_SCRIPT=scripts/restart.sh BASE_URL="${1:-http://localhost:8080}"
|
||||
echo "== after: service hostname StartedAt status"
|
||||
started
|
||||
Executable
+436
@@ -0,0 +1,436 @@
|
||||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import base64
|
||||
import collections
|
||||
import functools
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
COUNTERS = ("wretr", "wredis", "econ", "eresp", "hrsp_5xx")
|
||||
|
||||
|
||||
@functools.cache
|
||||
def published(port, fallback):
|
||||
command = ["docker", "compose", "port", "lb", str(port)]
|
||||
found = subprocess.run(command, cwd=str(ROOT), capture_output=True, text=True, check=False)
|
||||
return found.stdout.strip() or fallback
|
||||
|
||||
|
||||
def base_url():
|
||||
return os.environ.get("BASE_URL") or "http://" + published(8080, "localhost:8080")
|
||||
|
||||
|
||||
def stats_url():
|
||||
return os.environ.get("LB_STATS_URL") or f"http://{published(8404, '127.0.0.1:8404')}/stats;csv"
|
||||
|
||||
|
||||
def call(method, path, body=None, headers=None, timeout=2.0):
|
||||
data = None if body is None else json.dumps(body, ensure_ascii=False).encode()
|
||||
head = {"Accept": "application/json"}
|
||||
if data is not None:
|
||||
head["Content-Type"] = "application/json"
|
||||
head.update(headers or {})
|
||||
request = urllib.request.Request(
|
||||
base_url().rstrip("/") + path, data=data, headers=head, method=method
|
||||
)
|
||||
try:
|
||||
with urllib.request.urlopen(request, timeout=timeout) as response:
|
||||
payload = json.loads(response.read() or b"null")
|
||||
return response.status, response.headers.get("X-Instance-Id", ""), payload
|
||||
except urllib.error.HTTPError as error:
|
||||
try:
|
||||
payload = json.loads(error.read() or b"null")
|
||||
except (OSError, ValueError):
|
||||
payload = None
|
||||
return error.code, error.headers.get("X-Instance-Id", ""), payload
|
||||
except (OSError, ValueError):
|
||||
return 0, "", None
|
||||
|
||||
|
||||
def emit(report, out=None):
|
||||
text = json.dumps(report, ensure_ascii=False, indent=2)
|
||||
if out:
|
||||
Path(out).parent.mkdir(parents=True, exist_ok=True)
|
||||
Path(out).write_text(text + "\n")
|
||||
print(text)
|
||||
|
||||
|
||||
def created(result, what):
|
||||
if result[0] != 201:
|
||||
raise SystemExit(f"{what} failed: HTTP {result[0]}")
|
||||
return result[2]
|
||||
|
||||
|
||||
def actor(label):
|
||||
username = f"p_{label}_{uuid.uuid4().hex[:12]}"
|
||||
password = "Probe-" + uuid.uuid4().hex
|
||||
body = {"username": username, "display_name": "probe " + label, "password": password}
|
||||
user = created(call("POST", "/api/v1/users", body, timeout=10), "register")
|
||||
basic = base64.b64encode(f"{username}:{password}".encode()).decode()
|
||||
login = call(
|
||||
"POST", "/api/v1/auth/sessions", headers={"Authorization": "Basic " + basic}, timeout=10
|
||||
)
|
||||
return {
|
||||
"id": user["id"],
|
||||
"auth": {"Authorization": "Bearer " + created(login, "login")["token"]},
|
||||
}
|
||||
|
||||
|
||||
def chat_of(owner, other):
|
||||
body = {"title": "probe", "member_ids": [owner["id"], other["id"]]}
|
||||
return created(call("POST", "/api/v1/chats", body, owner["auth"], timeout=10), "chat")["id"]
|
||||
|
||||
|
||||
def send(owner, chat, text, key):
|
||||
headers = dict(owner["auth"], **{"Idempotency-Key": key})
|
||||
return call("POST", f"/api/v1/chats/{chat}/messages", {"text": text}, headers)
|
||||
|
||||
|
||||
def lb_rows(backend="api"):
|
||||
try:
|
||||
with urllib.request.urlopen(stats_url(), timeout=2) as response:
|
||||
lines = response.read().decode().splitlines()
|
||||
except OSError:
|
||||
return None
|
||||
header = lines[0].lstrip("# ").split(",")
|
||||
rows = [dict(zip(header, line.split(","))) for line in lines[1:] if line]
|
||||
return [r for r in rows if r["pxname"] == backend]
|
||||
|
||||
|
||||
def lb_up():
|
||||
rows = lb_rows()
|
||||
if rows is None:
|
||||
return None
|
||||
return sum(1 for r in rows if r["svname"] != "BACKEND" and r["status"].startswith("UP"))
|
||||
|
||||
|
||||
def lb_counters():
|
||||
row = next((r for r in lb_rows() or [] if r["svname"] == "BACKEND"), {})
|
||||
return {name: int(row.get(name) or 0) for name in COUNTERS}
|
||||
|
||||
|
||||
def run_k6(out, env, on_start=None):
|
||||
Path(out).mkdir(parents=True, exist_ok=True)
|
||||
started = []
|
||||
process = subprocess.Popen(
|
||||
[str(ROOT / "scripts/k6.sh"), str(out)],
|
||||
env=dict(os.environ, **env),
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT,
|
||||
text=True,
|
||||
)
|
||||
with (Path(out) / "k6.txt").open("w") as log:
|
||||
for line in process.stdout:
|
||||
log.write(line)
|
||||
if not started and "LOAD-START" in line:
|
||||
started.append(time.time())
|
||||
if on_start:
|
||||
on_start()
|
||||
return process.wait(), started[0] if started else None
|
||||
|
||||
|
||||
def k6_metrics(path, duration):
|
||||
metrics = json.loads(Path(path).read_text())["metrics"]
|
||||
|
||||
def value(name, field):
|
||||
return metrics.get(name + "{scenario:measure}", {}).get("values", {}).get(field, 0.0)
|
||||
|
||||
count = value("http_reqs", "count")
|
||||
return {
|
||||
"requests": int(count),
|
||||
"rps": round(count / duration, 2),
|
||||
"ok_rps": round(count * (1 - value("http_req_failed", "rate")) / duration, 2),
|
||||
"failed_ratio": round(value("http_req_failed", "rate"), 5),
|
||||
"dropped_iterations": int(value("dropped_iterations", "count")),
|
||||
"p50_ms": round(value("http_req_duration", "med"), 2),
|
||||
"p95_ms": round(value("http_req_duration", "p(95)"), 2),
|
||||
"p99_ms": round(value("http_req_duration", "p(99)"), 2),
|
||||
}
|
||||
|
||||
|
||||
def race(alice, chat, parallel):
|
||||
key, text = uuid.uuid4().hex, "same key " + uuid.uuid4().hex
|
||||
barrier, results = threading.Barrier(parallel), []
|
||||
|
||||
def worker():
|
||||
barrier.wait()
|
||||
results.append(send(alice, chat, text, key))
|
||||
|
||||
threads = [threading.Thread(target=worker) for _ in range(parallel)]
|
||||
for thread in threads:
|
||||
thread.start()
|
||||
for thread in threads:
|
||||
thread.join()
|
||||
bodies = [r[2] for r in results if r[0] == 201]
|
||||
identical = bool(bodies) and all(b == bodies[0] for b in bodies)
|
||||
return text, results, identical, send(alice, chat, text + " changed", key)[0]
|
||||
|
||||
|
||||
def check(args):
|
||||
alice, bob = actor("alice"), actor("bob")
|
||||
chat = chat_of(alice, bob)
|
||||
seen = collections.Counter(
|
||||
call("GET", "/api/v1/users/me", headers=alice["auth"])[1] for _ in range(args.samples)
|
||||
)
|
||||
rounds = [race(alice, chat, args.parallel) for _ in range(min(args.rounds, 100))]
|
||||
page = call("GET", f"/api/v1/chats/{chat}/messages?limit=100", headers=bob["auth"])[2]
|
||||
copies = collections.Counter(m.get("text") for m in (page or {"items": []})["items"])
|
||||
statuses = collections.Counter(r[0] for _, results, _, _ in rounds for r in results)
|
||||
served = collections.Counter(r[1] for _, results, _, _ in rounds for r in results)
|
||||
same = {
|
||||
"rounds": len(rounds),
|
||||
"parallel": args.parallel,
|
||||
"statuses": dict(statuses),
|
||||
"instances": dict(served),
|
||||
"identical_bodies_rounds": sum(1 for _, _, identical, _ in rounds if identical),
|
||||
"single_copy_rounds": sum(1 for text, _, _, _ in rounds if copies[text] == 1),
|
||||
"conflict_rounds": sum(1 for _, _, _, other in rounds if other == 409),
|
||||
}
|
||||
passed = (
|
||||
len(seen) >= 2
|
||||
and statuses == {201: len(rounds) * args.parallel}
|
||||
and len(served) >= 2
|
||||
and same["identical_bodies_rounds"] == len(rounds)
|
||||
and same["single_copy_rounds"] == len(rounds)
|
||||
and same["conflict_rounds"] == len(rounds)
|
||||
)
|
||||
emit({"instances_one_token": dict(seen), "same_key": same, "pass": passed}, args.out)
|
||||
return 0 if passed else 1
|
||||
|
||||
|
||||
def record(stop, alice, chat):
|
||||
probes, trace = [], []
|
||||
|
||||
def prober():
|
||||
key = uuid.uuid4().hex
|
||||
while not stop.is_set():
|
||||
started = time.time()
|
||||
status, instance, _ = send(alice, chat, "probe " + key, key)
|
||||
probes.append((started, status, round(1000 * (time.time() - started), 1), instance))
|
||||
if status == 201:
|
||||
key = uuid.uuid4().hex
|
||||
time.sleep(0.1)
|
||||
|
||||
def poller():
|
||||
while not stop.is_set():
|
||||
trace.append((time.time(), lb_up()))
|
||||
time.sleep(0.1)
|
||||
|
||||
threads = [threading.Thread(target=t, daemon=True) for t in (prober, poller)]
|
||||
for thread in threads:
|
||||
thread.start()
|
||||
return probes, trace, threads
|
||||
|
||||
|
||||
def write_csv(path, header, rows):
|
||||
with path.open("w") as handle:
|
||||
handle.write(header + "\n")
|
||||
handle.writelines(",".join(str(v) for v in row) + "\n" for row in rows)
|
||||
|
||||
|
||||
def summarize(args, probes, trace, action, expected):
|
||||
t0, end = action["start"], action["end"]
|
||||
label = args.label or os.environ.get("TARGET_INSTANCE_ID", "action")
|
||||
failures = [t - t0 for t, s, _, _ in probes if s != 201 and t >= t0]
|
||||
down = [t for t, up in trace if t >= t0 and up is not None and up < expected]
|
||||
back = [t for t, up in trace if down and t > down[0] and up is not None and up >= expected]
|
||||
outage = [t for t in down if not back or t < back[0]] or [end]
|
||||
served = [(t, i) for t, s, _, i in probes if s == 201]
|
||||
before = collections.Counter(i for t, i in served if t < t0)
|
||||
during = collections.Counter(i for t, i in served if outage[0] <= t <= outage[-1])
|
||||
after = collections.Counter(i for t, i in served if t > end)
|
||||
return {
|
||||
"label": label,
|
||||
"action": args.command,
|
||||
"action_exit": action["exit"],
|
||||
"action_seconds": round(end - t0, 3),
|
||||
"action_finished_before_load_end": end < action["load_end"],
|
||||
"lb_up_before": expected,
|
||||
"lb_up_min": min((up for _, up in trace if up is not None), default=None),
|
||||
"detection_seconds": round(down[0] - t0, 3) if down else None,
|
||||
"rejoin_seconds": round(back[0] - t0, 3) if back else None,
|
||||
"probe": {
|
||||
"attempts": len(probes),
|
||||
"failed_attempts": sum(1 for _, s, _, _ in probes if s != 201),
|
||||
"first_failure_s": round(failures[0], 3) if failures else None,
|
||||
"recovery_s": round(failures[-1], 3) if failures else 0.0,
|
||||
"instances_before": dict(before),
|
||||
"instances_during_outage": dict(during),
|
||||
"instances_after": dict(after),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def verdict(args, result):
|
||||
probe, k6 = result["probe"], result["k6"]
|
||||
checks = {
|
||||
"action_ok": result["action_exit"] == 0 and result["action_finished_before_load_end"],
|
||||
"load_errors_ok": k6["exit"] == 0 and k6["failed_ratio"] <= args.max_failed_ratio,
|
||||
"write_recovery_ok": probe["recovery_s"] <= args.max_recovery,
|
||||
}
|
||||
if args.expect == "dip":
|
||||
checks["detected"] = result["detection_seconds"] is not None
|
||||
checks["target_excluded"] = probe["instances_during_outage"].get(result["label"], 0) == 0
|
||||
checks["rejoined"] = result["rejoin_seconds"] is not None and bool(
|
||||
probe["instances_after"].get(result["label"])
|
||||
)
|
||||
if args.expect == "replace":
|
||||
checks["capacity_kept"] = (result["lb_up_min"] or 0) >= result["lb_up_before"]
|
||||
old, new = set(probe["instances_before"]), set(probe["instances_after"])
|
||||
checks["all_replaced"] = bool(new) and not old & new
|
||||
return checks
|
||||
|
||||
|
||||
def load(args):
|
||||
out = Path(args.out)
|
||||
out.mkdir(parents=True, exist_ok=True)
|
||||
expected = lb_up() or 0
|
||||
alice, bob = actor("alice"), actor("bob")
|
||||
stop, action = threading.Event(), {}
|
||||
probes, trace, threads = record(stop, alice, chat_of(alice, bob))
|
||||
|
||||
def act():
|
||||
time.sleep(args.warmup + args.lead)
|
||||
action["start"] = time.time()
|
||||
with (out / "action.txt").open("w") as log:
|
||||
action["exit"] = subprocess.call(
|
||||
args.command, cwd=str(ROOT), stdout=log, stderr=subprocess.STDOUT
|
||||
)
|
||||
action["end"] = time.time()
|
||||
|
||||
runner = threading.Thread(target=act)
|
||||
env = {"RATE": str(args.rate), "WARMUP": str(args.warmup), "DURATION": str(args.duration)}
|
||||
counters = lb_counters()
|
||||
code, load_start = run_k6(out / "k6", env, runner.start)
|
||||
if load_start is not None:
|
||||
runner.join()
|
||||
action["load_end"] = load_start + args.warmup + args.duration
|
||||
stop.set()
|
||||
for thread in threads:
|
||||
thread.join()
|
||||
if "start" not in action:
|
||||
raise SystemExit(f"k6 failed before the action (exit {code}), see {out / 'k6/k6.txt'}")
|
||||
t0 = action["start"]
|
||||
write_csv(
|
||||
out / "probe.csv",
|
||||
"t_rel_s,status,latency_ms,instance",
|
||||
((round(t - t0, 3), *r) for t, *r in probes),
|
||||
)
|
||||
write_csv(out / "lb_up.csv", "t_rel_s,up", ((round(t - t0, 3), up) for t, up in trace))
|
||||
result = summarize(args, probes, trace, action, expected)
|
||||
result["lb_counters"] = {k: v - counters[k] for k, v in lb_counters().items()}
|
||||
result["k6"] = dict(
|
||||
k6_metrics(out / "k6/summary.json", args.duration), exit=code, rate=args.rate
|
||||
)
|
||||
result["checks"] = verdict(args, result)
|
||||
result["pass"] = all(result["checks"].values())
|
||||
(out / "result.json").write_text(json.dumps(result, ensure_ascii=False, indent=2) + "\n")
|
||||
print(json.dumps(result, ensure_ascii=False))
|
||||
return 0 if result["pass"] else 1
|
||||
|
||||
|
||||
def detect(args):
|
||||
before = lb_up() or 0
|
||||
until = before - 1 if args.until is None else args.until
|
||||
started = time.time()
|
||||
process = subprocess.Popen(args.command, cwd=str(ROOT))
|
||||
detected = acted = None
|
||||
while time.time() - started < args.timeout and (detected is None or acted is None):
|
||||
if acted is None and process.poll() is not None:
|
||||
acted = time.time() - started
|
||||
if detected is None and (lb_up() or 0) <= until:
|
||||
detected = time.time() - started
|
||||
time.sleep(0.05)
|
||||
code = process.wait()
|
||||
acted = time.time() - started if acted is None else acted
|
||||
status, instance, payload = call("GET", "/health/live")
|
||||
restored = None
|
||||
if args.restore:
|
||||
begun = time.time()
|
||||
subprocess.call(["sh", "-c", args.restore], cwd=str(ROOT))
|
||||
while (lb_up() or 0) < before and time.time() - begun < 3 * args.timeout:
|
||||
time.sleep(0.1)
|
||||
restored = round(time.time() - begun, 3) if (lb_up() or 0) >= before else None
|
||||
checks = {
|
||||
"action_ok": code == 0,
|
||||
"detected_in_time": detected is not None and detected <= args.max + args.slack,
|
||||
"lb_answer_ok": status == args.lb_status,
|
||||
}
|
||||
if args.within_action:
|
||||
checks["excluded_before_action_end"] = detected is not None and detected < acted
|
||||
if args.restore:
|
||||
checks["restored"] = restored is not None
|
||||
report = {
|
||||
"action": args.command,
|
||||
"action_exit": code,
|
||||
"action_seconds": round(acted, 3),
|
||||
"lb_up_before": before,
|
||||
"lb_up_counted_as_detected": until,
|
||||
"detection_seconds": None if detected is None else round(detected, 3),
|
||||
"declared_max_seconds": args.max,
|
||||
"measurement_slack_seconds": args.slack,
|
||||
"lb_answer_after_detection": {
|
||||
"path": "/health/live",
|
||||
"status": status,
|
||||
"x_instance_id_present": bool(instance),
|
||||
"error_code": ((payload or {}).get("error") or {}).get("code"),
|
||||
},
|
||||
"restore": args.restore,
|
||||
"restore_seconds": restored,
|
||||
"lb_up_after_restore": lb_up(),
|
||||
"checks": checks,
|
||||
"pass": all(checks.values()),
|
||||
}
|
||||
emit(report, args.out)
|
||||
return 0 if report["pass"] else 1
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="LB probes over the public HTTP contract")
|
||||
sub = parser.add_subparsers(dest="mode", required=True)
|
||||
one = sub.add_parser("check", help="instances behind one token and same-key races")
|
||||
one.add_argument("--samples", type=int, default=40)
|
||||
one.add_argument("--parallel", type=int, default=8)
|
||||
one.add_argument("--rounds", type=int, default=1)
|
||||
one.add_argument("--out", help="JSON file, written only after a complete report")
|
||||
under = sub.add_parser("load", help="run an action under k6 load and measure recovery")
|
||||
under.add_argument("--out", required=True)
|
||||
under.add_argument("--label")
|
||||
under.add_argument("--expect", choices=("dip", "replace"), default="dip")
|
||||
under.add_argument("--rate", type=int, default=int(os.environ.get("RATE", "50")))
|
||||
under.add_argument("--warmup", type=int, default=10)
|
||||
under.add_argument("--duration", type=int, default=int(os.environ.get("DURATION", "60")))
|
||||
under.add_argument("--lead", type=int, default=15)
|
||||
under.add_argument("--max-failed-ratio", type=float, default=0.01)
|
||||
under.add_argument("--max-recovery", type=float, default=10.0)
|
||||
under.add_argument("command", nargs=argparse.REMAINDER)
|
||||
watch = sub.add_parser("detect", help="time until the LB takes replicas out of rotation")
|
||||
watch.add_argument("--max", type=float, required=True, help="declared worst detection, s")
|
||||
watch.add_argument("--slack", type=float, default=0.25, help="docker CLI and polling, s")
|
||||
watch.add_argument("--until", type=int, help="UP servers counted as detected")
|
||||
watch.add_argument("--within-action", action="store_true")
|
||||
watch.add_argument("--lb-status", type=int, default=200)
|
||||
watch.add_argument("--restore", help="shell command that brings replicas back")
|
||||
watch.add_argument("--timeout", type=float, default=20)
|
||||
watch.add_argument("--out", help="JSON file, written only after a complete report")
|
||||
watch.add_argument("command", nargs=argparse.REMAINDER)
|
||||
args = parser.parse_args()
|
||||
if args.mode == "check":
|
||||
return check(args)
|
||||
args.command = [c for c in args.command if c != "--"]
|
||||
if not args.command:
|
||||
parser.error(f"{args.mode} needs an action command after --")
|
||||
return load(args) if args.mode == "load" else detect(args)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Executable
+12
@@ -0,0 +1,12 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
defined=$(docker compose config --services)
|
||||
stores=""
|
||||
for service in ${STATE_SERVICES:-postgres valkey}; do
|
||||
if printf '%s\n' "$defined" | grep -qx "$service"; then stores="$stores $service"; fi
|
||||
done
|
||||
[ -n "$stores" ] || { echo "no state services found" >&2; exit 3; }
|
||||
docker compose stop lb "${API_SERVICE:-api}"
|
||||
docker compose restart $stores
|
||||
docker compose up -d --no-recreate --wait --wait-timeout "${WAIT_TIMEOUT:-75}"
|
||||
Executable
+5
@@ -0,0 +1,5 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
docker compose up -d --no-deps --no-recreate --wait --wait-timeout "${WAIT_TIMEOUT:-60}" "${API_SERVICE:-api}"
|
||||
scripts/lb-wait.sh
|
||||
Executable
+20
@@ -0,0 +1,20 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
service=${API_SERVICE:-api}
|
||||
show() { docker inspect -f '{{.Config.Hostname}} image={{slice .Image 7 19}}' "$1"; }
|
||||
echo "$(date -u +%T) migrate once before the first replacement"
|
||||
docker compose run --rm --no-deps -T migrate 2>&1
|
||||
old=$(docker compose ps -q "$service")
|
||||
count=$(printf '%s\n' "$old" | grep -c .)
|
||||
for id in $old; do
|
||||
running=$(docker compose ps -q "$service")
|
||||
docker compose up -d --no-deps --no-recreate --wait --wait-timeout "${WAIT_TIMEOUT:-90}" --scale "$service=$((count + 1))" "$service" 2>&1
|
||||
scripts/lb-wait.sh $((count + 1))
|
||||
new=$(docker compose ps -q "$service" | grep -v -F "$running")
|
||||
echo "$(date -u +%T) replace $(show "$id") -> $(show "$new")"
|
||||
docker stop -t "${STOP_TIMEOUT:-6}" "$id" >/dev/null
|
||||
docker rm "$id" >/dev/null
|
||||
done
|
||||
scripts/lb-wait.sh "$count"
|
||||
echo "$(date -u +%T) done: $count replicas replaced"
|
||||
Executable
+20
@@ -0,0 +1,20 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
umask 077
|
||||
mkdir -p secrets
|
||||
random() { od -An -N32 -tx1 /dev/urandom | tr -d ' \n'; }
|
||||
put() {
|
||||
if [ ! -s "secrets/$1" ]; then
|
||||
printf '%s' "$2" > "secrets/$1"
|
||||
chmod 644 "secrets/$1"
|
||||
echo "создан secrets/$1"
|
||||
fi
|
||||
}
|
||||
put postgres_password "$(random)"
|
||||
put database_url "postgresql://messenger:$(cat secrets/postgres_password)@postgres:5432/messenger"
|
||||
put cursor_secret "$(random)"
|
||||
put valkey_password "$(random)"
|
||||
put valkey.conf "requirepass $(cat secrets/valkey_password)"
|
||||
put cache_url "redis://:$(cat secrets/valkey_password)@valkey:6379/0"
|
||||
[ -f .env ] || { cp .env.example .env; echo "создан .env из .env.example"; }
|
||||
@@ -0,0 +1,59 @@
|
||||
\set ON_ERROR_STOP on
|
||||
\set big '''00000000-0000-7000-8000-00000000c001'''
|
||||
\set cursor_at '''2026-01-01T02:46:40Z'''
|
||||
\set cursor_id '''00000000-0000-7000-8001-000000002710'''
|
||||
|
||||
PREPARE history_first (uuid, int) AS
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = $1
|
||||
ORDER BY created_at, id
|
||||
LIMIT $2;
|
||||
|
||||
PREPARE history_after (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;
|
||||
|
||||
PREPARE history_offset (uuid, int, int) AS
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = $1
|
||||
ORDER BY created_at, id
|
||||
LIMIT $2 OFFSET $3;
|
||||
|
||||
SELECT count(*) AS chat_messages FROM messages WHERE chat_id = :big;
|
||||
SELECT count(*) AS all_messages FROM messages;
|
||||
SELECT pg_size_pretty(pg_relation_size('messages_history_idx')) AS history_index_size;
|
||||
|
||||
\echo '== Первая страница, limit=50 (запрос HISTORY_FIRST приложения, LIMIT limit+1)'
|
||||
EXPLAIN (ANALYZE, BUFFERS) EXECUTE history_first(:big, 51);
|
||||
|
||||
\echo '== Глубокая страница: keyset после 10 000-го сообщения (запрос HISTORY_AFTER приложения)'
|
||||
EXPLAIN (ANALYZE, BUFFERS) EXECUTE history_after(:big, :cursor_at, :cursor_id, 51);
|
||||
|
||||
\echo '== Та же глубокая страница через OFFSET 10000 (не используется приложением, для сравнения)'
|
||||
EXPLAIN (ANALYZE, BUFFERS) EXECUTE history_offset(:big, 51, 10000);
|
||||
|
||||
\echo '== Проверка: keyset и OFFSET возвращают одну и ту же страницу'
|
||||
SELECT (
|
||||
SELECT array_agg(id ORDER BY created_at, id) FROM (
|
||||
SELECT id, created_at FROM messages
|
||||
WHERE chat_id = :big AND (created_at, id) > (:cursor_at, :cursor_id)
|
||||
ORDER BY created_at, id LIMIT 51
|
||||
) AS keyset
|
||||
) = (
|
||||
SELECT array_agg(id ORDER BY created_at, id) FROM (
|
||||
SELECT id, created_at FROM messages
|
||||
WHERE chat_id = :big
|
||||
ORDER BY created_at, id LIMIT 51 OFFSET 10000
|
||||
) AS skipped
|
||||
) AS same_page;
|
||||
|
||||
\echo '== Контрольный план без индекса истории (DROP INDEX внутри транзакции, затем ROLLBACK)'
|
||||
BEGIN;
|
||||
DROP INDEX messages_history_idx;
|
||||
EXPLAIN (ANALYZE, BUFFERS) EXECUTE history_after(:big, :cursor_at, :cursor_id, 51);
|
||||
ROLLBACK;
|
||||
@@ -0,0 +1,40 @@
|
||||
\set ON_ERROR_STOP on
|
||||
\set author '''00000000-0000-7000-8000-00000000a001'''
|
||||
\set big '''00000000-0000-7000-8000-00000000c001'''
|
||||
\set epoch '''2026-01-01T00:00:00Z'''
|
||||
|
||||
INSERT INTO users (id, username, display_name, password_hash, created_at)
|
||||
VALUES (:author, 'seed_author', 'Синтетический автор', '$argon2id$v=19$m=19456,t=2,p=1$c2VlZA$c2VlZA', :epoch)
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
INSERT INTO chats (id, title, created_at)
|
||||
SELECT ('00000000-0000-7000-8000-00000000c' || lpad(to_hex(chat), 3, '0'))::uuid,
|
||||
'Синтетический чат ' || chat, :epoch
|
||||
FROM generate_series(1, 21) AS chat
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
INSERT INTO chat_members (chat_id, user_id, position, chat_created_at)
|
||||
SELECT id, :author, 1, created_at FROM chats WHERE title LIKE 'Синтетический чат %'
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
||||
SELECT ('00000000-0000-7000-8001-' || lpad(to_hex(n), 12, '0'))::uuid, :big, :author,
|
||||
'синтетическое сообщение ' || n, :epoch::timestamptz + n * interval '1 second'
|
||||
FROM generate_series(1, 20000) AS n
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
||||
SELECT ('00000000-0000-7000-8002-' || lpad(to_hex(chat * 1000 + n), 12, '0'))::uuid,
|
||||
('00000000-0000-7000-8000-00000000c' || lpad(to_hex(chat), 3, '0'))::uuid, :author,
|
||||
'фоновое сообщение ' || n, :epoch::timestamptz + n * interval '1 second'
|
||||
FROM generate_series(2, 21) AS chat, generate_series(1, 500) AS n
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
UPDATE chats
|
||||
SET last_message_at = (SELECT max(created_at) FROM messages WHERE messages.chat_id = chats.id)
|
||||
WHERE title LIKE 'Синтетический чат %';
|
||||
|
||||
ANALYZE;
|
||||
|
||||
SELECT count(*) FILTER (WHERE chat_id = :big) AS big_chat_messages, count(*) AS all_messages
|
||||
FROM messages;
|
||||
Executable
+9
@@ -0,0 +1,9 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
|
||||
cd "$(dirname "$0")/.."
|
||||
ids=$(docker compose ps -q "${API_SERVICE:-api}")
|
||||
[ -n "$ids" ] || { echo "no running ${API_SERVICE:-api} replicas" >&2; exit 3; }
|
||||
target=$(docker inspect -f '{{.Id}} {{.Config.Hostname}}' $ids | awk -v t="$TARGET_INSTANCE_ID" '$2 == t {print $1}')
|
||||
[ -n "$target" ] || { echo "no running ${API_SERVICE:-api} replica answers as $TARGET_INSTANCE_ID" >&2; exit 3; }
|
||||
docker stop -t "${STOP_TIMEOUT:-6}" "$target" >/dev/null
|
||||
@@ -0,0 +1,66 @@
|
||||
import asyncio
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
from typing import Any
|
||||
|
||||
from gunicorn.arbiter import Arbiter
|
||||
from gunicorn.glogging import Logger as GunicornBaseLogger
|
||||
from uvicorn.server import Server
|
||||
from uvicorn_worker import UvicornWorker
|
||||
|
||||
from messenger.main import LOG_FIELDS
|
||||
from messenger.shared.entrypoints.health import DRAINING
|
||||
from messenger.shared.infrastructure.logging import configure
|
||||
|
||||
LOG_LEVEL = os.environ.get("LOG_LEVEL", "INFO")
|
||||
SHUTDOWN_SECONDS = int(os.environ.get("SHUTDOWN_TIMEOUT_SECONDS", "5"))
|
||||
DRAIN_SECONDS = float(os.environ.get("DRAIN_SECONDS", "2"))
|
||||
|
||||
|
||||
class StructlogGunicornLogger(GunicornBaseLogger):
|
||||
def setup(self, cfg: object) -> None:
|
||||
configure(LOG_LEVEL, LOG_FIELDS)
|
||||
|
||||
def access(self, resp: object, req: object, environ: object, request_time: object) -> None:
|
||||
return
|
||||
|
||||
|
||||
class DrainingServer(Server):
|
||||
async def main_loop(self) -> None:
|
||||
await super().main_loop()
|
||||
if not self.force_exit:
|
||||
DRAINING.set()
|
||||
logging.getLogger("uvicorn.error").info("Draining for %.1f s", DRAIN_SECONDS)
|
||||
await asyncio.sleep(DRAIN_SECONDS)
|
||||
|
||||
|
||||
class MessengerWorker(UvicornWorker):
|
||||
CONFIG_KWARGS: dict[str, Any] = {
|
||||
"access_log": False,
|
||||
"server_header": False,
|
||||
"date_header": False,
|
||||
"timeout_graceful_shutdown": max(1, int(SHUTDOWN_SECONDS - DRAIN_SECONDS)),
|
||||
}
|
||||
|
||||
async def _serve(self) -> None:
|
||||
self.config.app = self.wsgi
|
||||
server = DrainingServer(config=self.config)
|
||||
self._install_sigquit_handler()
|
||||
await server.serve(sockets=self.sockets)
|
||||
if not server.started:
|
||||
sys.exit(Arbiter.WORKER_BOOT_ERROR)
|
||||
|
||||
|
||||
bind = f"{os.environ.get('HOST', '0.0.0.0')}:{os.environ.get('PORT', '8080')}"
|
||||
workers = int(os.environ.get("WEB_CONCURRENCY", "2"))
|
||||
worker_class = "messenger.gunicorn_conf.MessengerWorker"
|
||||
logger_class = StructlogGunicornLogger
|
||||
loglevel = LOG_LEVEL.lower()
|
||||
accesslog = None
|
||||
errorlog = "-"
|
||||
timeout = int(float(os.environ.get("REQUEST_TIMEOUT_SECONDS", "8")) + 5)
|
||||
graceful_timeout = SHUTDOWN_SECONDS
|
||||
keepalive = 5
|
||||
preload_app = False
|
||||
control_socket_disable = True
|
||||
@@ -0,0 +1,5 @@
|
||||
from messenger.shared.application.messagebus import CommandBus
|
||||
|
||||
|
||||
class IdentityCommandBus(CommandBus):
|
||||
__slots__ = ()
|
||||
@@ -0,0 +1,22 @@
|
||||
from dataclasses import dataclass, field
|
||||
from uuid import UUID
|
||||
|
||||
from messenger.identity.domain.models import Session
|
||||
from messenger.shared.application.commands import Command
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class RegisterUser(Command):
|
||||
username: str
|
||||
display_name: str
|
||||
password: str = field(repr=False)
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class OpenSession(Command):
|
||||
user_id: UUID
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class CloseSession(Command):
|
||||
session: Session
|
||||
@@ -0,0 +1,76 @@
|
||||
from datetime import timedelta
|
||||
|
||||
from messenger.identity.application.commands import CloseSession, OpenSession, RegisterUser
|
||||
from messenger.identity.application.ports import (
|
||||
IdentityUnitOfWork,
|
||||
PasswordHasher,
|
||||
SessionDirectory,
|
||||
)
|
||||
from messenger.identity.domain.events import SessionClosed, SessionOpened
|
||||
from messenger.identity.domain.models import Session, User
|
||||
from messenger.identity.domain.values import DisplayName, Password, Username
|
||||
from messenger.shared.application.ports import Clock, IdGenerator
|
||||
|
||||
|
||||
class RegisterUserHandler:
|
||||
__slots__ = ("_clock", "_hasher", "_ids", "_uow")
|
||||
|
||||
def __init__(
|
||||
self, uow: IdentityUnitOfWork, clock: Clock, ids: IdGenerator, hasher: PasswordHasher
|
||||
) -> None:
|
||||
self._uow = uow
|
||||
self._clock = clock
|
||||
self._ids = ids
|
||||
self._hasher = hasher
|
||||
|
||||
async def __call__(self, command: RegisterUser) -> User:
|
||||
username = Username(command.username)
|
||||
display_name = DisplayName(command.display_name)
|
||||
password = Password(command.password)
|
||||
password_hash = await self._hasher.hash(password.value)
|
||||
user, event = User.register(
|
||||
user_id=self._ids.new_id(),
|
||||
username=username,
|
||||
display_name=display_name,
|
||||
password_hash=password_hash,
|
||||
registered_at=self._clock.now(),
|
||||
)
|
||||
async with self._uow.transaction():
|
||||
await self._uow.users.add(user)
|
||||
self._uow.record(event)
|
||||
await self._uow.commit()
|
||||
return user
|
||||
|
||||
|
||||
class OpenSessionHandler:
|
||||
__slots__ = ("_clock", "_ttl", "_uow")
|
||||
|
||||
def __init__(self, uow: IdentityUnitOfWork, clock: Clock, ttl: timedelta) -> None:
|
||||
self._uow = uow
|
||||
self._clock = clock
|
||||
self._ttl = ttl
|
||||
|
||||
async def __call__(self, command: OpenSession) -> tuple[str, Session]:
|
||||
token, session = Session.issue(
|
||||
user_id=command.user_id, issued_at=self._clock.now(), ttl=self._ttl
|
||||
)
|
||||
async with self._uow.transaction():
|
||||
await self._uow.sessions.add(session)
|
||||
self._uow.record(SessionOpened(user_id=session.user_id, expires_at=session.expires_at))
|
||||
await self._uow.commit()
|
||||
return token, session
|
||||
|
||||
|
||||
class CloseSessionHandler:
|
||||
__slots__ = ("_sessions", "_uow")
|
||||
|
||||
def __init__(self, uow: IdentityUnitOfWork, sessions: SessionDirectory) -> None:
|
||||
self._uow = uow
|
||||
self._sessions = sessions
|
||||
|
||||
async def __call__(self, command: CloseSession) -> None:
|
||||
await self._sessions.invalidate(command.session)
|
||||
async with self._uow.transaction():
|
||||
await self._uow.sessions.remove(command.session.token_hash)
|
||||
self._uow.record(SessionClosed(user_id=command.session.user_id))
|
||||
await self._uow.commit()
|
||||
@@ -0,0 +1,48 @@
|
||||
from datetime import datetime
|
||||
from typing import Protocol, runtime_checkable
|
||||
from uuid import UUID
|
||||
|
||||
from messenger.identity.domain.models import Session, User
|
||||
from messenger.shared.application.ports import UnitOfWork
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class UserRepository(Protocol):
|
||||
async def add(self, user: User) -> None: ...
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class SessionRepository(Protocol):
|
||||
async def add(self, session: Session) -> None: ...
|
||||
|
||||
async def remove(self, token_hash: bytes) -> None: ...
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class IdentityUnitOfWork(UnitOfWork, Protocol):
|
||||
@property
|
||||
def users(self) -> UserRepository: ...
|
||||
|
||||
@property
|
||||
def sessions(self) -> SessionRepository: ...
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class UserDirectory(Protocol):
|
||||
async def find(self, user_id: UUID) -> User | None: ...
|
||||
|
||||
async def find_by_username(self, username: str) -> User | None: ...
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class SessionDirectory(Protocol):
|
||||
async def find(self, token_hash: bytes, now: datetime) -> Session | None: ...
|
||||
|
||||
async def invalidate(self, session: Session) -> None: ...
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class PasswordHasher(Protocol):
|
||||
async def hash(self, password: str) -> str: ...
|
||||
|
||||
async def verify(self, password_hash: str | None, password: str) -> bool: ...
|
||||
@@ -0,0 +1,43 @@
|
||||
from uuid import UUID
|
||||
|
||||
from messenger.identity.application.ports import PasswordHasher, SessionDirectory, UserDirectory
|
||||
from messenger.identity.domain.errors import ActorNotIdentified
|
||||
from messenger.identity.domain.models import Session, User, token_digest
|
||||
from messenger.shared.application.ports import Clock
|
||||
|
||||
|
||||
class GetCurrentUser:
|
||||
__slots__ = ("_directory",)
|
||||
|
||||
def __init__(self, directory: UserDirectory) -> None:
|
||||
self._directory = directory
|
||||
|
||||
async def __call__(self, user_id: UUID) -> User:
|
||||
user = await self._directory.find(user_id)
|
||||
if user is None:
|
||||
raise ActorNotIdentified("идентификация не подтверждена")
|
||||
return user
|
||||
|
||||
|
||||
class VerifyCredentials:
|
||||
__slots__ = ("_directory", "_hasher")
|
||||
|
||||
def __init__(self, directory: UserDirectory, hasher: PasswordHasher) -> None:
|
||||
self._directory = directory
|
||||
self._hasher = hasher
|
||||
|
||||
async def __call__(self, username: str, password: str) -> User | None:
|
||||
user = await self._directory.find_by_username(username)
|
||||
valid = await self._hasher.verify(user.password_hash if user else None, password)
|
||||
return user if valid else None
|
||||
|
||||
|
||||
class Authenticate:
|
||||
__slots__ = ("_clock", "_sessions")
|
||||
|
||||
def __init__(self, sessions: SessionDirectory, clock: Clock) -> None:
|
||||
self._sessions = sessions
|
||||
self._clock = clock
|
||||
|
||||
async def __call__(self, token: str) -> Session | None:
|
||||
return await self._sessions.find(token_digest(token), self._clock.now())
|
||||
@@ -0,0 +1,12 @@
|
||||
from pydantic import Field, SecretStr
|
||||
|
||||
from messenger.shared.config import Settings
|
||||
|
||||
|
||||
class IdentitySettings(Settings):
|
||||
session_ttl_seconds: int = Field(default=3600, ge=1, le=30 * 24 * 3600)
|
||||
password_hash_concurrency: int = Field(default=2, ge=1, le=64)
|
||||
cache_url: SecretStr | None = None
|
||||
session_cache_ttl_seconds: int = Field(default=300, ge=1, le=24 * 3600)
|
||||
cache_timeout_seconds: float = Field(default=0.2, gt=0, le=5)
|
||||
cache_cooldown_seconds: float = Field(default=1.0, ge=0, le=60)
|
||||
@@ -0,0 +1,17 @@
|
||||
from messenger.shared.domain.errors import DomainError
|
||||
|
||||
|
||||
class IdentityError(DomainError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidUserData(IdentityError):
|
||||
pass
|
||||
|
||||
|
||||
class UsernameAlreadyTaken(IdentityError):
|
||||
pass
|
||||
|
||||
|
||||
class ActorNotIdentified(IdentityError):
|
||||
pass
|
||||
@@ -0,0 +1,25 @@
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime
|
||||
from uuid import UUID
|
||||
|
||||
from messenger.shared.domain.events import DomainEvent
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class UserRegistered(DomainEvent):
|
||||
user_id: UUID
|
||||
registered_at: datetime
|
||||
name: str = field(init=False, default="user.registered")
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class SessionOpened(DomainEvent):
|
||||
user_id: UUID
|
||||
expires_at: datetime
|
||||
name: str = field(init=False, default="session.opened")
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class SessionClosed(DomainEvent):
|
||||
user_id: UUID
|
||||
name: str = field(init=False, default="session.closed")
|
||||
@@ -0,0 +1,62 @@
|
||||
import secrets
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime, timedelta
|
||||
from hashlib import sha256
|
||||
from uuid import UUID
|
||||
|
||||
from messenger.identity.domain.events import UserRegistered
|
||||
from messenger.identity.domain.values import DisplayName, Username
|
||||
from messenger.shared.domain.events import DomainEvent
|
||||
|
||||
TOKEN_BYTES = 32
|
||||
|
||||
|
||||
def token_digest(token: str) -> bytes:
|
||||
return sha256(token.encode()).digest()
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class User:
|
||||
id: UUID
|
||||
username: Username
|
||||
display_name: DisplayName
|
||||
created_at: datetime
|
||||
password_hash: str = field(repr=False)
|
||||
|
||||
@classmethod
|
||||
def register(
|
||||
cls,
|
||||
*,
|
||||
user_id: UUID,
|
||||
username: Username,
|
||||
display_name: DisplayName,
|
||||
password_hash: str,
|
||||
registered_at: datetime,
|
||||
) -> tuple[User, DomainEvent]:
|
||||
user = cls(
|
||||
id=user_id,
|
||||
username=username,
|
||||
display_name=display_name,
|
||||
created_at=registered_at,
|
||||
password_hash=password_hash,
|
||||
)
|
||||
return user, UserRegistered(user_id=user.id, registered_at=registered_at)
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class Session:
|
||||
token_hash: bytes = field(repr=False)
|
||||
user_id: UUID
|
||||
created_at: datetime
|
||||
expires_at: datetime
|
||||
|
||||
@classmethod
|
||||
def issue(cls, *, user_id: UUID, issued_at: datetime, ttl: timedelta) -> tuple[str, Session]:
|
||||
token = secrets.token_urlsafe(TOKEN_BYTES)
|
||||
session = cls(
|
||||
token_hash=token_digest(token),
|
||||
user_id=user_id,
|
||||
created_at=issued_at,
|
||||
expires_at=issued_at + ttl,
|
||||
)
|
||||
return token, session
|
||||
@@ -0,0 +1,54 @@
|
||||
import re
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
from messenger.identity.domain.errors import InvalidUserData
|
||||
|
||||
USERNAME_PATTERN = re.compile(r"\A[a-z0-9_]{3,32}\Z")
|
||||
DISPLAY_NAME_MAX = 100
|
||||
PASSWORD_MIN = 12
|
||||
PASSWORD_MAX = 128
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class Username:
|
||||
value: str
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if not isinstance(self.value, str):
|
||||
raise InvalidUserData("поле username должно быть строкой")
|
||||
if USERNAME_PATTERN.match(self.value) is None:
|
||||
raise InvalidUserData("поле username должно соответствовать [a-z0-9_]{3,32}")
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.value
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class DisplayName:
|
||||
value: str
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if not isinstance(self.value, str):
|
||||
raise InvalidUserData("поле display_name должно быть строкой")
|
||||
if not self.value.strip():
|
||||
raise InvalidUserData("поле display_name не может быть пустым или состоять из пробелов")
|
||||
if len(self.value) > DISPLAY_NAME_MAX:
|
||||
raise InvalidUserData(f"поле display_name длиннее {DISPLAY_NAME_MAX} символов Unicode")
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.value
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class Password:
|
||||
value: str = field(repr=False)
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if not isinstance(self.value, str):
|
||||
raise InvalidUserData("поле password должно быть строкой")
|
||||
if not self.value.strip():
|
||||
raise InvalidUserData("поле password не может состоять из пробелов")
|
||||
if not PASSWORD_MIN <= len(self.value) <= PASSWORD_MAX:
|
||||
raise InvalidUserData(
|
||||
f"поле password: от {PASSWORD_MIN} до {PASSWORD_MAX} символов Unicode"
|
||||
)
|
||||
@@ -0,0 +1,98 @@
|
||||
import re
|
||||
from base64 import b64decode
|
||||
from binascii import Error as Base64Error
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import Depends, Request
|
||||
from starlette.types import Scope
|
||||
|
||||
from messenger.identity.application.queries import Authenticate, VerifyCredentials
|
||||
from messenger.identity.domain.models import Session, User
|
||||
from messenger.shared.entrypoints.errors import Unauthenticated
|
||||
from messenger.shared.entrypoints.pipeline import Rejection, header
|
||||
|
||||
LOGIN_PATH = "/api/v1/auth/sessions"
|
||||
PROTECTED_PREFIX = "/api/v1/"
|
||||
PUBLIC_ROUTES = frozenset({("POST", "/api/v1/users")})
|
||||
BASIC_CHALLENGE = 'Basic realm="messenger", charset="UTF-8"'
|
||||
BEARER_CHALLENGE = 'Bearer realm="messenger"'
|
||||
AUTHORIZATION = b"authorization"
|
||||
TOKEN = re.compile(r"[A-Za-z0-9\-._~+/]{1,512}=*")
|
||||
|
||||
|
||||
def normalized(path: str) -> str:
|
||||
return path.rstrip("/") or "/"
|
||||
|
||||
|
||||
def challenge_for(path: str) -> str:
|
||||
return BASIC_CHALLENGE if normalized(path) == LOGIN_PATH else BEARER_CHALLENGE
|
||||
|
||||
|
||||
def basic_credentials(value: str) -> tuple[str, str] | None:
|
||||
scheme, _, encoded = value.partition(" ")
|
||||
if scheme.lower() != "basic":
|
||||
return None
|
||||
try:
|
||||
decoded = b64decode(encoded.strip(), validate=True).decode("utf-8")
|
||||
except Base64Error, UnicodeDecodeError:
|
||||
return None
|
||||
username, separator, password = decoded.partition(":")
|
||||
if not separator or not username.isascii():
|
||||
return None
|
||||
return username, password
|
||||
|
||||
|
||||
def bearer_token(value: str) -> str | None:
|
||||
scheme, _, token = value.partition(" ")
|
||||
token = token.strip()
|
||||
if scheme.lower() != "bearer" or TOKEN.fullmatch(token) is None:
|
||||
return None
|
||||
return token
|
||||
|
||||
|
||||
def rejection(path: str) -> Rejection:
|
||||
return Rejection(401, "unauthorized", (("www-authenticate", challenge_for(path)),))
|
||||
|
||||
|
||||
async def guard(scope: Scope) -> Rejection | None:
|
||||
method, path = scope["method"], normalized(scope["path"])
|
||||
if not path.startswith(PROTECTED_PREFIX) or (method, path) in PUBLIC_ROUTES:
|
||||
return None
|
||||
container = scope["app"].state.dishka_container
|
||||
state = scope.setdefault("state", {})
|
||||
raw = header(scope, AUTHORIZATION)
|
||||
if (method, path) == ("POST", LOGIN_PATH):
|
||||
credentials = basic_credentials(raw)
|
||||
verify = await container.get(VerifyCredentials)
|
||||
user = await verify(*credentials) if credentials else None
|
||||
if user is None:
|
||||
return rejection(path)
|
||||
state["user"] = user
|
||||
state["actor"] = user.id
|
||||
return None
|
||||
token = bearer_token(raw)
|
||||
authenticate = await container.get(Authenticate)
|
||||
session = await authenticate(token) if token else None
|
||||
if session is None:
|
||||
return rejection(path)
|
||||
state["session"] = session
|
||||
state["actor"] = session.user_id
|
||||
return None
|
||||
|
||||
|
||||
async def verified_user(request: Request) -> User:
|
||||
user = getattr(request.state, "user", None)
|
||||
if not isinstance(user, User):
|
||||
raise Unauthenticated("учётные данные не подтверждены")
|
||||
return user
|
||||
|
||||
|
||||
async def current_session(request: Request) -> Session:
|
||||
session = getattr(request.state, "session", None)
|
||||
if not isinstance(session, Session):
|
||||
raise Unauthenticated("сессия не подтверждена")
|
||||
return session
|
||||
|
||||
|
||||
type VerifiedUser = Annotated[User, Depends(verified_user)]
|
||||
type CurrentSession = Annotated[Session, Depends(current_session)]
|
||||
@@ -0,0 +1,12 @@
|
||||
from messenger.identity.domain.errors import (
|
||||
ActorNotIdentified,
|
||||
InvalidUserData,
|
||||
UsernameAlreadyTaken,
|
||||
)
|
||||
from messenger.shared.domain.errors import DomainError
|
||||
|
||||
ERROR_CODES: dict[type[DomainError], tuple[str, int]] = {
|
||||
InvalidUserData: ("invalid_request", 400),
|
||||
UsernameAlreadyTaken: ("conflict", 409),
|
||||
ActorNotIdentified: ("unauthorized", 401),
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
from dishka.integrations.fastapi import FromDishka, inject
|
||||
from fastapi import APIRouter, Request, Response, status
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
|
||||
from messenger.identity.application.bus import IdentityCommandBus
|
||||
from messenger.identity.application.commands import CloseSession, OpenSession, RegisterUser
|
||||
from messenger.identity.application.queries import GetCurrentUser
|
||||
from messenger.identity.entrypoints.authentication import CurrentSession, VerifiedUser
|
||||
from messenger.identity.entrypoints.schemas import CreateUserRequest, SessionOut, UserOut
|
||||
from messenger.shared.entrypoints.actor import Actor
|
||||
|
||||
router = APIRouter(prefix="/api/v1/users", tags=["users"])
|
||||
sessions_router = APIRouter(prefix="/api/v1/auth/sessions", tags=["sessions"])
|
||||
|
||||
|
||||
@router.post("", status_code=status.HTTP_201_CREATED)
|
||||
@inject
|
||||
async def create_user(
|
||||
body: CreateUserRequest,
|
||||
bus: FromDishka[IdentityCommandBus],
|
||||
) -> UserOut:
|
||||
user = await bus.execute(
|
||||
RegisterUser(username=body.username, display_name=body.display_name, password=body.password)
|
||||
)
|
||||
return UserOut.of(user)
|
||||
|
||||
|
||||
@router.get("/me")
|
||||
@inject
|
||||
async def current_user(
|
||||
actor: Actor,
|
||||
query: FromDishka[GetCurrentUser],
|
||||
) -> UserOut:
|
||||
return UserOut.of(await query(actor))
|
||||
|
||||
|
||||
@sessions_router.post("", status_code=status.HTTP_201_CREATED)
|
||||
@inject
|
||||
async def open_session(
|
||||
request: Request,
|
||||
user: VerifiedUser,
|
||||
bus: FromDishka[IdentityCommandBus],
|
||||
) -> SessionOut:
|
||||
if await request.body():
|
||||
raise RequestValidationError([])
|
||||
token, session = await bus.execute(OpenSession(user_id=user.id))
|
||||
return SessionOut.of(token, session, user)
|
||||
|
||||
|
||||
@sessions_router.delete("/current", status_code=status.HTTP_204_NO_CONTENT)
|
||||
@inject
|
||||
async def close_session(
|
||||
session: CurrentSession,
|
||||
bus: FromDishka[IdentityCommandBus],
|
||||
) -> Response:
|
||||
await bus.execute(CloseSession(session=session))
|
||||
return Response(status_code=status.HTTP_204_NO_CONTENT)
|
||||
@@ -0,0 +1,112 @@
|
||||
from collections.abc import AsyncIterator
|
||||
from datetime import timedelta
|
||||
|
||||
from dishka import Provider, Scope, provide
|
||||
from redis.asyncio import Redis
|
||||
from redis.asyncio.retry import Retry
|
||||
from redis.backoff import NoBackoff
|
||||
|
||||
from messenger.identity.application.bus import IdentityCommandBus
|
||||
from messenger.identity.application.commands import CloseSession, OpenSession, RegisterUser
|
||||
from messenger.identity.application.handlers import (
|
||||
CloseSessionHandler,
|
||||
OpenSessionHandler,
|
||||
RegisterUserHandler,
|
||||
)
|
||||
from messenger.identity.application.ports import (
|
||||
IdentityUnitOfWork,
|
||||
PasswordHasher,
|
||||
SessionDirectory,
|
||||
UserDirectory,
|
||||
)
|
||||
from messenger.identity.application.queries import Authenticate, GetCurrentUser, VerifyCredentials
|
||||
from messenger.identity.config import IdentitySettings
|
||||
from messenger.identity.infrastructure.argon2 import Argon2PasswordHasher
|
||||
from messenger.identity.infrastructure.postgres import (
|
||||
PostgresIdentityUnitOfWork,
|
||||
PostgresSessionDirectory,
|
||||
PostgresUserDirectory,
|
||||
)
|
||||
from messenger.identity.infrastructure.valkey import CachedSessionDirectory
|
||||
from messenger.shared.application.commands import Command
|
||||
from messenger.shared.application.messagebus import CommandHandler
|
||||
from messenger.shared.application.ports import Clock, EventPublisher, IdGenerator, Logger
|
||||
from messenger.shared.infrastructure.postgres import Database
|
||||
|
||||
|
||||
class IdentityProvider(Provider):
|
||||
def __init__(self, settings: IdentitySettings) -> None:
|
||||
super().__init__()
|
||||
self._settings = settings
|
||||
|
||||
@provide(scope=Scope.APP)
|
||||
def settings(self) -> IdentitySettings:
|
||||
return self._settings
|
||||
|
||||
@provide(scope=Scope.APP)
|
||||
def hasher(self, settings: IdentitySettings) -> PasswordHasher:
|
||||
return Argon2PasswordHasher(settings.password_hash_concurrency)
|
||||
|
||||
@provide(scope=Scope.APP)
|
||||
def users(self, database: Database) -> UserDirectory:
|
||||
return PostgresUserDirectory(database)
|
||||
|
||||
@provide(scope=Scope.APP)
|
||||
async def sessions(
|
||||
self, database: Database, settings: IdentitySettings, logger: Logger
|
||||
) -> AsyncIterator[SessionDirectory]:
|
||||
origin = PostgresSessionDirectory(database)
|
||||
if settings.cache_url is None:
|
||||
yield origin
|
||||
return
|
||||
client = Redis.from_url(
|
||||
settings.cache_url.get_secret_value(),
|
||||
socket_connect_timeout=settings.cache_timeout_seconds,
|
||||
socket_timeout=settings.cache_timeout_seconds,
|
||||
retry=Retry(NoBackoff(), 1),
|
||||
)
|
||||
try:
|
||||
yield CachedSessionDirectory(
|
||||
origin,
|
||||
client,
|
||||
logger,
|
||||
limit=timedelta(seconds=settings.session_cache_ttl_seconds),
|
||||
cooldown=settings.cache_cooldown_seconds,
|
||||
)
|
||||
finally:
|
||||
await client.aclose()
|
||||
|
||||
@provide(scope=Scope.APP)
|
||||
def verify(self, users: UserDirectory, hasher: PasswordHasher) -> VerifyCredentials:
|
||||
return VerifyCredentials(users, hasher)
|
||||
|
||||
@provide(scope=Scope.APP)
|
||||
def authenticate(self, sessions: SessionDirectory, clock: Clock) -> Authenticate:
|
||||
return Authenticate(sessions, clock)
|
||||
|
||||
@provide(scope=Scope.REQUEST)
|
||||
def unit_of_work(self, database: Database) -> IdentityUnitOfWork:
|
||||
return PostgresIdentityUnitOfWork(database)
|
||||
|
||||
@provide(scope=Scope.REQUEST)
|
||||
def current_user(self, users: UserDirectory) -> GetCurrentUser:
|
||||
return GetCurrentUser(users)
|
||||
|
||||
@provide(scope=Scope.REQUEST)
|
||||
def command_bus(
|
||||
self,
|
||||
uow: IdentityUnitOfWork,
|
||||
publisher: EventPublisher,
|
||||
clock: Clock,
|
||||
ids: IdGenerator,
|
||||
hasher: PasswordHasher,
|
||||
sessions: SessionDirectory,
|
||||
settings: IdentitySettings,
|
||||
) -> IdentityCommandBus:
|
||||
ttl = timedelta(seconds=settings.session_ttl_seconds)
|
||||
handlers: dict[type[Command], CommandHandler] = {
|
||||
RegisterUser: RegisterUserHandler(uow, clock, ids, hasher),
|
||||
OpenSession: OpenSessionHandler(uow, clock, ttl),
|
||||
CloseSession: CloseSessionHandler(uow, sessions),
|
||||
}
|
||||
return IdentityCommandBus(handlers, uow, publisher)
|
||||
@@ -0,0 +1,20 @@
|
||||
from messenger.identity.domain.events import SessionClosed, SessionOpened, UserRegistered
|
||||
from messenger.shared.application.ports import Logger
|
||||
|
||||
LOG_FIELDS = frozenset({"actor_id"})
|
||||
|
||||
|
||||
class IdentityJournal:
|
||||
__slots__ = ("_logger",)
|
||||
|
||||
def __init__(self, logger: Logger) -> None:
|
||||
self._logger = logger
|
||||
|
||||
async def on_user_registered(self, event: UserRegistered) -> None:
|
||||
self._logger.info("audit.user_registered", actor_id=str(event.user_id))
|
||||
|
||||
async def on_session_opened(self, event: SessionOpened) -> None:
|
||||
self._logger.info("audit.session_opened", actor_id=str(event.user_id))
|
||||
|
||||
async def on_session_closed(self, event: SessionClosed) -> None:
|
||||
self._logger.info("audit.session_closed", actor_id=str(event.user_id))
|
||||
@@ -0,0 +1,42 @@
|
||||
from datetime import datetime
|
||||
from typing import Literal
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel, ConfigDict
|
||||
|
||||
from messenger.identity.domain.models import Session, User
|
||||
|
||||
|
||||
class CreateUserRequest(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid", strict=True)
|
||||
|
||||
username: str
|
||||
display_name: str
|
||||
password: str
|
||||
|
||||
|
||||
class UserOut(BaseModel):
|
||||
id: UUID
|
||||
username: str
|
||||
display_name: str
|
||||
created_at: datetime
|
||||
|
||||
@classmethod
|
||||
def of(cls, user: User) -> UserOut:
|
||||
return cls(
|
||||
id=user.id,
|
||||
username=user.username.value,
|
||||
display_name=user.display_name.value,
|
||||
created_at=user.created_at,
|
||||
)
|
||||
|
||||
|
||||
class SessionOut(BaseModel):
|
||||
token: str
|
||||
token_type: Literal["Bearer"] = "Bearer"
|
||||
expires_at: datetime
|
||||
user: UserOut
|
||||
|
||||
@classmethod
|
||||
def of(cls, token: str, session: Session, user: User) -> SessionOut:
|
||||
return cls(token=token, expires_at=session.expires_at, user=UserOut.of(user))
|
||||
@@ -0,0 +1,35 @@
|
||||
import asyncio
|
||||
import secrets
|
||||
|
||||
from argon2 import PasswordHasher, Type
|
||||
from argon2.exceptions import InvalidHashError, VerificationError
|
||||
|
||||
TIME_COST = 2
|
||||
MEMORY_COST_KIB = 19_456
|
||||
PARALLELISM = 1
|
||||
|
||||
|
||||
class Argon2PasswordHasher:
|
||||
__slots__ = ("_dummy", "_hasher", "_slots")
|
||||
|
||||
def __init__(self, concurrency: int) -> None:
|
||||
self._hasher = PasswordHasher(
|
||||
time_cost=TIME_COST,
|
||||
memory_cost=MEMORY_COST_KIB,
|
||||
parallelism=PARALLELISM,
|
||||
type=Type.ID,
|
||||
)
|
||||
self._slots = asyncio.Semaphore(concurrency)
|
||||
self._dummy = self._hasher.hash(secrets.token_urlsafe(16))
|
||||
|
||||
async def hash(self, password: str) -> str:
|
||||
async with self._slots:
|
||||
return await asyncio.to_thread(self._hasher.hash, password)
|
||||
|
||||
async def verify(self, password_hash: str | None, password: str) -> bool:
|
||||
async with self._slots:
|
||||
try:
|
||||
await asyncio.to_thread(self._hasher.verify, password_hash or self._dummy, password)
|
||||
except VerificationError, InvalidHashError:
|
||||
return False
|
||||
return password_hash is not None
|
||||
@@ -0,0 +1,124 @@
|
||||
from datetime import datetime
|
||||
from uuid import UUID
|
||||
|
||||
from asyncpg import Record
|
||||
|
||||
from messenger.identity.domain.errors import UsernameAlreadyTaken
|
||||
from messenger.identity.domain.models import Session, User
|
||||
from messenger.identity.domain.values import DisplayName, Username
|
||||
from messenger.shared.infrastructure.postgres import Connection, Database, PostgresUnitOfWork
|
||||
|
||||
INSERT_USER = """
|
||||
INSERT INTO users (id, username, display_name, password_hash, created_at)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
ON CONFLICT (username) DO NOTHING
|
||||
RETURNING true
|
||||
"""
|
||||
USER_BY_ID = """
|
||||
SELECT id, username, display_name, password_hash, created_at FROM users WHERE id = $1
|
||||
"""
|
||||
USER_BY_USERNAME = """
|
||||
SELECT id, username, display_name, password_hash, created_at FROM users WHERE username = $1
|
||||
"""
|
||||
INSERT_SESSION = """
|
||||
WITH expired AS (DELETE FROM sessions WHERE user_id = $2 AND expires_at <= $3)
|
||||
INSERT INTO sessions (token_hash, user_id, created_at, expires_at) VALUES ($1, $2, $3, $4)
|
||||
"""
|
||||
DELETE_SESSION = "DELETE FROM sessions WHERE token_hash = $1"
|
||||
ACTIVE_SESSION = """
|
||||
SELECT token_hash, user_id, created_at, expires_at
|
||||
FROM sessions
|
||||
WHERE token_hash = $1 AND expires_at > $2
|
||||
"""
|
||||
|
||||
|
||||
def user_of(row: Record | None) -> User | None:
|
||||
if row is None:
|
||||
return None
|
||||
return User(
|
||||
id=row["id"],
|
||||
username=Username(row["username"]),
|
||||
display_name=DisplayName(row["display_name"]),
|
||||
created_at=row["created_at"],
|
||||
password_hash=row["password_hash"],
|
||||
)
|
||||
|
||||
|
||||
class PostgresUserRepository:
|
||||
__slots__ = ("_connection",)
|
||||
|
||||
def __init__(self, connection: Connection) -> None:
|
||||
self._connection = connection
|
||||
|
||||
async def add(self, user: User) -> None:
|
||||
inserted = await self._connection.fetchval(
|
||||
INSERT_USER,
|
||||
user.id,
|
||||
user.username.value,
|
||||
user.display_name.value,
|
||||
user.password_hash,
|
||||
user.created_at,
|
||||
)
|
||||
if inserted is None:
|
||||
raise UsernameAlreadyTaken("username уже занят")
|
||||
|
||||
|
||||
class PostgresSessionRepository:
|
||||
__slots__ = ("_connection",)
|
||||
|
||||
def __init__(self, connection: Connection) -> None:
|
||||
self._connection = connection
|
||||
|
||||
async def add(self, session: Session) -> None:
|
||||
await self._connection.execute(
|
||||
INSERT_SESSION,
|
||||
session.token_hash,
|
||||
session.user_id,
|
||||
session.created_at,
|
||||
session.expires_at,
|
||||
)
|
||||
|
||||
async def remove(self, token_hash: bytes) -> None:
|
||||
await self._connection.execute(DELETE_SESSION, token_hash)
|
||||
|
||||
|
||||
class PostgresIdentityUnitOfWork(PostgresUnitOfWork):
|
||||
__slots__ = ()
|
||||
|
||||
@property
|
||||
def users(self) -> PostgresUserRepository:
|
||||
return PostgresUserRepository(self.connection)
|
||||
|
||||
@property
|
||||
def sessions(self) -> PostgresSessionRepository:
|
||||
return PostgresSessionRepository(self.connection)
|
||||
|
||||
|
||||
class PostgresUserDirectory:
|
||||
__slots__ = ("_database",)
|
||||
|
||||
def __init__(self, database: Database) -> None:
|
||||
self._database = database
|
||||
|
||||
async def find(self, user_id: UUID) -> User | None:
|
||||
async with self._database.connection() as connection:
|
||||
return user_of(await connection.fetchrow(USER_BY_ID, user_id))
|
||||
|
||||
async def find_by_username(self, username: str) -> User | None:
|
||||
async with self._database.connection() as connection:
|
||||
return user_of(await connection.fetchrow(USER_BY_USERNAME, username))
|
||||
|
||||
|
||||
class PostgresSessionDirectory:
|
||||
__slots__ = ("_database",)
|
||||
|
||||
def __init__(self, database: Database) -> None:
|
||||
self._database = database
|
||||
|
||||
async def find(self, token_hash: bytes, now: datetime) -> Session | None:
|
||||
async with self._database.connection() as connection:
|
||||
row = await connection.fetchrow(ACTIVE_SESSION, token_hash, now)
|
||||
return None if row is None else Session(**dict(row))
|
||||
|
||||
async def invalidate(self, session: Session) -> None:
|
||||
return None
|
||||
@@ -0,0 +1,102 @@
|
||||
import time
|
||||
from collections.abc import Awaitable
|
||||
from datetime import datetime, timedelta
|
||||
from uuid import UUID
|
||||
|
||||
from redis.asyncio import Redis
|
||||
from redis.exceptions import RedisError
|
||||
|
||||
from messenger.identity.application.ports import SessionDirectory
|
||||
from messenger.identity.domain.models import Session
|
||||
from messenger.shared.application.errors import Unavailable
|
||||
from messenger.shared.application.ports import Logger
|
||||
|
||||
REVOKED = "-"
|
||||
FAILURES = (RedisError, OSError)
|
||||
|
||||
|
||||
def key_of(token_hash: bytes) -> str:
|
||||
return f"session:{token_hash.hex()}"
|
||||
|
||||
|
||||
def milliseconds(moment: datetime) -> int:
|
||||
return int(moment.timestamp() * 1000)
|
||||
|
||||
|
||||
def encode(session: Session) -> str:
|
||||
return f"{session.user_id} {session.created_at.isoformat()} {session.expires_at.isoformat()}"
|
||||
|
||||
|
||||
def decode(token_hash: bytes, value: str) -> Session:
|
||||
user_id, created_at, expires_at = value.split(" ")
|
||||
return Session(
|
||||
token_hash=token_hash,
|
||||
user_id=UUID(user_id),
|
||||
created_at=datetime.fromisoformat(created_at),
|
||||
expires_at=datetime.fromisoformat(expires_at),
|
||||
)
|
||||
|
||||
|
||||
class CachedSessionDirectory:
|
||||
__slots__ = ("_client", "_cooldown", "_limit", "_logger", "_origin", "_resume_at")
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
origin: SessionDirectory,
|
||||
client: Redis,
|
||||
logger: Logger,
|
||||
*,
|
||||
limit: timedelta,
|
||||
cooldown: float,
|
||||
) -> None:
|
||||
self._origin = origin
|
||||
self._client = client
|
||||
self._logger = logger
|
||||
self._limit = limit
|
||||
self._cooldown = cooldown
|
||||
self._resume_at = 0.0
|
||||
|
||||
async def find(self, token_hash: bytes, now: datetime) -> Session | None:
|
||||
key = key_of(token_hash)
|
||||
cached = await self._read(key)
|
||||
if cached == REVOKED:
|
||||
return None
|
||||
if cached is not None:
|
||||
session = decode(token_hash, cached)
|
||||
return session if session.expires_at > now else None
|
||||
session = await self._origin.find(token_hash, now)
|
||||
if session is not None and self._closed():
|
||||
until = milliseconds(min(session.expires_at, now + self._limit))
|
||||
await self._quietly(self._client.set(key, encode(session), pxat=until, nx=True))
|
||||
return session
|
||||
|
||||
async def invalidate(self, session: Session) -> None:
|
||||
if not self._closed():
|
||||
raise Unavailable("кэш сессий недоступен")
|
||||
try:
|
||||
await self._client.set(
|
||||
key_of(session.token_hash), REVOKED, pxat=milliseconds(session.expires_at)
|
||||
)
|
||||
except FAILURES as error:
|
||||
self._trip(error)
|
||||
raise Unavailable("кэш сессий недоступен") from error
|
||||
|
||||
async def _read(self, key: str) -> str | None:
|
||||
if not self._closed():
|
||||
return None
|
||||
value = await self._quietly(self._client.get(key))
|
||||
return value.decode() if isinstance(value, bytes) else value
|
||||
|
||||
async def _quietly[ResultT](self, call: Awaitable[ResultT]) -> ResultT | None:
|
||||
try:
|
||||
return await call
|
||||
except FAILURES as error:
|
||||
self._trip(error)
|
||||
return None
|
||||
|
||||
def _closed(self) -> bool:
|
||||
return time.monotonic() >= self._resume_at
|
||||
|
||||
def _trip(self, error: Exception) -> None:
|
||||
self._resume_at = time.monotonic() + self._cooldown
|
||||
self._logger.warning("cache.bypassed", error=type(error).__name__, limit=self._cooldown)
|
||||
@@ -0,0 +1,104 @@
|
||||
from collections.abc import AsyncIterator
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from dishka import make_async_container
|
||||
from dishka.integrations.fastapi import FastapiProvider, setup_dishka
|
||||
from fastapi import FastAPI
|
||||
|
||||
from messenger.identity.config import IdentitySettings
|
||||
from messenger.identity.domain.events import SessionClosed, SessionOpened, UserRegistered
|
||||
from messenger.identity.entrypoints.authentication import challenge_for, guard
|
||||
from messenger.identity.entrypoints.errors import ERROR_CODES as IDENTITY_ERRORS
|
||||
from messenger.identity.entrypoints.http import router as users_router
|
||||
from messenger.identity.entrypoints.http import sessions_router
|
||||
from messenger.identity.entrypoints.ioc import IdentityProvider
|
||||
from messenger.identity.entrypoints.journal import LOG_FIELDS as IDENTITY_FIELDS
|
||||
from messenger.identity.entrypoints.journal import IdentityJournal
|
||||
from messenger.messaging.config import MessagingSettings
|
||||
from messenger.messaging.domain.events import ChatCreated, MessagePosted
|
||||
from messenger.messaging.entrypoints.errors import ERROR_CODES as MESSAGING_ERRORS
|
||||
from messenger.messaging.entrypoints.http import chats_router, messages_router
|
||||
from messenger.messaging.entrypoints.ioc import MessagingProvider
|
||||
from messenger.messaging.entrypoints.journal import LOG_FIELDS as MESSAGING_FIELDS
|
||||
from messenger.messaging.entrypoints.journal import MessagingJournal
|
||||
from messenger.shared.application.messagebus import EventBus
|
||||
from messenger.shared.application.ports import Logger
|
||||
from messenger.shared.config import PlatformSettings
|
||||
from messenger.shared.entrypoints import errors, health
|
||||
from messenger.shared.entrypoints.config import TransportSettings
|
||||
from messenger.shared.entrypoints.ioc import PlatformProvider
|
||||
from messenger.shared.entrypoints.pipeline import RequestPipeline
|
||||
from messenger.shared.infrastructure.logging import PLATFORM_FIELDS, StructlogLogger
|
||||
|
||||
TRANSPORT_FIELDS = frozenset({"dropped_total", "queue", "instance"})
|
||||
|
||||
LOG_FIELDS = PLATFORM_FIELDS | TRANSPORT_FIELDS | IDENTITY_FIELDS | MESSAGING_FIELDS
|
||||
ERROR_CODES = {**IDENTITY_ERRORS, **MESSAGING_ERRORS}
|
||||
|
||||
|
||||
def wire_events(bus: EventBus, logger: Logger) -> None:
|
||||
identity = IdentityJournal(logger)
|
||||
bus.subscribe(UserRegistered, identity.on_user_registered)
|
||||
bus.subscribe(SessionOpened, identity.on_session_opened)
|
||||
bus.subscribe(SessionClosed, identity.on_session_closed)
|
||||
|
||||
messaging = MessagingJournal(logger)
|
||||
bus.subscribe(ChatCreated, messaging.on_chat_created)
|
||||
bus.subscribe(MessagePosted, messaging.on_message_posted)
|
||||
|
||||
|
||||
def create_app(
|
||||
platform: PlatformSettings | None = None,
|
||||
transport: TransportSettings | None = None,
|
||||
identity: IdentitySettings | None = None,
|
||||
messaging: MessagingSettings | None = None,
|
||||
) -> FastAPI:
|
||||
platform = platform or PlatformSettings()
|
||||
transport = transport or TransportSettings()
|
||||
identity = identity or IdentitySettings()
|
||||
messaging = messaging or MessagingSettings()
|
||||
logger = StructlogLogger(level=platform.log_level, allowed_fields=LOG_FIELDS)
|
||||
container = make_async_container(
|
||||
PlatformProvider(platform, logger),
|
||||
IdentityProvider(identity),
|
||||
MessagingProvider(messaging),
|
||||
FastapiProvider(),
|
||||
)
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(_app: FastAPI) -> AsyncIterator[None]:
|
||||
bus = await container.get(EventBus)
|
||||
wire_events(bus, logger)
|
||||
bus.start()
|
||||
logger.info("service.started")
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
await bus.stop()
|
||||
await container.close()
|
||||
logger.info("service.stopped")
|
||||
|
||||
app = FastAPI(
|
||||
title="MTUSI Messenger",
|
||||
version="1.4.0",
|
||||
lifespan=lifespan,
|
||||
docs_url="/docs" if transport.docs else None,
|
||||
redoc_url=None,
|
||||
openapi_url="/openapi.json" if transport.docs else None,
|
||||
)
|
||||
app.add_middleware(
|
||||
RequestPipeline,
|
||||
logger=logger,
|
||||
max_bytes=transport.max_body_bytes,
|
||||
timeout=transport.request_timeout_seconds,
|
||||
guard=guard,
|
||||
instance=platform.instance_id,
|
||||
)
|
||||
errors.install(app, logger, ERROR_CODES, challenge_for, platform.instance_id)
|
||||
app.include_router(health.router)
|
||||
app.include_router(users_router)
|
||||
app.include_router(sessions_router)
|
||||
app.include_router(chats_router)
|
||||
app.include_router(messages_router)
|
||||
setup_dishka(container, app)
|
||||
return app
|
||||
@@ -0,0 +1,5 @@
|
||||
from messenger.shared.application.messagebus import CommandBus
|
||||
|
||||
|
||||
class MessagingCommandBus(CommandBus):
|
||||
__slots__ = ()
|
||||
@@ -0,0 +1,21 @@
|
||||
from collections.abc import Sequence
|
||||
from dataclasses import dataclass
|
||||
from uuid import UUID
|
||||
|
||||
from messenger.shared.application.commands import Command
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class CreateChat(Command):
|
||||
creator_id: UUID
|
||||
title: str
|
||||
member_ids: Sequence[UUID]
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class PostMessage(Command):
|
||||
sender_id: UUID
|
||||
chat_id: UUID
|
||||
text: str
|
||||
key: str
|
||||
fingerprint: bytes
|
||||
@@ -0,0 +1,62 @@
|
||||
from datetime import timedelta
|
||||
|
||||
from messenger.messaging.application.commands import CreateChat, PostMessage
|
||||
from messenger.messaging.application.ports import MessagingUnitOfWork
|
||||
from messenger.messaging.domain.errors import ChatNotFound
|
||||
from messenger.messaging.domain.models import Chat, Message, Receipt
|
||||
from messenger.shared.application.ports import Clock, IdGenerator
|
||||
|
||||
|
||||
class CreateChatHandler:
|
||||
__slots__ = ("_clock", "_ids", "_uow")
|
||||
|
||||
def __init__(self, uow: MessagingUnitOfWork, clock: Clock, ids: IdGenerator) -> None:
|
||||
self._uow = uow
|
||||
self._clock = clock
|
||||
self._ids = ids
|
||||
|
||||
async def __call__(self, command: CreateChat) -> Chat:
|
||||
chat = Chat.create(
|
||||
chat_id=self._ids.new_id(),
|
||||
title=command.title,
|
||||
member_ids=command.member_ids,
|
||||
creator_id=command.creator_id,
|
||||
created_at=self._clock.now(),
|
||||
)
|
||||
async with self._uow.transaction():
|
||||
await self._uow.chats.add(chat)
|
||||
await self._uow.commit()
|
||||
return chat
|
||||
|
||||
|
||||
class PostMessageHandler:
|
||||
__slots__ = ("_clock", "_ids", "_ttl", "_uow")
|
||||
|
||||
def __init__(
|
||||
self, uow: MessagingUnitOfWork, clock: Clock, ids: IdGenerator, ttl: timedelta
|
||||
) -> None:
|
||||
self._uow = uow
|
||||
self._clock = clock
|
||||
self._ids = ids
|
||||
self._ttl = ttl
|
||||
|
||||
async def __call__(self, command: PostMessage) -> Message:
|
||||
async with self._uow.transaction():
|
||||
chat = await self._uow.chats.get(command.chat_id)
|
||||
if chat is None:
|
||||
raise ChatNotFound("чат не найден")
|
||||
now = self._clock.now()
|
||||
message = chat.post(
|
||||
message_id=self._ids.new_id(),
|
||||
sender_id=command.sender_id,
|
||||
text=command.text,
|
||||
now=now,
|
||||
)
|
||||
known = await self._uow.chats.receipt(
|
||||
chat.id, command.sender_id, command.key, now - self._ttl
|
||||
)
|
||||
if known is not None:
|
||||
return known.replay(command.fingerprint)
|
||||
await self._uow.chats.append(Receipt(command.key, command.fingerprint, message))
|
||||
await self._uow.commit()
|
||||
return message
|
||||
@@ -0,0 +1,46 @@
|
||||
from datetime import datetime
|
||||
from typing import Protocol, runtime_checkable
|
||||
from uuid import UUID
|
||||
|
||||
from messenger.messaging.domain.keys import HistoryKey
|
||||
from messenger.messaging.domain.models import Chat, Message, Receipt
|
||||
from messenger.shared.application.ports import UnitOfWork
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class CursorCodec(Protocol):
|
||||
def encode(self, scope: str, key: HistoryKey) -> str: ...
|
||||
|
||||
def decode(self, scope: str, raw: str) -> HistoryKey: ...
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class ChatRepository(Protocol):
|
||||
async def add(self, chat: Chat) -> None: ...
|
||||
|
||||
async def get(self, chat_id: UUID) -> Chat | None: ...
|
||||
|
||||
async def receipt(
|
||||
self, chat_id: UUID, sender_id: UUID, key: str, since: datetime
|
||||
) -> Receipt | None: ...
|
||||
|
||||
async def append(self, receipt: Receipt) -> None: ...
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class MessagingUnitOfWork(UnitOfWork, Protocol):
|
||||
@property
|
||||
def chats(self) -> ChatRepository: ...
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class ChatDirectory(Protocol):
|
||||
async def chat_for_member(self, chat_id: UUID, user_id: UUID) -> Chat | None: ...
|
||||
|
||||
async def chats_of(
|
||||
self, user_id: UUID, *, after: HistoryKey | None, limit: int
|
||||
) -> tuple[list[Chat], bool]: ...
|
||||
|
||||
async def history(
|
||||
self, chat_id: UUID, *, after: HistoryKey | None, limit: int
|
||||
) -> tuple[list[Message], bool]: ...
|
||||
@@ -0,0 +1,73 @@
|
||||
from dataclasses import dataclass
|
||||
from uuid import UUID
|
||||
|
||||
from messenger.messaging.application.ports import ChatDirectory, CursorCodec
|
||||
from messenger.messaging.domain.errors import ChatNotFound
|
||||
from messenger.messaging.domain.keys import HistoryKey
|
||||
from messenger.messaging.domain.models import Chat, Message
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class Page[ItemT]:
|
||||
items: list[ItemT]
|
||||
next_cursor: str | None
|
||||
|
||||
|
||||
def chats_scope(user_id: UUID) -> str:
|
||||
return f"chats:{user_id}"
|
||||
|
||||
|
||||
def messages_scope(chat_id: UUID) -> str:
|
||||
return f"messages:{chat_id}"
|
||||
|
||||
|
||||
class GetChat:
|
||||
__slots__ = ("_chats",)
|
||||
|
||||
def __init__(self, chats: ChatDirectory) -> None:
|
||||
self._chats = chats
|
||||
|
||||
async def __call__(self, chat_id: UUID, user_id: UUID) -> Chat:
|
||||
chat = await self._chats.chat_for_member(chat_id, user_id)
|
||||
if chat is None:
|
||||
raise ChatNotFound("чат не найден")
|
||||
return chat
|
||||
|
||||
|
||||
class ListChats:
|
||||
__slots__ = ("_chats", "_cursors")
|
||||
|
||||
def __init__(self, chats: ChatDirectory, cursors: CursorCodec) -> None:
|
||||
self._chats = chats
|
||||
self._cursors = cursors
|
||||
|
||||
async def __call__(self, user_id: UUID, *, limit: int, cursor: str | None) -> Page[Chat]:
|
||||
scope = chats_scope(user_id)
|
||||
after: HistoryKey | None = self._cursors.decode(scope, cursor) if cursor else None
|
||||
items, has_more = await self._chats.chats_of(user_id, after=after, limit=limit)
|
||||
next_cursor = self._cursors.encode(scope, items[-1].key) if has_more and items else None
|
||||
return Page(items=items, next_cursor=next_cursor)
|
||||
|
||||
|
||||
class ReadHistory:
|
||||
__slots__ = ("_chats", "_cursors")
|
||||
|
||||
def __init__(self, chats: ChatDirectory, cursors: CursorCodec) -> None:
|
||||
self._chats = chats
|
||||
self._cursors = cursors
|
||||
|
||||
async def __call__(
|
||||
self,
|
||||
chat_id: UUID,
|
||||
user_id: UUID,
|
||||
*,
|
||||
limit: int,
|
||||
cursor: str | None,
|
||||
) -> Page[Message]:
|
||||
if await self._chats.chat_for_member(chat_id, user_id) is None:
|
||||
raise ChatNotFound("чат не найден")
|
||||
scope = messages_scope(chat_id)
|
||||
after: HistoryKey | None = self._cursors.decode(scope, cursor) if cursor else None
|
||||
items, has_more = await self._chats.history(chat_id, after=after, limit=limit)
|
||||
next_cursor = self._cursors.encode(scope, items[-1].key) if has_more and items else None
|
||||
return Page(items=items, next_cursor=next_cursor)
|
||||
@@ -0,0 +1,8 @@
|
||||
from pydantic import Field, SecretStr
|
||||
|
||||
from messenger.shared.config import Settings
|
||||
|
||||
|
||||
class MessagingSettings(Settings):
|
||||
cursor_secret: SecretStr = Field(min_length=16)
|
||||
idempotency_ttl_seconds: int = Field(default=86_400, ge=1)
|
||||
@@ -0,0 +1,25 @@
|
||||
from messenger.shared.domain.errors import DomainError
|
||||
|
||||
|
||||
class MessagingError(DomainError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidChatData(MessagingError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidMessageText(MessagingError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidPageRequest(MessagingError):
|
||||
pass
|
||||
|
||||
|
||||
class ChatNotFound(MessagingError):
|
||||
pass
|
||||
|
||||
|
||||
class IdempotencyConflict(MessagingError):
|
||||
pass
|
||||
@@ -0,0 +1,24 @@
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime
|
||||
from uuid import UUID
|
||||
|
||||
from messenger.shared.domain.events import DomainEvent
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class ChatCreated(DomainEvent):
|
||||
chat_id: UUID
|
||||
creator_id: UUID
|
||||
member_count: int
|
||||
created_at: datetime
|
||||
name: str = field(init=False, default="chat.created")
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class MessagePosted(DomainEvent):
|
||||
message_id: UUID
|
||||
chat_id: UUID
|
||||
sender_id: UUID
|
||||
text_length: int
|
||||
created_at: datetime
|
||||
name: str = field(init=False, default="message.posted")
|
||||
@@ -0,0 +1,3 @@
|
||||
from datetime import datetime
|
||||
|
||||
type HistoryKey = tuple[datetime, int]
|
||||
@@ -0,0 +1,142 @@
|
||||
from collections.abc import Sequence
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime, timedelta
|
||||
from uuid import UUID
|
||||
|
||||
from messenger.messaging.domain.errors import ChatNotFound, IdempotencyConflict, InvalidChatData
|
||||
from messenger.messaging.domain.events import ChatCreated, MessagePosted
|
||||
from messenger.messaging.domain.keys import HistoryKey
|
||||
from messenger.messaging.domain.values import CHAT_MEMBERS_MAX, ChatTitle, MessageText
|
||||
from messenger.shared.domain.events import DomainEvent
|
||||
|
||||
TICK = timedelta(microseconds=1)
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class Message:
|
||||
id: UUID
|
||||
chat_id: UUID
|
||||
sender_id: UUID
|
||||
text: MessageText
|
||||
created_at: datetime
|
||||
|
||||
@property
|
||||
def key(self) -> HistoryKey:
|
||||
return (self.created_at, self.id.int)
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class Receipt:
|
||||
key: str
|
||||
fingerprint: bytes
|
||||
message: Message
|
||||
|
||||
def replay(self, fingerprint: bytes) -> Message:
|
||||
if fingerprint != self.fingerprint:
|
||||
raise IdempotencyConflict("ключ идемпотентности уже использован с другим телом")
|
||||
return self.message
|
||||
|
||||
|
||||
class Chat:
|
||||
__slots__ = (
|
||||
"_events",
|
||||
"_members",
|
||||
"created_at",
|
||||
"id",
|
||||
"last_message_at",
|
||||
"member_ids",
|
||||
"title",
|
||||
)
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
chat_id: UUID,
|
||||
title: ChatTitle,
|
||||
member_ids: Sequence[UUID],
|
||||
created_at: datetime,
|
||||
last_message_at: datetime | None = None,
|
||||
) -> None:
|
||||
self.id = chat_id
|
||||
self.title = title
|
||||
self.member_ids: tuple[UUID, ...] = tuple(member_ids)
|
||||
self.created_at = created_at
|
||||
self.last_message_at = last_message_at
|
||||
self._members: frozenset[UUID] = frozenset(member_ids)
|
||||
self._events: list[DomainEvent] = []
|
||||
|
||||
@classmethod
|
||||
def create(
|
||||
cls,
|
||||
*,
|
||||
chat_id: UUID,
|
||||
title: str,
|
||||
member_ids: Sequence[UUID],
|
||||
creator_id: UUID,
|
||||
created_at: datetime,
|
||||
) -> Chat:
|
||||
if not member_ids:
|
||||
raise InvalidChatData("поле member_ids не может быть пустым")
|
||||
if len(member_ids) > CHAT_MEMBERS_MAX:
|
||||
raise InvalidChatData(f"поле member_ids содержит больше {CHAT_MEMBERS_MAX} участников")
|
||||
unique = set(member_ids)
|
||||
if len(unique) != len(member_ids):
|
||||
raise InvalidChatData("поле member_ids содержит дубликаты")
|
||||
if creator_id not in unique:
|
||||
raise InvalidChatData("создатель чата обязан входить в список member_ids")
|
||||
chat = cls(
|
||||
chat_id=chat_id,
|
||||
title=ChatTitle(title),
|
||||
member_ids=member_ids,
|
||||
created_at=created_at,
|
||||
)
|
||||
chat.record(
|
||||
ChatCreated(
|
||||
chat_id=chat.id,
|
||||
creator_id=creator_id,
|
||||
member_count=len(unique),
|
||||
created_at=created_at,
|
||||
)
|
||||
)
|
||||
return chat
|
||||
|
||||
@property
|
||||
def key(self) -> HistoryKey:
|
||||
return (self.created_at, self.id.int)
|
||||
|
||||
def record(self, event: DomainEvent) -> None:
|
||||
self._events.append(event)
|
||||
|
||||
def is_member(self, user_id: UUID) -> bool:
|
||||
return user_id in self._members
|
||||
|
||||
def require_member(self, user_id: UUID) -> None:
|
||||
if not self.is_member(user_id):
|
||||
raise ChatNotFound("чат не найден")
|
||||
|
||||
def post(self, *, message_id: UUID, sender_id: UUID, text: str, now: datetime) -> Message:
|
||||
self.require_member(sender_id)
|
||||
last = self.last_message_at
|
||||
created_at = now if last is None or now > last else last + TICK
|
||||
message = Message(
|
||||
id=message_id,
|
||||
chat_id=self.id,
|
||||
sender_id=sender_id,
|
||||
text=MessageText(text),
|
||||
created_at=created_at,
|
||||
)
|
||||
self.last_message_at = created_at
|
||||
self.record(
|
||||
MessagePosted(
|
||||
message_id=message.id,
|
||||
chat_id=self.id,
|
||||
sender_id=sender_id,
|
||||
text_length=len(message.text.value),
|
||||
created_at=created_at,
|
||||
)
|
||||
)
|
||||
return message
|
||||
|
||||
def collect_events(self) -> list[DomainEvent]:
|
||||
events, self._events = self._events, []
|
||||
return events
|
||||
@@ -0,0 +1,39 @@
|
||||
from dataclasses import dataclass
|
||||
|
||||
from messenger.messaging.domain.errors import InvalidChatData, InvalidMessageText
|
||||
|
||||
CHAT_TITLE_MAX = 100
|
||||
MESSAGE_TEXT_MAX = 4000
|
||||
CHAT_MEMBERS_MAX = 100
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class ChatTitle:
|
||||
value: str
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if not isinstance(self.value, str):
|
||||
raise InvalidChatData("поле title должно быть строкой")
|
||||
if not self.value.strip():
|
||||
raise InvalidChatData("поле title не может быть пустым или состоять из пробелов")
|
||||
if len(self.value) > CHAT_TITLE_MAX:
|
||||
raise InvalidChatData(f"поле title длиннее {CHAT_TITLE_MAX} символов Unicode")
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.value
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class MessageText:
|
||||
value: str
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if not isinstance(self.value, str):
|
||||
raise InvalidMessageText("поле text должно быть строкой")
|
||||
if not self.value.strip():
|
||||
raise InvalidMessageText("поле text не может быть пустым или состоять из пробелов")
|
||||
if len(self.value) > MESSAGE_TEXT_MAX:
|
||||
raise InvalidMessageText(f"поле text длиннее {MESSAGE_TEXT_MAX} символов Unicode")
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.value
|
||||
@@ -0,0 +1,16 @@
|
||||
from messenger.messaging.domain.errors import (
|
||||
ChatNotFound,
|
||||
IdempotencyConflict,
|
||||
InvalidChatData,
|
||||
InvalidMessageText,
|
||||
InvalidPageRequest,
|
||||
)
|
||||
from messenger.shared.domain.errors import DomainError
|
||||
|
||||
ERROR_CODES: dict[type[DomainError], tuple[str, int]] = {
|
||||
InvalidChatData: ("invalid_request", 400),
|
||||
InvalidMessageText: ("invalid_request", 400),
|
||||
InvalidPageRequest: ("invalid_request", 400),
|
||||
ChatNotFound: ("not_found", 404),
|
||||
IdempotencyConflict: ("conflict", 409),
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
from typing import Annotated
|
||||
from uuid import UUID
|
||||
|
||||
from dishka.integrations.fastapi import FromDishka, inject
|
||||
from fastapi import APIRouter, Query, status
|
||||
|
||||
from messenger.messaging.application.bus import MessagingCommandBus
|
||||
from messenger.messaging.application.commands import CreateChat, PostMessage
|
||||
from messenger.messaging.application.queries import GetChat, ListChats, ReadHistory
|
||||
from messenger.messaging.entrypoints.schemas import (
|
||||
ChatOut,
|
||||
CreateChatRequest,
|
||||
CreateMessageRequest,
|
||||
IdempotencyKey,
|
||||
MessageOut,
|
||||
PageOut,
|
||||
PageQuery,
|
||||
)
|
||||
from messenger.shared.entrypoints.actor import Actor
|
||||
|
||||
chats_router = APIRouter(prefix="/api/v1/chats", tags=["chats"])
|
||||
messages_router = APIRouter(prefix="/api/v1/chats/{chat_id}/messages", tags=["messages"])
|
||||
|
||||
|
||||
@chats_router.post("", status_code=status.HTTP_201_CREATED)
|
||||
@inject
|
||||
async def create_chat(
|
||||
actor: Actor,
|
||||
body: CreateChatRequest,
|
||||
bus: FromDishka[MessagingCommandBus],
|
||||
) -> ChatOut:
|
||||
chat = await bus.execute(
|
||||
CreateChat(creator_id=actor, title=body.title, member_ids=body.member_ids)
|
||||
)
|
||||
return ChatOut.of(chat)
|
||||
|
||||
|
||||
@chats_router.get("")
|
||||
@inject
|
||||
async def list_chats(
|
||||
actor: Actor,
|
||||
page: Annotated[PageQuery, Query()],
|
||||
query: FromDishka[ListChats],
|
||||
) -> PageOut[ChatOut]:
|
||||
chats = await query(actor, limit=page.limit, cursor=page.cursor)
|
||||
return PageOut[ChatOut].of(chats, ChatOut.of)
|
||||
|
||||
|
||||
@chats_router.get("/{chat_id}")
|
||||
@inject
|
||||
async def read_chat(
|
||||
chat_id: UUID,
|
||||
actor: Actor,
|
||||
query: FromDishka[GetChat],
|
||||
) -> ChatOut:
|
||||
return ChatOut.of(await query(chat_id, actor))
|
||||
|
||||
|
||||
@messages_router.post("", status_code=status.HTTP_201_CREATED)
|
||||
@inject
|
||||
async def post_message(
|
||||
chat_id: UUID,
|
||||
actor: Actor,
|
||||
body: CreateMessageRequest,
|
||||
key: IdempotencyKey,
|
||||
bus: FromDishka[MessagingCommandBus],
|
||||
) -> MessageOut:
|
||||
message = await bus.execute(
|
||||
PostMessage(
|
||||
sender_id=actor,
|
||||
chat_id=chat_id,
|
||||
text=body.text,
|
||||
key=key,
|
||||
fingerprint=body.fingerprint(),
|
||||
)
|
||||
)
|
||||
return MessageOut.of(message)
|
||||
|
||||
|
||||
@messages_router.get("")
|
||||
@inject
|
||||
async def read_history(
|
||||
chat_id: UUID,
|
||||
actor: Actor,
|
||||
page: Annotated[PageQuery, Query()],
|
||||
query: FromDishka[ReadHistory],
|
||||
) -> PageOut[MessageOut]:
|
||||
messages = await query(chat_id, actor, limit=page.limit, cursor=page.cursor)
|
||||
return PageOut[MessageOut].of(messages, MessageOut.of)
|
||||
@@ -0,0 +1,69 @@
|
||||
from datetime import timedelta
|
||||
|
||||
from dishka import Provider, Scope, provide
|
||||
|
||||
from messenger.messaging.application.bus import MessagingCommandBus
|
||||
from messenger.messaging.application.commands import CreateChat, PostMessage
|
||||
from messenger.messaging.application.handlers import CreateChatHandler, PostMessageHandler
|
||||
from messenger.messaging.application.ports import ChatDirectory, CursorCodec, MessagingUnitOfWork
|
||||
from messenger.messaging.application.queries import GetChat, ListChats, ReadHistory
|
||||
from messenger.messaging.config import MessagingSettings
|
||||
from messenger.messaging.infrastructure.cursor import SignedCursorCodec
|
||||
from messenger.messaging.infrastructure.postgres import (
|
||||
PostgresChatDirectory,
|
||||
PostgresMessagingUnitOfWork,
|
||||
)
|
||||
from messenger.shared.application.commands import Command
|
||||
from messenger.shared.application.messagebus import CommandHandler
|
||||
from messenger.shared.application.ports import Clock, EventPublisher, IdGenerator
|
||||
from messenger.shared.infrastructure.postgres import Database
|
||||
|
||||
|
||||
class MessagingProvider(Provider):
|
||||
def __init__(self, settings: MessagingSettings) -> None:
|
||||
super().__init__()
|
||||
self._settings = settings
|
||||
|
||||
@provide(scope=Scope.APP)
|
||||
def settings(self) -> MessagingSettings:
|
||||
return self._settings
|
||||
|
||||
@provide(scope=Scope.APP)
|
||||
def directory(self, database: Database) -> ChatDirectory:
|
||||
return PostgresChatDirectory(database)
|
||||
|
||||
@provide(scope=Scope.APP)
|
||||
def cursors(self, settings: MessagingSettings) -> CursorCodec:
|
||||
return SignedCursorCodec(settings.cursor_secret.get_secret_value().encode())
|
||||
|
||||
@provide(scope=Scope.REQUEST)
|
||||
def unit_of_work(self, database: Database) -> MessagingUnitOfWork:
|
||||
return PostgresMessagingUnitOfWork(database)
|
||||
|
||||
@provide(scope=Scope.REQUEST)
|
||||
def chat(self, directory: ChatDirectory) -> GetChat:
|
||||
return GetChat(directory)
|
||||
|
||||
@provide(scope=Scope.REQUEST)
|
||||
def chat_list(self, directory: ChatDirectory, cursors: CursorCodec) -> ListChats:
|
||||
return ListChats(directory, cursors)
|
||||
|
||||
@provide(scope=Scope.REQUEST)
|
||||
def history(self, directory: ChatDirectory, cursors: CursorCodec) -> ReadHistory:
|
||||
return ReadHistory(directory, cursors)
|
||||
|
||||
@provide(scope=Scope.REQUEST)
|
||||
def command_bus(
|
||||
self,
|
||||
uow: MessagingUnitOfWork,
|
||||
publisher: EventPublisher,
|
||||
clock: Clock,
|
||||
ids: IdGenerator,
|
||||
settings: MessagingSettings,
|
||||
) -> MessagingCommandBus:
|
||||
ttl = timedelta(seconds=settings.idempotency_ttl_seconds)
|
||||
handlers: dict[type[Command], CommandHandler] = {
|
||||
CreateChat: CreateChatHandler(uow, clock, ids),
|
||||
PostMessage: PostMessageHandler(uow, clock, ids, ttl),
|
||||
}
|
||||
return MessagingCommandBus(handlers, uow, publisher)
|
||||
@@ -0,0 +1,27 @@
|
||||
from messenger.messaging.domain.events import ChatCreated, MessagePosted
|
||||
from messenger.shared.application.ports import Logger
|
||||
|
||||
LOG_FIELDS = frozenset({"chat_id", "message_id", "count"})
|
||||
|
||||
|
||||
class MessagingJournal:
|
||||
__slots__ = ("_logger",)
|
||||
|
||||
def __init__(self, logger: Logger) -> None:
|
||||
self._logger = logger
|
||||
|
||||
async def on_chat_created(self, event: ChatCreated) -> None:
|
||||
self._logger.info(
|
||||
"audit.chat_created",
|
||||
chat_id=str(event.chat_id),
|
||||
actor_id=str(event.creator_id),
|
||||
count=event.member_count,
|
||||
)
|
||||
|
||||
async def on_message_posted(self, event: MessagePosted) -> None:
|
||||
self._logger.info(
|
||||
"audit.message_posted",
|
||||
chat_id=str(event.chat_id),
|
||||
actor_id=str(event.sender_id),
|
||||
message_id=str(event.message_id),
|
||||
)
|
||||
@@ -0,0 +1,82 @@
|
||||
from collections.abc import Callable
|
||||
from datetime import datetime
|
||||
from hashlib import sha256
|
||||
from typing import Annotated, Self
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import Header
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
from messenger.messaging.application.queries import Page
|
||||
from messenger.messaging.domain.models import Chat, Message
|
||||
|
||||
STRICT = ConfigDict(extra="forbid", strict=True)
|
||||
|
||||
type IdempotencyKey = Annotated[
|
||||
str,
|
||||
Header(alias="Idempotency-Key", min_length=1, max_length=128, pattern=r"^[A-Za-z0-9._:-]+$"),
|
||||
]
|
||||
|
||||
|
||||
class CreateChatRequest(BaseModel):
|
||||
model_config = STRICT
|
||||
|
||||
title: str
|
||||
member_ids: list[Annotated[UUID, Field(strict=False)]]
|
||||
|
||||
|
||||
class CreateMessageRequest(BaseModel):
|
||||
model_config = STRICT
|
||||
|
||||
text: str
|
||||
|
||||
def fingerprint(self) -> bytes:
|
||||
return sha256(self.model_dump_json().encode()).digest()
|
||||
|
||||
|
||||
class PageQuery(BaseModel):
|
||||
limit: int = Field(default=50, ge=1, le=100)
|
||||
cursor: str | None = None
|
||||
|
||||
|
||||
class ChatOut(BaseModel):
|
||||
id: UUID
|
||||
title: str
|
||||
member_ids: tuple[UUID, ...]
|
||||
created_at: datetime
|
||||
|
||||
@classmethod
|
||||
def of(cls, chat: Chat) -> ChatOut:
|
||||
return cls(
|
||||
id=chat.id,
|
||||
title=chat.title.value,
|
||||
member_ids=chat.member_ids,
|
||||
created_at=chat.created_at,
|
||||
)
|
||||
|
||||
|
||||
class MessageOut(BaseModel):
|
||||
id: UUID
|
||||
chat_id: UUID
|
||||
sender_id: UUID
|
||||
text: str
|
||||
created_at: datetime
|
||||
|
||||
@classmethod
|
||||
def of(cls, message: Message) -> MessageOut:
|
||||
return cls(
|
||||
id=message.id,
|
||||
chat_id=message.chat_id,
|
||||
sender_id=message.sender_id,
|
||||
text=message.text.value,
|
||||
created_at=message.created_at,
|
||||
)
|
||||
|
||||
|
||||
class PageOut[ItemT](BaseModel):
|
||||
items: list[ItemT]
|
||||
next_cursor: str | None
|
||||
|
||||
@classmethod
|
||||
def of[SourceT](cls, page: Page[SourceT], view: Callable[[SourceT], ItemT]) -> Self:
|
||||
return cls(items=[view(item) for item in page.items], next_cursor=page.next_cursor)
|
||||
@@ -0,0 +1,59 @@
|
||||
import hmac
|
||||
from base64 import urlsafe_b64decode, urlsafe_b64encode
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from hashlib import sha256
|
||||
|
||||
from messenger.messaging.domain.errors import InvalidPageRequest
|
||||
from messenger.messaging.domain.keys import HistoryKey
|
||||
|
||||
VERSION = "1"
|
||||
SIGNATURE_BYTES = 12
|
||||
EPOCH = datetime(1970, 1, 1, tzinfo=UTC)
|
||||
PARTS = 4
|
||||
|
||||
|
||||
def _encode_part(value: bytes) -> str:
|
||||
return urlsafe_b64encode(value).decode("ascii").rstrip("=")
|
||||
|
||||
|
||||
def _decode_part(value: str) -> bytes:
|
||||
padding = "=" * (-len(value) % 4)
|
||||
try:
|
||||
return urlsafe_b64decode(value + padding)
|
||||
except (ValueError, TypeError) as error:
|
||||
raise InvalidPageRequest("параметр cursor повреждён") from error
|
||||
|
||||
|
||||
class SignedCursorCodec:
|
||||
__slots__ = ("_secret",)
|
||||
|
||||
def __init__(self, secret: bytes) -> None:
|
||||
self._secret = secret
|
||||
|
||||
def encode(self, scope: str, key: HistoryKey) -> str:
|
||||
moment, identifier = key
|
||||
micros = (moment - EPOCH) // timedelta(microseconds=1)
|
||||
body = f"{VERSION}.{micros}.{identifier:032x}"
|
||||
return f"{body}.{_encode_part(self._sign(scope, body))}"
|
||||
|
||||
def decode(self, scope: str, raw: str) -> HistoryKey:
|
||||
parts = raw.split(".")
|
||||
if len(parts) != PARTS or parts[0] != VERSION:
|
||||
raise InvalidPageRequest("параметр cursor не распознан")
|
||||
body = ".".join(parts[:3])
|
||||
if not hmac.compare_digest(self._sign(scope, body), _decode_part(parts[3])):
|
||||
raise InvalidPageRequest("параметр cursor недействителен для этого запроса")
|
||||
try:
|
||||
micros = int(parts[1])
|
||||
identifier = int(parts[2], 16)
|
||||
except ValueError as error:
|
||||
raise InvalidPageRequest("параметр cursor повреждён") from error
|
||||
try:
|
||||
moment = EPOCH + timedelta(microseconds=micros)
|
||||
except (OverflowError, ValueError) as error:
|
||||
raise InvalidPageRequest("параметр cursor повреждён") from error
|
||||
return (moment, identifier)
|
||||
|
||||
def _sign(self, scope: str, body: str) -> bytes:
|
||||
payload = f"{scope}|{body}".encode()
|
||||
return hmac.new(self._secret, payload, sha256).digest()[:SIGNATURE_BYTES]
|
||||
@@ -0,0 +1,201 @@
|
||||
from collections.abc import Sequence
|
||||
from datetime import datetime
|
||||
from uuid import UUID
|
||||
|
||||
from asyncpg import ForeignKeyViolationError, Record
|
||||
|
||||
from messenger.messaging.domain.errors import InvalidChatData
|
||||
from messenger.messaging.domain.keys import HistoryKey
|
||||
from messenger.messaging.domain.models import Chat, Message, Receipt
|
||||
from messenger.messaging.domain.values import ChatTitle, MessageText
|
||||
from messenger.shared.infrastructure.postgres import (
|
||||
Connection,
|
||||
Database,
|
||||
EventSource,
|
||||
PostgresUnitOfWork,
|
||||
)
|
||||
|
||||
INSERT_CHAT = "INSERT INTO chats (id, title, created_at) VALUES ($1, $2, $3)"
|
||||
INSERT_MEMBERS = """
|
||||
INSERT INTO chat_members (chat_id, user_id, position, chat_created_at)
|
||||
SELECT $1, member.user_id, member.position, $3
|
||||
FROM unnest($2::uuid[]) WITH ORDINALITY AS member (user_id, position)
|
||||
"""
|
||||
LOCK_CHAT = """
|
||||
SELECT c.id, c.title, c.created_at, c.last_message_at,
|
||||
ARRAY(SELECT user_id FROM chat_members WHERE chat_id = c.id ORDER BY position) AS member_ids
|
||||
FROM chats c
|
||||
WHERE c.id = $1
|
||||
FOR UPDATE
|
||||
"""
|
||||
RECEIPT = """
|
||||
SELECT k.fingerprint, m.id, m.chat_id, m.sender_id, m.text, m.created_at
|
||||
FROM idempotency_keys k JOIN messages m ON m.id = k.message_id
|
||||
WHERE k.chat_id = $1 AND k.user_id = $2 AND k.key = $3 AND k.created_at > $4
|
||||
"""
|
||||
APPEND_MESSAGE = """
|
||||
WITH message AS (
|
||||
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
RETURNING id, chat_id, sender_id, created_at
|
||||
), receipt AS (
|
||||
INSERT INTO idempotency_keys (chat_id, user_id, key, fingerprint, message_id, created_at)
|
||||
SELECT chat_id, sender_id, $6, $7, id, created_at FROM message
|
||||
ON CONFLICT (chat_id, user_id, key) DO UPDATE
|
||||
SET fingerprint = EXCLUDED.fingerprint,
|
||||
message_id = EXCLUDED.message_id,
|
||||
created_at = EXCLUDED.created_at
|
||||
)
|
||||
UPDATE chats SET last_message_at = message.created_at
|
||||
FROM message
|
||||
WHERE chats.id = message.chat_id
|
||||
"""
|
||||
CHAT_OF_MEMBER = """
|
||||
SELECT c.id, c.title, c.created_at,
|
||||
ARRAY(SELECT user_id FROM chat_members WHERE chat_id = c.id ORDER BY position) AS member_ids
|
||||
FROM chat_members m JOIN chats c ON c.id = m.chat_id
|
||||
WHERE m.chat_id = $1 AND m.user_id = $2
|
||||
"""
|
||||
CHATS_FIRST = """
|
||||
SELECT c.id, c.title, c.created_at,
|
||||
ARRAY(SELECT user_id FROM chat_members WHERE chat_id = c.id ORDER BY position) AS member_ids
|
||||
FROM chat_members m JOIN chats c ON c.id = m.chat_id
|
||||
WHERE m.user_id = $1
|
||||
ORDER BY m.chat_created_at, m.chat_id
|
||||
LIMIT $2
|
||||
"""
|
||||
CHATS_AFTER = """
|
||||
SELECT c.id, c.title, c.created_at,
|
||||
ARRAY(SELECT user_id FROM chat_members WHERE chat_id = c.id ORDER BY position) AS member_ids
|
||||
FROM chat_members m JOIN chats c ON c.id = m.chat_id
|
||||
WHERE m.user_id = $1 AND (m.chat_created_at, m.chat_id) > ($2, $3)
|
||||
ORDER BY m.chat_created_at, m.chat_id
|
||||
LIMIT $4
|
||||
"""
|
||||
HISTORY_FIRST = """
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = $1
|
||||
ORDER BY created_at, id
|
||||
LIMIT $2
|
||||
"""
|
||||
HISTORY_AFTER = """
|
||||
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
|
||||
"""
|
||||
|
||||
|
||||
def chat_of(row: Record) -> Chat:
|
||||
return Chat(
|
||||
chat_id=row["id"],
|
||||
title=ChatTitle(row["title"]),
|
||||
member_ids=row["member_ids"],
|
||||
created_at=row["created_at"],
|
||||
last_message_at=row.get("last_message_at"),
|
||||
)
|
||||
|
||||
|
||||
def message_of(row: Record) -> Message:
|
||||
return Message(
|
||||
id=row["id"],
|
||||
chat_id=row["chat_id"],
|
||||
sender_id=row["sender_id"],
|
||||
text=MessageText(row["text"]),
|
||||
created_at=row["created_at"],
|
||||
)
|
||||
|
||||
|
||||
def page[ItemT](rows: Sequence[ItemT], limit: int) -> tuple[list[ItemT], bool]:
|
||||
return list(rows[:limit]), len(rows) > limit
|
||||
|
||||
|
||||
class PostgresChatRepository:
|
||||
__slots__ = ("_connection", "_seen")
|
||||
|
||||
def __init__(self, connection: Connection, seen: list[EventSource]) -> None:
|
||||
self._connection = connection
|
||||
self._seen = seen
|
||||
|
||||
async def add(self, chat: Chat) -> None:
|
||||
await self._connection.execute(INSERT_CHAT, chat.id, chat.title.value, chat.created_at)
|
||||
try:
|
||||
await self._connection.execute(
|
||||
INSERT_MEMBERS, chat.id, list(chat.member_ids), chat.created_at
|
||||
)
|
||||
except ForeignKeyViolationError as error:
|
||||
raise InvalidChatData("поле member_ids содержит неизвестного пользователя") from error
|
||||
self._seen.append(chat)
|
||||
|
||||
async def get(self, chat_id: UUID) -> Chat | None:
|
||||
row = await self._connection.fetchrow(LOCK_CHAT, chat_id)
|
||||
if row is None:
|
||||
return None
|
||||
chat = chat_of(row)
|
||||
self._seen.append(chat)
|
||||
return chat
|
||||
|
||||
async def receipt(
|
||||
self, chat_id: UUID, sender_id: UUID, key: str, since: datetime
|
||||
) -> Receipt | None:
|
||||
row = await self._connection.fetchrow(RECEIPT, chat_id, sender_id, key, since)
|
||||
return None if row is None else Receipt(key, row["fingerprint"], message_of(row))
|
||||
|
||||
async def append(self, receipt: Receipt) -> None:
|
||||
message = receipt.message
|
||||
await self._connection.execute(
|
||||
APPEND_MESSAGE,
|
||||
message.id,
|
||||
message.chat_id,
|
||||
message.sender_id,
|
||||
message.text.value,
|
||||
message.created_at,
|
||||
receipt.key,
|
||||
receipt.fingerprint,
|
||||
)
|
||||
|
||||
|
||||
class PostgresMessagingUnitOfWork(PostgresUnitOfWork):
|
||||
__slots__ = ()
|
||||
|
||||
@property
|
||||
def chats(self) -> PostgresChatRepository:
|
||||
return PostgresChatRepository(self.connection, self.seen)
|
||||
|
||||
|
||||
class PostgresChatDirectory:
|
||||
__slots__ = ("_database",)
|
||||
|
||||
def __init__(self, database: Database) -> None:
|
||||
self._database = database
|
||||
|
||||
async def chat_for_member(self, chat_id: UUID, user_id: UUID) -> Chat | None:
|
||||
async with self._database.connection() as connection:
|
||||
row = await connection.fetchrow(CHAT_OF_MEMBER, chat_id, user_id)
|
||||
return None if row is None else chat_of(row)
|
||||
|
||||
async def chats_of(
|
||||
self, user_id: UUID, *, after: HistoryKey | None, limit: int
|
||||
) -> tuple[list[Chat], bool]:
|
||||
async with self._database.connection() as connection:
|
||||
if after is None:
|
||||
rows = await connection.fetch(CHATS_FIRST, user_id, limit + 1)
|
||||
else:
|
||||
rows = await connection.fetch(
|
||||
CHATS_AFTER, user_id, after[0], UUID(int=after[1]), limit + 1
|
||||
)
|
||||
return page([chat_of(row) for row in rows], limit)
|
||||
|
||||
async def history(
|
||||
self, chat_id: UUID, *, after: HistoryKey | None, limit: int
|
||||
) -> tuple[list[Message], bool]:
|
||||
async with self._database.connection() as connection:
|
||||
if after is None:
|
||||
rows = await connection.fetch(HISTORY_FIRST, chat_id, limit + 1)
|
||||
else:
|
||||
rows = await connection.fetch(
|
||||
HISTORY_AFTER, chat_id, after[0], UUID(int=after[1]), limit + 1
|
||||
)
|
||||
return page([message_of(row) for row in rows], limit)
|
||||
@@ -0,0 +1,70 @@
|
||||
import asyncio
|
||||
import hashlib
|
||||
from importlib import resources
|
||||
|
||||
import asyncpg
|
||||
|
||||
from messenger.shared.config import PlatformSettings
|
||||
from messenger.shared.infrastructure.logging import PLATFORM_FIELDS, StructlogLogger
|
||||
|
||||
LOCK_KEY = 20_260_002
|
||||
JOURNAL = """
|
||||
CREATE TABLE IF NOT EXISTS schema_migrations (
|
||||
version text PRIMARY KEY,
|
||||
checksum text NOT NULL,
|
||||
applied_at timestamptz NOT NULL DEFAULT now()
|
||||
)
|
||||
"""
|
||||
|
||||
|
||||
class MigrationChangedError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def migrations() -> list[tuple[str, str]]:
|
||||
folder = resources.files("messenger") / "migrations"
|
||||
found = (item for item in folder.iterdir() if item.name.endswith(".sql"))
|
||||
return sorted((item.name.removesuffix(".sql"), item.read_text()) for item in found)
|
||||
|
||||
|
||||
async def migrate(dsn: str) -> list[str]:
|
||||
connection = await asyncpg.connect(dsn, timeout=10)
|
||||
try:
|
||||
await connection.execute("SELECT pg_advisory_lock($1)", LOCK_KEY)
|
||||
await connection.execute(JOURNAL)
|
||||
applied = dict(await connection.fetch("SELECT version, checksum FROM schema_migrations"))
|
||||
done: list[str] = []
|
||||
for version, sql in migrations():
|
||||
checksum = hashlib.sha256(sql.encode()).hexdigest()
|
||||
if version in applied:
|
||||
if applied[version] != checksum:
|
||||
raise MigrationChangedError(version)
|
||||
continue
|
||||
async with connection.transaction():
|
||||
await connection.execute(sql)
|
||||
await connection.execute(
|
||||
"INSERT INTO schema_migrations (version, checksum) VALUES ($1, $2)",
|
||||
version,
|
||||
checksum,
|
||||
)
|
||||
done.append(version)
|
||||
return done
|
||||
finally:
|
||||
await connection.close()
|
||||
|
||||
|
||||
def main() -> None:
|
||||
settings = PlatformSettings()
|
||||
logger = StructlogLogger(level=settings.log_level, allowed_fields=PLATFORM_FIELDS | {"version"})
|
||||
try:
|
||||
done = asyncio.run(migrate(settings.database_url.get_secret_value()))
|
||||
except MigrationChangedError as error:
|
||||
logger.error("migration.changed", version=str(error))
|
||||
raise SystemExit(1) from None
|
||||
for version in done:
|
||||
logger.info("migration.applied", version=version)
|
||||
logger.info("migration.completed", reason="up to date" if not done else "applied")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,46 @@
|
||||
CREATE TABLE users (
|
||||
id uuid PRIMARY KEY,
|
||||
username text NOT NULL CONSTRAINT users_username_key UNIQUE
|
||||
CONSTRAINT users_username_format CHECK (username ~ '^[a-z0-9_]{3,32}$'),
|
||||
display_name text NOT NULL CHECK (char_length(display_name) BETWEEN 1 AND 100),
|
||||
password_hash text NOT NULL CHECK (password_hash LIKE '$argon2id$%'),
|
||||
created_at timestamptz NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE sessions (
|
||||
token_hash bytea PRIMARY KEY CHECK (octet_length(token_hash) = 32),
|
||||
user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
||||
created_at timestamptz NOT NULL,
|
||||
expires_at timestamptz NOT NULL,
|
||||
CHECK (expires_at > created_at)
|
||||
);
|
||||
|
||||
CREATE INDEX sessions_user_idx ON sessions (user_id);
|
||||
|
||||
CREATE TABLE chats (
|
||||
id uuid PRIMARY KEY,
|
||||
title text NOT NULL CHECK (char_length(title) BETWEEN 1 AND 100),
|
||||
created_at timestamptz NOT NULL,
|
||||
last_message_at timestamptz
|
||||
);
|
||||
|
||||
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 CHECK (position BETWEEN 1 AND 100),
|
||||
chat_created_at timestamptz NOT NULL,
|
||||
PRIMARY KEY (chat_id, user_id)
|
||||
);
|
||||
|
||||
CREATE INDEX chat_members_user_idx ON chat_members (user_id, chat_created_at, chat_id);
|
||||
|
||||
CREATE TABLE messages (
|
||||
id uuid PRIMARY KEY,
|
||||
chat_id uuid NOT NULL,
|
||||
sender_id uuid NOT NULL,
|
||||
text text NOT NULL CHECK (char_length(text) BETWEEN 1 AND 4000),
|
||||
created_at timestamptz NOT NULL,
|
||||
FOREIGN KEY (chat_id, sender_id) REFERENCES chat_members (chat_id, user_id)
|
||||
);
|
||||
|
||||
CREATE INDEX messages_history_idx ON messages (chat_id, created_at, id);
|
||||
@@ -0,0 +1,10 @@
|
||||
CREATE TABLE idempotency_keys (
|
||||
chat_id uuid NOT NULL,
|
||||
user_id uuid NOT NULL,
|
||||
key text NOT NULL CHECK (key ~ '^[A-Za-z0-9._:-]{1,128}$'),
|
||||
fingerprint bytea NOT NULL CHECK (octet_length(fingerprint) = 32),
|
||||
message_id uuid NOT NULL REFERENCES messages (id) ON DELETE CASCADE,
|
||||
created_at timestamptz NOT NULL,
|
||||
PRIMARY KEY (chat_id, user_id, key),
|
||||
FOREIGN KEY (chat_id, user_id) REFERENCES chat_members (chat_id, user_id)
|
||||
);
|
||||
@@ -0,0 +1,2 @@
|
||||
class Command:
|
||||
pass
|
||||
@@ -0,0 +1,2 @@
|
||||
class Unavailable(Exception):
|
||||
pass
|
||||
@@ -0,0 +1,123 @@
|
||||
import asyncio
|
||||
from collections.abc import Awaitable, Callable, Mapping, Sequence
|
||||
from contextlib import suppress
|
||||
from typing import Any
|
||||
|
||||
from messenger.shared.application.commands import Command
|
||||
from messenger.shared.application.ports import EventPublisher, Logger, UnitOfWork
|
||||
from messenger.shared.domain.events import DomainEvent
|
||||
|
||||
type CommandHandler = Callable[[Any], Awaitable[Any]]
|
||||
type EventHandler[EventT: DomainEvent] = Callable[[EventT], Awaitable[None]]
|
||||
|
||||
|
||||
class CommandBus:
|
||||
__slots__ = ("_handlers", "_publisher", "_uow")
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
handlers: Mapping[type[Command], CommandHandler],
|
||||
uow: UnitOfWork,
|
||||
publisher: EventPublisher,
|
||||
) -> None:
|
||||
self._handlers = handlers
|
||||
self._uow = uow
|
||||
self._publisher = publisher
|
||||
|
||||
async def execute(self, command: Command) -> Any:
|
||||
handler = self._handlers.get(type(command))
|
||||
if handler is None:
|
||||
raise LookupError(f"нет обработчика для команды {type(command).__name__}")
|
||||
result = await handler(command)
|
||||
events = self._uow.collect_new_events()
|
||||
if events:
|
||||
await self._publisher.publish(events)
|
||||
return result
|
||||
|
||||
|
||||
class EventBus:
|
||||
__slots__ = ("_deferred", "_dropped", "_logger", "_projections", "_queue", "_worker")
|
||||
|
||||
def __init__(self, logger: Logger, *, max_queue: int = 10_000) -> None:
|
||||
self._logger = logger
|
||||
self._queue: asyncio.Queue[DomainEvent] = asyncio.Queue(maxsize=max_queue)
|
||||
self._projections: dict[type[DomainEvent], list[Any]] = {}
|
||||
self._deferred: dict[type[DomainEvent], list[Any]] = {}
|
||||
self._worker: asyncio.Task[None] | None = None
|
||||
self._dropped = 0
|
||||
|
||||
def project[EventT: DomainEvent](
|
||||
self, event_type: type[EventT], handler: EventHandler[EventT]
|
||||
) -> None:
|
||||
self._projections.setdefault(event_type, []).append(handler)
|
||||
|
||||
def subscribe[EventT: DomainEvent](
|
||||
self, event_type: type[EventT], handler: EventHandler[EventT]
|
||||
) -> None:
|
||||
self._deferred.setdefault(event_type, []).append(handler)
|
||||
|
||||
async def emit(self, event: DomainEvent) -> bool:
|
||||
await self._run(self._projections.get(type(event), []), event, "projection")
|
||||
if not self._deferred.get(type(event)):
|
||||
return True
|
||||
try:
|
||||
self._queue.put_nowait(event)
|
||||
except asyncio.QueueFull:
|
||||
self._dropped += 1
|
||||
self._logger.warning(
|
||||
"event.dropped", event_name=event.name, dropped_total=self._dropped
|
||||
)
|
||||
return False
|
||||
return True
|
||||
|
||||
@property
|
||||
def dropped(self) -> int:
|
||||
return self._dropped
|
||||
|
||||
@property
|
||||
def pending(self) -> int:
|
||||
return self._queue.qsize()
|
||||
|
||||
def start(self) -> None:
|
||||
if self._worker is None:
|
||||
self._worker = asyncio.create_task(self._drain())
|
||||
|
||||
async def stop(self) -> None:
|
||||
if self._worker is None:
|
||||
return
|
||||
await self._queue.join()
|
||||
self._worker.cancel()
|
||||
with suppress(asyncio.CancelledError):
|
||||
await self._worker
|
||||
self._worker = None
|
||||
|
||||
async def _drain(self) -> None:
|
||||
while True:
|
||||
event = await self._queue.get()
|
||||
try:
|
||||
await self._run(self._deferred.get(type(event), []), event, "subscriber")
|
||||
finally:
|
||||
self._queue.task_done()
|
||||
|
||||
async def _run(self, handlers: Sequence[Any], event: DomainEvent, kind: str) -> None:
|
||||
for handler in handlers:
|
||||
try:
|
||||
await handler(event)
|
||||
except Exception as error:
|
||||
self._logger.error(
|
||||
"event.handler_failed",
|
||||
event_name=event.name,
|
||||
reason=kind,
|
||||
error=type(error).__name__,
|
||||
)
|
||||
|
||||
|
||||
class InMemoryEventPublisher:
|
||||
__slots__ = ("_bus",)
|
||||
|
||||
def __init__(self, bus: EventBus) -> None:
|
||||
self._bus = bus
|
||||
|
||||
async def publish(self, events: Sequence[DomainEvent]) -> None:
|
||||
for event in events:
|
||||
await self._bus.emit(event)
|
||||
@@ -0,0 +1,42 @@
|
||||
from collections.abc import Sequence
|
||||
from contextlib import AbstractAsyncContextManager
|
||||
from datetime import datetime
|
||||
from typing import Any, Protocol, runtime_checkable
|
||||
from uuid import UUID
|
||||
|
||||
from messenger.shared.domain.events import DomainEvent
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class Clock(Protocol):
|
||||
def now(self) -> datetime: ...
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class IdGenerator(Protocol):
|
||||
def new_id(self) -> UUID: ...
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class Logger(Protocol):
|
||||
def info(self, event: str, /, **fields: Any) -> None: ...
|
||||
|
||||
def warning(self, event: str, /, **fields: Any) -> None: ...
|
||||
|
||||
def error(self, event: str, /, **fields: Any) -> None: ...
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class UnitOfWork(Protocol):
|
||||
def transaction(self) -> AbstractAsyncContextManager[None]: ...
|
||||
|
||||
async def commit(self) -> None: ...
|
||||
|
||||
def record(self, event: DomainEvent) -> None: ...
|
||||
|
||||
def collect_new_events(self) -> list[DomainEvent]: ...
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class EventPublisher(Protocol):
|
||||
async def publish(self, events: Sequence[DomainEvent]) -> None: ...
|
||||
@@ -0,0 +1,49 @@
|
||||
import os
|
||||
import socket
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from pydantic import Field, SecretStr
|
||||
from pydantic.fields import FieldInfo
|
||||
from pydantic_settings import BaseSettings, PydanticBaseSettingsSource, SettingsConfigDict
|
||||
|
||||
CONFIG = SettingsConfigDict(extra="ignore", frozen=True)
|
||||
|
||||
|
||||
class FileVariables(PydanticBaseSettingsSource):
|
||||
def get_field_value(self, field: FieldInfo, field_name: str) -> tuple[Any, str, bool]:
|
||||
path = os.environ.get(f"{field_name.upper()}_FILE")
|
||||
return (Path(path).read_text().strip() if path else None), field_name, False
|
||||
|
||||
def __call__(self) -> dict[str, Any]:
|
||||
found = {
|
||||
name: self.get_field_value(field, name)[0]
|
||||
for name, field in self.settings_cls.model_fields.items()
|
||||
}
|
||||
return {name: value for name, value in found.items() if value is not None}
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
model_config = CONFIG
|
||||
|
||||
@classmethod
|
||||
def settings_customise_sources(
|
||||
cls,
|
||||
settings_cls: type[BaseSettings],
|
||||
init_settings: PydanticBaseSettingsSource,
|
||||
env_settings: PydanticBaseSettingsSource,
|
||||
dotenv_settings: PydanticBaseSettingsSource,
|
||||
file_secret_settings: PydanticBaseSettingsSource,
|
||||
) -> tuple[PydanticBaseSettingsSource, ...]:
|
||||
return init_settings, env_settings, FileVariables(settings_cls)
|
||||
|
||||
|
||||
class PlatformSettings(Settings):
|
||||
log_level: str = "INFO"
|
||||
instance_id: str = Field(default_factory=socket.gethostname, min_length=1)
|
||||
event_queue_max: int = Field(default=10_000, ge=16)
|
||||
database_url: SecretStr
|
||||
db_pool_max_size: int = Field(default=8, ge=1, le=50)
|
||||
db_connect_timeout_seconds: float = Field(default=1.0, gt=0, le=30)
|
||||
db_acquire_timeout_seconds: float = Field(default=1.0, gt=0, le=30)
|
||||
db_command_timeout_seconds: float = Field(default=2.0, gt=0, le=60)
|
||||
@@ -0,0 +1,4 @@
|
||||
class DomainError(Exception):
|
||||
def __init__(self, detail: str) -> None:
|
||||
super().__init__(detail)
|
||||
self.detail = detail
|
||||
@@ -0,0 +1,6 @@
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class DomainEvent:
|
||||
name: str = field(init=False, default="domain.event")
|
||||
@@ -0,0 +1,16 @@
|
||||
from typing import Annotated
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import Depends, Request
|
||||
|
||||
from messenger.shared.entrypoints.errors import Unauthenticated
|
||||
|
||||
|
||||
async def current_actor(request: Request) -> UUID:
|
||||
actor = getattr(request.state, "actor", None)
|
||||
if not isinstance(actor, UUID):
|
||||
raise Unauthenticated("заголовок идентификации обязателен и должен быть действительным")
|
||||
return actor
|
||||
|
||||
|
||||
type Actor = Annotated[UUID, Depends(current_actor)]
|
||||
@@ -0,0 +1,10 @@
|
||||
from pydantic import Field
|
||||
|
||||
from messenger.shared.config import Settings
|
||||
|
||||
|
||||
class TransportSettings(Settings):
|
||||
docs: bool = False
|
||||
max_body_bytes: int = Field(default=1_048_576, ge=4_096)
|
||||
request_timeout_seconds: float = Field(default=8.0, gt=0, le=300)
|
||||
shutdown_timeout_seconds: int = Field(default=5, ge=1, le=10)
|
||||
@@ -0,0 +1,136 @@
|
||||
from collections.abc import Callable, Mapping
|
||||
|
||||
from fastapi import FastAPI, Request, Response, status
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from starlette.exceptions import HTTPException
|
||||
|
||||
from messenger.shared.application.errors import Unavailable
|
||||
from messenger.shared.application.ports import Logger
|
||||
from messenger.shared.domain.errors import DomainError
|
||||
from messenger.shared.entrypoints.schemas import ErrorEnvelope
|
||||
|
||||
REQUEST_ID_HEADER = "X-Request-Id"
|
||||
INSTANCE_ID_HEADER = "X-Instance-Id"
|
||||
|
||||
type ErrorCodes = Mapping[type[DomainError], tuple[str, int]]
|
||||
type Challenge = Callable[[str], str]
|
||||
|
||||
MESSAGES = {
|
||||
"invalid_request": "Запрос не соответствует контракту",
|
||||
"unauthorized": "Аутентификация не предоставлена или недействительна",
|
||||
"not_found": "Объект не найден",
|
||||
"conflict": "Конфликт состояния",
|
||||
"payload_too_large": "Тело запроса больше допустимого",
|
||||
"unsupported_media_type": "Неподдерживаемый тип содержимого",
|
||||
"rate_limited": "Слишком много запросов",
|
||||
"unavailable": "Сервис временно недоступен",
|
||||
"internal_error": "Внутренняя ошибка",
|
||||
}
|
||||
|
||||
STATUS_CODES = {
|
||||
status.HTTP_400_BAD_REQUEST: "invalid_request",
|
||||
status.HTTP_401_UNAUTHORIZED: "unauthorized",
|
||||
status.HTTP_404_NOT_FOUND: "not_found",
|
||||
status.HTTP_409_CONFLICT: "conflict",
|
||||
status.HTTP_413_CONTENT_TOO_LARGE: "payload_too_large",
|
||||
status.HTTP_415_UNSUPPORTED_MEDIA_TYPE: "unsupported_media_type",
|
||||
status.HTTP_422_UNPROCESSABLE_CONTENT: "invalid_request",
|
||||
status.HTTP_429_TOO_MANY_REQUESTS: "rate_limited",
|
||||
status.HTTP_503_SERVICE_UNAVAILABLE: "unavailable",
|
||||
}
|
||||
|
||||
|
||||
class Unauthenticated(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def classify(error: DomainError, codes: ErrorCodes) -> tuple[str, int]:
|
||||
for kind in type(error).__mro__:
|
||||
known = codes.get(kind) # type: ignore[arg-type]
|
||||
if known is not None:
|
||||
return known
|
||||
return "internal_error", status.HTTP_500_INTERNAL_SERVER_ERROR
|
||||
|
||||
|
||||
def route_of(request: Request) -> str:
|
||||
route = request.scope.get("route")
|
||||
return getattr(route, "path", request.url.path)
|
||||
|
||||
|
||||
def install(
|
||||
app: FastAPI, logger: Logger, codes: ErrorCodes, challenge: Challenge, instance: str
|
||||
) -> None:
|
||||
def error_response(request: Request, code: str, detail: str, http_status: int) -> Response:
|
||||
request_id = getattr(request.state, "request_id", "") or "unknown"
|
||||
headers = {REQUEST_ID_HEADER: request_id, INSTANCE_ID_HEADER: instance}
|
||||
if http_status == status.HTTP_401_UNAUTHORIZED:
|
||||
headers["WWW-Authenticate"] = challenge(request.url.path)
|
||||
return Response(
|
||||
status_code=http_status,
|
||||
content=ErrorEnvelope.of(code, detail, request_id).model_dump_json(),
|
||||
media_type="application/json",
|
||||
headers=headers,
|
||||
)
|
||||
|
||||
@app.exception_handler(DomainError)
|
||||
async def _domain(request: Request, exc: DomainError) -> Response:
|
||||
code, http_status = classify(exc, codes)
|
||||
return error_response(request, code, exc.detail, http_status)
|
||||
|
||||
@app.exception_handler(Unavailable)
|
||||
async def _unavailable(request: Request, exc: Unavailable) -> Response:
|
||||
logger.warning(
|
||||
"dependency.unavailable",
|
||||
route=route_of(request),
|
||||
error=type(exc.__cause__ or exc).__name__,
|
||||
request_id=getattr(request.state, "request_id", None),
|
||||
)
|
||||
return error_response(
|
||||
request,
|
||||
"unavailable",
|
||||
MESSAGES["unavailable"],
|
||||
status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
)
|
||||
|
||||
@app.exception_handler(Unauthenticated)
|
||||
async def _unauthenticated(request: Request, _exc: Unauthenticated) -> Response:
|
||||
return error_response(
|
||||
request,
|
||||
"unauthorized",
|
||||
MESSAGES["unauthorized"],
|
||||
status.HTTP_401_UNAUTHORIZED,
|
||||
)
|
||||
|
||||
@app.exception_handler(RequestValidationError)
|
||||
async def _validation(request: Request, _exc: RequestValidationError) -> Response:
|
||||
return error_response(
|
||||
request,
|
||||
"invalid_request",
|
||||
MESSAGES["invalid_request"],
|
||||
status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
|
||||
@app.exception_handler(HTTPException)
|
||||
async def _http(request: Request, exc: HTTPException) -> Response:
|
||||
http_status = exc.status_code
|
||||
if http_status == status.HTTP_405_METHOD_NOT_ALLOWED:
|
||||
http_status = status.HTTP_404_NOT_FOUND
|
||||
code = STATUS_CODES.get(http_status, "internal_error")
|
||||
return error_response(
|
||||
request, code, MESSAGES.get(code, MESSAGES["internal_error"]), http_status
|
||||
)
|
||||
|
||||
@app.exception_handler(Exception)
|
||||
async def _unexpected(request: Request, exc: Exception) -> Response:
|
||||
logger.error(
|
||||
"request.failed",
|
||||
route=route_of(request),
|
||||
error=type(exc).__name__,
|
||||
request_id=getattr(request.state, "request_id", None),
|
||||
)
|
||||
return error_response(
|
||||
request,
|
||||
"internal_error",
|
||||
MESSAGES["internal_error"],
|
||||
status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
)
|
||||
@@ -0,0 +1,25 @@
|
||||
from threading import Event
|
||||
|
||||
from dishka.integrations.fastapi import FromDishka, inject
|
||||
from fastapi import APIRouter
|
||||
|
||||
from messenger.shared.application.errors import Unavailable
|
||||
from messenger.shared.entrypoints.schemas import HealthOut
|
||||
from messenger.shared.infrastructure.postgres import Database
|
||||
|
||||
router = APIRouter(tags=["health"])
|
||||
DRAINING = Event()
|
||||
|
||||
|
||||
@router.get("/health/live")
|
||||
async def live() -> HealthOut:
|
||||
return HealthOut()
|
||||
|
||||
|
||||
@router.get("/health/ready")
|
||||
@inject
|
||||
async def ready(database: FromDishka[Database]) -> HealthOut:
|
||||
if DRAINING.is_set():
|
||||
raise Unavailable("Реплика выходит из ротации")
|
||||
await database.ping()
|
||||
return HealthOut()
|
||||
@@ -0,0 +1,54 @@
|
||||
from collections.abc import AsyncIterator
|
||||
|
||||
from dishka import Provider, Scope, provide
|
||||
|
||||
from messenger.shared.application.messagebus import EventBus, InMemoryEventPublisher
|
||||
from messenger.shared.application.ports import Clock, EventPublisher, IdGenerator, Logger
|
||||
from messenger.shared.config import PlatformSettings
|
||||
from messenger.shared.infrastructure.clock import MonotonicUtcClock
|
||||
from messenger.shared.infrastructure.ids import Uuid7Generator
|
||||
from messenger.shared.infrastructure.postgres import Database
|
||||
|
||||
|
||||
class PlatformProvider(Provider):
|
||||
def __init__(self, settings: PlatformSettings, logger: Logger) -> None:
|
||||
super().__init__()
|
||||
self._settings = settings
|
||||
self._logger = logger
|
||||
|
||||
@provide(scope=Scope.APP)
|
||||
def settings(self) -> PlatformSettings:
|
||||
return self._settings
|
||||
|
||||
@provide(scope=Scope.APP)
|
||||
def logger(self) -> Logger:
|
||||
return self._logger
|
||||
|
||||
@provide(scope=Scope.APP)
|
||||
def clock(self) -> Clock:
|
||||
return MonotonicUtcClock()
|
||||
|
||||
@provide(scope=Scope.APP)
|
||||
def ids(self) -> IdGenerator:
|
||||
return Uuid7Generator()
|
||||
|
||||
@provide(scope=Scope.APP)
|
||||
async def database(self, settings: PlatformSettings) -> AsyncIterator[Database]:
|
||||
database = Database(
|
||||
settings.database_url.get_secret_value(),
|
||||
max_size=settings.db_pool_max_size,
|
||||
connect_timeout=settings.db_connect_timeout_seconds,
|
||||
acquire_timeout=settings.db_acquire_timeout_seconds,
|
||||
command_timeout=settings.db_command_timeout_seconds,
|
||||
)
|
||||
await database.open()
|
||||
yield database
|
||||
await database.close()
|
||||
|
||||
@provide(scope=Scope.APP)
|
||||
def event_bus(self, logger: Logger, settings: PlatformSettings) -> EventBus:
|
||||
return EventBus(logger, max_queue=settings.event_queue_max)
|
||||
|
||||
@provide(scope=Scope.APP)
|
||||
def publisher(self, bus: EventBus) -> EventPublisher:
|
||||
return InMemoryEventPublisher(bus)
|
||||
@@ -0,0 +1,165 @@
|
||||
import asyncio
|
||||
import time
|
||||
from collections.abc import Awaitable, Callable
|
||||
from dataclasses import dataclass
|
||||
from uuid import uuid4
|
||||
|
||||
from starlette.types import ASGIApp, Message, Receive, Scope, Send
|
||||
|
||||
from messenger.shared.application.errors import Unavailable
|
||||
from messenger.shared.application.ports import Logger
|
||||
from messenger.shared.entrypoints.errors import INSTANCE_ID_HEADER, MESSAGES, REQUEST_ID_HEADER
|
||||
from messenger.shared.entrypoints.schemas import ErrorEnvelope
|
||||
|
||||
type Headers = tuple[tuple[str, str], ...]
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class Rejection:
|
||||
status: int
|
||||
code: str
|
||||
headers: Headers = ()
|
||||
|
||||
|
||||
type Guard = Callable[[Scope], Awaitable[Rejection | None]]
|
||||
|
||||
HEADER_BYTES = REQUEST_ID_HEADER.lower().encode()
|
||||
INSTANCE_BYTES = INSTANCE_ID_HEADER.lower().encode()
|
||||
STAMPED = frozenset({HEADER_BYTES, INSTANCE_BYTES})
|
||||
MAX_REQUEST_ID = 200
|
||||
|
||||
|
||||
def header(scope: Scope, name: bytes) -> str:
|
||||
for key, value in scope["headers"]:
|
||||
if key == name:
|
||||
return value.decode("latin-1").strip()
|
||||
return ""
|
||||
|
||||
|
||||
async def send_error(
|
||||
send: Send, status: int, code: str, request_id: str, headers: Headers = ()
|
||||
) -> None:
|
||||
body = ErrorEnvelope.of(code, MESSAGES[code], request_id).model_dump_json().encode()
|
||||
await send(
|
||||
{
|
||||
"type": "http.response.start",
|
||||
"status": status,
|
||||
"headers": [
|
||||
(b"content-type", b"application/json"),
|
||||
(b"content-length", str(len(body)).encode()),
|
||||
(HEADER_BYTES, request_id.encode("latin-1", "replace")),
|
||||
*((name.encode(), value.encode("latin-1")) for name, value in headers),
|
||||
],
|
||||
}
|
||||
)
|
||||
await send({"type": "http.response.body", "body": body})
|
||||
|
||||
|
||||
class RequestPipeline:
|
||||
__slots__ = ("_app", "_guard", "_instance", "_logger", "_max_bytes", "_timeout")
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
app: ASGIApp,
|
||||
logger: Logger,
|
||||
max_bytes: int,
|
||||
timeout: float,
|
||||
guard: Guard,
|
||||
instance: str,
|
||||
) -> None:
|
||||
self._app = app
|
||||
self._logger = logger
|
||||
self._max_bytes = max_bytes
|
||||
self._timeout = timeout
|
||||
self._guard = guard
|
||||
self._instance = instance.encode()
|
||||
|
||||
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
|
||||
if scope["type"] != "http":
|
||||
await self._app(scope, receive, send)
|
||||
return
|
||||
|
||||
incoming = header(scope, HEADER_BYTES)
|
||||
request_id = incoming[:MAX_REQUEST_ID] if incoming else uuid4().hex
|
||||
state = scope.setdefault("state", {})
|
||||
state["request_id"] = request_id
|
||||
state["actor"] = None
|
||||
started = time.perf_counter()
|
||||
status_holder = [500]
|
||||
answered = [False]
|
||||
|
||||
async def wrapped_send(message: Message) -> None:
|
||||
if message["type"] == "http.response.start":
|
||||
status_holder[0] = message["status"]
|
||||
answered[0] = True
|
||||
headers = [
|
||||
(key, value)
|
||||
for key, value in message.get("headers", [])
|
||||
if key.lower() not in STAMPED
|
||||
]
|
||||
headers.append((HEADER_BYTES, request_id.encode("latin-1", "replace")))
|
||||
headers.append((INSTANCE_BYTES, self._instance))
|
||||
message = {**message, "headers": headers}
|
||||
await send(message)
|
||||
|
||||
declared = header(scope, b"content-length")
|
||||
if declared.isdigit() and int(declared) > self._max_bytes:
|
||||
await send_error(wrapped_send, 413, "payload_too_large", request_id)
|
||||
self._log(scope, status_holder[0], request_id, started)
|
||||
return
|
||||
|
||||
rejection: Rejection | None = None
|
||||
try:
|
||||
async with asyncio.timeout(self._timeout):
|
||||
rejection = await self._guard(scope)
|
||||
if rejection is None:
|
||||
await self._app(scope, receive, wrapped_send)
|
||||
except Unavailable as error:
|
||||
self._logger.warning(
|
||||
"dependency.unavailable",
|
||||
route=self._route(scope),
|
||||
error=type(error.__cause__ or error).__name__,
|
||||
request_id=request_id,
|
||||
)
|
||||
rejection = Rejection(503, "unavailable")
|
||||
except TimeoutError:
|
||||
self._logger.warning(
|
||||
"request.timeout",
|
||||
method=scope["method"],
|
||||
route=self._route(scope),
|
||||
request_id=request_id,
|
||||
limit=self._timeout,
|
||||
duration_ms=round((time.perf_counter() - started) * 1000, 3),
|
||||
)
|
||||
if not answered[0]:
|
||||
await send_error(wrapped_send, 503, "unavailable", request_id)
|
||||
self._log(scope, status_holder[0], request_id, started)
|
||||
return
|
||||
except Exception:
|
||||
self._logger.error(
|
||||
"request.unhandled",
|
||||
method=scope["method"],
|
||||
route=self._route(scope),
|
||||
request_id=request_id,
|
||||
duration_ms=round((time.perf_counter() - started) * 1000, 3),
|
||||
)
|
||||
raise
|
||||
if rejection is not None and not answered[0]:
|
||||
await send_error(
|
||||
wrapped_send, rejection.status, rejection.code, request_id, rejection.headers
|
||||
)
|
||||
self._log(scope, status_holder[0], request_id, started)
|
||||
|
||||
def _route(self, scope: Scope) -> str:
|
||||
route = scope.get("route")
|
||||
return getattr(route, "path", scope["path"])
|
||||
|
||||
def _log(self, scope: Scope, status: int, request_id: str, started: float) -> None:
|
||||
self._logger.info(
|
||||
"request.handled",
|
||||
method=scope["method"],
|
||||
route=self._route(scope),
|
||||
status=status,
|
||||
request_id=request_id,
|
||||
duration_ms=round((time.perf_counter() - started) * 1000, 3),
|
||||
)
|
||||
@@ -0,0 +1,21 @@
|
||||
from typing import Literal
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class ErrorDetail(BaseModel):
|
||||
code: str
|
||||
message: str
|
||||
request_id: str
|
||||
|
||||
|
||||
class ErrorEnvelope(BaseModel):
|
||||
error: ErrorDetail
|
||||
|
||||
@classmethod
|
||||
def of(cls, code: str, message: str, request_id: str) -> ErrorEnvelope:
|
||||
return cls(error=ErrorDetail(code=code, message=message, request_id=request_id))
|
||||
|
||||
|
||||
class HealthOut(BaseModel):
|
||||
status: Literal["ok"] = "ok"
|
||||
@@ -0,0 +1,17 @@
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
TICK = timedelta(microseconds=1)
|
||||
|
||||
|
||||
class MonotonicUtcClock:
|
||||
__slots__ = ("_last",)
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._last: datetime | None = None
|
||||
|
||||
def now(self) -> datetime:
|
||||
current = datetime.now(UTC)
|
||||
if self._last is not None and current <= self._last:
|
||||
current = self._last + TICK
|
||||
self._last = current
|
||||
return current
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user