forked from mtusi2026/mtusi2026lab4
Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d790976c30 | ||
|
|
60e8f4474b | ||
|
|
1c3011bf28 | ||
|
|
89e9e7b4d4 | ||
|
|
083d0d80cc |
@@ -18,3 +18,14 @@ __pycache__
|
||||
node_modules
|
||||
evidence
|
||||
.DS_Store
|
||||
assets
|
||||
contracts
|
||||
examples
|
||||
scripts
|
||||
tests
|
||||
*.md
|
||||
Makefile
|
||||
lab.json
|
||||
requirements-dev.txt
|
||||
Dockerfile*
|
||||
.dockerignore
|
||||
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
FROM golang:1.27.1-alpine3.24 AS build
|
||||
WORKDIR /src
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
COPY cmd ./cmd
|
||||
COPY internal ./internal
|
||||
RUN CGO_ENABLED=0 go build -trimpath -o /out/server ./cmd/server \
|
||||
&& CGO_ENABLED=0 go build -trimpath -o /out/s3-init ./cmd/s3-init \
|
||||
&& CGO_ENABLED=0 go build -trimpath -o /out/reconcile ./cmd/reconcile \
|
||||
&& CGO_ENABLED=0 go build -trimpath -o /out/s3tool ./cmd/s3tool
|
||||
|
||||
FROM alpine:3.24.1
|
||||
ARG BUILD_ID=dev
|
||||
LABEL org.opencontainers.image.revision=$BUILD_ID
|
||||
RUN adduser -D -H -u 10001 app
|
||||
COPY --from=build /out/server /out/s3-init /out/reconcile /out/s3tool /usr/local/bin/
|
||||
USER app
|
||||
ENV APP_HOST=0.0.0.0 APP_PORT=8080
|
||||
EXPOSE 8080
|
||||
HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \
|
||||
CMD wget -q -O /dev/null "http://127.0.0.1:${APP_PORT}/health/ready" || exit 1
|
||||
ENTRYPOINT ["/usr/local/bin/server"]
|
||||
@@ -2,12 +2,23 @@ PYTHON ?= python3
|
||||
BASE_URL ?= http://localhost:8080
|
||||
CA_FILE ?=
|
||||
ACTION_SCRIPT ?=
|
||||
ENGINE ?= $(shell command -v podman >/dev/null 2>&1 && echo podman || echo docker)
|
||||
COMPOSE ?= $(if $(filter podman,$(ENGINE)),podman-compose,docker compose)
|
||||
TEST_DATABASE_URL ?= $(shell sed 's/@postgres:5432/@127.0.0.1:55432/' secrets/database_url 2>/dev/null)
|
||||
TEST_REDIS_URL ?= redis://127.0.0.1:56379/0
|
||||
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
|
||||
|
||||
.PHONY: help check validate test load test-persistence test-failover
|
||||
.PHONY: help up down check validate test load test-persistence test-failover test-idempotency test-attachments test-contract test-memory test-cost test-reconcile test-go
|
||||
|
||||
help:
|
||||
@echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes"
|
||||
@echo "up/down: start/stop the stand, volumes kept; test-idempotency: one key in parallel via LB; test-attachments: attachment contract checks; test-memory: concurrent upload memory; test-cost: reconciler traversal cost; test-reconcile: reconciler dry-run; test-go: Go tests against the running stand"
|
||||
|
||||
up:
|
||||
$(COMPOSE) up -d --build
|
||||
|
||||
down:
|
||||
$(COMPOSE) down
|
||||
|
||||
check:
|
||||
$(PYTHON) scripts/check_contract.py
|
||||
@@ -28,3 +39,25 @@ 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)
|
||||
|
||||
test-idempotency:
|
||||
BASE_URL="$(BASE_URL)" $(PYTHON) scripts/idempotency-race.py
|
||||
|
||||
test-attachments:
|
||||
BASE_URL="$(BASE_URL)" $(PYTHON) scripts/attachments-check.py
|
||||
|
||||
test-contract:
|
||||
BASE_URL="$(BASE_URL)" $(PYTHON) scripts/contract-audit.py
|
||||
|
||||
test-memory:
|
||||
BASE_URL="$(BASE_URL)" $(PYTHON) scripts/upload-load.py --concurrency 10 --output evidence/upload-memory.json
|
||||
|
||||
test-cost:
|
||||
$(PYTHON) scripts/track2-cost.py
|
||||
|
||||
test-reconcile:
|
||||
scripts/reconcile.sh -grace 0s
|
||||
|
||||
test-go:
|
||||
@TEST_DATABASE_URL="$(TEST_DATABASE_URL)" TEST_REDIS_URL="$(TEST_REDIS_URL)" go test -count=1 -v ./...
|
||||
|
||||
|
||||
@@ -1,37 +1,183 @@
|
||||
# Отчёт — лабораторная 4
|
||||
|
||||
- Автор, группа: TODO
|
||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
||||
- Commit/PR: TODO после публикации
|
||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
||||
- Автор, группа: Спесивцев Даниил, БПИ2301
|
||||
- Целевая оценка: 5, трек 2 — согласованность двух хранилищ
|
||||
- Commit/PR: commit 60e8f4474b9f635acbbc4920ff04c7901431acda
|
||||
- Использованные источники, библиотеки и помощь ИИ: Go 1.27, стандартная библиотека и перечисленные ниже модули; S3 API и MinIO-совместимая семантика — по документации AWS и RustFS. При подготовке решения и текста отчёта использовались LLM; все команды и результаты в отчёте воспроизведены на локальном стенде.
|
||||
|
||||
Библиотеки и компоненты:
|
||||
|
||||
| Компонент | Версия | Назначение |
|
||||
| --- | --- | --- |
|
||||
| go-chi/chi | v5.3.2 | HTTP-роутинг и middleware |
|
||||
| jackc/pgx | v5.11.0 | PostgreSQL, пул `pgxpool`, транзакции |
|
||||
| redis/go-redis | v9.23.0 | общие сессии |
|
||||
| golang.org/x/crypto | v0.58.0 | Argon2id |
|
||||
| minio/minio-go | v7.4.1 | S3-клиент (API и операторские утилиты) |
|
||||
| minio/madmin-go | v3.0.110 | идемпотентный bootstrap пользователя и политики |
|
||||
| migrate/migrate | v4.20.1 (образ) | однократные миграции |
|
||||
| HAProxy | 3.4.7-alpine (образ) | единая точка входа |
|
||||
|
||||
## Запуск из чистого clone
|
||||
|
||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
||||
Стенд: i5-11400H (12 потоков), 15 GiB RAM, Linux 7.3.5, podman 6.2.0 + podman-compose 1.6.0 (`evidence/env.txt`). Образы закреплены тегами: `rustfs/rustfs:1.0.6`, `postgres:18.6-alpine3.24`, `redis:8.10.3-alpine3.23`, `haproxy:3.4.7-alpine`, `migrate/migrate:v4.20.1`, сборка API — `golang:1.27.1-alpine3.24` → `alpine:3.24.1`.
|
||||
|
||||
```sh
|
||||
scripts/init-secrets.sh
|
||||
make up
|
||||
until curl -sf http://localhost:8080/health/ready; do sleep 1; done
|
||||
|
||||
make check
|
||||
make validate PYTHON=.venv/bin/python
|
||||
make test
|
||||
make test-attachments
|
||||
make test-contract
|
||||
make test-memory
|
||||
make test-cost
|
||||
make test-reconcile
|
||||
make test-go
|
||||
make test-idempotency
|
||||
make test-persistence ACTION_SCRIPT=scripts/restart.sh
|
||||
make test-failover ACTION_SCRIPT=scripts/stop-one.sh
|
||||
|
||||
scripts/reconcile.sh # dry-run, grace 1h
|
||||
scripts/reconcile.sh -grace 0s -dry-run=false
|
||||
|
||||
make down # остановка, volumes сохраняются
|
||||
podman-compose down -v # намеренное удаление учебных данных
|
||||
```
|
||||
|
||||
Движок выбирается так же, как в lab3: `ENGINE=podman`/`COMPOSE=podman-compose` при наличии podman, иначе docker. `compose.yaml` проходит `docker compose config --quiet` и `podman-compose config`.
|
||||
|
||||
`scripts/init-secrets.sh` создаёт локальные файлы секретов, повторный запуск их не перезаписывает (проверено хэшами, `evidence/credentials.txt`). Bootstrap S3 (`s3-init`) идемпотентен и запускается один раз перед API.
|
||||
|
||||
## Архитектура и решения
|
||||
|
||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab3, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
||||
```text
|
||||
client → lb (HAProxy :8080) ──edge──▶ api ×2 ──backend──▶ postgres (метаданные, источник истины)
|
||||
├────────▶ rustfs (байты, приватный bucket)
|
||||
└────────▶ redis (сессии)
|
||||
|
||||
migrate (one-shot) ─▶ postgres
|
||||
s3-init (one-shot) ─▶ rustfs: bucket + policy + app user
|
||||
reconcile (операторский запуск) ─▶ rustfs + postgres
|
||||
```
|
||||
|
||||
| Роль | Service | Образ | Health/readiness | Volume | Сеть / порты | Credentials |
|
||||
| --- | --- | --- | --- | --- | --- | --- |
|
||||
| LB | `lb` | `haproxy:3.4.7-alpine` | активная проверка `/health/ready` | — | edge, публикует `8080` | нет |
|
||||
| API | `api` ×2 | `localhost/messenger-api:lab4` | `/health/live`; `/health/ready` = PostgreSQL + Redis + S3 + не draining | — | edge + backend, порт не публикуется | `database_url`, `cache_url`, app-креды S3 |
|
||||
| БД | `postgres` | `postgres:18.6-alpine3.24` | `pg_isready` | `pgdata` | backend, `127.0.0.1:55432` для `make test-go` | `postgres_password` |
|
||||
| Сессии | `redis` | `redis:8.10.3-alpine3.23` | `redis-cli ping` | `redisdata` (AOF) | backend, `127.0.0.1:56379` | нет |
|
||||
| S3 | `s3` | `rustfs/rustfs:1.0.6` | `GET /health` | `s3data` | backend, порт 9000 не публикуется, консоль отключена | root-креды |
|
||||
| Bootstrap | `s3-init` | тот же образ | exit 0 | — | backend | root + app креды |
|
||||
| Миграции | `migrate` | `migrate/migrate:v4.20.1` | exit 0 | SQL read-only | backend | `database_url` |
|
||||
|
||||
Ключевые решения:
|
||||
|
||||
- **Хранилище.** Выбран RustFS: MinIO-совместимый S3, публично доступный образ с обычным semver-тегом (`1.0.6`). MinIO community-образы на момент работы не отдаются ни Docker Hub, ни Quay (проверено), поэтому закреплять их тег было бы невоспроизводимо.
|
||||
- **Разделение данных.** В PostgreSQL только метаданные и связи; байты — в приватном bucket. Ключ объекта `chats/{chat_id}/{attachment_id}` генерирует сервер, `filename` — только для отображения. В таблице `attachments` нет бинарных колонок (`evidence/storage.txt`).
|
||||
- **Порядок записи.** `PUT` в S3 → транзакция PostgreSQL (метаданные + ключ идемпотентности + связи). При ошибке БД выполняется компенсирующий `DELETE`; если и он не удался — объект остаётся и снимается reconciler'ом после grace period.
|
||||
- **Креды.** API работает отдельным S3-пользователем с политикой `ListBucket`/`GetBucketLocation` на bucket и `GetObject`/`PutObject`/`DeleteObject` на объекты. Root-креды смонтированы только в `s3` и `s3-init`; удаление bucket app-кредами запрещено (`Access Denied`).
|
||||
- **Ограничение RAM.** Тело ограничено 10 MiB и читается потоково в буфер; до полной распаковки проверяются формат (`image.DecodeConfig`) и лимит 40 млн пикселей; полное декодирование идёт под семафором (`MAX_CONCURRENT_DECODES=1`), общая параллельность загрузок — `MAX_CONCURRENT_UPLOADS=8`. Один декод 40-мегапиксельного PNG — самая дорогая операция, поэтому верхняя граница памяти детерминирована.
|
||||
- **Идемпотентность сообщений.** Ключ, сообщение и связи `message_attachments` пишутся одной транзакцией; в хэш входят `text` и канонизированный (отсортированный) набор `attachment_ids`, отсутствие поля эквивалентно `[]`.
|
||||
- **Reconciler.** Сверяет объекты S3 (по префиксу) со строками `attachments`. Orphan-объект удаляется только если он старше grace period и на него нет строки метаданных. «Мёртвая» строка метаданных (объект отсутствует) удаляется, только если сообщения на неё не ссылаются; используемая строка сохраняется и попадает в отчёт, потому что восстановить утраченные байты нельзя, а удаление нарушило бы историю.
|
||||
- **Соответствие `.env.example`.** `SESSION_STORE=postgres` в примере, фактически используются Redis-сессии из lab3 (контракт допускает оба варианта); `S3_ACCESS_KEY_ID_FILE`/`S3_SECRET_ACCESS_KEY_FILE` указывают на app-креды; `MAX_UPLOAD_BYTES`/`MAX_IMAGE_PIXELS` передаются в API; `API_REPLICAS` → `scale`; `IDEMPOTENCY_TTL_SECONDS` не используется — ключи хранятся бессрочно.
|
||||
|
||||
Таймауты: PostgreSQL `statement_timeout` 5 s / connect 3 s; readiness-ping 1 s; HTTP-сервер 5/15/15/60 s; HAProxy connect/server/check 2/20/1 s; graceful shutdown: drain 3 s + `Shutdown` 5 s при `stop_grace_period: 10s`.
|
||||
|
||||
## Доказательства критериев
|
||||
|
||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
||||
| Критерий | Команда/сценарий | Наблюдаемый результат | Файл |
|
||||
| --- | --- | --- | --- |
|
||||
| Публичный smoke | make test | TODO | TODO |
|
||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
||||
|
||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
||||
| Публичный smoke | `make test` | PASS | `evidence/smoke.txt` |
|
||||
| Контракт вложений и сообщений | `make test-attachments` | 23/23 проверки | `evidence/negative.txt` |
|
||||
| Аудит краевых случаев контракта | `make test-contract` | 30/30 проверки | `evidence/contract-audit.txt` |
|
||||
| Полная OpenAPI-валидация | `make validate PYTHON=.venv/bin/python` | PASS | вывод команды |
|
||||
| Собственные Go-тесты | `make test-go` | 26 PASS | `evidence/test-go.txt` |
|
||||
| ≥2 реплики, одна точка входа, `X-Instance-Id` | `make up`, 20 запросов через LB | 10/10 на каждую реплику, публикует только `lb` | `evidence/instances.txt` |
|
||||
| S3 в стенде, приватный bucket, постоянный volume | анонимный GET и listing | 403, порт наружу не публикуется | `evidence/private-bucket.txt` |
|
||||
| Метаданные в PostgreSQL, байты в S3 | `psql`, листинг volume | в таблице нет бинарных колонок, объект лежит в `s3data` | `evidence/storage.txt` |
|
||||
| `201 ready` после S3 и БД; скачивание байт-в-байт | `make test` | SHA-256 и размер совпадают | `evidence/smoke.txt` |
|
||||
| Доступ только участникам (404 постороннему) | `make test`, `make test-attachments` | 404 для metadata и content | `evidence/smoke.txt`, `evidence/negative.txt` |
|
||||
| Ограничения 10 MiB / 40 млн пикселей / MIME / filename | `make test-attachments` | 413, 415, 400; PNG ровно 40 000 000 px принят | `evidence/negative.txt` |
|
||||
| Ошибки пользователя не превращаются в 500 | `make test-attachments` | только 400/404/409/413/415 | `evidence/negative.txt` |
|
||||
| Upload → остановка реплики → download через другую | `make test-failover` ×2 | 0 ошибок, восстановление 3.19–3.32 s | `evidence/failover-1.txt`, `evidence/failover-2.txt` |
|
||||
| Restart с volumes сохраняет метаданные и оригинал | `make test-persistence` | PASS, контейнеры перезапущены, volumes те же | `evidence/persistence.txt` |
|
||||
| Идемпотентность сообщений через разные реплики | `make test-idempotency` | 20 запросов → одно сообщение, обе реплики | `evidence/idempotency-race.txt` |
|
||||
| Миграции отдельным контейнером | `make up` | версия 3, `dirty=f`, повторный запуск — `no change` | `evidence/migrate.txt` |
|
||||
| Лимиты и секреты по ролям | `inspect`, `ls /run/secrets` | лимиты у всех сервисов, секреты только у нужных ролей | `evidence/limits.txt` |
|
||||
| Недоступность S3 | `/health/live` 200, `/health/ready` 503, клиент через LB получает 503 | `evidence/s3-outage.txt` |
|
||||
| Ограничение памяти, ≥10 параллельных загрузок | `make test-memory` | пик 333 МБ при лимите 512 МБ, 10/10 успешно | `evidence/memory.txt`, `evidence/upload-memory.json` |
|
||||
| Отказ между S3 и БД не оставляет объектов | инъекция отказа вставки + `reconcile` | 503, число объектов не изменилось | `evidence/compensation.txt` |
|
||||
| Идемпотентный bootstrap | `s3-init` дважды | одинаковый результат | `evidence/credentials.txt` |
|
||||
| Ограниченные креды API | удаление bucket app-кредами | `Access Denied` | `evidence/credentials.txt` |
|
||||
| Трек 2: reconciler, инъекции с обеих сторон | `scripts/reconcile.sh`, `scripts/s3tool.sh` | детекция и безопасный ремонт | `evidence/track2-injection.txt` |
|
||||
| Трек 2: dry-run и повторный запуск | `-dry-run`, повтор `-dry-run=false` | второй прогон ничего не меняет | `evidence/reconcile.txt`, `evidence/track2-injection.txt` |
|
||||
| Трек 2: свежие и используемые не удаляются | apply с grace 1h | свежий объект и используемое вложение целы | `evidence/track2-injection.txt` |
|
||||
| Трек 2: стоимость обхода | `make test-cost` | 0.033–0.09 мс/объект, 1 LIST-страница на 1000 | `evidence/track2-cost.txt` |
|
||||
|
||||
## Эксперименты и отказы
|
||||
|
||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
||||
### Память при загрузках
|
||||
|
||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
||||
Гипотеза: лимиты тела и пикселей вместе с семафором декодирования не дают одной загрузке занять неограниченную память.
|
||||
|
||||
Методика: 10 одновременных загрузок PNG 8000×5000 (40 000 000 пикселей, 126 609 байт) на реплику, лимит API 512 MiB, `MAX_CONCURRENT_UPLOADS=8`, `MAX_CONCURRENT_DECODES=1`; пик снимался через `/sys/fs/cgroup/memory.peak` после пересоздания реплик.
|
||||
|
||||
Результат: 10/10 успешно, 1.01 s, p50 613 мс, p95 1040 мс, пик **333 МБ** на реплику (65 % лимита). При `MAX_CONCURRENT_DECODES=2` пик упирался в лимит 512 МБ, поэтому значение по умолчанию — 1.
|
||||
|
||||
### Стоимость обхода (трек 2)
|
||||
|
||||
| Объектов | Скан | S3-запросов (LIST) | DB-запросов | мс/объект | Ремонт (удаление) |
|
||||
| --- | --- | --- | --- | --- | --- |
|
||||
| 100 | 9 мс | 1 | 2 | 0.09 | 1.3 s (100 DELETE) |
|
||||
| 1000 | 34 мс | 1 | 2 | 0.034 | 12.2 s (1000 DELETE) |
|
||||
| 5000 | 166 мс | 5 | 2 | 0.033 | 69.9 s (5000 DELETE) |
|
||||
|
||||
Скан линеен по числу объектов и страниц LIST (1000 на страницу), число обращений к БД постоянно. Ремонт доминируется последовательными DELETE — это осознанное упрощение, возможное улучшение — пакетное или параллельное удаление.
|
||||
|
||||
### Инъекции и восстановление
|
||||
|
||||
| Воздействие | Ожидание | Наблюдение |
|
||||
| --- | --- | --- |
|
||||
| Отказ вставки метаданных после успешного `PUT` | объект удаляется компенсацией, ответ 503 | число объектов не изменилось, после снятия инъекции загрузка снова 201 |
|
||||
| Объект без метаданных (свежий) | не удаляется в пределах grace | при `-grace 1h` `deleted_objects=0`, объект на месте |
|
||||
| Объект без метаданных (старше grace) | удаляется | `-grace 0s` → `deleted_objects=1`, повторный прогон — 0 |
|
||||
| Метаданные без объекта, сообщение не ссылается | строка удаляется | `deleted_metadata=1` |
|
||||
| Метаданные без объекта, сообщение ссылается | строка сохраняется, попадает в отчёт | `retained_missing=1`, строка в БД остаётся |
|
||||
| Остановка реплики под нагрузкой | без потери подтверждённых данных | 85–100 успешных записей, 0 ошибок |
|
||||
| Перезапуск всех сервисов | данные и токен сохраняются | PASS, volumes не тронуты |
|
||||
| Недоступность S3 | `/health/live` 200, `/health/ready` 503, восстановление | 200 / 503 / 200 |
|
||||
|
||||
## Аудит контракта и спорные места
|
||||
|
||||
Краевые случаи проверяются отдельным сьютом `make test-contract` (`scripts/contract-audit.py`, 30 проверок, `evidence/contract-audit.txt`). Аудит выявил три места, где реализация была мягче контракта; они исправлены:
|
||||
|
||||
| Место | Было | Стало | Основание |
|
||||
| --- | --- | --- | --- |
|
||||
| TODO | TODO | TODO | TODO |
|
||||
| `attachment_ids: null` | принималось как отсутствие поля | `400` | схема: поле — массив; «неверные типы — 400» |
|
||||
| `?variant=` (пустое значение) | считалось `original` | `400` | «в lab4 допустим только `original`»; отсутствие параметра — отдельный случай |
|
||||
| `filename` из одних пробелов | принималось | `400` | «обязательные строки не состоят только из пробельных символов» |
|
||||
|
||||
Решения по остальным неоднозначностям:
|
||||
|
||||
- `Content-Type` с параметрами (`image/png; charset=utf-8`) принимается: сравнивается медиатип, параметры не влияют.
|
||||
- Порядок проверок загрузки: `Content-Type` (415) → размер тела (413) → MIME по содержимому (415) → лимит пикселей (413) → целостность (415). Если нарушено несколько правил, возвращается первая сработавшая проверка; контракт приоритет не задаёт.
|
||||
- Пустой или пробельный `text` разрешён только с вложениями (`anyOf` в схеме); без вложений — `400`.
|
||||
- `variant` проверяется до проверки членства: посторонний с неизвестным вариантом получает `400`, а не `404`. Существование чужого объекта это не раскрывает: для любого `id` ответ одинаков.
|
||||
- Ключи идемпотентности хранятся бессрочно; контракт требует лишь повтор «в течение 24 часов».
|
||||
- Неподдерживаемый HTTP-метод на известном пути даёт `405` с кодом `invalid_request` — контракт этот код не описывает, поведение выбрано явно.
|
||||
- `429 rate_limited` есть в перечислении кодов, но rate limiting не входит в обязательный минимум и не реализован.
|
||||
- Cursor привязан к ресурсу: курсор истории чата на списке чатов и наоборот даёт `400` (проверяется аудитом).
|
||||
- S3 включён в readiness. Пока объектное хранилище недоступно, реплики выводятся из балансировщика и клиент получает `503` на любой запрос, включая текстовые сообщения; так гарантируется, что обязательный синхронный путь (сообщения с вложениями) не обслуживается частично. `/health/live` при этом остаётся `200` (`evidence/s3-outage.txt`).
|
||||
- Валидация выполняется до проверки доступа только там, где это не раскрывает существование объекта; порядок «сначала аутентификация, затем ресурс» в middleware сохранён.
|
||||
|
||||
## Ограничения и следующие шаги
|
||||
|
||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|
||||
- Один Docker-host: отказ host, единственного PostgreSQL и единственного LB не выдерживается. Проверен только отказ одной API-реплики.
|
||||
- Reconciler — операторская утилита, запускается вручную; расписания и фонового worker'а нет (это уже тема lab5). Удаление объектов последовательное.
|
||||
- Если объект утрачен, но на него ссылается сообщение, автоматический ремонт невозможен: байты не восстановить, а удаление строки нарушило бы историю. Такие случаи остаются в отчёте reconciler'а и требуют решения оператора. В демонстрационном наборе данных есть одно такое вложение — оно появилось в результате учения и остаётся видимым в `missing`.
|
||||
- Тело загрузки буферизуется в память: верхняя граница — `MAX_CONCURRENT_UPLOADS × MAX_UPLOAD_BYTES` (по умолчанию 80 MiB) плюс один декод.
|
||||
- Нет ограничения частоты запросов, TLS/mTLS, асинхронной обработки и вариантов изображений — это следующие лабораторные.
|
||||
- `filename` хранится как отображаемое имя и не используется в ключах; санитизация ограничена контролем недопустимых символов.
|
||||
|
||||
@@ -0,0 +1,216 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
"uuid"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"messenger/internal/config"
|
||||
"messenger/internal/reconcile"
|
||||
"messenger/internal/repository/postgres"
|
||||
"messenger/internal/repository/s3"
|
||||
)
|
||||
|
||||
type countingTransport struct {
|
||||
base http.RoundTripper
|
||||
count atomic.Int64
|
||||
}
|
||||
|
||||
func (t *countingTransport) RoundTrip(request *http.Request) (*http.Response, error) {
|
||||
t.count.Add(1)
|
||||
return t.base.RoundTrip(request)
|
||||
}
|
||||
|
||||
func main() {
|
||||
dryRun := flag.Bool("dry-run", true, "report only, do not delete or repair anything")
|
||||
grace := flag.Duration("grace", time.Hour, "minimum object age before it can be deleted")
|
||||
prefix := flag.String("prefix", "chats/", "object key prefix to scan")
|
||||
limit := flag.Int("limit", 10000, "maximum number of objects to scan")
|
||||
repairMissing := flag.Bool("repair-missing", true, "delete metadata rows whose object is gone and that no message references")
|
||||
flag.Parse()
|
||||
|
||||
if err := run(*dryRun, *grace, *prefix, *limit, *repairMissing); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "reconcile failed:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run(dryRun bool, grace time.Duration, prefix string, limit int, repairMissing bool) error {
|
||||
if grace < 0 {
|
||||
return fmt.Errorf("grace must not be negative")
|
||||
}
|
||||
if limit <= 0 {
|
||||
return fmt.Errorf("limit must be positive")
|
||||
}
|
||||
started := time.Now()
|
||||
|
||||
endpoint := getenv("S3_ENDPOINT", "http://s3:9000")
|
||||
region := getenv("S3_REGION", "us-east-1")
|
||||
bucket := getenv("S3_BUCKET", "messenger-attachments")
|
||||
accessKeyID, err := config.ReadSecret("S3_ACCESS_KEY_ID_FILE", "/run/secrets/s3_access_key_id")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
secretAccessKey, err := config.ReadSecret("S3_SECRET_ACCESS_KEY_FILE", "/run/secrets/s3_secret_access_key")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
databaseURL, err := config.ReadSecret("DATABASE_URL_FILE", "/run/secrets/database_url")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
|
||||
defer cancel()
|
||||
|
||||
transport := &countingTransport{base: http.DefaultTransport}
|
||||
store, err := s3.NewWithTransport(endpoint, region, accessKeyID, secretAccessKey, bucket, transport)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
scanned, truncated, err := store.List(ctx, prefix, limit)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pool, err := postgres.NewPool(ctx, databaseURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
rows, err := loadMetadata(ctx, pool)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
referenced, err := loadReferences(ctx, pool)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
orphans, missing := reconcile.Plan(toObjects(scanned), rows, referenced, prefix, time.Now(), grace)
|
||||
if truncated {
|
||||
missing = nil
|
||||
}
|
||||
|
||||
report := reconcile.Report{
|
||||
Prefix: prefix,
|
||||
GraceSeconds: int64(grace.Seconds()),
|
||||
DryRun: dryRun,
|
||||
ScannedObjects: len(scanned),
|
||||
ScannedRows: len(rows),
|
||||
Truncated: truncated,
|
||||
Orphans: orphans,
|
||||
Missing: missing,
|
||||
}
|
||||
dbQueries := 2
|
||||
|
||||
if !dryRun {
|
||||
for _, orphan := range orphans {
|
||||
if err := store.Delete(ctx, orphan.Key); err != nil {
|
||||
report.DeleteFailures++
|
||||
continue
|
||||
}
|
||||
report.DeletedObjects++
|
||||
}
|
||||
if repairMissing {
|
||||
for _, item := range missing {
|
||||
if item.Referenced {
|
||||
report.RetainedMissing++
|
||||
continue
|
||||
}
|
||||
id, err := uuid.Parse(item.ID)
|
||||
if err != nil {
|
||||
report.DeleteFailures++
|
||||
continue
|
||||
}
|
||||
tag, err := pool.Exec(ctx, `DELETE FROM attachments WHERE id = $1`, id)
|
||||
dbQueries++
|
||||
if err != nil {
|
||||
report.DeleteFailures++
|
||||
continue
|
||||
}
|
||||
report.DeletedMetadata += int(tag.RowsAffected())
|
||||
}
|
||||
} else {
|
||||
for _, item := range missing {
|
||||
if item.Referenced {
|
||||
report.RetainedMissing++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
report.S3Requests = transport.count.Load()
|
||||
report.DBQueries = dbQueries
|
||||
report.DurationMS = time.Since(started).Milliseconds()
|
||||
if report.DurationMS > 0 {
|
||||
report.ObjectsPerSecond = float64(report.ScannedObjects) / (float64(report.DurationMS) / 1000)
|
||||
}
|
||||
|
||||
encoder := json.NewEncoder(os.Stdout)
|
||||
encoder.SetIndent("", " ")
|
||||
if err := encoder.Encode(report); err != nil {
|
||||
return err
|
||||
}
|
||||
if report.DeleteFailures > 0 {
|
||||
return fmt.Errorf("%d repair operations failed", report.DeleteFailures)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func loadMetadata(ctx context.Context, pool *pgxpool.Pool) ([]reconcile.Metadata, error) {
|
||||
rows, err := pool.Query(ctx, `SELECT id, object_key, chat_id, created_at FROM attachments`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pgx.CollectRows(rows, func(row pgx.CollectableRow) (reconcile.Metadata, error) {
|
||||
var metadata reconcile.Metadata
|
||||
err := row.Scan(&metadata.ID, &metadata.ObjectKey, &metadata.ChatID, &metadata.CreatedAt)
|
||||
return metadata, err
|
||||
})
|
||||
}
|
||||
|
||||
func loadReferences(ctx context.Context, pool *pgxpool.Pool) (map[uuid.UUID]bool, error) {
|
||||
rows, err := pool.Query(ctx, `SELECT DISTINCT attachment_id FROM message_attachments`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ids, err := pgx.CollectRows(rows, func(row pgx.CollectableRow) (uuid.UUID, error) {
|
||||
var id uuid.UUID
|
||||
err := row.Scan(&id)
|
||||
return id, err
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
referenced := make(map[uuid.UUID]bool, len(ids))
|
||||
for _, id := range ids {
|
||||
referenced[id] = true
|
||||
}
|
||||
return referenced, nil
|
||||
}
|
||||
|
||||
func toObjects(scanned []s3.ObjectInfo) []reconcile.Object {
|
||||
objects := make([]reconcile.Object, len(scanned))
|
||||
for i, info := range scanned {
|
||||
objects[i] = reconcile.Object{Key: info.Key, Size: info.Size, LastModified: info.LastModified}
|
||||
}
|
||||
return objects
|
||||
}
|
||||
|
||||
func getenv(key, def string) string {
|
||||
if value := os.Getenv(key); value != "" {
|
||||
return value
|
||||
}
|
||||
return def
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/minio/madmin-go/v3"
|
||||
|
||||
"messenger/internal/config"
|
||||
"messenger/internal/repository/s3"
|
||||
)
|
||||
|
||||
const policyName = "messenger-app"
|
||||
|
||||
func main() {
|
||||
if err := run(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "s3-init failed:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run() error {
|
||||
endpoint := getenv("S3_ENDPOINT", "http://s3:9000")
|
||||
region := getenv("S3_REGION", "us-east-1")
|
||||
bucket := getenv("S3_BUCKET", "messenger-attachments")
|
||||
rootAccessKey, err := config.ReadSecret("S3_ROOT_ACCESS_KEY_ID_FILE", "/run/secrets/s3_root_access_key")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rootSecretKey, err := config.ReadSecret("S3_ROOT_SECRET_ACCESS_KEY_FILE", "/run/secrets/s3_root_secret_key")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
appAccessKey, err := config.ReadSecret("S3_ACCESS_KEY_ID_FILE", "/run/secrets/s3_access_key_id")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
appSecretKey, err := config.ReadSecret("S3_SECRET_ACCESS_KEY_FILE", "/run/secrets/s3_secret_access_key")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
||||
defer cancel()
|
||||
|
||||
store, err := s3.New(endpoint, region, rootAccessKey, rootSecretKey, bucket)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := store.EnsureBucket(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("bucket ready:", bucket)
|
||||
|
||||
policy, err := policyDocument(bucket)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
host, secure := s3.EndpointHost(endpoint)
|
||||
admin, err := madmin.New(host, rootAccessKey, rootSecretKey, secure)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := retry(ctx, func(ctx context.Context) error { return admin.AddCannedPolicy(ctx, policyName, policy) }); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("policy ready:", policyName)
|
||||
if err := retry(ctx, func(ctx context.Context) error {
|
||||
return admin.SetUser(ctx, appAccessKey, appSecretKey, madmin.AccountEnabled)
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("application credentials ready")
|
||||
if err := retry(ctx, func(ctx context.Context) error {
|
||||
_, err := admin.AttachPolicy(ctx, madmin.PolicyAssociationReq{Policies: []string{policyName}, User: appAccessKey})
|
||||
return err
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("policy attached to application credentials")
|
||||
return nil
|
||||
}
|
||||
|
||||
func retry(ctx context.Context, fn func(context.Context) error) error {
|
||||
var err error
|
||||
for attempt := 1; attempt <= 12; attempt++ {
|
||||
if err = fn(ctx); err == nil {
|
||||
return nil
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return err
|
||||
case <-time.After(time.Duration(attempt) * time.Second):
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func policyDocument(bucket string) ([]byte, error) {
|
||||
document := map[string]any{
|
||||
"Version": "2012-10-17",
|
||||
"Statement": []map[string]any{
|
||||
{
|
||||
"Effect": "Allow",
|
||||
"Action": []string{"s3:ListBucket", "s3:GetBucketLocation"},
|
||||
"Resource": []string{"arn:aws:s3:::" + bucket},
|
||||
},
|
||||
{
|
||||
"Effect": "Allow",
|
||||
"Action": []string{"s3:GetObject", "s3:PutObject", "s3:DeleteObject"},
|
||||
"Resource": []string{"arn:aws:s3:::" + bucket + "/*"},
|
||||
},
|
||||
},
|
||||
}
|
||||
return json.Marshal(document)
|
||||
}
|
||||
|
||||
func getenv(key, def string) string {
|
||||
if value := os.Getenv(key); value != "" {
|
||||
return value
|
||||
}
|
||||
return def
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"messenger/internal/config"
|
||||
"messenger/internal/repository/s3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
usage()
|
||||
}
|
||||
if err := run(os.Args[1], os.Args[2:]); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "s3tool failed:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run(command string, args []string) error {
|
||||
store, err := newStore()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
||||
defer cancel()
|
||||
|
||||
switch command {
|
||||
case "put":
|
||||
if len(args) != 2 {
|
||||
usage()
|
||||
}
|
||||
content, err := readContent(args[1])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := store.Put(ctx, args[0], bytes.NewReader(content), int64(len(content)), "application/octet-stream"); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("put %s (%d bytes)\n", args[0], len(content))
|
||||
case "delete":
|
||||
if len(args) != 1 {
|
||||
usage()
|
||||
}
|
||||
if err := store.Delete(ctx, args[0]); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("deleted %s\n", args[0])
|
||||
case "list":
|
||||
prefix := ""
|
||||
if len(args) > 0 {
|
||||
prefix = args[0]
|
||||
}
|
||||
objects, _, err := store.List(ctx, prefix, 1000)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, object := range objects {
|
||||
fmt.Printf("%s\t%d\t%s\n", object.Key, object.Size, object.LastModified.UTC().Format(time.RFC3339))
|
||||
}
|
||||
case "buckets":
|
||||
buckets, err := store.Buckets(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, bucket := range buckets {
|
||||
fmt.Println(bucket)
|
||||
}
|
||||
case "remove-bucket":
|
||||
if len(args) != 1 {
|
||||
usage()
|
||||
}
|
||||
if err := store.RemoveBucket(ctx, args[0]); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("removed %s\n", args[0])
|
||||
case "seed":
|
||||
if len(args) != 3 {
|
||||
usage()
|
||||
}
|
||||
count, err := strconv.Atoi(args[1])
|
||||
if err != nil || count <= 0 {
|
||||
return fmt.Errorf("count must be a positive integer")
|
||||
}
|
||||
size, err := strconv.Atoi(args[2])
|
||||
if err != nil || size <= 0 {
|
||||
return fmt.Errorf("size must be a positive integer")
|
||||
}
|
||||
if err := seed(ctx, store, args[0], count, size); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("seeded %d objects under %s\n", count, args[0])
|
||||
default:
|
||||
usage()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func readContent(source string) ([]byte, error) {
|
||||
if source == "-" {
|
||||
return io.ReadAll(os.Stdin)
|
||||
}
|
||||
return os.ReadFile(source)
|
||||
}
|
||||
|
||||
func newStore() (*s3.Store, error) {
|
||||
accessKeyID, err := config.ReadSecret("S3_ACCESS_KEY_ID_FILE", "/run/secrets/s3_access_key_id")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
secretAccessKey, err := config.ReadSecret("S3_SECRET_ACCESS_KEY_FILE", "/run/secrets/s3_secret_access_key")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s3.New(getenv("S3_ENDPOINT", "http://s3:9000"), getenv("S3_REGION", "us-east-1"),
|
||||
accessKeyID, secretAccessKey, getenv("S3_BUCKET", "messenger-attachments"))
|
||||
}
|
||||
|
||||
func seed(ctx context.Context, store *s3.Store, prefix string, count, size int) error {
|
||||
content := make([]byte, size)
|
||||
workers := 8
|
||||
work := make(chan int)
|
||||
errors := make(chan error, workers)
|
||||
var group sync.WaitGroup
|
||||
for i := 0; i < workers; i++ {
|
||||
group.Add(1)
|
||||
go func() {
|
||||
defer group.Done()
|
||||
for index := range work {
|
||||
key := fmt.Sprintf("%s/%06d", prefix, index)
|
||||
if err := store.Put(ctx, key, bytes.NewReader(content), int64(size), "application/octet-stream"); err != nil {
|
||||
errors <- err
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
for index := 0; index < count; index++ {
|
||||
work <- index
|
||||
}
|
||||
close(work)
|
||||
group.Wait()
|
||||
close(errors)
|
||||
return <-errors
|
||||
}
|
||||
|
||||
func usage() {
|
||||
fmt.Fprintln(os.Stderr, "usage: s3tool put <key> <file> | delete <key> | list [prefix] | buckets | remove-bucket <name> | seed <prefix> <count> <bytes>")
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
func getenv(key, def string) string {
|
||||
if value := os.Getenv(key); value != "" {
|
||||
return value
|
||||
}
|
||||
return def
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"messenger/internal/config"
|
||||
"messenger/internal/handler"
|
||||
"messenger/internal/repository/postgres"
|
||||
"messenger/internal/repository/redis"
|
||||
"messenger/internal/repository/s3"
|
||||
"messenger/internal/router"
|
||||
"messenger/internal/service"
|
||||
)
|
||||
|
||||
const (
|
||||
drainDelay = 3 * time.Second
|
||||
shutdownTimeout = 5 * time.Second
|
||||
)
|
||||
|
||||
func main() {
|
||||
log := slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
||||
if err := run(log); err != nil {
|
||||
log.Error("server failed", "err", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run(log *slog.Logger) error {
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
instanceID, err := os.Hostname()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pool, err := postgres.NewPool(context.Background(), cfg.DatabaseURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
store := postgres.New(pool)
|
||||
|
||||
redisClient, err := redis.NewClient(cfg.CacheURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer redisClient.Close()
|
||||
sessions := redis.NewSessionStore(redisClient)
|
||||
|
||||
users := service.NewUserService(store)
|
||||
auth := service.NewAuthService(store, sessions, cfg.SessionTTL)
|
||||
chats := service.NewChatService(store)
|
||||
messages := service.NewMessageService(chats, store)
|
||||
|
||||
objects, err := s3.New(cfg.S3Endpoint, cfg.S3Region, cfg.S3AccessKeyID, cfg.S3SecretAccessKey, cfg.S3Bucket)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
attachments := service.NewAttachmentService(chats, store, objects, cfg.MaxUploadBytes, cfg.MaxImagePixels,
|
||||
cfg.MaxConcurrentUploads, cfg.MaxConcurrentDecodes)
|
||||
|
||||
deps := []handler.Pinger{store.Ping, sessions.Ping, objects.Ping}
|
||||
|
||||
h := handler.New(users, auth, chats, messages, attachments, deps, log)
|
||||
srv := &http.Server{
|
||||
Addr: cfg.Addr(),
|
||||
Handler: router.New(h, instanceID),
|
||||
ReadHeaderTimeout: 5 * time.Second,
|
||||
ReadTimeout: 15 * time.Second,
|
||||
WriteTimeout: 15 * time.Second,
|
||||
IdleTimeout: 60 * time.Second,
|
||||
}
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, os.Interrupt)
|
||||
defer stop()
|
||||
|
||||
errCh := make(chan error, 1)
|
||||
go func() {
|
||||
log.Info("starting server", "addr", srv.Addr, "instance_id", instanceID, "env", cfg.Env, "session_ttl", cfg.SessionTTL.String())
|
||||
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
errCh <- err
|
||||
}
|
||||
}()
|
||||
|
||||
select {
|
||||
case err := <-errCh:
|
||||
return err
|
||||
case <-ctx.Done():
|
||||
}
|
||||
log.Info("shutdown signal received, draining", "drain", drainDelay.String(), "timeout", shutdownTimeout.String())
|
||||
h.StartDraining()
|
||||
srv.SetKeepAlivesEnabled(false)
|
||||
time.Sleep(drainDelay)
|
||||
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
|
||||
defer cancel()
|
||||
if err := srv.Shutdown(shutdownCtx); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Info("server stopped")
|
||||
return nil
|
||||
}
|
||||
+216
@@ -0,0 +1,216 @@
|
||||
name: messenger-lab4
|
||||
|
||||
services:
|
||||
postgres:
|
||||
image: docker.io/library/postgres:18.6-alpine3.24
|
||||
environment:
|
||||
POSTGRES_USER: messenger
|
||||
POSTGRES_DB: messenger
|
||||
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
|
||||
secrets:
|
||||
- postgres_password
|
||||
volumes:
|
||||
- pgdata:/var/lib/postgresql
|
||||
networks:
|
||||
- backend
|
||||
ports:
|
||||
- "127.0.0.1:55432:5432"
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"]
|
||||
interval: 2s
|
||||
timeout: 3s
|
||||
retries: 30
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "1.0"
|
||||
memory: 512M
|
||||
restart: unless-stopped
|
||||
|
||||
redis:
|
||||
image: docker.io/library/redis:8.10.3-alpine3.23
|
||||
command: ["redis-server", "--appendonly", "yes", "--appendfsync", "everysec"]
|
||||
volumes:
|
||||
- redisdata:/data
|
||||
networks:
|
||||
- backend
|
||||
ports:
|
||||
- "127.0.0.1:56379:6379"
|
||||
healthcheck:
|
||||
test: ["CMD", "redis-cli", "ping"]
|
||||
interval: 2s
|
||||
timeout: 3s
|
||||
retries: 30
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.5"
|
||||
memory: 128M
|
||||
restart: unless-stopped
|
||||
|
||||
s3:
|
||||
image: docker.io/rustfs/rustfs:1.0.6
|
||||
environment:
|
||||
RUSTFS_ACCESS_KEY_FILE: /run/secrets/s3_root_access_key
|
||||
RUSTFS_SECRET_KEY_FILE: /run/secrets/s3_root_secret_key
|
||||
RUSTFS_REGION: ${S3_REGION:-us-east-1}
|
||||
RUSTFS_CONSOLE_ENABLE: "false"
|
||||
RUSTFS_OBS_LOGGER_LEVEL: warn
|
||||
secrets:
|
||||
- s3_root_access_key
|
||||
- s3_root_secret_key
|
||||
volumes:
|
||||
- s3data:/data
|
||||
networks:
|
||||
- backend
|
||||
healthcheck:
|
||||
test: ["CMD", "curl", "-f", "http://127.0.0.1:9000/health"]
|
||||
interval: 2s
|
||||
timeout: 3s
|
||||
retries: 30
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "1.0"
|
||||
memory: 512M
|
||||
restart: unless-stopped
|
||||
|
||||
s3-init:
|
||||
build: .
|
||||
image: localhost/messenger-api:lab4
|
||||
entrypoint: ["/usr/local/bin/s3-init"]
|
||||
environment:
|
||||
S3_ENDPOINT: ${S3_ENDPOINT:-http://s3:9000}
|
||||
S3_REGION: ${S3_REGION:-us-east-1}
|
||||
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
||||
S3_ROOT_ACCESS_KEY_ID_FILE: /run/secrets/s3_root_access_key
|
||||
S3_ROOT_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_root_secret_key
|
||||
S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id
|
||||
S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key
|
||||
secrets:
|
||||
- s3_root_access_key
|
||||
- s3_root_secret_key
|
||||
- s3_access_key_id
|
||||
- s3_secret_access_key
|
||||
networks:
|
||||
- backend
|
||||
depends_on:
|
||||
s3:
|
||||
condition: service_healthy
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.5"
|
||||
memory: 256M
|
||||
restart: "no"
|
||||
|
||||
migrate:
|
||||
image: docker.io/migrate/migrate:v4.20.1
|
||||
entrypoint: ["/bin/sh", "-c"]
|
||||
command: ['migrate -path /migrations -database "$$(cat /run/secrets/database_url)" up']
|
||||
secrets:
|
||||
- database_url
|
||||
volumes:
|
||||
- ./internal/repository/postgres/migrations:/migrations:ro
|
||||
networks:
|
||||
- backend
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.5"
|
||||
memory: 64M
|
||||
restart: "no"
|
||||
|
||||
api:
|
||||
build: .
|
||||
image: localhost/messenger-api:lab4
|
||||
environment:
|
||||
APP_HOST: 0.0.0.0
|
||||
APP_PORT: "8080"
|
||||
APP_ENV: ${APP_ENV:-lab}
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
DATABASE_URL_FILE: /run/secrets/database_url
|
||||
CACHE_URL_FILE: /run/secrets/cache_url
|
||||
S3_ENDPOINT: ${S3_ENDPOINT:-http://s3:9000}
|
||||
S3_REGION: ${S3_REGION:-us-east-1}
|
||||
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
||||
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: ${MAX_UPLOAD_BYTES:-10485760}
|
||||
MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000}
|
||||
MAX_CONCURRENT_UPLOADS: ${MAX_CONCURRENT_UPLOADS:-8}
|
||||
MAX_CONCURRENT_DECODES: ${MAX_CONCURRENT_DECODES:-1}
|
||||
secrets:
|
||||
- database_url
|
||||
- cache_url
|
||||
- s3_access_key_id
|
||||
- s3_secret_access_key
|
||||
networks:
|
||||
- edge
|
||||
- backend
|
||||
healthcheck:
|
||||
test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:8080/health/ready"]
|
||||
interval: 5s
|
||||
timeout: 2s
|
||||
retries: 3
|
||||
start_period: 5s
|
||||
stop_grace_period: 10s
|
||||
scale: ${API_REPLICAS:-2}
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "1.0"
|
||||
memory: 512M
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
redis:
|
||||
condition: service_healthy
|
||||
s3:
|
||||
condition: service_healthy
|
||||
s3-init:
|
||||
condition: service_completed_successfully
|
||||
restart: unless-stopped
|
||||
|
||||
lb:
|
||||
image: docker.io/library/haproxy:3.4.7-alpine
|
||||
volumes:
|
||||
- ./infra/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
|
||||
networks:
|
||||
- edge
|
||||
ports:
|
||||
- "8080:8080"
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.5"
|
||||
memory: 64M
|
||||
restart: unless-stopped
|
||||
|
||||
networks:
|
||||
edge:
|
||||
backend:
|
||||
|
||||
secrets:
|
||||
postgres_password:
|
||||
file: ./secrets/postgres_password
|
||||
database_url:
|
||||
file: ./secrets/database_url
|
||||
cache_url:
|
||||
file: ./secrets/cache_url
|
||||
s3_root_access_key:
|
||||
file: ./secrets/s3_root_access_key
|
||||
s3_root_secret_key:
|
||||
file: ./secrets/s3_root_secret_key
|
||||
s3_access_key_id:
|
||||
file: ./secrets/s3_access_key_id
|
||||
s3_secret_access_key:
|
||||
file: ./secrets/s3_secret_access_key
|
||||
|
||||
volumes:
|
||||
pgdata:
|
||||
redisdata:
|
||||
s3data:
|
||||
@@ -0,0 +1,28 @@
|
||||
2026-11-20T03:51:30+03:00
|
||||
make test-attachments
|
||||
|
||||
BASE_URL="http://localhost:8080" python3 scripts/attachments-check.py
|
||||
ok: upload 201 ready, sha256=99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75, size=111817
|
||||
ok: jpeg upload/download byte-identical; jpeg declared as png -> 415
|
||||
ok: png with 40,000,000 pixels accepted and downloaded byte-identical
|
||||
ok: filename with slash -> 400
|
||||
ok: filename with NUL -> 400
|
||||
ok: empty filename -> 400
|
||||
ok: declared jpeg with png body -> 415
|
||||
ok: truncated png -> 415
|
||||
ok: 10485761 bytes -> 413
|
||||
ok: text/plain -> 415
|
||||
ok: unknown variant -> 400
|
||||
ok: variant=original -> 200
|
||||
ok: unknown attachment id -> 404
|
||||
ok: upload by outsider -> 404
|
||||
ok: attachment from another chat -> 404
|
||||
ok: unknown attachment id in message -> 404
|
||||
ok: duplicate attachment_ids -> 400
|
||||
ok: eleven attachment_ids -> 400
|
||||
ok: empty text without attachments -> 400
|
||||
ok: idempotent replay with same attachment set -> same message
|
||||
ok: absent attachment_ids equals [] for idempotency
|
||||
ok: same key with a different attachment set -> 409
|
||||
ok: history exposes attachment_ids on every message
|
||||
PASS: 23 attachment contract checks
|
||||
@@ -0,0 +1,5 @@
|
||||
2026-11-20T04:38:30+03:00
|
||||
make check
|
||||
|
||||
python3 scripts/check_contract.py
|
||||
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
|
||||
@@ -0,0 +1,79 @@
|
||||
2026-11-20T04:41:27+03:00
|
||||
failure between S3 write and database commit: an object must not stay unaccounted
|
||||
|
||||
$ scripts/reconcile.sh # baseline
|
||||
{
|
||||
"prefix": "chats/",
|
||||
"grace_seconds": 3600,
|
||||
"dry_run": true,
|
||||
"scanned_objects": 17,
|
||||
"scanned_rows": 17,
|
||||
"truncated": false,
|
||||
"orphans": [],
|
||||
"missing": [],
|
||||
"deleted_objects": 0,
|
||||
"deleted_metadata": 0,
|
||||
"retained_missing": 0,
|
||||
"delete_failures": 0,
|
||||
"s3_requests": 1,
|
||||
"db_queries": 2,
|
||||
"duration_ms": 7,
|
||||
"objects_per_second": 2428.5714285714284
|
||||
}
|
||||
|
||||
$ podman exec -i postgres psql < scripts/sql/inject-db-failure.sql
|
||||
CREATE FUNCTION
|
||||
NOTICE: trigger "inject_attachment_insert_failure" for relation "attachments" does not exist, skipping
|
||||
DROP TRIGGER
|
||||
CREATE TRIGGER
|
||||
|
||||
$ scripts/upload-once.py --expect 503 # insert fails after a successful S3 PUT
|
||||
status=503
|
||||
|
||||
$ scripts/reconcile.sh # same object count: compensating DELETE removed the object
|
||||
{
|
||||
"prefix": "chats/",
|
||||
"grace_seconds": 3600,
|
||||
"dry_run": true,
|
||||
"scanned_objects": 17,
|
||||
"scanned_rows": 17,
|
||||
"truncated": false,
|
||||
"orphans": [],
|
||||
"missing": [],
|
||||
"deleted_objects": 0,
|
||||
"deleted_metadata": 0,
|
||||
"retained_missing": 0,
|
||||
"delete_failures": 0,
|
||||
"s3_requests": 1,
|
||||
"db_queries": 2,
|
||||
"duration_ms": 7,
|
||||
"objects_per_second": 2428.5714285714284
|
||||
}
|
||||
|
||||
$ podman exec -i postgres psql < scripts/sql/reset-db-failure.sql
|
||||
DROP TRIGGER
|
||||
DROP FUNCTION
|
||||
|
||||
$ scripts/upload-once.py --expect 201 # service healthy again
|
||||
status=201
|
||||
attachment=7dd89013-32f1-4732-a2fd-529fe2081bb8 size=653
|
||||
|
||||
$ scripts/reconcile.sh # one more accounted object
|
||||
{
|
||||
"prefix": "chats/",
|
||||
"grace_seconds": 3600,
|
||||
"dry_run": true,
|
||||
"scanned_objects": 18,
|
||||
"scanned_rows": 18,
|
||||
"truncated": false,
|
||||
"orphans": [],
|
||||
"missing": [],
|
||||
"deleted_objects": 0,
|
||||
"deleted_metadata": 0,
|
||||
"retained_missing": 0,
|
||||
"delete_failures": 0,
|
||||
"s3_requests": 1,
|
||||
"db_queries": 2,
|
||||
"duration_ms": 7,
|
||||
"objects_per_second": 2571.4285714285716
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
2026-11-20T04:53:36+03:00
|
||||
make test-contract
|
||||
|
||||
BASE_URL="http://localhost:8080" python3 scripts/contract-audit.py
|
||||
ok: explicit attachment_ids=null is 400
|
||||
ok: empty variant is 400
|
||||
ok: whitespace-only filename is 400
|
||||
ok: Content-Type parameters are accepted
|
||||
ok: 41,000,000 pixels is 413
|
||||
ok: text length bounds
|
||||
ok: attachment count bounds
|
||||
ok: duplicate attachment_ids is 400
|
||||
ok: idempotent replay with the same set
|
||||
ok: absent and empty attachment_ids agree
|
||||
ok: attachment from another chat is 404
|
||||
ok: unknown attachment id is 404
|
||||
ok: outsider access is 404
|
||||
ok: upload to unknown chat is 404
|
||||
ok: media errors are 415
|
||||
ok: oversize is 413
|
||||
ok: filename errors are 400
|
||||
ok: chat validation errors are 400
|
||||
ok: pagination errors are 400
|
||||
ok: idempotency errors
|
||||
ok: null request fields are 400
|
||||
ok: more than 100 members is 400
|
||||
ok: user field validation
|
||||
ok: missing Content-Type is 415
|
||||
ok: variant is case sensitive
|
||||
ok: filename control characters are 400
|
||||
ok: chat title bounds
|
||||
ok: cursor is bound to the endpoint
|
||||
ok: 204 responses carry headers and no body
|
||||
ok: download Content-Length matches the bytes
|
||||
PASS: 30 checks
|
||||
@@ -0,0 +1,25 @@
|
||||
2026-11-20T04:43:58+03:00
|
||||
separate restricted S3 credentials for the API and idempotent bucket bootstrap
|
||||
|
||||
$ podman-compose run --rm s3-init (first run)
|
||||
bucket ready: messenger-attachments
|
||||
policy ready: messenger-app
|
||||
application credentials ready
|
||||
policy attached to application credentials
|
||||
|
||||
$ podman-compose run --rm s3-init (second run: idempotent)
|
||||
bucket ready: messenger-attachments
|
||||
policy ready: messenger-app
|
||||
application credentials ready
|
||||
policy attached to application credentials
|
||||
|
||||
secret files mounted into api vs s3 (hashes differ)
|
||||
api_1 app secret sha256: 02b5014e8a0f03ac52a0a32d19abe13cd0e64ad9a9c61f178e3eede9989c82ce
|
||||
s3 root secret sha256: 970780881d80d85eb3f084c147c71e390fa563e0e022fa75cd8170d5a607cacc
|
||||
|
||||
secrets per role
|
||||
messenger-lab4_api_1: cache_url database_url s3_access_key_id s3_secret_access_key
|
||||
messenger-lab4_s3_1: s3_root_access_key s3_root_secret_key
|
||||
|
||||
$ scripts/s3tool.sh remove-bucket messenger-attachments (must be denied)
|
||||
s3tool failed: dependency unavailable: Access Denied
|
||||
@@ -0,0 +1,7 @@
|
||||
2026-11-20T04:56:32+03:00
|
||||
|
||||
users | chats | messages | attachments | links | db_size
|
||||
-------+-------+----------+-------------+-------+---------
|
||||
125 | 43 | 634 | 101 | 56 | 8958 kB
|
||||
(1 row)
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
2026-11-20T04:56:17+03:00
|
||||
|
||||
Linux archlinux 7.3.5-arch1-1 #1 SMP PREEMPT_DYNAMIC Tue, 17 Nov 2026 22:28:33 +0000 x86_64 GNU/Linux
|
||||
12
|
||||
total used free shared buff/cache available
|
||||
Mem: 15Gi 6.5Gi 3.6Gi 440Mi 5.8Gi 8.8Gi
|
||||
Swap: 8.0Gi 187Mi 7.8Gi
|
||||
podman version 6.2.0
|
||||
podman version 6.2.0
|
||||
podman-compose version 1.6.0
|
||||
go version go1.27.4-X:nodwarf5 linux/amd64
|
||||
|
||||
images
|
||||
docker.io/library/haproxy:3.4.7-alpine 49c6afb26f02
|
||||
docker.io/library/postgres:18.6-alpine3.24 57b23a0326ee
|
||||
docker.io/library/redis:8.10.3-alpine3.23 9722accf3e8e
|
||||
docker.io/migrate/migrate:v4.20.1 719a6f5d0ade
|
||||
docker.io/rustfs/rustfs:1.0.6 7cd1c573d76d
|
||||
localhost/messenger-api:lab4 4c57d3f41dfb
|
||||
@@ -0,0 +1,7 @@
|
||||
2026-11-20T04:55:04+03:00
|
||||
make test-failover ACTION_SCRIPT=scripts/stop-one.sh
|
||||
|
||||
python3 tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://localhost:8080"
|
||||
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
|
||||
{"observed_instances_before": 2, "successful_writes": 88, "failed_attempts": 0, "first_recovery_seconds": 3.256}
|
||||
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||
@@ -0,0 +1,7 @@
|
||||
2026-11-20T04:55:38+03:00
|
||||
make test-failover ACTION_SCRIPT=scripts/stop-one.sh
|
||||
|
||||
python3 tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://localhost:8080"
|
||||
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
|
||||
{"observed_instances_before": 2, "successful_writes": 99, "failed_attempts": 0, "first_recovery_seconds": 3.236}
|
||||
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||
@@ -0,0 +1,11 @@
|
||||
2026-11-20T04:53:46+03:00
|
||||
make test-idempotency
|
||||
|
||||
BASE_URL="http://localhost:8080" python3 scripts/idempotency-race.py
|
||||
parallel requests with one key: 20
|
||||
successful responses (201): 20
|
||||
failed requests: 0 []
|
||||
distinct message ids in responses: 1
|
||||
messages stored in history: 1
|
||||
instances that served requests: ['72c9d7b6cc57', 'e6d2e0d15c36']
|
||||
OK: exactly one message created
|
||||
@@ -0,0 +1,23 @@
|
||||
2026-11-20T04:56:16+03:00
|
||||
API replicas and single entrypoint
|
||||
|
||||
messenger-lab4_api_1 Up 53 seconds (healthy)
|
||||
messenger-lab4_api_2 Up 21 seconds (healthy)
|
||||
messenger-lab4_lb_1 Up About a minute
|
||||
messenger-lab4_postgres_1 Up About a minute (healthy)
|
||||
messenger-lab4_redis_1 Up About a minute (healthy)
|
||||
messenger-lab4_s3_1 Up About a minute (healthy)
|
||||
|
||||
published ports (only lb must publish 8080)
|
||||
|
||||
messenger-lab4_api_1 8080/tcp
|
||||
messenger-lab4_api_2 8080/tcp
|
||||
messenger-lab4_lb_1 0.0.0.0:8080->8080/tcp
|
||||
messenger-lab4_postgres_1 127.0.0.1:55432->5432/tcp
|
||||
messenger-lab4_redis_1 127.0.0.1:56379->6379/tcp
|
||||
messenger-lab4_s3_1 9000-9001/tcp
|
||||
|
||||
X-Instance-Id distribution over 20 requests through LB
|
||||
|
||||
10 x-instance-id: a089fe9cc382
|
||||
10 x-instance-id: f309c2ef4e46
|
||||
@@ -0,0 +1,20 @@
|
||||
2026-11-20T04:56:16+03:00
|
||||
per-container limits, networks, secrets
|
||||
|
||||
messenger-lab4_api_1 nano_cpus=1000000000 memory=536870912 networks=messenger-lab4_backend messenger-lab4_edge
|
||||
messenger-lab4_api_2 nano_cpus=1000000000 memory=536870912 networks=messenger-lab4_backend messenger-lab4_edge
|
||||
messenger-lab4_lb_1 nano_cpus=500000000 memory=67108864 networks=messenger-lab4_edge
|
||||
messenger-lab4_postgres_1 nano_cpus=1000000000 memory=536870912 networks=messenger-lab4_backend
|
||||
messenger-lab4_redis_1 nano_cpus=500000000 memory=134217728 networks=messenger-lab4_backend
|
||||
messenger-lab4_s3_1 nano_cpus=1000000000 memory=536870912 networks=messenger-lab4_backend
|
||||
messenger-lab4_s3-init_1 nano_cpus=500000000 memory=268435456 networks=messenger-lab4_backend
|
||||
messenger-lab4_migrate_1 nano_cpus=500000000 memory=67108864 networks=messenger-lab4_backend
|
||||
|
||||
ls /run/secrets per running container
|
||||
|
||||
messenger-lab4_api_1: cache_url database_url s3_access_key_id s3_secret_access_key
|
||||
messenger-lab4_api_2: cache_url database_url s3_access_key_id s3_secret_access_key
|
||||
messenger-lab4_lb_1:
|
||||
messenger-lab4_postgres_1: postgres_password
|
||||
messenger-lab4_redis_1:
|
||||
messenger-lab4_s3_1: s3_root_access_key s3_root_secret_key
|
||||
@@ -0,0 +1,36 @@
|
||||
2026-11-20T04:54:15+03:00
|
||||
10 concurrent uploads of a 8000x5000 PNG (40,000,000 pixels) per replica; API limit 512 MiB; MAX_CONCURRENT_UPLOADS=8, MAX_CONCURRENT_DECODES=1
|
||||
|
||||
BASE_URL="http://localhost:8080" python3 scripts/upload-load.py --concurrency 10 --output evidence/upload-memory.json
|
||||
{
|
||||
"concurrency": 10,
|
||||
"uploads": 10,
|
||||
"fixture": "large.png",
|
||||
"fixture_bytes": 126609,
|
||||
"elapsed_seconds": 1.009,
|
||||
"successful": 10,
|
||||
"failed": 0,
|
||||
"failures": [],
|
||||
"latency_ms": {
|
||||
"p50": 660.9,
|
||||
"p95": 999.3,
|
||||
"p99": 999.3
|
||||
},
|
||||
"api_containers": [
|
||||
"messenger-lab4_api_1",
|
||||
"messenger-lab4_api_2"
|
||||
],
|
||||
"peak_bytes_before": {
|
||||
"messenger-lab4_api_1": 5951488,
|
||||
"messenger-lab4_api_2": 4993024
|
||||
},
|
||||
"peak_bytes_after": {
|
||||
"messenger-lab4_api_1": 348913664,
|
||||
"messenger-lab4_api_2": 348721152
|
||||
},
|
||||
"peak_delta_bytes": {
|
||||
"messenger-lab4_api_1": 342962176,
|
||||
"messenger-lab4_api_2": 343728128
|
||||
},
|
||||
"peak_max_mb": 332.75
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
2026-11-20T04:56:17+03:00
|
||||
migrate container logs and schema version
|
||||
|
||||
1/u init (18.672238ms)
|
||||
2/u message_idempotency (27.931065ms)
|
||||
3/u attachments (37.481418ms)
|
||||
no change
|
||||
no change
|
||||
|
||||
schema_migrations
|
||||
|
||||
version | dirty
|
||||
---------+-------
|
||||
3 | f
|
||||
(1 row)
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
2026-11-20T04:53:36+03:00
|
||||
make test-attachments
|
||||
|
||||
BASE_URL="http://localhost:8080" python3 scripts/attachments-check.py
|
||||
ok: upload 201 ready, sha256=99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75, size=111817
|
||||
ok: jpeg upload/download byte-identical; jpeg declared as png -> 415
|
||||
ok: png with 40,000,000 pixels accepted and downloaded byte-identical
|
||||
ok: filename with slash -> 400
|
||||
ok: filename with NUL -> 400
|
||||
ok: empty filename -> 400
|
||||
ok: declared jpeg with png body -> 415
|
||||
ok: truncated png -> 415
|
||||
ok: 10485761 bytes -> 413
|
||||
ok: text/plain -> 415
|
||||
ok: unknown variant -> 400
|
||||
ok: variant=original -> 200
|
||||
ok: unknown attachment id -> 404
|
||||
ok: upload by outsider -> 404
|
||||
ok: attachment from another chat -> 404
|
||||
ok: unknown attachment id in message -> 404
|
||||
ok: duplicate attachment_ids -> 400
|
||||
ok: eleven attachment_ids -> 400
|
||||
ok: empty text without attachments -> 400
|
||||
ok: idempotent replay with same attachment set -> same message
|
||||
ok: absent attachment_ids equals [] for idempotency
|
||||
ok: same key with a different attachment set -> 409
|
||||
ok: history exposes attachment_ids on every message
|
||||
PASS: 23 attachment contract checks
|
||||
@@ -0,0 +1,32 @@
|
||||
2026-11-20T04:54:22+03:00
|
||||
make test-persistence ACTION_SCRIPT=scripts/restart.sh
|
||||
|
||||
python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost:8080"
|
||||
Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.
|
||||
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||
|
||||
--- hook scope: containers and volumes before/after scripts/restart.sh ---
|
||||
|
||||
messenger-lab4_api_1 a089fe9cc382 Up 5 seconds (healthy)
|
||||
messenger-lab4_api_2 f309c2ef4e46 Up 5 seconds (healthy)
|
||||
messenger-lab4_lb_1 efc7da123526 Up 5 seconds
|
||||
messenger-lab4_postgres_1 c64e954cf92a Up 5 seconds (healthy)
|
||||
messenger-lab4_redis_1 90ac9f642b09 Up 5 seconds (healthy)
|
||||
messenger-lab4_s3_1 6ae98c5aef4f Up 5 seconds (healthy)
|
||||
messenger-lab4_pgdata
|
||||
messenger-lab4_redisdata
|
||||
messenger-lab4_s3data
|
||||
|
||||
$ scripts/restart.sh
|
||||
|
||||
|
||||
messenger-lab4_api_1 a089fe9cc382 Up 15 seconds (healthy)
|
||||
messenger-lab4_api_2 f309c2ef4e46 Up 15 seconds (healthy)
|
||||
messenger-lab4_lb_1 efc7da123526 Up 15 seconds
|
||||
messenger-lab4_postgres_1 c64e954cf92a Up 15 seconds (healthy)
|
||||
messenger-lab4_redis_1 90ac9f642b09 Up 15 seconds (healthy)
|
||||
messenger-lab4_s3_1 6ae98c5aef4f Up 15 seconds (healthy)
|
||||
messenger-lab4_pgdata
|
||||
messenger-lab4_redisdata
|
||||
messenger-lab4_s3data
|
||||
ready: 200
|
||||
@@ -0,0 +1,22 @@
|
||||
2026-11-20T04:44:19+03:00
|
||||
bucket privacy and disabled console (checks outside the application API)
|
||||
|
||||
$ anonymous GET of an existing object from the backend network
|
||||
Connecting to s3:9000 (10.89.5.22:9000)
|
||||
HTTP/1.1 403 Forbidden
|
||||
wget: server returned error: HTTP/1.1 403 Forbidden
|
||||
|
||||
$ anonymous bucket listing
|
||||
Connecting to s3:9000 (10.89.5.22:9000)
|
||||
HTTP/1.1 403 Forbidden
|
||||
wget: server returned error: HTTP/1.1 403 Forbidden
|
||||
|
||||
$ console port inside the s3 container
|
||||
console_http=000
|
||||
console-closed
|
||||
|
||||
$ published ports
|
||||
messenger-lab4_api_1 8080/tcp
|
||||
messenger-lab4_api_2 8080/tcp
|
||||
messenger-lab4_lb_1 0.0.0.0:8080->8080/tcp
|
||||
messenger-lab4_s3_1 9000-9001/tcp
|
||||
@@ -0,0 +1,183 @@
|
||||
2026-11-20T04:41:03+03:00
|
||||
reconciler drill: baseline, grace period, dry-run, apply, repeat
|
||||
|
||||
$ scripts/reconcile.sh # dry-run, grace 1h, clean baseline
|
||||
{
|
||||
"prefix": "chats/",
|
||||
"grace_seconds": 3600,
|
||||
"dry_run": true,
|
||||
"scanned_objects": 18,
|
||||
"scanned_rows": 18,
|
||||
"truncated": false,
|
||||
"orphans": [],
|
||||
"missing": [],
|
||||
"deleted_objects": 0,
|
||||
"deleted_metadata": 0,
|
||||
"retained_missing": 0,
|
||||
"delete_failures": 0,
|
||||
"s3_requests": 1,
|
||||
"db_queries": 2,
|
||||
"duration_ms": 7,
|
||||
"objects_per_second": 2571.4285714285716
|
||||
}
|
||||
|
||||
$ scripts/s3tool.sh put chats/aaaaaaaa-0000-4000-8000-000000000000/bbbbbbbb-0000-4000-8000-000000000000 - < scripts/fixtures/sample.jpg
|
||||
put chats/aaaaaaaa-0000-4000-8000-000000000000/bbbbbbbb-0000-4000-8000-000000000000 (653 bytes)
|
||||
|
||||
$ scripts/reconcile.sh # fresh orphan skipped by grace
|
||||
{
|
||||
"prefix": "chats/",
|
||||
"grace_seconds": 3600,
|
||||
"dry_run": true,
|
||||
"scanned_objects": 19,
|
||||
"scanned_rows": 18,
|
||||
"truncated": false,
|
||||
"orphans": [],
|
||||
"missing": [],
|
||||
"deleted_objects": 0,
|
||||
"deleted_metadata": 0,
|
||||
"retained_missing": 0,
|
||||
"delete_failures": 0,
|
||||
"s3_requests": 1,
|
||||
"db_queries": 2,
|
||||
"duration_ms": 8,
|
||||
"objects_per_second": 2375
|
||||
}
|
||||
|
||||
$ scripts/reconcile.sh -grace 0s # detected, dry-run keeps it
|
||||
{
|
||||
"prefix": "chats/",
|
||||
"grace_seconds": 0,
|
||||
"dry_run": true,
|
||||
"scanned_objects": 19,
|
||||
"scanned_rows": 18,
|
||||
"truncated": false,
|
||||
"orphans": [
|
||||
{
|
||||
"key": "chats/aaaaaaaa-0000-4000-8000-000000000000/bbbbbbbb-0000-4000-8000-000000000000",
|
||||
"size_bytes": 653,
|
||||
"last_modified": "2026-11-20T01:41:03Z",
|
||||
"age_seconds": 0
|
||||
}
|
||||
],
|
||||
"missing": [],
|
||||
"deleted_objects": 0,
|
||||
"deleted_metadata": 0,
|
||||
"retained_missing": 0,
|
||||
"delete_failures": 0,
|
||||
"s3_requests": 1,
|
||||
"db_queries": 2,
|
||||
"duration_ms": 7,
|
||||
"objects_per_second": 2714.285714285714
|
||||
}
|
||||
|
||||
$ scripts/reconcile.sh -grace 0s -dry-run=false # apply
|
||||
{
|
||||
"prefix": "chats/",
|
||||
"grace_seconds": 0,
|
||||
"dry_run": false,
|
||||
"scanned_objects": 19,
|
||||
"scanned_rows": 18,
|
||||
"truncated": false,
|
||||
"orphans": [
|
||||
{
|
||||
"key": "chats/aaaaaaaa-0000-4000-8000-000000000000/bbbbbbbb-0000-4000-8000-000000000000",
|
||||
"size_bytes": 653,
|
||||
"last_modified": "2026-11-20T01:41:03Z",
|
||||
"age_seconds": 4
|
||||
}
|
||||
],
|
||||
"missing": [],
|
||||
"deleted_objects": 1,
|
||||
"deleted_metadata": 0,
|
||||
"retained_missing": 0,
|
||||
"delete_failures": 0,
|
||||
"s3_requests": 2,
|
||||
"db_queries": 2,
|
||||
"duration_ms": 15,
|
||||
"objects_per_second": 1266.6666666666667
|
||||
}
|
||||
|
||||
$ scripts/reconcile.sh -grace 0s -dry-run=false # repeat: nothing left
|
||||
{
|
||||
"prefix": "chats/",
|
||||
"grace_seconds": 0,
|
||||
"dry_run": false,
|
||||
"scanned_objects": 18,
|
||||
"scanned_rows": 18,
|
||||
"truncated": false,
|
||||
"orphans": [],
|
||||
"missing": [],
|
||||
"deleted_objects": 0,
|
||||
"deleted_metadata": 0,
|
||||
"retained_missing": 0,
|
||||
"delete_failures": 0,
|
||||
"s3_requests": 1,
|
||||
"db_queries": 2,
|
||||
"duration_ms": 8,
|
||||
"objects_per_second": 2250
|
||||
}
|
||||
|
||||
missing drill: metadata row without object
|
||||
|
||||
$ scripts/s3tool.sh delete chats/<chat>/<attachment> of an existing attachment
|
||||
$ scripts/s3tool.sh delete chats/bd1c9e17-0366-44b4-bd98-0a479a2a637c/1e9b9e12-eab2-4846-8952-e739a4257b37
|
||||
deleted chats/bd1c9e17-0366-44b4-bd98-0a479a2a637c/1e9b9e12-eab2-4846-8952-e739a4257b37
|
||||
|
||||
$ scripts/reconcile.sh -grace 0s # missing reported, metadata kept in dry-run
|
||||
{
|
||||
"prefix": "chats/",
|
||||
"grace_seconds": 0,
|
||||
"dry_run": true,
|
||||
"scanned_objects": 17,
|
||||
"scanned_rows": 18,
|
||||
"truncated": false,
|
||||
"orphans": [],
|
||||
"missing": [
|
||||
{
|
||||
"id": "1e9b9e12-eab2-4846-8952-e739a4257b37",
|
||||
"object_key": "chats/bd1c9e17-0366-44b4-bd98-0a479a2a637c/1e9b9e12-eab2-4846-8952-e739a4257b37",
|
||||
"chat_id": "bd1c9e17-0366-44b4-bd98-0a479a2a637c",
|
||||
"created_at": "2026-11-20T01:40:28Z",
|
||||
"referenced_by_message": false
|
||||
}
|
||||
],
|
||||
"deleted_objects": 0,
|
||||
"deleted_metadata": 0,
|
||||
"retained_missing": 0,
|
||||
"delete_failures": 0,
|
||||
"s3_requests": 1,
|
||||
"db_queries": 2,
|
||||
"duration_ms": 9,
|
||||
"objects_per_second": 1888.8888888888891
|
||||
}
|
||||
|
||||
metadata rows still present: 1
|
||||
|
||||
$ scripts/reconcile.sh -grace 0s -dry-run=false # safe repair: unreferenced dead row removed
|
||||
{
|
||||
"prefix": "chats/",
|
||||
"grace_seconds": 0,
|
||||
"dry_run": false,
|
||||
"scanned_objects": 17,
|
||||
"scanned_rows": 18,
|
||||
"truncated": false,
|
||||
"orphans": [],
|
||||
"missing": [
|
||||
{
|
||||
"id": "1e9b9e12-eab2-4846-8952-e739a4257b37",
|
||||
"object_key": "chats/bd1c9e17-0366-44b4-bd98-0a479a2a637c/1e9b9e12-eab2-4846-8952-e739a4257b37",
|
||||
"chat_id": "bd1c9e17-0366-44b4-bd98-0a479a2a637c",
|
||||
"created_at": "2026-11-20T01:40:28Z",
|
||||
"referenced_by_message": false
|
||||
}
|
||||
],
|
||||
"deleted_objects": 0,
|
||||
"deleted_metadata": 1,
|
||||
"retained_missing": 0,
|
||||
"delete_failures": 0,
|
||||
"s3_requests": 1,
|
||||
"db_queries": 3,
|
||||
"duration_ms": 8,
|
||||
"objects_per_second": 2125
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
2026-11-20T04:57:42+03:00
|
||||
S3 unavailable: readiness and client-visible behaviour
|
||||
|
||||
$ GET /health/ready on a replica
|
||||
HTTP/1.1 503 Service Unavailable
|
||||
wget: server returned error: HTTP/1.1 503 Service Unavailable
|
||||
|
||||
$ GET /health/live on a replica (process is alive)
|
||||
HTTP/1.1 200 OK
|
||||
Content-Type: application/json; charset=utf-8
|
||||
X-Instance-Id: a089fe9cc382
|
||||
|
||||
$ client request through the load balancer
|
||||
status=503
|
||||
|
||||
$ readiness after S3 recovery
|
||||
status=200
|
||||
@@ -0,0 +1,5 @@
|
||||
2026-11-20T04:53:35+03:00
|
||||
make test
|
||||
|
||||
python3 tests/smoke.py --base-url "http://localhost:8080"
|
||||
PASS: lab4 public API scenario; infrastructure and grading evidence remain separate.
|
||||
@@ -0,0 +1,32 @@
|
||||
2026-11-20T04:56:31+03:00
|
||||
object bytes live in the RustFS volume, metadata in PostgreSQL
|
||||
|
||||
id | object_key | size_bytes | sha256
|
||||
--------------------------------------+---------------------------------------------------------------------------------+------------+------------------------------------------------------------------
|
||||
82d5d4f3-e97a-4aea-9501-69ef2b74e138 | chats/e8618579-667a-4423-aa62-5402596e57d8/82d5d4f3-e97a-4aea-9501-69ef2b74e138 | 111817 | 99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75
|
||||
(1 row)
|
||||
|
||||
|
||||
S3 volume listing for that object
|
||||
|
||||
total 112
|
||||
-rw-r--r-- 1 rustfs rustfs 112398 Nov 20 01:55 xl.meta
|
||||
|
||||
attachments table columns (no binary content)
|
||||
|
||||
column_name | data_type
|
||||
--------------+--------------------------
|
||||
id | uuid
|
||||
chat_id | uuid
|
||||
owner_id | uuid
|
||||
filename | text
|
||||
content_type | text
|
||||
size_bytes | bigint
|
||||
sha256 | text
|
||||
object_key | text
|
||||
status | text
|
||||
error_code | text
|
||||
created_at | timestamp with time zone
|
||||
updated_at | timestamp with time zone
|
||||
(12 rows)
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
2026-11-20T04:53:38+03:00
|
||||
make test-go
|
||||
|
||||
? messenger/cmd/reconcile [no test files]
|
||||
? messenger/cmd/s3-init [no test files]
|
||||
? messenger/cmd/s3tool [no test files]
|
||||
? messenger/cmd/server [no test files]
|
||||
? messenger/internal/config [no test files]
|
||||
? messenger/internal/domain [no test files]
|
||||
=== RUN TestSessionExpiry
|
||||
--- PASS: TestSessionExpiry (3.09s)
|
||||
=== RUN TestLogout
|
||||
--- PASS: TestLogout (0.13s)
|
||||
=== RUN TestWrongPassword
|
||||
--- PASS: TestWrongPassword (0.10s)
|
||||
=== RUN TestRedisUnavailable
|
||||
redis: 2026/11/20 04:53:42 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
|
||||
redis: 2026/11/20 04:53:43 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
|
||||
redis: 2026/11/20 04:53:43 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
|
||||
redis: 2026/11/20 04:53:44 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
|
||||
redis: 2026/11/20 04:53:44 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
|
||||
redis: 2026/11/20 04:53:44 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
|
||||
--- PASS: TestRedisUnavailable (2.95s)
|
||||
=== RUN TestDatabaseUnavailable
|
||||
--- PASS: TestDatabaseUnavailable (0.17s)
|
||||
=== RUN TestInvalidJSON
|
||||
--- PASS: TestInvalidJSON (0.01s)
|
||||
=== RUN TestForeignChat
|
||||
--- PASS: TestForeignChat (0.18s)
|
||||
=== RUN TestEmptyMessage
|
||||
redis: 2026/11/20 04:53:45 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
|
||||
--- PASS: TestEmptyMessage (0.08s)
|
||||
=== RUN TestDuplicateUsername
|
||||
--- PASS: TestDuplicateUsername (0.10s)
|
||||
=== RUN TestPaginationMultiplePages
|
||||
--- PASS: TestPaginationMultiplePages (0.09s)
|
||||
=== RUN TestConcurrentMessages
|
||||
--- PASS: TestConcurrentMessages (0.57s)
|
||||
PASS
|
||||
ok messenger/internal/handler 7.474s
|
||||
=== RUN TestPlanDetectsOrphansAndMissing
|
||||
--- PASS: TestPlanDetectsOrphansAndMissing (0.00s)
|
||||
=== RUN TestPlanHonoursGraceAndPrefix
|
||||
--- PASS: TestPlanHonoursGraceAndPrefix (0.00s)
|
||||
=== RUN TestPlanNeverDeletesReferencedObjects
|
||||
--- PASS: TestPlanNeverDeletesReferencedObjects (0.00s)
|
||||
=== RUN TestPlanOnlyReportsRowsInsidePrefix
|
||||
--- PASS: TestPlanOnlyReportsRowsInsidePrefix (0.00s)
|
||||
=== RUN TestPlanOutputIsSorted
|
||||
--- PASS: TestPlanOutputIsSorted (0.00s)
|
||||
PASS
|
||||
ok messenger/internal/reconcile 0.002s
|
||||
=== RUN TestRegisterUsernameRace
|
||||
--- PASS: TestRegisterUsernameRace (0.02s)
|
||||
=== RUN TestCreateChatRollback
|
||||
--- PASS: TestCreateChatRollback (0.02s)
|
||||
PASS
|
||||
ok messenger/internal/repository/postgres 0.040s
|
||||
? messenger/internal/repository/redis [no test files]
|
||||
? messenger/internal/repository/s3 [no test files]
|
||||
? messenger/internal/router [no test files]
|
||||
=== RUN TestCheckFilename
|
||||
--- PASS: TestCheckFilename (0.00s)
|
||||
=== RUN TestDetectImageType
|
||||
--- PASS: TestDetectImageType (0.00s)
|
||||
=== RUN TestCheckPixels
|
||||
--- PASS: TestCheckPixels (0.00s)
|
||||
=== RUN TestCheckIntegrity
|
||||
--- PASS: TestCheckIntegrity (0.00s)
|
||||
=== RUN TestCheckMessageText
|
||||
--- PASS: TestCheckMessageText (0.00s)
|
||||
=== RUN TestCheckAttachmentIDs
|
||||
--- PASS: TestCheckAttachmentIDs (0.00s)
|
||||
=== RUN TestMessageHash
|
||||
--- PASS: TestMessageHash (0.00s)
|
||||
=== RUN TestReadLimited
|
||||
--- PASS: TestReadLimited (0.00s)
|
||||
PASS
|
||||
ok messenger/internal/service 0.004s
|
||||
@@ -0,0 +1,41 @@
|
||||
2026-11-20T04:42:15+03:00
|
||||
traversal cost of the reconciler (synthetic objects under chats/bench/, 1 KiB each); scan uses LIST pages, repair deletes one by one
|
||||
|
||||
{
|
||||
"prefix": "chats/bench",
|
||||
"measurements": [
|
||||
{
|
||||
"objects": 100,
|
||||
"scan_ms": 9,
|
||||
"scan_s3_requests": 1,
|
||||
"scan_db_queries": 2,
|
||||
"scan_objects_per_second": 11111.1,
|
||||
"scan_ms_per_object": 0.09,
|
||||
"repair_ms": 1324,
|
||||
"repair_s3_requests": 101,
|
||||
"repair_deleted": 100
|
||||
},
|
||||
{
|
||||
"objects": 1000,
|
||||
"scan_ms": 34,
|
||||
"scan_s3_requests": 1,
|
||||
"scan_db_queries": 2,
|
||||
"scan_objects_per_second": 29411.8,
|
||||
"scan_ms_per_object": 0.034,
|
||||
"repair_ms": 12232,
|
||||
"repair_s3_requests": 1001,
|
||||
"repair_deleted": 1000
|
||||
},
|
||||
{
|
||||
"objects": 5000,
|
||||
"scan_ms": 166,
|
||||
"scan_s3_requests": 5,
|
||||
"scan_db_queries": 2,
|
||||
"scan_objects_per_second": 30120.5,
|
||||
"scan_ms_per_object": 0.0332,
|
||||
"repair_ms": 69929,
|
||||
"repair_s3_requests": 5005,
|
||||
"repair_deleted": 5000
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
2026-11-20T04:41:49+03:00
|
||||
two-store consistency: injections on both sides, dry-run, safe repair, repeat, protection of fresh and used data
|
||||
|
||||
injections:
|
||||
S3 has an object that has no metadata row (fresh orphan)
|
||||
metadata exists but the object is gone: one attachment used by a message, one unused
|
||||
|
||||
$ scripts/s3tool.sh put chats/aaaaaaaa-0000-4000-8000-000000000000/bbbbbbbb-0000-4000-8000-000000000000 - < scripts/fixtures/sample.jpg
|
||||
put chats/aaaaaaaa-0000-4000-8000-000000000000/bbbbbbbb-0000-4000-8000-000000000000 (653 bytes)
|
||||
$ scripts/s3tool.sh delete chats/47f62016-7525-4910-9823-0b21f2664b10/6a9f0506-2126-497a-b8b4-89ac728fe5a1 # used
|
||||
deleted chats/47f62016-7525-4910-9823-0b21f2664b10/6a9f0506-2126-497a-b8b4-89ac728fe5a1
|
||||
$ scripts/s3tool.sh delete chats/d03b2c6b-67fd-4b5d-bc17-9e926440ff8d/c49ab2de-258a-4851-8ad2-f7f585fd3488 # unused
|
||||
deleted chats/d03b2c6b-67fd-4b5d-bc17-9e926440ff8d/c49ab2de-258a-4851-8ad2-f7f585fd3488
|
||||
|
||||
$ scripts/reconcile.sh -grace 1h # dry-run
|
||||
{
|
||||
"prefix": "chats/",
|
||||
"grace_seconds": 3600,
|
||||
"dry_run": true,
|
||||
"scanned_objects": 19,
|
||||
"scanned_rows": 20,
|
||||
"truncated": false,
|
||||
"orphans": [],
|
||||
"missing": [
|
||||
{
|
||||
"id": "6a9f0506-2126-497a-b8b4-89ac728fe5a1",
|
||||
"object_key": "chats/47f62016-7525-4910-9823-0b21f2664b10/6a9f0506-2126-497a-b8b4-89ac728fe5a1",
|
||||
"chat_id": "47f62016-7525-4910-9823-0b21f2664b10",
|
||||
"created_at": "2026-11-20T01:41:41Z",
|
||||
"referenced_by_message": true
|
||||
},
|
||||
{
|
||||
"id": "c49ab2de-258a-4851-8ad2-f7f585fd3488",
|
||||
"object_key": "chats/d03b2c6b-67fd-4b5d-bc17-9e926440ff8d/c49ab2de-258a-4851-8ad2-f7f585fd3488",
|
||||
"chat_id": "d03b2c6b-67fd-4b5d-bc17-9e926440ff8d",
|
||||
"created_at": "2026-11-20T01:41:41Z",
|
||||
"referenced_by_message": false
|
||||
}
|
||||
],
|
||||
"deleted_objects": 0,
|
||||
"deleted_metadata": 0,
|
||||
"retained_missing": 0,
|
||||
"delete_failures": 0,
|
||||
"s3_requests": 1,
|
||||
"db_queries": 2,
|
||||
"duration_ms": 8,
|
||||
"objects_per_second": 2375
|
||||
}
|
||||
|
||||
$ scripts/reconcile.sh -grace 1h -dry-run=false # apply: fresh object survives, safe metadata repair
|
||||
{
|
||||
"prefix": "chats/",
|
||||
"grace_seconds": 3600,
|
||||
"dry_run": false,
|
||||
"scanned_objects": 19,
|
||||
"scanned_rows": 20,
|
||||
"truncated": false,
|
||||
"orphans": [],
|
||||
"missing": [
|
||||
{
|
||||
"id": "6a9f0506-2126-497a-b8b4-89ac728fe5a1",
|
||||
"object_key": "chats/47f62016-7525-4910-9823-0b21f2664b10/6a9f0506-2126-497a-b8b4-89ac728fe5a1",
|
||||
"chat_id": "47f62016-7525-4910-9823-0b21f2664b10",
|
||||
"created_at": "2026-11-20T01:41:41Z",
|
||||
"referenced_by_message": true
|
||||
},
|
||||
{
|
||||
"id": "c49ab2de-258a-4851-8ad2-f7f585fd3488",
|
||||
"object_key": "chats/d03b2c6b-67fd-4b5d-bc17-9e926440ff8d/c49ab2de-258a-4851-8ad2-f7f585fd3488",
|
||||
"chat_id": "d03b2c6b-67fd-4b5d-bc17-9e926440ff8d",
|
||||
"created_at": "2026-11-20T01:41:41Z",
|
||||
"referenced_by_message": false
|
||||
}
|
||||
],
|
||||
"deleted_objects": 0,
|
||||
"deleted_metadata": 1,
|
||||
"retained_missing": 1,
|
||||
"delete_failures": 0,
|
||||
"s3_requests": 1,
|
||||
"db_queries": 3,
|
||||
"duration_ms": 12,
|
||||
"objects_per_second": 1583.3333333333333
|
||||
}
|
||||
|
||||
$ scripts/s3tool.sh list chats/aaaaaaaa-0000-4000-8000-000000000000 # fresh orphan still present
|
||||
chats/aaaaaaaa-0000-4000-8000-000000000000/bbbbbbbb-0000-4000-8000-000000000000 653 2026-11-20T01:41:49Z
|
||||
|
||||
$ scripts/reconcile.sh -grace 0s # now eligible for deletion
|
||||
{
|
||||
"prefix": "chats/",
|
||||
"grace_seconds": 0,
|
||||
"dry_run": true,
|
||||
"scanned_objects": 19,
|
||||
"scanned_rows": 19,
|
||||
"truncated": false,
|
||||
"orphans": [
|
||||
{
|
||||
"key": "chats/aaaaaaaa-0000-4000-8000-000000000000/bbbbbbbb-0000-4000-8000-000000000000",
|
||||
"size_bytes": 653,
|
||||
"last_modified": "2026-11-20T01:41:49Z",
|
||||
"age_seconds": 7
|
||||
}
|
||||
],
|
||||
"missing": [
|
||||
{
|
||||
"id": "6a9f0506-2126-497a-b8b4-89ac728fe5a1",
|
||||
"object_key": "chats/47f62016-7525-4910-9823-0b21f2664b10/6a9f0506-2126-497a-b8b4-89ac728fe5a1",
|
||||
"chat_id": "47f62016-7525-4910-9823-0b21f2664b10",
|
||||
"created_at": "2026-11-20T01:41:41Z",
|
||||
"referenced_by_message": true
|
||||
}
|
||||
],
|
||||
"deleted_objects": 0,
|
||||
"deleted_metadata": 0,
|
||||
"retained_missing": 0,
|
||||
"delete_failures": 0,
|
||||
"s3_requests": 1,
|
||||
"db_queries": 2,
|
||||
"duration_ms": 8,
|
||||
"objects_per_second": 2375
|
||||
}
|
||||
|
||||
$ scripts/reconcile.sh -grace 0s -dry-run=false # apply
|
||||
{
|
||||
"prefix": "chats/",
|
||||
"grace_seconds": 0,
|
||||
"dry_run": false,
|
||||
"scanned_objects": 19,
|
||||
"scanned_rows": 19,
|
||||
"truncated": false,
|
||||
"orphans": [
|
||||
{
|
||||
"key": "chats/aaaaaaaa-0000-4000-8000-000000000000/bbbbbbbb-0000-4000-8000-000000000000",
|
||||
"size_bytes": 653,
|
||||
"last_modified": "2026-11-20T01:41:49Z",
|
||||
"age_seconds": 13
|
||||
}
|
||||
],
|
||||
"missing": [
|
||||
{
|
||||
"id": "6a9f0506-2126-497a-b8b4-89ac728fe5a1",
|
||||
"object_key": "chats/47f62016-7525-4910-9823-0b21f2664b10/6a9f0506-2126-497a-b8b4-89ac728fe5a1",
|
||||
"chat_id": "47f62016-7525-4910-9823-0b21f2664b10",
|
||||
"created_at": "2026-11-20T01:41:41Z",
|
||||
"referenced_by_message": true
|
||||
}
|
||||
],
|
||||
"deleted_objects": 1,
|
||||
"deleted_metadata": 0,
|
||||
"retained_missing": 1,
|
||||
"delete_failures": 0,
|
||||
"s3_requests": 2,
|
||||
"db_queries": 2,
|
||||
"duration_ms": 15,
|
||||
"objects_per_second": 1266.6666666666667
|
||||
}
|
||||
|
||||
$ scripts/reconcile.sh -grace 0s -dry-run=false # repeat: idempotent
|
||||
{
|
||||
"prefix": "chats/",
|
||||
"grace_seconds": 0,
|
||||
"dry_run": false,
|
||||
"scanned_objects": 18,
|
||||
"scanned_rows": 19,
|
||||
"truncated": false,
|
||||
"orphans": [],
|
||||
"missing": [
|
||||
{
|
||||
"id": "6a9f0506-2126-497a-b8b4-89ac728fe5a1",
|
||||
"object_key": "chats/47f62016-7525-4910-9823-0b21f2664b10/6a9f0506-2126-497a-b8b4-89ac728fe5a1",
|
||||
"chat_id": "47f62016-7525-4910-9823-0b21f2664b10",
|
||||
"created_at": "2026-11-20T01:41:41Z",
|
||||
"referenced_by_message": true
|
||||
}
|
||||
],
|
||||
"deleted_objects": 0,
|
||||
"deleted_metadata": 0,
|
||||
"retained_missing": 1,
|
||||
"delete_failures": 0,
|
||||
"s3_requests": 1,
|
||||
"db_queries": 2,
|
||||
"duration_ms": 9,
|
||||
"objects_per_second": 2000.0000000000002
|
||||
}
|
||||
|
||||
used attachments remain untouched (rows and objects)
|
||||
used_attachments | ready
|
||||
------------------+-------
|
||||
3 | 3
|
||||
(1 row)
|
||||
|
||||
retained referenced metadata row for the attachment whose object was deleted: 1
|
||||
object of that referenced attachment is still absent: 0
|
||||
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"concurrency": 10,
|
||||
"uploads": 10,
|
||||
"fixture": "large.png",
|
||||
"fixture_bytes": 126609,
|
||||
"elapsed_seconds": 1.009,
|
||||
"successful": 10,
|
||||
"failed": 0,
|
||||
"failures": [],
|
||||
"latency_ms": {
|
||||
"p50": 660.9,
|
||||
"p95": 999.3,
|
||||
"p99": 999.3
|
||||
},
|
||||
"api_containers": [
|
||||
"messenger-lab4_api_1",
|
||||
"messenger-lab4_api_2"
|
||||
],
|
||||
"peak_bytes_before": {
|
||||
"messenger-lab4_api_1": 5951488,
|
||||
"messenger-lab4_api_2": 4993024
|
||||
},
|
||||
"peak_bytes_after": {
|
||||
"messenger-lab4_api_1": 348913664,
|
||||
"messenger-lab4_api_2": 348721152
|
||||
},
|
||||
"peak_delta_bytes": {
|
||||
"messenger-lab4_api_1": 342962176,
|
||||
"messenger-lab4_api_2": 343728128
|
||||
},
|
||||
"peak_max_mb": 332.75
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
module messenger
|
||||
|
||||
go 1.27.1
|
||||
|
||||
require (
|
||||
github.com/go-chi/chi/v5 v5.3.2
|
||||
github.com/jackc/pgx/v5 v5.11.0
|
||||
github.com/minio/madmin-go/v3 v3.0.110
|
||||
github.com/minio/minio-go/v7 v7.4.1
|
||||
github.com/redis/go-redis/v9 v9.23.0
|
||||
golang.org/x/crypto v0.58.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/go-ole/go-ole v1.3.0 // indirect
|
||||
github.com/golang-jwt/jwt/v4 v4.5.2 // indirect
|
||||
github.com/golang/protobuf v1.5.4 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||
github.com/klauspost/compress v1.19.2 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.4.0 // indirect
|
||||
github.com/klauspost/crc32 v1.3.0 // indirect
|
||||
github.com/lufia/plan9stats v0.0.0-20250317134145-8bc96cf8fc35 // indirect
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
|
||||
github.com/minio/crc64nvme v1.1.1 // indirect
|
||||
github.com/minio/md5-simd v1.1.2 // indirect
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
||||
github.com/philhofer/fwd v1.2.0 // indirect
|
||||
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
|
||||
github.com/prometheus/client_model v0.6.2 // indirect
|
||||
github.com/prometheus/common v0.63.0 // indirect
|
||||
github.com/prometheus/procfs v0.16.0 // indirect
|
||||
github.com/prometheus/prom2json v1.4.2 // indirect
|
||||
github.com/prometheus/prometheus v0.303.0 // indirect
|
||||
github.com/rs/xid v1.6.0 // indirect
|
||||
github.com/safchain/ethtool v0.5.10 // indirect
|
||||
github.com/secure-io/sio-go v0.3.1 // indirect
|
||||
github.com/shirou/gopsutil/v3 v3.24.5 // indirect
|
||||
github.com/shoenig/go-m1cpu v0.1.6 // indirect
|
||||
github.com/tinylib/msgp v1.6.4 // indirect
|
||||
github.com/tklauser/go-sysconf v0.3.15 // indirect
|
||||
github.com/tklauser/numcpus v0.10.0 // indirect
|
||||
github.com/yusufpapurcu/wmi v1.2.4 // indirect
|
||||
github.com/zeebo/xxh3 v1.1.0 // indirect
|
||||
go.uber.org/atomic v1.12.0 // indirect
|
||||
go.yaml.in/yaml/v3 v3.0.5 // indirect
|
||||
golang.org/x/net v0.60.0 // indirect
|
||||
golang.org/x/sync v0.24.0 // indirect
|
||||
golang.org/x/sys v0.49.0 // indirect
|
||||
golang.org/x/text v0.43.0 // indirect
|
||||
google.golang.org/protobuf v1.36.12 // indirect
|
||||
gopkg.in/ini.v1 v1.67.3 // indirect
|
||||
)
|
||||
@@ -0,0 +1,141 @@
|
||||
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
||||
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
|
||||
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
|
||||
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
|
||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||
github.com/go-chi/chi/v5 v5.3.2 h1:5YQkICvTCSZ25hoRsyJazN0scjzKGiu4VAUc7H1o1nY=
|
||||
github.com/go-chi/chi/v5 v5.3.2/go.mod h1:R+tYY2hNuVUUjxoPtqUdgBqevM9s9njzkTLutVsOCto=
|
||||
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
|
||||
github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE=
|
||||
github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78=
|
||||
github.com/golang-jwt/jwt/v4 v4.5.2 h1:YtQM7lnr8iZ+j5q71MGKkNw9Mn7AjHM68uc9g5fXeUI=
|
||||
github.com/golang-jwt/jwt/v4 v4.5.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
|
||||
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
||||
github.com/jackc/pgx/v5 v5.11.0 h1:IzBBtyK9AHqf98cctWFifYSci2hgQR/cd56wB4p+ogg=
|
||||
github.com/jackc/pgx/v5 v5.11.0/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4=
|
||||
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
|
||||
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||
github.com/klauspost/compress v1.19.2 h1:hMRETovs/pu/dVWN7zIT1PGG8t509MwT6bO7XSi26R8=
|
||||
github.com/klauspost/compress v1.19.2/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
|
||||
github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
github.com/klauspost/cpuid/v2 v2.4.0 h1:S6Hrbc7+ywsr0r+RLapfGBHfyefhCTwEh3A0tV913Dw=
|
||||
github.com/klauspost/cpuid/v2 v2.4.0/go.mod h1:19jmZ9mjzoF//ddRSUsv0zfBTJWh3QJh9FNxZTMrGxU=
|
||||
github.com/klauspost/crc32 v1.3.0 h1:sSmTt3gUt81RP655XGZPElI0PelVTZ6YwCRnPSupoFM=
|
||||
github.com/klauspost/crc32 v1.3.0/go.mod h1:D7kQaZhnkX/Y0tstFGf8VUzv2UofNGqCjnC3zdHB0Hw=
|
||||
github.com/lufia/plan9stats v0.0.0-20250317134145-8bc96cf8fc35 h1:PpXWgLPs+Fqr325bN2FD2ISlRRztXibcX6e8f5FR5Dc=
|
||||
github.com/lufia/plan9stats v0.0.0-20250317134145-8bc96cf8fc35/go.mod h1:autxFIvghDt3jPTLoqZ9OZ7s9qTGNAWmYCjVFWPX/zg=
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
|
||||
github.com/minio/crc64nvme v1.1.1 h1:8dwx/Pz49suywbO+auHCBpCtlW1OfpcLN7wYgVR6wAI=
|
||||
github.com/minio/crc64nvme v1.1.1/go.mod h1:eVfm2fAzLlxMdUGc0EEBGSMmPwmXD5XiNRpnu9J3bvg=
|
||||
github.com/minio/madmin-go/v3 v3.0.110 h1:FIYekj7YPc430ffpXFWiUtyut3qBt/unIAcDzJn9H5M=
|
||||
github.com/minio/madmin-go/v3 v3.0.110/go.mod h1:WOe2kYmYl1OIlY2DSRHVQ8j1v4OItARQ6jGyQqcCud8=
|
||||
github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34=
|
||||
github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM=
|
||||
github.com/minio/minio-go/v7 v7.4.1 h1:PtU/zIOfQRWZ+QydXAZysc+xZYHdG+BBR4Z6TUvy73w=
|
||||
github.com/minio/minio-go/v7 v7.4.1/go.mod h1:nQnQi7zTfXR4lREtYXZQWqo5aw0Rqv+ZqCGrTORygRk=
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
||||
github.com/philhofer/fwd v1.2.0 h1:e6DnBTl7vGY+Gz322/ASL4Gyp1FspeMvx1RNDoToZuM=
|
||||
github.com/philhofer/fwd v1.2.0/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 h1:o4JXh1EVt9k/+g42oCprj/FisM4qX9L3sZB3upGN2ZU=
|
||||
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
|
||||
github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk=
|
||||
github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE=
|
||||
github.com/prometheus/common v0.63.0 h1:YR/EIY1o3mEFP/kZCD7iDMnLPlGyuU2Gb3HIcXnA98k=
|
||||
github.com/prometheus/common v0.63.0/go.mod h1:VVFF/fBIoToEnWRVkYoXEkq3R3paCoxG9PXP74SnV18=
|
||||
github.com/prometheus/procfs v0.16.0 h1:xh6oHhKwnOJKMYiYBDWmkHqQPyiY40sny36Cmx2bbsM=
|
||||
github.com/prometheus/procfs v0.16.0/go.mod h1:8veyXUu3nGP7oaCxhX6yeaM5u4stL2FeMXnCqhDthZg=
|
||||
github.com/prometheus/prom2json v1.4.2 h1:PxCTM+Whqi/eykO1MKsEL0p/zMpxp9ybpsmdFamw6po=
|
||||
github.com/prometheus/prom2json v1.4.2/go.mod h1:zuvPm7u3epZSbXPWHny6G+o8ETgu6eAK3oPr6yFkRWE=
|
||||
github.com/prometheus/prometheus v0.303.0 h1:wsNNsbd4EycMCphYnTmNY9JASBVbp7NWwJna857cGpA=
|
||||
github.com/prometheus/prometheus v0.303.0/go.mod h1:8PMRi+Fk1WzopMDeb0/6hbNs9nV6zgySkU/zds5Lu3o=
|
||||
github.com/redis/go-redis/v9 v9.23.0 h1:/TTVdJa8BRNsybwwGGnmiYktHNlUXtbOQxlWpHBPL3s=
|
||||
github.com/redis/go-redis/v9 v9.23.0/go.mod h1:EWP2UUk+XDaI1s5nQB5mablZN6vccXlmBQTyfnF87D0=
|
||||
github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
|
||||
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
|
||||
github.com/safchain/ethtool v0.5.10 h1:Im294gZtuf4pSGJRAOGKaASNi3wMeFaGaWuSaomedpc=
|
||||
github.com/safchain/ethtool v0.5.10/go.mod h1:w9jh2Lx7YBR4UwzLkzCmWl85UY0W2uZdd7/DckVE5+c=
|
||||
github.com/secure-io/sio-go v0.3.1 h1:dNvY9awjabXTYGsTF1PiCySl9Ltofk9GA3VdWlo7rRc=
|
||||
github.com/secure-io/sio-go v0.3.1/go.mod h1:+xbkjDzPjwh4Axd07pRKSNriS9SCiYksWnZqdnfpQxs=
|
||||
github.com/shirou/gopsutil/v3 v3.24.5 h1:i0t8kL+kQTvpAYToeuiVk3TgDeKOFioZO3Ztz/iZ9pI=
|
||||
github.com/shirou/gopsutil/v3 v3.24.5/go.mod h1:bsoOS1aStSs9ErQ1WWfxllSeS1K5D+U30r2NfcubMVk=
|
||||
github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM=
|
||||
github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ=
|
||||
github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU=
|
||||
github.com/shoenig/test v0.6.4/go.mod h1:byHiCGXqrVaflBLAMq/srcZIHynQPQgeyvkvXnjqq0k=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE=
|
||||
github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg=
|
||||
github.com/tinylib/msgp v1.6.4 h1:mOwYbyYDLPj35mkA2BjjYejgJk9BuHxDdvRnb6v2ZcQ=
|
||||
github.com/tinylib/msgp v1.6.4/go.mod h1:RSp0LW9oSxFut3KzESt5Voq4GVWyS+PSulT77roAqEA=
|
||||
github.com/tklauser/go-sysconf v0.3.15 h1:VE89k0criAymJ/Os65CSn1IXaol+1wrsFHEB8Ol49K4=
|
||||
github.com/tklauser/go-sysconf v0.3.15/go.mod h1:Dmjwr6tYFIseJw7a3dRLJfsHAMXZ3nEnL/aZY+0IuI4=
|
||||
github.com/tklauser/numcpus v0.10.0 h1:18njr6LDBk1zuna922MgdjQuJFjrdppsZG60sHGfjso=
|
||||
github.com/tklauser/numcpus v0.10.0/go.mod h1:BiTKazU708GQTYF4mB+cmlpT2Is1gLk7XVuEeem8LsQ=
|
||||
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
|
||||
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
|
||||
github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ=
|
||||
github.com/zeebo/assert v1.3.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0=
|
||||
github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs=
|
||||
github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s=
|
||||
go.uber.org/atomic v1.12.0 h1:BvcXdFKuviU4fTL/f+SxdQ5qJX/Jix8pAkgdUcb3XOE=
|
||||
go.uber.org/atomic v1.12.0/go.mod h1:I6c4cg+6HCxRjfjSsYtApoFILnpc0CGUdGkXVqbYVNk=
|
||||
go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
|
||||
go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.58.0 h1:COkYLr4k7nDI4r6QtMML2AfzeIswWHAJthLS6K/M/54=
|
||||
golang.org/x/crypto v0.58.0/go.mod h1:Xh+kl5A+M0gMiWDytH2Hhr5Z7WbNCpv/eB/EFgfSk+w=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.60.0 h1:79p50tfZlm0J9YfoDsSi639qSXNGVwEzOPLCxM2FsYU=
|
||||
golang.org/x/net v0.60.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg=
|
||||
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.24.0 h1:iqiDrGoDgyuvAKrhLQthUEzU/R378N78gXbBjxed7oA=
|
||||
golang.org/x/sync v0.24.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA=
|
||||
golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.43.0 h1:1QivrlhwAsnMOcqgOdl9my7s9OyZl8O5/UGUTJVXoWc=
|
||||
golang.org/x/text v0.43.0/go.mod h1:hbSIYA/amXcRXTFSZqh8tWHEoqRvvbKPJ2uz/KU+a+s=
|
||||
google.golang.org/protobuf v1.36.12 h1:pJOKDDOyeXErUroCihFAd5LQuwXBSpVnKGrj5o/fwxc=
|
||||
google.golang.org/protobuf v1.36.12/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/ini.v1 v1.67.3 h1:iM9Lhz5MRSGhHVGGwCuzG9KO8PoirCXj/m/qTmOJJQw=
|
||||
gopkg.in/ini.v1 v1.67.3/go.mod h1:x/cyOwCgZqOkJoDIJ3c1KNHMo10+nLGAhh+kn3Zizss=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
@@ -0,0 +1,30 @@
|
||||
global
|
||||
log stdout format raw local0 info
|
||||
maxconn 2000
|
||||
|
||||
defaults
|
||||
mode http
|
||||
log global
|
||||
option httplog
|
||||
timeout connect 2s
|
||||
timeout client 30s
|
||||
timeout server 20s
|
||||
timeout http-request 10s
|
||||
timeout check 1s
|
||||
retries 3
|
||||
option redispatch
|
||||
|
||||
resolvers dns
|
||||
parse-resolv-conf
|
||||
hold valid 1s
|
||||
|
||||
frontend public
|
||||
bind :8080
|
||||
default_backend api
|
||||
|
||||
backend api
|
||||
balance roundrobin
|
||||
option httpchk GET /health/ready
|
||||
http-check expect status 200
|
||||
default-server check inter 1s fall 2 rise 2 resolvers dns init-addr none
|
||||
server-template api 4 api:8080
|
||||
@@ -0,0 +1,129 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Host string
|
||||
Port string
|
||||
Env string
|
||||
SessionTTL time.Duration
|
||||
DatabaseURL string
|
||||
CacheURL string
|
||||
S3Endpoint string
|
||||
S3Region string
|
||||
S3Bucket string
|
||||
S3AccessKeyID string
|
||||
S3SecretAccessKey string
|
||||
MaxUploadBytes int64
|
||||
MaxImagePixels int64
|
||||
MaxConcurrentUploads int
|
||||
MaxConcurrentDecodes int
|
||||
}
|
||||
|
||||
func Load() (Config, error) {
|
||||
ttl, err := strconv.Atoi(getenv("SESSION_TTL_SECONDS", "3600"))
|
||||
if err != nil || ttl <= 0 {
|
||||
return Config{}, fmt.Errorf("SESSION_TTL_SECONDS must be a positive integer")
|
||||
}
|
||||
maxUploadBytes, err := positiveInt64("MAX_UPLOAD_BYTES", "10485760")
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
maxImagePixels, err := positiveInt64("MAX_IMAGE_PIXELS", "40000000")
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
maxConcurrentUploads, err := positiveInt("MAX_CONCURRENT_UPLOADS", "8")
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
maxConcurrentDecodes, err := positiveInt("MAX_CONCURRENT_DECODES", "1")
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
databaseURL, err := ReadSecret("DATABASE_URL_FILE", "/run/secrets/database_url")
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
cacheURL, err := ReadSecret("CACHE_URL_FILE", "/run/secrets/cache_url")
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
accessKeyID, err := ReadSecret("S3_ACCESS_KEY_ID_FILE", "/run/secrets/s3_access_key_id")
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
secretAccessKey, err := ReadSecret("S3_SECRET_ACCESS_KEY_FILE", "/run/secrets/s3_secret_access_key")
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
if accessKeyID == "" || secretAccessKey == "" {
|
||||
return Config{}, fmt.Errorf("S3_ACCESS_KEY_ID_FILE and S3_SECRET_ACCESS_KEY_FILE must not be empty")
|
||||
}
|
||||
bucket := getenv("S3_BUCKET", "messenger-attachments")
|
||||
if strings.TrimSpace(bucket) == "" {
|
||||
return Config{}, fmt.Errorf("S3_BUCKET must not be empty")
|
||||
}
|
||||
endpoint := getenv("S3_ENDPOINT", "http://s3:9000")
|
||||
if strings.TrimSpace(endpoint) == "" {
|
||||
return Config{}, fmt.Errorf("S3_ENDPOINT must not be empty")
|
||||
}
|
||||
return Config{
|
||||
Host: getenv("APP_HOST", "0.0.0.0"),
|
||||
Port: getenv("APP_PORT", "8080"),
|
||||
Env: getenv("APP_ENV", "lab"),
|
||||
SessionTTL: time.Duration(ttl) * time.Second,
|
||||
DatabaseURL: databaseURL,
|
||||
CacheURL: cacheURL,
|
||||
S3Endpoint: endpoint,
|
||||
S3Region: getenv("S3_REGION", "us-east-1"),
|
||||
S3Bucket: bucket,
|
||||
S3AccessKeyID: accessKeyID,
|
||||
S3SecretAccessKey: secretAccessKey,
|
||||
MaxUploadBytes: maxUploadBytes,
|
||||
MaxImagePixels: maxImagePixels,
|
||||
MaxConcurrentUploads: maxConcurrentUploads,
|
||||
MaxConcurrentDecodes: maxConcurrentDecodes,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c Config) Addr() string { return net.JoinHostPort(c.Host, c.Port) }
|
||||
|
||||
func positiveInt64(key, def string) (int64, error) {
|
||||
value, err := strconv.ParseInt(getenv(key, def), 10, 64)
|
||||
if err != nil || value <= 0 {
|
||||
return 0, fmt.Errorf("%s must be a positive integer", key)
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func positiveInt(key, def string) (int, error) {
|
||||
value, err := strconv.Atoi(getenv(key, def))
|
||||
if err != nil || value <= 0 {
|
||||
return 0, fmt.Errorf("%s must be a positive integer", key)
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func ReadSecret(key, def string) (string, error) {
|
||||
path := getenv(key, def)
|
||||
b, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%s: cannot read %s", key, path)
|
||||
}
|
||||
return strings.TrimSpace(string(b)), nil
|
||||
}
|
||||
|
||||
func getenv(key, def string) string {
|
||||
if v := os.Getenv(key); v != "" {
|
||||
return v
|
||||
}
|
||||
return def
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"time"
|
||||
"uuid"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID uuid.UUID
|
||||
Username string
|
||||
DisplayName string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type Chat struct {
|
||||
ID uuid.UUID
|
||||
Title string
|
||||
MemberIDs []uuid.UUID
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
func (c Chat) HasMember(userID uuid.UUID) bool {
|
||||
for _, id := range c.MemberIDs {
|
||||
if id == userID {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
ID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
SenderID uuid.UUID
|
||||
Text string
|
||||
AttachmentIDs []uuid.UUID
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
const (
|
||||
AttachmentReady = "ready"
|
||||
ContentPNG = "image/png"
|
||||
ContentJPEG = "image/jpeg"
|
||||
)
|
||||
|
||||
type Attachment struct {
|
||||
ID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
OwnerID uuid.UUID
|
||||
Filename string
|
||||
ContentType string
|
||||
SizeBytes int64
|
||||
SHA256 string
|
||||
ObjectKey string
|
||||
Status string
|
||||
ErrorCode *string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
type Session struct {
|
||||
Token string
|
||||
ExpiresAt time.Time
|
||||
User User
|
||||
}
|
||||
|
||||
type Cursor struct {
|
||||
Scope uuid.UUID
|
||||
CreatedAt time.Time
|
||||
ID uuid.UUID
|
||||
}
|
||||
|
||||
func CompareKey(aTime time.Time, aID uuid.UUID, bTime time.Time, bID uuid.UUID) int {
|
||||
if c := aTime.Compare(bTime); c != 0 {
|
||||
return c
|
||||
}
|
||||
return aID.Compare(bID)
|
||||
}
|
||||
|
||||
type Page[T any] struct {
|
||||
Items []T
|
||||
Next *Cursor
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package domain
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrInvalidInput = errors.New("invalid input")
|
||||
ErrUnauthorized = errors.New("unauthorized")
|
||||
ErrNotFound = errors.New("not found")
|
||||
ErrConflict = errors.New("conflict")
|
||||
ErrUnavailable = errors.New("dependency unavailable")
|
||||
ErrTooLarge = errors.New("payload too large")
|
||||
ErrUnsupportedMedia = errors.New("unsupported media type")
|
||||
)
|
||||
@@ -0,0 +1,82 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"io"
|
||||
"mime"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"uuid"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
func (h *Handler) UploadAttachment(w http.ResponseWriter, r *http.Request) {
|
||||
chatID, err := chatIDParam(r)
|
||||
if err != nil {
|
||||
h.fail(w, r, err)
|
||||
return
|
||||
}
|
||||
filename := r.URL.Query().Get("filename")
|
||||
if filename == "" {
|
||||
h.fail(w, r, badRequest("filename is required"))
|
||||
return
|
||||
}
|
||||
contentType, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
||||
if err != nil {
|
||||
contentType = ""
|
||||
}
|
||||
attachment, err := h.attachments.Upload(r.Context(), actor(r), chatID, filename, contentType, r.Body)
|
||||
if err != nil {
|
||||
h.fail(w, r, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusCreated, toAttachmentResponse(attachment))
|
||||
}
|
||||
|
||||
func (h *Handler) GetAttachment(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := attachmentIDParam(r)
|
||||
if err != nil {
|
||||
h.fail(w, r, err)
|
||||
return
|
||||
}
|
||||
attachment, err := h.attachments.Get(r.Context(), actor(r), id)
|
||||
if err != nil {
|
||||
h.fail(w, r, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, toAttachmentResponse(attachment))
|
||||
}
|
||||
|
||||
func (h *Handler) DownloadAttachment(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := attachmentIDParam(r)
|
||||
if err != nil {
|
||||
h.fail(w, r, err)
|
||||
return
|
||||
}
|
||||
query := r.URL.Query()
|
||||
variant := query.Get("variant")
|
||||
if query.Has("variant") && variant == "" {
|
||||
h.fail(w, r, badRequest("variant must be original"))
|
||||
return
|
||||
}
|
||||
attachment, content, err := h.attachments.Open(r.Context(), actor(r), id, variant)
|
||||
if err != nil {
|
||||
h.fail(w, r, err)
|
||||
return
|
||||
}
|
||||
defer content.Close()
|
||||
w.Header().Set("Content-Type", attachment.ContentType)
|
||||
w.Header().Set("Content-Length", strconv.FormatInt(attachment.SizeBytes, 10))
|
||||
w.WriteHeader(http.StatusOK)
|
||||
if _, err := io.Copy(w, content); err != nil {
|
||||
h.log.Error("attachment download interrupted", "request_id", w.Header().Get(headerRequestID), "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
func attachmentIDParam(r *http.Request) (uuid.UUID, error) {
|
||||
id, ok := parseUUID(chi.URLParam(r, "attachment_id"))
|
||||
if !ok {
|
||||
return uuid.UUID{}, badRequest("attachment_id must be a UUID")
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"messenger/internal/domain"
|
||||
)
|
||||
|
||||
func (h *Handler) CreateSession(w http.ResponseWriter, r *http.Request) {
|
||||
username, password, ok := basicCredentials(r)
|
||||
if !ok {
|
||||
h.failLogin(w, r, domain.ErrUnauthorized)
|
||||
return
|
||||
}
|
||||
session, err := h.auth.Login(r.Context(), username, password)
|
||||
if err != nil {
|
||||
h.failLogin(w, r, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusCreated, toSessionResponse(session))
|
||||
}
|
||||
|
||||
func (h *Handler) failLogin(w http.ResponseWriter, r *http.Request, err error) {
|
||||
if errors.Is(err, domain.ErrUnauthorized) {
|
||||
w.Header().Set(headerWWWAuthenticate, basicChallenge)
|
||||
}
|
||||
h.fail(w, r, err)
|
||||
}
|
||||
|
||||
func (h *Handler) DeleteSession(w http.ResponseWriter, r *http.Request) {
|
||||
if err := h.auth.Logout(r.Context(), current(r).token); err != nil {
|
||||
h.fail(w, r, err)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func basicCredentials(r *http.Request) (string, string, bool) {
|
||||
encoded, ok := authCredentials(r, "Basic")
|
||||
if !ok {
|
||||
return "", "", false
|
||||
}
|
||||
raw, err := base64.StdEncoding.DecodeString(encoded)
|
||||
if err != nil || !utf8.Valid(raw) {
|
||||
return "", "", false
|
||||
}
|
||||
username, password, ok := strings.Cut(string(raw), ":")
|
||||
return username, password, ok
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package handler_test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
deadDatabaseURL = "postgres://messenger:x@127.0.0.1:1/messenger?sslmode=disable"
|
||||
deadRedisURL = "redis://127.0.0.1:1/0"
|
||||
)
|
||||
|
||||
func TestSessionExpiry(t *testing.T) {
|
||||
dbURL, redisURL := testURLs(t)
|
||||
srv := newStack(t, dbURL, redisURL, 2*time.Second)
|
||||
alice := createUser(t, srv, "alice")
|
||||
|
||||
if status, _ := do(t, srv, http.MethodGet, "/api/v1/users/me", alice.Token, ""); status != http.StatusOK {
|
||||
t.Fatalf("fresh session: got %d, want 200", status)
|
||||
}
|
||||
time.Sleep(3 * time.Second)
|
||||
resp := request(t, srv, http.MethodGet, "/api/v1/users/me", alice.Token, "")
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusUnauthorized {
|
||||
t.Fatalf("expired session: got %d, want 401", resp.StatusCode)
|
||||
}
|
||||
if got := resp.Header.Get("WWW-Authenticate"); !strings.HasPrefix(got, "Bearer") {
|
||||
t.Errorf("WWW-Authenticate = %q, want Bearer challenge", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogout(t *testing.T) {
|
||||
srv := newServer(t)
|
||||
alice := createUser(t, srv, "alice")
|
||||
|
||||
resp := request(t, srv, http.MethodDelete, "/api/v1/auth/sessions/current", alice.Token, "")
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusNoContent {
|
||||
t.Fatalf("logout: got %d, want 204", resp.StatusCode)
|
||||
}
|
||||
if status, _ := do(t, srv, http.MethodGet, "/api/v1/users/me", alice.Token, ""); status != http.StatusUnauthorized {
|
||||
t.Errorf("revoked token on /users/me: got %d, want 401", status)
|
||||
}
|
||||
if status, _ := do(t, srv, http.MethodDelete, "/api/v1/auth/sessions/current", alice.Token, ""); status != http.StatusUnauthorized {
|
||||
t.Errorf("repeated logout: got %d, want 401", status)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrongPassword(t *testing.T) {
|
||||
srv := newServer(t)
|
||||
alice := createUser(t, srv, "alice")
|
||||
|
||||
for name, password := range map[string]string{"wrong password": "Wrong-password-123", "unknown user": alice.Password} {
|
||||
username := alice.Username
|
||||
if name == "unknown user" {
|
||||
username = "nobody_" + alice.Username[:10]
|
||||
}
|
||||
resp := login(t, srv, username, password)
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusUnauthorized {
|
||||
t.Errorf("%s: got %d, want 401", name, resp.StatusCode)
|
||||
}
|
||||
if got := resp.Header.Get("WWW-Authenticate"); got != `Basic realm="messenger", charset="UTF-8"` {
|
||||
t.Errorf("%s: WWW-Authenticate = %q", name, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRedisUnavailable(t *testing.T) {
|
||||
dbURL, _ := testURLs(t)
|
||||
srv := newStack(t, dbURL, deadRedisURL, time.Hour)
|
||||
|
||||
if status, body := do(t, srv, http.MethodGet, "/api/v1/users/me", "some-token-from-before-the-outage", ""); status != http.StatusServiceUnavailable || errorCode(body) != "unavailable" {
|
||||
t.Errorf("bearer request: got %d %q, want 503 unavailable", status, errorCode(body))
|
||||
}
|
||||
if status, _ := do(t, srv, http.MethodGet, "/api/v1/users/me", "", ""); status != http.StatusUnauthorized {
|
||||
t.Errorf("anonymous request: got %d, want 401", status)
|
||||
}
|
||||
if status, _ := do(t, srv, http.MethodGet, "/health/ready", "", ""); status != http.StatusServiceUnavailable {
|
||||
t.Errorf("ready: got %d, want 503", status)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDatabaseUnavailable(t *testing.T) {
|
||||
dbURL, redisURL := testURLs(t)
|
||||
healthy := newStack(t, dbURL, redisURL, time.Hour)
|
||||
alice := createUser(t, healthy, "alice")
|
||||
|
||||
srv := newStack(t, deadDatabaseURL, redisURL, time.Hour)
|
||||
if status, body := do(t, srv, http.MethodGet, "/api/v1/users/me", alice.Token, ""); status != http.StatusServiceUnavailable || errorCode(body) != "unavailable" {
|
||||
t.Errorf("valid session: got %d %q, want 503 unavailable", status, errorCode(body))
|
||||
}
|
||||
if status, _ := do(t, srv, http.MethodPost, "/api/v1/users", "",
|
||||
`{"username":"db_down_user","display_name":"X","password":"long-enough-pass"}`); status != http.StatusServiceUnavailable {
|
||||
t.Errorf("register: got %d, want 503", status)
|
||||
}
|
||||
if status, _ := do(t, srv, http.MethodGet, "/health/ready", "", ""); status != http.StatusServiceUnavailable {
|
||||
t.Errorf("ready: got %d, want 503", status)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"uuid"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
"messenger/internal/domain"
|
||||
"messenger/internal/service"
|
||||
)
|
||||
|
||||
func (h *Handler) CreateChat(w http.ResponseWriter, r *http.Request) {
|
||||
var req createChatRequest
|
||||
if err := decodeJSON(w, r, &req); err != nil {
|
||||
h.fail(w, r, err)
|
||||
return
|
||||
}
|
||||
if req.Title == nil || req.MemberIDs == nil {
|
||||
h.fail(w, r, badRequest("title and member_ids are required"))
|
||||
return
|
||||
}
|
||||
memberIDs := make([]uuid.UUID, len(*req.MemberIDs))
|
||||
for i, s := range *req.MemberIDs {
|
||||
id, ok := parseUUID(s)
|
||||
if !ok {
|
||||
h.fail(w, r, badRequest("member_ids must contain UUIDs"))
|
||||
return
|
||||
}
|
||||
memberIDs[i] = id
|
||||
}
|
||||
chat, err := h.chats.Create(r.Context(), actor(r), *req.Title, memberIDs)
|
||||
if err != nil {
|
||||
h.fail(w, r, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusCreated, toChatResponse(chat))
|
||||
}
|
||||
|
||||
func (h *Handler) ListChats(w http.ResponseWriter, r *http.Request) {
|
||||
limit, cursor, err := pageParams(r)
|
||||
if err != nil {
|
||||
h.fail(w, r, err)
|
||||
return
|
||||
}
|
||||
page, err := h.chats.List(r.Context(), actor(r), cursor, limit)
|
||||
if err != nil {
|
||||
h.fail(w, r, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, toPageResponse(page, toChatResponse))
|
||||
}
|
||||
|
||||
func (h *Handler) GetChat(w http.ResponseWriter, r *http.Request) {
|
||||
chatID, err := chatIDParam(r)
|
||||
if err != nil {
|
||||
h.fail(w, r, err)
|
||||
return
|
||||
}
|
||||
chat, err := h.chats.Get(r.Context(), actor(r), chatID)
|
||||
if err != nil {
|
||||
h.fail(w, r, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, toChatResponse(chat))
|
||||
}
|
||||
|
||||
func chatIDParam(r *http.Request) (uuid.UUID, error) {
|
||||
id, ok := parseUUID(chi.URLParam(r, "chat_id"))
|
||||
if !ok {
|
||||
return uuid.UUID{}, badRequest("chat_id must be a UUID")
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func parseUUID(s string) (uuid.UUID, bool) {
|
||||
if len(s) != 36 {
|
||||
return uuid.UUID{}, false
|
||||
}
|
||||
id, err := uuid.Parse(s)
|
||||
return id, err == nil
|
||||
}
|
||||
|
||||
func pageParams(r *http.Request) (int, *domain.Cursor, error) {
|
||||
q := r.URL.Query()
|
||||
limit := service.DefaultLimit
|
||||
if q.Has("limit") {
|
||||
n, err := strconv.Atoi(q.Get("limit"))
|
||||
if err != nil {
|
||||
return 0, nil, badRequest("limit must be an integer")
|
||||
}
|
||||
limit = n
|
||||
}
|
||||
var cursor *domain.Cursor
|
||||
if q.Has("cursor") {
|
||||
c, ok := decodeCursor(q.Get("cursor"))
|
||||
if !ok {
|
||||
return 0, nil, badRequest("invalid cursor")
|
||||
}
|
||||
cursor = &c
|
||||
}
|
||||
return limit, cursor, nil
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"uuid"
|
||||
|
||||
"messenger/internal/domain"
|
||||
)
|
||||
|
||||
func encodeCursor(c domain.Cursor) string {
|
||||
raw := c.Scope.String() + "." + strconv.FormatInt(c.CreatedAt.UnixMicro(), 10) + "." + c.ID.String()
|
||||
return base64.RawURLEncoding.EncodeToString([]byte(raw))
|
||||
}
|
||||
|
||||
func decodeCursor(s string) (domain.Cursor, bool) {
|
||||
raw, err := base64.RawURLEncoding.DecodeString(s)
|
||||
if err != nil {
|
||||
return domain.Cursor{}, false
|
||||
}
|
||||
parts := strings.Split(string(raw), ".")
|
||||
if len(parts) != 3 {
|
||||
return domain.Cursor{}, false
|
||||
}
|
||||
scope, err1 := uuid.Parse(parts[0])
|
||||
micros, err2 := strconv.ParseInt(parts[1], 10, 64)
|
||||
id, err3 := uuid.Parse(parts[2])
|
||||
if err1 != nil || err2 != nil || err3 != nil {
|
||||
return domain.Cursor{}, false
|
||||
}
|
||||
return domain.Cursor{Scope: scope, CreatedAt: time.UnixMicro(micros).UTC(), ID: id}, true
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
"uuid"
|
||||
|
||||
"messenger/internal/domain"
|
||||
)
|
||||
|
||||
type createUserRequest struct {
|
||||
Username *string `json:"username"`
|
||||
DisplayName *string `json:"display_name"`
|
||||
Password *string `json:"password"`
|
||||
}
|
||||
|
||||
type createChatRequest struct {
|
||||
Title *string `json:"title"`
|
||||
MemberIDs *[]string `json:"member_ids"`
|
||||
}
|
||||
|
||||
type createMessageRequest struct {
|
||||
Text *string `json:"text"`
|
||||
AttachmentIDs json.RawMessage `json:"attachment_ids"`
|
||||
}
|
||||
|
||||
type userResponse struct {
|
||||
ID string `json:"id"`
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display_name"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
}
|
||||
|
||||
type chatResponse struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
MemberIDs []string `json:"member_ids"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
}
|
||||
|
||||
type messageResponse struct {
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
SenderID string `json:"sender_id"`
|
||||
Text string `json:"text"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
AttachmentIDs []string `json:"attachment_ids"`
|
||||
}
|
||||
|
||||
type variantResponse struct {
|
||||
Name string `json:"name"`
|
||||
ContentType string `json:"content_type"`
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
SHA256 string `json:"sha256"`
|
||||
}
|
||||
|
||||
type attachmentResponse struct {
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
OwnerID string `json:"owner_id"`
|
||||
Filename string `json:"filename"`
|
||||
ContentType string `json:"content_type"`
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
SHA256 string `json:"sha256"`
|
||||
Status string `json:"status"`
|
||||
Variants []variantResponse `json:"variants"`
|
||||
ErrorCode *string `json:"error_code"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
|
||||
type pageResponse[T any] struct {
|
||||
Items []T `json:"items"`
|
||||
NextCursor *string `json:"next_cursor"`
|
||||
}
|
||||
|
||||
type sessionResponse struct {
|
||||
Token string `json:"token"`
|
||||
TokenType string `json:"token_type"`
|
||||
ExpiresAt string `json:"expires_at"`
|
||||
User userResponse `json:"user"`
|
||||
}
|
||||
|
||||
type healthResponse struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
type errorBody struct {
|
||||
Error errorDetails `json:"error"`
|
||||
}
|
||||
|
||||
type errorDetails struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
RequestID string `json:"request_id"`
|
||||
}
|
||||
|
||||
const timeLayout = "2006-01-02T15:04:05.000000Z"
|
||||
|
||||
func formatTime(t time.Time) string { return t.UTC().Format(timeLayout) }
|
||||
|
||||
func toUserResponse(u domain.User) userResponse {
|
||||
return userResponse{
|
||||
ID: u.ID.String(),
|
||||
Username: u.Username,
|
||||
DisplayName: u.DisplayName,
|
||||
CreatedAt: formatTime(u.CreatedAt),
|
||||
}
|
||||
}
|
||||
|
||||
func toSessionResponse(s domain.Session) sessionResponse {
|
||||
return sessionResponse{
|
||||
Token: s.Token,
|
||||
TokenType: "Bearer",
|
||||
ExpiresAt: formatTime(s.ExpiresAt),
|
||||
User: toUserResponse(s.User),
|
||||
}
|
||||
}
|
||||
|
||||
func toChatResponse(c domain.Chat) chatResponse {
|
||||
members := make([]string, len(c.MemberIDs))
|
||||
for i, id := range c.MemberIDs {
|
||||
members[i] = id.String()
|
||||
}
|
||||
return chatResponse{
|
||||
ID: c.ID.String(),
|
||||
Title: c.Title,
|
||||
MemberIDs: members,
|
||||
CreatedAt: formatTime(c.CreatedAt),
|
||||
}
|
||||
}
|
||||
|
||||
func toMessageResponse(m domain.Message) messageResponse {
|
||||
return messageResponse{
|
||||
ID: m.ID.String(),
|
||||
ChatID: m.ChatID.String(),
|
||||
SenderID: m.SenderID.String(),
|
||||
Text: m.Text,
|
||||
CreatedAt: formatTime(m.CreatedAt),
|
||||
AttachmentIDs: toIDStrings(m.AttachmentIDs),
|
||||
}
|
||||
}
|
||||
|
||||
func toAttachmentResponse(a domain.Attachment) attachmentResponse {
|
||||
return attachmentResponse{
|
||||
ID: a.ID.String(),
|
||||
ChatID: a.ChatID.String(),
|
||||
OwnerID: a.OwnerID.String(),
|
||||
Filename: a.Filename,
|
||||
ContentType: a.ContentType,
|
||||
SizeBytes: a.SizeBytes,
|
||||
SHA256: a.SHA256,
|
||||
Status: a.Status,
|
||||
Variants: []variantResponse{},
|
||||
ErrorCode: a.ErrorCode,
|
||||
CreatedAt: formatTime(a.CreatedAt),
|
||||
UpdatedAt: formatTime(a.UpdatedAt),
|
||||
}
|
||||
}
|
||||
|
||||
func toIDStrings(ids []uuid.UUID) []string {
|
||||
result := make([]string, len(ids))
|
||||
for i, id := range ids {
|
||||
result[i] = id.String()
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func toPageResponse[D, R any](p domain.Page[D], conv func(D) R) pageResponse[R] {
|
||||
items := make([]R, len(p.Items))
|
||||
for i, it := range p.Items {
|
||||
items[i] = conv(it)
|
||||
}
|
||||
resp := pageResponse[R]{Items: items}
|
||||
if p.Next != nil {
|
||||
s := encodeCursor(*p.Next)
|
||||
resp.NextCursor = &s
|
||||
}
|
||||
return resp
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"messenger/internal/domain"
|
||||
)
|
||||
|
||||
type apiError struct {
|
||||
status int
|
||||
code string
|
||||
message string
|
||||
}
|
||||
|
||||
func (e *apiError) Error() string { return e.message }
|
||||
|
||||
func badRequest(msg string) *apiError {
|
||||
return &apiError{http.StatusBadRequest, "invalid_request", msg}
|
||||
}
|
||||
|
||||
func writeError(w http.ResponseWriter, status int, code, message string) {
|
||||
writeJSON(w, status, errorBody{Error: errorDetails{
|
||||
Code: code,
|
||||
Message: message,
|
||||
RequestID: w.Header().Get(headerRequestID),
|
||||
}})
|
||||
}
|
||||
|
||||
func (h *Handler) fail(w http.ResponseWriter, r *http.Request, err error) {
|
||||
var ae *apiError
|
||||
switch {
|
||||
case errors.As(err, &ae):
|
||||
writeError(w, ae.status, ae.code, ae.message)
|
||||
case errors.Is(err, domain.ErrInvalidInput):
|
||||
writeError(w, http.StatusBadRequest, "invalid_request", err.Error())
|
||||
case errors.Is(err, domain.ErrUnauthorized):
|
||||
if w.Header().Get(headerWWWAuthenticate) == "" {
|
||||
w.Header().Set(headerWWWAuthenticate, bearerChallenge)
|
||||
}
|
||||
writeError(w, http.StatusUnauthorized, "unauthorized", "valid authentication is required")
|
||||
case errors.Is(err, domain.ErrNotFound):
|
||||
writeError(w, http.StatusNotFound, "not_found", "resource not found")
|
||||
case errors.Is(err, domain.ErrConflict):
|
||||
writeError(w, http.StatusConflict, "conflict", err.Error())
|
||||
case errors.Is(err, domain.ErrTooLarge):
|
||||
writeError(w, http.StatusRequestEntityTooLarge, "payload_too_large", err.Error())
|
||||
case errors.Is(err, domain.ErrUnsupportedMedia):
|
||||
writeError(w, http.StatusUnsupportedMediaType, "unsupported_media_type", err.Error())
|
||||
case errors.Is(err, domain.ErrUnavailable):
|
||||
h.log.Error("dependency unavailable", "request_id", w.Header().Get(headerRequestID),
|
||||
"method", r.Method, "path", r.URL.Path, "err", err)
|
||||
writeError(w, http.StatusServiceUnavailable, "unavailable", "service temporarily unavailable")
|
||||
default:
|
||||
h.log.Error("unexpected error", "request_id", w.Header().Get(headerRequestID),
|
||||
"method", r.Method, "path", r.URL.Path, "err", err)
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "internal server error")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"sync/atomic"
|
||||
|
||||
"messenger/internal/service"
|
||||
)
|
||||
|
||||
type Pinger func(ctx context.Context) error
|
||||
|
||||
type Handler struct {
|
||||
users *service.UserService
|
||||
auth *service.AuthService
|
||||
chats *service.ChatService
|
||||
messages *service.MessageService
|
||||
attachments *service.AttachmentService
|
||||
deps []Pinger
|
||||
log *slog.Logger
|
||||
draining atomic.Bool
|
||||
}
|
||||
|
||||
func New(users *service.UserService, auth *service.AuthService, chats *service.ChatService,
|
||||
messages *service.MessageService, attachments *service.AttachmentService,
|
||||
deps []Pinger, log *slog.Logger) *Handler {
|
||||
return &Handler{users: users, auth: auth, chats: chats, messages: messages,
|
||||
attachments: attachments, deps: deps, log: log}
|
||||
}
|
||||
|
||||
func (h *Handler) StartDraining() {
|
||||
h.draining.Store(true)
|
||||
}
|
||||
|
||||
func (h *Handler) NotFound(w http.ResponseWriter, _ *http.Request) {
|
||||
writeError(w, http.StatusNotFound, "not_found", "resource not found")
|
||||
}
|
||||
|
||||
func (h *Handler) MethodNotAllowed(w http.ResponseWriter, _ *http.Request) {
|
||||
writeError(w, http.StatusMethodNotAllowed, "invalid_request", "method not allowed")
|
||||
}
|
||||
@@ -0,0 +1,359 @@
|
||||
package handler_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
"uuid"
|
||||
|
||||
"messenger/internal/domain"
|
||||
"messenger/internal/handler"
|
||||
"messenger/internal/repository/postgres"
|
||||
"messenger/internal/repository/redis"
|
||||
"messenger/internal/router"
|
||||
"messenger/internal/service"
|
||||
)
|
||||
|
||||
type stubObjectStore struct {
|
||||
objects map[string][]byte
|
||||
}
|
||||
|
||||
func newStubObjectStore() *stubObjectStore {
|
||||
return &stubObjectStore{objects: map[string][]byte{}}
|
||||
}
|
||||
|
||||
func (s *stubObjectStore) Put(_ context.Context, key string, r io.Reader, _ int64, _ string) error {
|
||||
data, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.objects[key] = data
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *stubObjectStore) Get(_ context.Context, key string) (io.ReadCloser, error) {
|
||||
data, ok := s.objects[key]
|
||||
if !ok {
|
||||
return nil, domain.ErrNotFound
|
||||
}
|
||||
return io.NopCloser(strings.NewReader(string(data))), nil
|
||||
}
|
||||
|
||||
func (s *stubObjectStore) Delete(_ context.Context, key string) error {
|
||||
delete(s.objects, key)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *stubObjectStore) Ping(context.Context) error { return nil }
|
||||
|
||||
func testURLs(t *testing.T) (string, string) {
|
||||
t.Helper()
|
||||
dbURL, redisURL := os.Getenv("TEST_DATABASE_URL"), os.Getenv("TEST_REDIS_URL")
|
||||
if dbURL == "" || redisURL == "" {
|
||||
t.Skip("TEST_DATABASE_URL and TEST_REDIS_URL are required")
|
||||
}
|
||||
return dbURL, redisURL
|
||||
}
|
||||
|
||||
func newServer(t *testing.T) *httptest.Server {
|
||||
t.Helper()
|
||||
dbURL, redisURL := testURLs(t)
|
||||
return newStack(t, dbURL, redisURL, time.Hour)
|
||||
}
|
||||
|
||||
func newStack(t *testing.T, dbURL, redisURL string, ttl time.Duration) *httptest.Server {
|
||||
t.Helper()
|
||||
pool, err := postgres.NewPool(context.Background(), dbURL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(pool.Close)
|
||||
store := postgres.New(pool)
|
||||
client, err := redis.NewClient(redisURL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { client.Close() })
|
||||
sessions := redis.NewSessionStore(client)
|
||||
|
||||
users := service.NewUserService(store)
|
||||
auth := service.NewAuthService(store, sessions, ttl)
|
||||
chats := service.NewChatService(store)
|
||||
messages := service.NewMessageService(chats, store)
|
||||
attachments := service.NewAttachmentService(chats, store, newStubObjectStore(), 10485760, 40000000, 8, 2)
|
||||
h := handler.New(users, auth, chats, messages, attachments, []handler.Pinger{store.Ping, sessions.Ping}, slog.New(slog.DiscardHandler))
|
||||
srv := httptest.NewServer(router.New(h, "test"))
|
||||
t.Cleanup(srv.Close)
|
||||
return srv
|
||||
}
|
||||
|
||||
type testUser struct {
|
||||
ID string
|
||||
Username string
|
||||
Password string
|
||||
Token string
|
||||
}
|
||||
|
||||
func request(t *testing.T, srv *httptest.Server, method, path, token, body string) *http.Response {
|
||||
t.Helper()
|
||||
req, err := http.NewRequest(method, srv.URL+path, strings.NewReader(body))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if body != "" {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
if token != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
}
|
||||
if method == http.MethodPost && strings.HasSuffix(path, "/messages") {
|
||||
req.Header.Set("Idempotency-Key", uuid.New().String())
|
||||
}
|
||||
resp, err := srv.Client().Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return resp
|
||||
}
|
||||
|
||||
func do(t *testing.T, srv *httptest.Server, method, path, token, body string) (int, map[string]any) {
|
||||
t.Helper()
|
||||
resp := request(t, srv, method, path, token, body)
|
||||
defer resp.Body.Close()
|
||||
var out map[string]any
|
||||
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
|
||||
t.Fatalf("%s %s: invalid JSON response: %v", method, path, err)
|
||||
}
|
||||
return resp.StatusCode, out
|
||||
}
|
||||
|
||||
func errorCode(body map[string]any) string {
|
||||
e, _ := body["error"].(map[string]any)
|
||||
code, _ := e["code"].(string)
|
||||
return code
|
||||
}
|
||||
|
||||
func basic(username, password string) string {
|
||||
return "Basic " + base64.StdEncoding.EncodeToString([]byte(username+":"+password))
|
||||
}
|
||||
|
||||
func login(t *testing.T, srv *httptest.Server, username, password string) *http.Response {
|
||||
t.Helper()
|
||||
req, err := http.NewRequest(http.MethodPost, srv.URL+"/api/v1/auth/sessions", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.Header.Set("Authorization", basic(username, password))
|
||||
resp, err := srv.Client().Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return resp
|
||||
}
|
||||
|
||||
func createUser(t *testing.T, srv *httptest.Server, label string) testUser {
|
||||
t.Helper()
|
||||
u := testUser{
|
||||
Username: label + "_" + strings.ReplaceAll(uuid.New().String(), "-", "")[:12],
|
||||
Password: "Test-password-" + label,
|
||||
}
|
||||
status, body := do(t, srv, http.MethodPost, "/api/v1/users", "",
|
||||
fmt.Sprintf(`{"username":%q,"display_name":"Test","password":%q}`, u.Username, u.Password))
|
||||
if status != http.StatusCreated {
|
||||
t.Fatalf("create user %s: status %d", u.Username, status)
|
||||
}
|
||||
u.ID = body["id"].(string)
|
||||
|
||||
resp := login(t, srv, u.Username, u.Password)
|
||||
defer resp.Body.Close()
|
||||
var session map[string]any
|
||||
if err := json.NewDecoder(resp.Body).Decode(&session); err != nil || resp.StatusCode != http.StatusCreated {
|
||||
t.Fatalf("login %s: status %d", u.Username, resp.StatusCode)
|
||||
}
|
||||
u.Token = session["token"].(string)
|
||||
return u
|
||||
}
|
||||
|
||||
func createChat(t *testing.T, srv *httptest.Server, owner testUser, members ...testUser) string {
|
||||
t.Helper()
|
||||
ids := make([]string, len(members))
|
||||
for i, m := range members {
|
||||
ids[i] = m.ID
|
||||
}
|
||||
raw, _ := json.Marshal(ids)
|
||||
status, body := do(t, srv, http.MethodPost, "/api/v1/chats", owner.Token,
|
||||
fmt.Sprintf(`{"title":"Test","member_ids":%s}`, raw))
|
||||
if status != http.StatusCreated {
|
||||
t.Fatalf("create chat: status %d", status)
|
||||
}
|
||||
return body["id"].(string)
|
||||
}
|
||||
|
||||
func TestInvalidJSON(t *testing.T) {
|
||||
srv := newServer(t)
|
||||
for _, body := range []string{`{"username":`, `not json`, `{"username":5,"display_name":"x","password":"long-enough-pass"}`} {
|
||||
status, resp := do(t, srv, http.MethodPost, "/api/v1/users", "", body)
|
||||
if status != http.StatusBadRequest || errorCode(resp) != "invalid_request" {
|
||||
t.Errorf("body %q: got %d %q, want 400 invalid_request", body, status, errorCode(resp))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestForeignChat(t *testing.T) {
|
||||
srv := newServer(t)
|
||||
alice := createUser(t, srv, "alice")
|
||||
bob := createUser(t, srv, "bob")
|
||||
eve := createUser(t, srv, "eve")
|
||||
chat := createChat(t, srv, alice, alice, bob)
|
||||
|
||||
cases := []struct{ method, path, body string }{
|
||||
{http.MethodGet, "/api/v1/chats/" + chat, ""},
|
||||
{http.MethodGet, "/api/v1/chats/" + chat + "/messages", ""},
|
||||
{http.MethodPost, "/api/v1/chats/" + chat + "/messages", `{"text":"hi"}`},
|
||||
}
|
||||
for _, c := range cases {
|
||||
status, resp := do(t, srv, c.method, c.path, eve.Token, c.body)
|
||||
if status != http.StatusNotFound || errorCode(resp) != "not_found" {
|
||||
t.Errorf("%s %s by outsider: got %d, want 404", c.method, c.path, status)
|
||||
}
|
||||
}
|
||||
|
||||
status, resp := do(t, srv, http.MethodGet, "/api/v1/chats", eve.Token, "")
|
||||
if status != http.StatusOK || len(resp["items"].([]any)) != 0 {
|
||||
t.Errorf("outsider chat list: got %d %v, want 200 with no items", status, resp["items"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmptyMessage(t *testing.T) {
|
||||
srv := newServer(t)
|
||||
alice := createUser(t, srv, "alice")
|
||||
chat := createChat(t, srv, alice, alice)
|
||||
|
||||
for _, text := range []string{"", " ", "\n\t"} {
|
||||
body, _ := json.Marshal(map[string]string{"text": text})
|
||||
status, resp := do(t, srv, http.MethodPost, "/api/v1/chats/"+chat+"/messages", alice.Token, string(body))
|
||||
if status != http.StatusBadRequest || errorCode(resp) != "invalid_request" {
|
||||
t.Errorf("text %q: got %d, want 400", text, status)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDuplicateUsername(t *testing.T) {
|
||||
srv := newServer(t)
|
||||
alice := createUser(t, srv, "alice")
|
||||
status, resp := do(t, srv, http.MethodPost, "/api/v1/users", "",
|
||||
fmt.Sprintf(`{"username":%q,"display_name":"Other","password":"another-password"}`, alice.Username))
|
||||
if status != http.StatusConflict || errorCode(resp) != "conflict" {
|
||||
t.Errorf("got %d %q, want 409 conflict", status, errorCode(resp))
|
||||
}
|
||||
}
|
||||
|
||||
func TestPaginationMultiplePages(t *testing.T) {
|
||||
srv := newServer(t)
|
||||
alice := createUser(t, srv, "alice")
|
||||
chat := createChat(t, srv, alice, alice)
|
||||
|
||||
const total, limit = 7, 3
|
||||
for i := range total {
|
||||
status, _ := do(t, srv, http.MethodPost, "/api/v1/chats/"+chat+"/messages", alice.Token, fmt.Sprintf(`{"text":"msg %d"}`, i))
|
||||
if status != http.StatusCreated {
|
||||
t.Fatalf("send message %d: status %d", i, status)
|
||||
}
|
||||
}
|
||||
|
||||
seen := map[string]bool{}
|
||||
pages := 0
|
||||
prevCreatedAt := ""
|
||||
cursor := ""
|
||||
for {
|
||||
path := fmt.Sprintf("/api/v1/chats/%s/messages?limit=%d", chat, limit)
|
||||
if cursor != "" {
|
||||
path += "&cursor=" + cursor
|
||||
}
|
||||
status, resp := do(t, srv, http.MethodGet, path, alice.Token, "")
|
||||
if status != http.StatusOK {
|
||||
t.Fatalf("page %d: status %d", pages+1, status)
|
||||
}
|
||||
pages++
|
||||
for _, it := range resp["items"].([]any) {
|
||||
m := it.(map[string]any)
|
||||
id, createdAt := m["id"].(string), m["created_at"].(string)
|
||||
if seen[id] {
|
||||
t.Errorf("message %s returned twice", id)
|
||||
}
|
||||
if createdAt < prevCreatedAt {
|
||||
t.Errorf("messages out of order: %s after %s", createdAt, prevCreatedAt)
|
||||
}
|
||||
seen[id] = true
|
||||
prevCreatedAt = createdAt
|
||||
}
|
||||
next, ok := resp["next_cursor"].(string)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
cursor = next
|
||||
}
|
||||
|
||||
if len(seen) != total {
|
||||
t.Errorf("got %d messages, want %d", len(seen), total)
|
||||
}
|
||||
if want := (total + limit - 1) / limit; pages != want {
|
||||
t.Errorf("got %d pages, want %d", pages, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConcurrentMessages(t *testing.T) {
|
||||
srv := newServer(t)
|
||||
const clients, perClient = 10, 10
|
||||
|
||||
users := make([]testUser, clients)
|
||||
for i := range users {
|
||||
users[i] = createUser(t, srv, fmt.Sprintf("client_%d", i))
|
||||
}
|
||||
chat := createChat(t, srv, users[0], users...)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
errs := make(chan error, clients*perClient)
|
||||
for c, user := range users {
|
||||
wg.Go(func() {
|
||||
for i := range perClient {
|
||||
status, _ := do(t, srv, http.MethodPost, "/api/v1/chats/"+chat+"/messages", user.Token,
|
||||
fmt.Sprintf(`{"text":"client %d msg %d"}`, c, i))
|
||||
if status != http.StatusCreated {
|
||||
errs <- fmt.Errorf("client %d msg %d: status %d", c, i, status)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
wg.Wait()
|
||||
close(errs)
|
||||
for err := range errs {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
status, resp := do(t, srv, http.MethodGet, "/api/v1/chats/"+chat+"/messages?limit=100", users[0].Token, "")
|
||||
if status != http.StatusOK {
|
||||
t.Fatalf("read history: status %d", status)
|
||||
}
|
||||
items := resp["items"].([]any)
|
||||
ids := map[string]bool{}
|
||||
for _, it := range items {
|
||||
ids[it.(map[string]any)["id"].(string)] = true
|
||||
}
|
||||
if len(items) != clients*perClient || len(ids) != clients*perClient {
|
||||
t.Errorf("got %d messages (%d unique), want %d", len(items), len(ids), clients*perClient)
|
||||
}
|
||||
if resp["next_cursor"] != nil {
|
||||
t.Errorf("next_cursor = %v, want null", resp["next_cursor"])
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"messenger/internal/domain"
|
||||
)
|
||||
|
||||
const readyTimeout = time.Second
|
||||
|
||||
func (h *Handler) Live(w http.ResponseWriter, _ *http.Request) {
|
||||
writeJSON(w, http.StatusOK, healthResponse{Status: "ok"})
|
||||
}
|
||||
|
||||
func (h *Handler) Ready(w http.ResponseWriter, r *http.Request) {
|
||||
if h.draining.Load() {
|
||||
h.fail(w, r, fmt.Errorf("%w: draining", domain.ErrUnavailable))
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), readyTimeout)
|
||||
defer cancel()
|
||||
for _, dep := range h.deps {
|
||||
if err := dep(ctx); err != nil {
|
||||
h.fail(w, r, fmt.Errorf("%w: %v", domain.ErrUnavailable, err))
|
||||
return
|
||||
}
|
||||
}
|
||||
writeJSON(w, http.StatusOK, healthResponse{Status: "ok"})
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"mime"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
const maxBodyBytes = 1 << 20
|
||||
|
||||
func writeJSON(w http.ResponseWriter, status int, v any) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(status)
|
||||
_ = json.NewEncoder(w).Encode(v)
|
||||
}
|
||||
|
||||
func decodeJSON(w http.ResponseWriter, r *http.Request, dst any) error {
|
||||
mt, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
||||
if err != nil || mt != "application/json" {
|
||||
return &apiError{http.StatusUnsupportedMediaType, "unsupported_media_type", "Content-Type must be application/json"}
|
||||
}
|
||||
|
||||
raw, err := io.ReadAll(http.MaxBytesReader(w, r.Body, maxBodyBytes))
|
||||
if err != nil {
|
||||
var tooLarge *http.MaxBytesError
|
||||
if errors.As(err, &tooLarge) {
|
||||
return &apiError{http.StatusRequestEntityTooLarge, "payload_too_large", "request body is too large"}
|
||||
}
|
||||
return badRequest("failed to read request body")
|
||||
}
|
||||
if !utf8.Valid(raw) {
|
||||
return badRequest("request body must be valid UTF-8")
|
||||
}
|
||||
if err := checkKeys(raw, dst); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dec := json.NewDecoder(bytes.NewReader(raw))
|
||||
dec.DisallowUnknownFields()
|
||||
if err := dec.Decode(dst); err != nil {
|
||||
return badRequest("request body must be a valid JSON object matching the schema")
|
||||
}
|
||||
if err := dec.Decode(&struct{}{}); !errors.Is(err, io.EOF) {
|
||||
return badRequest("request body must contain a single JSON object")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkKeys(raw []byte, dst any) error {
|
||||
allowed := jsonFields(dst)
|
||||
dec := json.NewDecoder(bytes.NewReader(raw))
|
||||
if tok, err := dec.Token(); err != nil || tok != json.Delim('{') {
|
||||
return badRequest("request body must be a JSON object")
|
||||
}
|
||||
for dec.More() {
|
||||
tok, err := dec.Token()
|
||||
if err != nil {
|
||||
return badRequest("request body must be a valid JSON object matching the schema")
|
||||
}
|
||||
key, _ := tok.(string)
|
||||
if !allowed[key] {
|
||||
return badRequest("request body contains unknown field")
|
||||
}
|
||||
var value json.RawMessage
|
||||
if err := dec.Decode(&value); err != nil {
|
||||
return badRequest("request body must be a valid JSON object matching the schema")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func jsonFields(dst any) map[string]bool {
|
||||
t := reflect.TypeOf(dst).Elem()
|
||||
fields := make(map[string]bool, t.NumField())
|
||||
for i := range t.NumField() {
|
||||
name, _, _ := strings.Cut(t.Field(i).Tag.Get("json"), ",")
|
||||
fields[name] = true
|
||||
}
|
||||
return fields
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"uuid"
|
||||
)
|
||||
|
||||
const headerIdempotencyKey = "Idempotency-Key"
|
||||
|
||||
var idempotencyKeyRe = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`)
|
||||
|
||||
func (h *Handler) SendMessage(w http.ResponseWriter, r *http.Request) {
|
||||
chatID, err := chatIDParam(r)
|
||||
if err != nil {
|
||||
h.fail(w, r, err)
|
||||
return
|
||||
}
|
||||
key := r.Header.Get(headerIdempotencyKey)
|
||||
if !idempotencyKeyRe.MatchString(key) {
|
||||
h.fail(w, r, badRequest("Idempotency-Key header must match [A-Za-z0-9._:-]{1,128}"))
|
||||
return
|
||||
}
|
||||
var req createMessageRequest
|
||||
if err := decodeJSON(w, r, &req); err != nil {
|
||||
h.fail(w, r, err)
|
||||
return
|
||||
}
|
||||
if req.Text == nil {
|
||||
h.fail(w, r, badRequest("text is required"))
|
||||
return
|
||||
}
|
||||
var attachmentIDs []uuid.UUID
|
||||
if len(req.AttachmentIDs) > 0 {
|
||||
if bytes.Equal(bytes.TrimSpace(req.AttachmentIDs), []byte("null")) {
|
||||
h.fail(w, r, badRequest("attachment_ids must be an array of UUIDs"))
|
||||
return
|
||||
}
|
||||
var raw []string
|
||||
if err := json.Unmarshal(req.AttachmentIDs, &raw); err != nil {
|
||||
h.fail(w, r, badRequest("attachment_ids must be an array of UUIDs"))
|
||||
return
|
||||
}
|
||||
attachmentIDs = make([]uuid.UUID, len(raw))
|
||||
for i, value := range raw {
|
||||
id, ok := parseUUID(value)
|
||||
if !ok {
|
||||
h.fail(w, r, badRequest("attachment_ids must contain UUIDs"))
|
||||
return
|
||||
}
|
||||
attachmentIDs[i] = id
|
||||
}
|
||||
}
|
||||
msg, err := h.messages.Send(r.Context(), actor(r), chatID, key, *req.Text, attachmentIDs)
|
||||
if err != nil {
|
||||
h.fail(w, r, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusCreated, toMessageResponse(msg))
|
||||
}
|
||||
|
||||
func (h *Handler) ListMessages(w http.ResponseWriter, r *http.Request) {
|
||||
chatID, err := chatIDParam(r)
|
||||
if err != nil {
|
||||
h.fail(w, r, err)
|
||||
return
|
||||
}
|
||||
limit, cursor, err := pageParams(r)
|
||||
if err != nil {
|
||||
h.fail(w, r, err)
|
||||
return
|
||||
}
|
||||
page, err := h.messages.History(r.Context(), actor(r), chatID, cursor, limit)
|
||||
if err != nil {
|
||||
h.fail(w, r, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, toPageResponse(page, toMessageResponse))
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
"time"
|
||||
"uuid"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
|
||||
"messenger/internal/domain"
|
||||
)
|
||||
|
||||
const (
|
||||
headerRequestID = "X-Request-Id"
|
||||
headerInstanceID = "X-Instance-Id"
|
||||
headerWWWAuthenticate = "WWW-Authenticate"
|
||||
bearerChallenge = `Bearer realm="messenger"`
|
||||
basicChallenge = `Basic realm="messenger", charset="UTF-8"`
|
||||
)
|
||||
|
||||
func InstanceID(id string) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set(headerInstanceID, id)
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func RequestID(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set(headerRequestID, uuid.New().String())
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handler) AccessLog(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
|
||||
next.ServeHTTP(ww, r)
|
||||
|
||||
route := r.URL.Path
|
||||
if rc := chi.RouteContext(r.Context()); rc != nil && rc.RoutePattern() != "" {
|
||||
route = rc.RoutePattern()
|
||||
}
|
||||
h.log.Info("request",
|
||||
"request_id", w.Header().Get(headerRequestID),
|
||||
"method", r.Method,
|
||||
"route", route,
|
||||
"status", ww.Status(),
|
||||
"duration_ms", time.Since(start).Milliseconds(),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handler) Recoverer(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
defer func() {
|
||||
rec := recover()
|
||||
if rec == nil {
|
||||
return
|
||||
}
|
||||
if rec == http.ErrAbortHandler {
|
||||
panic(rec)
|
||||
}
|
||||
h.log.Error("panic", "request_id", w.Header().Get(headerRequestID),
|
||||
"method", r.Method, "path", r.URL.Path, "panic", rec, "stack", string(debug.Stack()))
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "internal server error")
|
||||
}()
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
type ctxKey struct{}
|
||||
|
||||
type principal struct {
|
||||
user domain.User
|
||||
token string
|
||||
}
|
||||
|
||||
func (h *Handler) Authenticate(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
token, ok := authCredentials(r, "Bearer")
|
||||
if !ok || token == "" {
|
||||
h.fail(w, r, domain.ErrUnauthorized)
|
||||
return
|
||||
}
|
||||
user, err := h.auth.Authenticate(r.Context(), token)
|
||||
if err != nil {
|
||||
h.fail(w, r, err)
|
||||
return
|
||||
}
|
||||
ctx := context.WithValue(r.Context(), ctxKey{}, principal{user: user, token: token})
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
|
||||
func authCredentials(r *http.Request, scheme string) (string, bool) {
|
||||
gotScheme, credentials, ok := strings.Cut(r.Header.Get("Authorization"), " ")
|
||||
if !ok || !strings.EqualFold(gotScheme, scheme) {
|
||||
return "", false
|
||||
}
|
||||
return credentials, true
|
||||
}
|
||||
|
||||
func current(r *http.Request) principal {
|
||||
p, ok := r.Context().Value(ctxKey{}).(principal)
|
||||
if !ok {
|
||||
panic(errors.New("handler: actor requested on unauthenticated route"))
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func actor(r *http.Request) domain.User {
|
||||
return current(r).user
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package handler
|
||||
|
||||
import "net/http"
|
||||
|
||||
func (h *Handler) RegisterUser(w http.ResponseWriter, r *http.Request) {
|
||||
var req createUserRequest
|
||||
if err := decodeJSON(w, r, &req); err != nil {
|
||||
h.fail(w, r, err)
|
||||
return
|
||||
}
|
||||
if req.Username == nil || req.DisplayName == nil || req.Password == nil {
|
||||
h.fail(w, r, badRequest("username, display_name and password are required strings"))
|
||||
return
|
||||
}
|
||||
user, err := h.users.Register(r.Context(), *req.Username, *req.DisplayName, *req.Password)
|
||||
if err != nil {
|
||||
h.fail(w, r, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusCreated, toUserResponse(user))
|
||||
}
|
||||
|
||||
func (h *Handler) CurrentUser(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, toUserResponse(actor(r)))
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package reconcile
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
"uuid"
|
||||
)
|
||||
|
||||
type Object struct {
|
||||
Key string
|
||||
Size int64
|
||||
LastModified time.Time
|
||||
}
|
||||
|
||||
type Metadata struct {
|
||||
ID uuid.UUID
|
||||
ObjectKey string
|
||||
ChatID uuid.UUID
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type Orphan struct {
|
||||
Key string `json:"key"`
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
LastModified string `json:"last_modified"`
|
||||
AgeSeconds int64 `json:"age_seconds"`
|
||||
}
|
||||
|
||||
type Missing struct {
|
||||
ID string `json:"id"`
|
||||
ObjectKey string `json:"object_key"`
|
||||
ChatID string `json:"chat_id"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
Referenced bool `json:"referenced_by_message"`
|
||||
}
|
||||
|
||||
type Report struct {
|
||||
Prefix string `json:"prefix"`
|
||||
GraceSeconds int64 `json:"grace_seconds"`
|
||||
DryRun bool `json:"dry_run"`
|
||||
ScannedObjects int `json:"scanned_objects"`
|
||||
ScannedRows int `json:"scanned_rows"`
|
||||
Truncated bool `json:"truncated"`
|
||||
Orphans []Orphan `json:"orphans"`
|
||||
Missing []Missing `json:"missing"`
|
||||
DeletedObjects int `json:"deleted_objects"`
|
||||
DeletedMetadata int `json:"deleted_metadata"`
|
||||
RetainedMissing int `json:"retained_missing"`
|
||||
DeleteFailures int `json:"delete_failures"`
|
||||
S3Requests int64 `json:"s3_requests"`
|
||||
DBQueries int `json:"db_queries"`
|
||||
DurationMS int64 `json:"duration_ms"`
|
||||
ObjectsPerSecond float64 `json:"objects_per_second"`
|
||||
}
|
||||
|
||||
func Plan(objects []Object, rows []Metadata, referenced map[uuid.UUID]bool, prefix string, now time.Time, grace time.Duration) ([]Orphan, []Missing) {
|
||||
known := make(map[string]struct{}, len(rows))
|
||||
existing := make(map[string]struct{}, len(objects))
|
||||
for _, row := range rows {
|
||||
known[row.ObjectKey] = struct{}{}
|
||||
}
|
||||
for _, object := range objects {
|
||||
existing[object.Key] = struct{}{}
|
||||
}
|
||||
|
||||
orphans := make([]Orphan, 0)
|
||||
for _, object := range objects {
|
||||
if !strings.HasPrefix(object.Key, prefix) {
|
||||
continue
|
||||
}
|
||||
if _, ok := known[object.Key]; ok {
|
||||
continue
|
||||
}
|
||||
age := now.Sub(object.LastModified)
|
||||
if age < grace {
|
||||
continue
|
||||
}
|
||||
orphans = append(orphans, Orphan{
|
||||
Key: object.Key,
|
||||
SizeBytes: object.Size,
|
||||
LastModified: object.LastModified.UTC().Format(time.RFC3339),
|
||||
AgeSeconds: int64(age.Seconds()),
|
||||
})
|
||||
}
|
||||
|
||||
missing := make([]Missing, 0)
|
||||
for _, row := range rows {
|
||||
if !strings.HasPrefix(row.ObjectKey, prefix) {
|
||||
continue
|
||||
}
|
||||
if _, ok := existing[row.ObjectKey]; ok {
|
||||
continue
|
||||
}
|
||||
missing = append(missing, Missing{
|
||||
ID: row.ID.String(),
|
||||
ObjectKey: row.ObjectKey,
|
||||
ChatID: row.ChatID.String(),
|
||||
CreatedAt: row.CreatedAt.UTC().Format(time.RFC3339),
|
||||
Referenced: referenced[row.ID],
|
||||
})
|
||||
}
|
||||
|
||||
sort.Slice(orphans, func(i, j int) bool { return orphans[i].Key < orphans[j].Key })
|
||||
sort.Slice(missing, func(i, j int) bool { return missing[i].ObjectKey < missing[j].ObjectKey })
|
||||
return orphans, missing
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package reconcile
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
"uuid"
|
||||
)
|
||||
|
||||
func TestPlanDetectsOrphansAndMissing(t *testing.T) {
|
||||
now := time.Date(2026, 11, 20, 12, 0, 0, 0, time.UTC)
|
||||
usedID, missingID, unreferencedID := uuid.New(), uuid.New(), uuid.New()
|
||||
objects := []Object{
|
||||
{Key: "chats/a/used", Size: 10, LastModified: now.Add(-2 * time.Hour)},
|
||||
{Key: "chats/a/orphan-old", Size: 20, LastModified: now.Add(-2 * time.Hour)},
|
||||
{Key: "chats/a/orphan-fresh", Size: 30, LastModified: now.Add(-1 * time.Minute)},
|
||||
{Key: "other/prefix/ignored", Size: 40, LastModified: now.Add(-2 * time.Hour)},
|
||||
}
|
||||
rows := []Metadata{
|
||||
{ID: usedID, ObjectKey: "chats/a/used", ChatID: uuid.New(), CreatedAt: now.Add(-2 * time.Hour)},
|
||||
{ID: missingID, ObjectKey: "chats/a/missing-referenced", ChatID: uuid.New(), CreatedAt: now.Add(-2 * time.Hour)},
|
||||
{ID: unreferencedID, ObjectKey: "chats/a/missing-orphan-row", ChatID: uuid.New(), CreatedAt: now.Add(-2 * time.Hour)},
|
||||
}
|
||||
referenced := map[uuid.UUID]bool{missingID: true}
|
||||
|
||||
orphans, missing := Plan(objects, rows, referenced, "chats/", now, time.Hour)
|
||||
|
||||
if len(orphans) != 1 || orphans[0].Key != "chats/a/orphan-old" {
|
||||
t.Fatalf("orphans = %+v, want only chats/a/orphan-old", orphans)
|
||||
}
|
||||
if len(missing) != 2 {
|
||||
t.Fatalf("missing = %+v, want two rows", missing)
|
||||
}
|
||||
byKey := map[string]Missing{}
|
||||
for _, item := range missing {
|
||||
byKey[item.ObjectKey] = item
|
||||
}
|
||||
if !byKey["chats/a/missing-referenced"].Referenced {
|
||||
t.Error("referenced missing row must be flagged")
|
||||
}
|
||||
if byKey["chats/a/missing-orphan-row"].Referenced {
|
||||
t.Error("unreferenced missing row must not be flagged")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlanHonoursGraceAndPrefix(t *testing.T) {
|
||||
now := time.Date(2026, 11, 20, 12, 0, 0, 0, time.UTC)
|
||||
objects := []Object{
|
||||
{Key: "chats/a/fresh", Size: 1, LastModified: now.Add(-30 * time.Second)},
|
||||
{Key: "outside/key", Size: 1, LastModified: now.Add(-48 * time.Hour)},
|
||||
}
|
||||
orphans, _ := Plan(objects, nil, nil, "chats/", now, time.Hour)
|
||||
if len(orphans) != 0 {
|
||||
t.Fatalf("orphans = %+v, want none", orphans)
|
||||
}
|
||||
|
||||
orphans, _ = Plan(objects, nil, nil, "chats/", now, 0)
|
||||
if len(orphans) != 1 || orphans[0].Key != "chats/a/fresh" {
|
||||
t.Fatalf("orphans with zero grace = %+v, want chats/a/fresh", orphans)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlanNeverDeletesReferencedObjects(t *testing.T) {
|
||||
now := time.Date(2026, 11, 20, 12, 0, 0, 0, time.UTC)
|
||||
objects := []Object{{Key: "chats/a/used", Size: 1, LastModified: now.Add(-100 * time.Hour)}}
|
||||
rows := []Metadata{{ID: uuid.New(), ObjectKey: "chats/a/used", ChatID: uuid.New(), CreatedAt: now.Add(-100 * time.Hour)}}
|
||||
|
||||
orphans, missing := Plan(objects, rows, nil, "chats/", now, 0)
|
||||
if len(orphans) != 0 {
|
||||
t.Fatalf("orphans = %+v, want none for referenced object", orphans)
|
||||
}
|
||||
if len(missing) != 0 {
|
||||
t.Fatalf("missing = %+v, want none", missing)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlanOnlyReportsRowsInsidePrefix(t *testing.T) {
|
||||
now := time.Date(2026, 11, 20, 12, 0, 0, 0, time.UTC)
|
||||
rows := []Metadata{
|
||||
{ID: uuid.New(), ObjectKey: "chats/a/inside", ChatID: uuid.New(), CreatedAt: now},
|
||||
{ID: uuid.New(), ObjectKey: "bench/outside", ChatID: uuid.New(), CreatedAt: now},
|
||||
}
|
||||
_, missing := Plan(nil, rows, nil, "chats/", now, 0)
|
||||
if len(missing) != 1 || missing[0].ObjectKey != "chats/a/inside" {
|
||||
t.Fatalf("missing = %+v, want only rows under the scanned prefix", missing)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlanOutputIsSorted(t *testing.T) {
|
||||
now := time.Date(2026, 11, 20, 12, 0, 0, 0, time.UTC)
|
||||
objects := []Object{
|
||||
{Key: "chats/a/z", LastModified: now.Add(-time.Hour)},
|
||||
{Key: "chats/a/b", LastModified: now.Add(-time.Hour)},
|
||||
{Key: "chats/a/m", LastModified: now.Add(-time.Hour)},
|
||||
}
|
||||
orphans, _ := Plan(objects, nil, nil, "chats/", now, 0)
|
||||
if len(orphans) != 3 || orphans[0].Key != "chats/a/b" || orphans[2].Key != "chats/a/z" {
|
||||
t.Fatalf("orphans are not sorted: %+v", orphans)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
DROP TABLE IF EXISTS messages;
|
||||
DROP TABLE IF EXISTS chat_members;
|
||||
DROP TABLE IF EXISTS chats;
|
||||
DROP TABLE IF EXISTS users;
|
||||
@@ -0,0 +1,32 @@
|
||||
CREATE TABLE users (
|
||||
id uuid PRIMARY KEY,
|
||||
username text NOT NULL UNIQUE,
|
||||
display_name text NOT NULL,
|
||||
password_hash text NOT NULL,
|
||||
created_at timestamptz NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE chats (
|
||||
id uuid PRIMARY KEY,
|
||||
title text NOT NULL,
|
||||
created_at timestamptz NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE chat_members (
|
||||
chat_id uuid NOT NULL REFERENCES chats (id) ON DELETE CASCADE,
|
||||
user_id uuid NOT NULL REFERENCES users (id),
|
||||
position int NOT NULL,
|
||||
PRIMARY KEY (chat_id, user_id)
|
||||
);
|
||||
|
||||
CREATE INDEX chat_members_user_id_idx ON chat_members (user_id);
|
||||
|
||||
CREATE TABLE messages (
|
||||
id uuid PRIMARY KEY,
|
||||
chat_id uuid NOT NULL REFERENCES chats (id) ON DELETE CASCADE,
|
||||
sender_id uuid NOT NULL REFERENCES users (id),
|
||||
text text NOT NULL,
|
||||
created_at timestamptz NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX messages_chat_created_id_idx ON messages (chat_id, created_at, id);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE message_idempotency;
|
||||
@@ -0,0 +1,9 @@
|
||||
CREATE TABLE message_idempotency (
|
||||
user_id uuid NOT NULL REFERENCES users (id),
|
||||
chat_id uuid NOT NULL REFERENCES chats (id) ON DELETE CASCADE,
|
||||
key text NOT NULL,
|
||||
request_hash text NOT NULL,
|
||||
message_id uuid NOT NULL REFERENCES messages (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
|
||||
created_at timestamptz NOT NULL,
|
||||
PRIMARY KEY (user_id, chat_id, key)
|
||||
);
|
||||
@@ -0,0 +1,2 @@
|
||||
DROP TABLE IF EXISTS message_attachments;
|
||||
DROP TABLE IF EXISTS attachments;
|
||||
@@ -0,0 +1,25 @@
|
||||
CREATE TABLE attachments (
|
||||
id uuid PRIMARY KEY,
|
||||
chat_id uuid NOT NULL REFERENCES chats (id) ON DELETE CASCADE,
|
||||
owner_id uuid NOT NULL REFERENCES users (id),
|
||||
filename text NOT NULL,
|
||||
content_type text NOT NULL,
|
||||
size_bytes bigint NOT NULL,
|
||||
sha256 text NOT NULL,
|
||||
object_key text NOT NULL UNIQUE,
|
||||
status text NOT NULL,
|
||||
error_code text,
|
||||
created_at timestamptz NOT NULL,
|
||||
updated_at timestamptz NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX attachments_chat_created_id_idx ON attachments (chat_id, created_at, id);
|
||||
|
||||
CREATE TABLE message_attachments (
|
||||
message_id uuid NOT NULL REFERENCES messages (id) ON DELETE CASCADE,
|
||||
attachment_id uuid NOT NULL REFERENCES attachments (id),
|
||||
position int NOT NULL,
|
||||
PRIMARY KEY (message_id, attachment_id)
|
||||
);
|
||||
|
||||
CREATE INDEX message_attachments_attachment_id_idx ON message_attachments (attachment_id);
|
||||
@@ -0,0 +1,103 @@
|
||||
package postgres_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
"uuid"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"messenger/internal/domain"
|
||||
"messenger/internal/repository/postgres"
|
||||
)
|
||||
|
||||
func newStore(t *testing.T) (*postgres.Store, *pgxpool.Pool) {
|
||||
t.Helper()
|
||||
dbURL := os.Getenv("TEST_DATABASE_URL")
|
||||
if dbURL == "" {
|
||||
t.Skip("TEST_DATABASE_URL is required")
|
||||
}
|
||||
pool, err := postgres.NewPool(context.Background(), dbURL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(pool.Close)
|
||||
return postgres.New(pool), pool
|
||||
}
|
||||
|
||||
func newUser(username string) domain.User {
|
||||
return domain.User{ID: uuid.New(), Username: username, DisplayName: "Test", CreatedAt: time.Now().UTC()}
|
||||
}
|
||||
|
||||
func TestRegisterUsernameRace(t *testing.T) {
|
||||
store, pool := newStore(t)
|
||||
ctx := context.Background()
|
||||
username := "race_" + uuid.New().String()[:8]
|
||||
|
||||
const workers = 20
|
||||
var wg sync.WaitGroup
|
||||
errs := make([]error, workers)
|
||||
start := make(chan struct{})
|
||||
for i := range workers {
|
||||
wg.Go(func() {
|
||||
<-start
|
||||
errs[i] = store.CreateUser(ctx, newUser(username), "hash")
|
||||
})
|
||||
}
|
||||
close(start)
|
||||
wg.Wait()
|
||||
|
||||
created := 0
|
||||
for _, err := range errs {
|
||||
switch {
|
||||
case err == nil:
|
||||
created++
|
||||
case !errors.Is(err, domain.ErrConflict):
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
if created != 1 {
|
||||
t.Errorf("created %d users, want exactly 1", created)
|
||||
}
|
||||
var rows int
|
||||
if err := pool.QueryRow(ctx, `SELECT count(*) FROM users WHERE username = $1`, username).Scan(&rows); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if rows != 1 {
|
||||
t.Errorf("users rows = %d, want 1", rows)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateChatRollback(t *testing.T) {
|
||||
store, pool := newStore(t)
|
||||
ctx := context.Background()
|
||||
alice := newUser("rb_" + uuid.New().String()[:8])
|
||||
if err := store.CreateUser(ctx, alice, "hash"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
chat := domain.Chat{
|
||||
ID: uuid.New(),
|
||||
Title: "rollback",
|
||||
MemberIDs: []uuid.UUID{alice.ID, uuid.New()},
|
||||
CreatedAt: time.Now().UTC(),
|
||||
}
|
||||
if err := store.CreateChat(ctx, chat); !errors.Is(err, domain.ErrNotFound) {
|
||||
t.Fatalf("CreateChat with unknown member: got %v, want ErrNotFound", err)
|
||||
}
|
||||
|
||||
var chats, members int
|
||||
if err := pool.QueryRow(ctx, `SELECT count(*) FROM chats WHERE id = $1`, chat.ID).Scan(&chats); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := pool.QueryRow(ctx, `SELECT count(*) FROM chat_members WHERE chat_id = $1`, chat.ID).Scan(&members); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if chats != 0 || members != 0 {
|
||||
t.Errorf("after rollback: chats=%d chat_members=%d, want 0 and 0", chats, members)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,355 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
"uuid"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"messenger/internal/domain"
|
||||
)
|
||||
|
||||
const (
|
||||
maxConns = 10
|
||||
connectTimeout = 3 * time.Second
|
||||
statementTimeout = "5000"
|
||||
)
|
||||
|
||||
type Store struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewPool(ctx context.Context, databaseURL string) (*pgxpool.Pool, error) {
|
||||
cfg, err := pgxpool.ParseConfig(databaseURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cfg.MaxConns = maxConns
|
||||
cfg.ConnConfig.ConnectTimeout = connectTimeout
|
||||
cfg.ConnConfig.RuntimeParams["statement_timeout"] = statementTimeout
|
||||
return pgxpool.NewWithConfig(ctx, cfg)
|
||||
}
|
||||
|
||||
func New(pool *pgxpool.Pool) *Store {
|
||||
return &Store{pool: pool}
|
||||
}
|
||||
|
||||
func (s *Store) Ping(ctx context.Context) error {
|
||||
return dbErr(s.pool.Ping(ctx))
|
||||
}
|
||||
|
||||
func (s *Store) CreateUser(ctx context.Context, u domain.User, passwordHash string) error {
|
||||
_, err := s.pool.Exec(ctx,
|
||||
`INSERT INTO users (id, username, display_name, password_hash, created_at) VALUES ($1, $2, $3, $4, $5)`,
|
||||
u.ID, u.Username, u.DisplayName, passwordHash, u.CreatedAt)
|
||||
return dbErr(err)
|
||||
}
|
||||
|
||||
func (s *Store) GetUser(ctx context.Context, id uuid.UUID) (domain.User, error) {
|
||||
var u domain.User
|
||||
err := s.pool.QueryRow(ctx,
|
||||
`SELECT id, username, display_name, created_at FROM users WHERE id = $1`, id,
|
||||
).Scan(&u.ID, &u.Username, &u.DisplayName, &u.CreatedAt)
|
||||
return u, dbErr(err)
|
||||
}
|
||||
|
||||
func (s *Store) GetUserByUsername(ctx context.Context, username string) (domain.User, string, error) {
|
||||
var u domain.User
|
||||
var hash string
|
||||
err := s.pool.QueryRow(ctx,
|
||||
`SELECT id, username, display_name, created_at, password_hash FROM users WHERE username = $1`, username,
|
||||
).Scan(&u.ID, &u.Username, &u.DisplayName, &u.CreatedAt, &hash)
|
||||
return u, hash, dbErr(err)
|
||||
}
|
||||
|
||||
func (s *Store) CreateChat(ctx context.Context, c domain.Chat) error {
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return dbErr(err)
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
if _, err := tx.Exec(ctx, `INSERT INTO chats (id, title, created_at) VALUES ($1, $2, $3)`,
|
||||
c.ID, c.Title, c.CreatedAt); err != nil {
|
||||
return dbErr(err)
|
||||
}
|
||||
for i, userID := range c.MemberIDs {
|
||||
if _, err := tx.Exec(ctx, `INSERT INTO chat_members (chat_id, user_id, position) VALUES ($1, $2, $3)`,
|
||||
c.ID, userID, i); err != nil {
|
||||
return dbErr(err)
|
||||
}
|
||||
}
|
||||
return dbErr(tx.Commit(ctx))
|
||||
}
|
||||
|
||||
func (s *Store) GetChat(ctx context.Context, id uuid.UUID) (domain.Chat, error) {
|
||||
var c domain.Chat
|
||||
err := s.pool.QueryRow(ctx, `SELECT id, title, created_at FROM chats WHERE id = $1`, id).
|
||||
Scan(&c.ID, &c.Title, &c.CreatedAt)
|
||||
if err != nil {
|
||||
return domain.Chat{}, dbErr(err)
|
||||
}
|
||||
chats := []domain.Chat{c}
|
||||
if err := s.loadMembers(ctx, chats); err != nil {
|
||||
return domain.Chat{}, err
|
||||
}
|
||||
return chats[0], nil
|
||||
}
|
||||
|
||||
func (s *Store) ListUserChats(ctx context.Context, userID uuid.UUID, after *domain.Cursor, limit int) ([]domain.Chat, bool, error) {
|
||||
var rows pgx.Rows
|
||||
var err error
|
||||
if after == nil {
|
||||
rows, err = s.pool.Query(ctx, `
|
||||
SELECT c.id, c.title, c.created_at
|
||||
FROM chats c
|
||||
JOIN chat_members m ON m.chat_id = c.id
|
||||
WHERE m.user_id = $1
|
||||
ORDER BY c.created_at, c.id
|
||||
LIMIT $2`, userID, limit+1)
|
||||
} else {
|
||||
rows, err = s.pool.Query(ctx, `
|
||||
SELECT c.id, c.title, c.created_at
|
||||
FROM chats c
|
||||
JOIN chat_members m ON m.chat_id = c.id
|
||||
WHERE m.user_id = $1 AND (c.created_at, c.id) > ($2, $3)
|
||||
ORDER BY c.created_at, c.id
|
||||
LIMIT $4`, userID, after.CreatedAt, after.ID, limit+1)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, false, dbErr(err)
|
||||
}
|
||||
chats, err := pgx.CollectRows(rows, func(row pgx.CollectableRow) (domain.Chat, error) {
|
||||
var c domain.Chat
|
||||
err := row.Scan(&c.ID, &c.Title, &c.CreatedAt)
|
||||
return c, err
|
||||
})
|
||||
if err != nil {
|
||||
return nil, false, dbErr(err)
|
||||
}
|
||||
more := len(chats) > limit
|
||||
if more {
|
||||
chats = chats[:limit]
|
||||
}
|
||||
if err := s.loadMembers(ctx, chats); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
return chats, more, nil
|
||||
}
|
||||
|
||||
func (s *Store) loadMembers(ctx context.Context, chats []domain.Chat) error {
|
||||
if len(chats) == 0 {
|
||||
return nil
|
||||
}
|
||||
ids := make([]uuid.UUID, len(chats))
|
||||
index := make(map[uuid.UUID]int, len(chats))
|
||||
for i, c := range chats {
|
||||
ids[i] = c.ID
|
||||
index[c.ID] = i
|
||||
}
|
||||
rows, err := s.pool.Query(ctx,
|
||||
`SELECT chat_id, user_id FROM chat_members WHERE chat_id = ANY($1) ORDER BY chat_id, position`, ids)
|
||||
if err != nil {
|
||||
return dbErr(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var chatID, userID uuid.UUID
|
||||
if err := rows.Scan(&chatID, &userID); err != nil {
|
||||
return dbErr(err)
|
||||
}
|
||||
i := index[chatID]
|
||||
chats[i].MemberIDs = append(chats[i].MemberIDs, userID)
|
||||
}
|
||||
return dbErr(rows.Err())
|
||||
}
|
||||
|
||||
func (s *Store) CreateMessageIdempotent(ctx context.Context, m domain.Message, key, requestHash string) (domain.Message, error) {
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return domain.Message{}, dbErr(err)
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
tag, err := tx.Exec(ctx, `
|
||||
INSERT INTO message_idempotency (user_id, chat_id, key, request_hash, message_id, created_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
ON CONFLICT DO NOTHING`,
|
||||
m.SenderID, m.ChatID, key, requestHash, m.ID, m.CreatedAt)
|
||||
if err != nil {
|
||||
return domain.Message{}, dbErr(err)
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
var storedHash string
|
||||
var stored domain.Message
|
||||
err := tx.QueryRow(ctx, `
|
||||
SELECT k.request_hash, m.id, m.chat_id, m.sender_id, m.text, m.created_at
|
||||
FROM message_idempotency k
|
||||
JOIN messages m ON m.id = k.message_id
|
||||
WHERE k.user_id = $1 AND k.chat_id = $2 AND k.key = $3`,
|
||||
m.SenderID, m.ChatID, key,
|
||||
).Scan(&storedHash, &stored.ID, &stored.ChatID, &stored.SenderID, &stored.Text, &stored.CreatedAt)
|
||||
if err != nil {
|
||||
return domain.Message{}, dbErr(err)
|
||||
}
|
||||
if storedHash != requestHash {
|
||||
return domain.Message{}, domain.ErrConflict
|
||||
}
|
||||
rows, err := tx.Query(ctx,
|
||||
`SELECT attachment_id FROM message_attachments WHERE message_id = $1 ORDER BY position`, stored.ID)
|
||||
if err != nil {
|
||||
return domain.Message{}, dbErr(err)
|
||||
}
|
||||
stored.AttachmentIDs, err = pgx.CollectRows(rows, func(row pgx.CollectableRow) (uuid.UUID, error) {
|
||||
var id uuid.UUID
|
||||
err := row.Scan(&id)
|
||||
return id, err
|
||||
})
|
||||
if err != nil {
|
||||
return domain.Message{}, dbErr(err)
|
||||
}
|
||||
if stored.AttachmentIDs == nil {
|
||||
stored.AttachmentIDs = []uuid.UUID{}
|
||||
}
|
||||
return stored, nil
|
||||
}
|
||||
|
||||
if _, err := tx.Exec(ctx,
|
||||
`INSERT INTO messages (id, chat_id, sender_id, text, created_at) VALUES ($1, $2, $3, $4, $5)`,
|
||||
m.ID, m.ChatID, m.SenderID, m.Text, m.CreatedAt); err != nil {
|
||||
return domain.Message{}, dbErr(err)
|
||||
}
|
||||
if len(m.AttachmentIDs) > 0 {
|
||||
tag, err := tx.Exec(ctx, `
|
||||
INSERT INTO message_attachments (message_id, attachment_id, position)
|
||||
SELECT $1, a.id, u.ord::int
|
||||
FROM unnest($2::uuid[]) WITH ORDINALITY AS u(id, ord)
|
||||
JOIN attachments a ON a.id = u.id AND a.chat_id = $3 AND a.status = $4`,
|
||||
m.ID, m.AttachmentIDs, m.ChatID, domain.AttachmentReady)
|
||||
if err != nil {
|
||||
return domain.Message{}, dbErr(err)
|
||||
}
|
||||
if int(tag.RowsAffected()) != len(m.AttachmentIDs) {
|
||||
return domain.Message{}, domain.ErrNotFound
|
||||
}
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return domain.Message{}, dbErr(err)
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (s *Store) ListMessages(ctx context.Context, chatID uuid.UUID, after *domain.Cursor, limit int) ([]domain.Message, bool, error) {
|
||||
var rows pgx.Rows
|
||||
var err error
|
||||
if after == nil {
|
||||
rows, err = s.pool.Query(ctx, `
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = $1
|
||||
ORDER BY created_at, id
|
||||
LIMIT $2`, chatID, limit+1)
|
||||
} else {
|
||||
rows, err = s.pool.Query(ctx, `
|
||||
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`, chatID, after.CreatedAt, after.ID, limit+1)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, false, dbErr(err)
|
||||
}
|
||||
msgs, err := pgx.CollectRows(rows, func(row pgx.CollectableRow) (domain.Message, error) {
|
||||
var m domain.Message
|
||||
err := row.Scan(&m.ID, &m.ChatID, &m.SenderID, &m.Text, &m.CreatedAt)
|
||||
return m, err
|
||||
})
|
||||
if err != nil {
|
||||
return nil, false, dbErr(err)
|
||||
}
|
||||
more := len(msgs) > limit
|
||||
if more {
|
||||
msgs = msgs[:limit]
|
||||
}
|
||||
if err := s.loadMessageAttachments(ctx, msgs); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
return msgs, more, nil
|
||||
}
|
||||
|
||||
func (s *Store) CreateAttachment(ctx context.Context, a domain.Attachment) error {
|
||||
_, err := s.pool.Exec(ctx, `
|
||||
INSERT INTO attachments (id, chat_id, owner_id, filename, content_type, size_bytes, sha256, object_key, status, error_code, created_at, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)`,
|
||||
a.ID, a.ChatID, a.OwnerID, a.Filename, a.ContentType, a.SizeBytes, a.SHA256, a.ObjectKey, a.Status, a.ErrorCode, a.CreatedAt, a.UpdatedAt)
|
||||
return dbErr(err)
|
||||
}
|
||||
|
||||
func (s *Store) GetAttachment(ctx context.Context, id uuid.UUID) (domain.Attachment, error) {
|
||||
var a domain.Attachment
|
||||
err := s.pool.QueryRow(ctx, `
|
||||
SELECT id, chat_id, owner_id, filename, content_type, size_bytes, sha256, object_key, status, error_code, created_at, updated_at
|
||||
FROM attachments WHERE id = $1`, id,
|
||||
).Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.ContentType, &a.SizeBytes, &a.SHA256, &a.ObjectKey, &a.Status, &a.ErrorCode, &a.CreatedAt, &a.UpdatedAt)
|
||||
return a, dbErr(err)
|
||||
}
|
||||
|
||||
func (s *Store) loadMessageAttachments(ctx context.Context, msgs []domain.Message) error {
|
||||
for i := range msgs {
|
||||
msgs[i].AttachmentIDs = []uuid.UUID{}
|
||||
}
|
||||
if len(msgs) == 0 {
|
||||
return nil
|
||||
}
|
||||
ids := make([]uuid.UUID, len(msgs))
|
||||
index := make(map[uuid.UUID]int, len(msgs))
|
||||
for i, m := range msgs {
|
||||
ids[i] = m.ID
|
||||
index[m.ID] = i
|
||||
}
|
||||
rows, err := s.pool.Query(ctx,
|
||||
`SELECT message_id, attachment_id FROM message_attachments WHERE message_id = ANY($1) ORDER BY message_id, position`, ids)
|
||||
if err != nil {
|
||||
return dbErr(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var messageID, attachmentID uuid.UUID
|
||||
if err := rows.Scan(&messageID, &attachmentID); err != nil {
|
||||
return dbErr(err)
|
||||
}
|
||||
i := index[messageID]
|
||||
msgs[i].AttachmentIDs = append(msgs[i].AttachmentIDs, attachmentID)
|
||||
}
|
||||
return dbErr(rows.Err())
|
||||
}
|
||||
|
||||
func dbErr(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return domain.ErrNotFound
|
||||
}
|
||||
var pgErr *pgconn.PgError
|
||||
if errors.As(err, &pgErr) {
|
||||
switch pgErr.Code {
|
||||
case "23505":
|
||||
return domain.ErrConflict
|
||||
case "23503":
|
||||
return domain.ErrNotFound
|
||||
}
|
||||
switch pgErr.Code[:2] {
|
||||
case "08", "53", "57":
|
||||
return fmt.Errorf("%w: %v", domain.ErrUnavailable, err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("%w: %v", domain.ErrUnavailable, err)
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
"uuid"
|
||||
|
||||
goredis "github.com/redis/go-redis/v9"
|
||||
|
||||
"messenger/internal/domain"
|
||||
)
|
||||
|
||||
const keyPrefix = "session:"
|
||||
|
||||
type SessionStore struct {
|
||||
client *goredis.Client
|
||||
}
|
||||
|
||||
func NewClient(url string) (*goredis.Client, error) {
|
||||
opts, err := goredis.ParseURL(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
opts.PoolSize = 10
|
||||
opts.DialTimeout = 2 * time.Second
|
||||
opts.ReadTimeout = time.Second
|
||||
opts.WriteTimeout = time.Second
|
||||
opts.MaxRetries = 0
|
||||
return goredis.NewClient(opts), nil
|
||||
}
|
||||
|
||||
func NewSessionStore(client *goredis.Client) *SessionStore {
|
||||
return &SessionStore{client: client}
|
||||
}
|
||||
|
||||
func (s *SessionStore) Ping(ctx context.Context) error {
|
||||
return cacheErr(s.client.Ping(ctx).Err())
|
||||
}
|
||||
|
||||
func (s *SessionStore) Create(ctx context.Context, tokenHash string, userID uuid.UUID, expiresAt time.Time) error {
|
||||
return cacheErr(s.client.SetArgs(ctx, keyPrefix+tokenHash, userID.String(),
|
||||
goredis.SetArgs{ExpireAt: expiresAt}).Err())
|
||||
}
|
||||
|
||||
func (s *SessionStore) Get(ctx context.Context, tokenHash string) (uuid.UUID, error) {
|
||||
v, err := s.client.Get(ctx, keyPrefix+tokenHash).Result()
|
||||
if errors.Is(err, goredis.Nil) {
|
||||
return uuid.UUID{}, domain.ErrUnauthorized
|
||||
}
|
||||
if err != nil {
|
||||
return uuid.UUID{}, cacheErr(err)
|
||||
}
|
||||
id, err := uuid.Parse(v)
|
||||
if err != nil {
|
||||
return uuid.UUID{}, domain.ErrUnauthorized
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (s *SessionStore) Delete(ctx context.Context, tokenHash string) error {
|
||||
return cacheErr(s.client.Del(ctx, keyPrefix+tokenHash).Err())
|
||||
}
|
||||
|
||||
func cacheErr(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("%w: %v", domain.ErrUnavailable, err)
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
package s3
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
|
||||
"messenger/internal/domain"
|
||||
)
|
||||
|
||||
type ObjectInfo struct {
|
||||
Key string
|
||||
Size int64
|
||||
LastModified time.Time
|
||||
}
|
||||
|
||||
type Store struct {
|
||||
client *minio.Client
|
||||
bucket string
|
||||
region string
|
||||
}
|
||||
|
||||
func EndpointHost(endpoint string) (string, bool) {
|
||||
host, secure := endpoint, false
|
||||
if parsed, err := url.Parse(endpoint); err == nil && parsed.Host != "" {
|
||||
host = parsed.Host
|
||||
secure = parsed.Scheme == "https"
|
||||
}
|
||||
return strings.TrimRight(host, "/"), secure
|
||||
}
|
||||
|
||||
func New(endpoint, region, accessKeyID, secretAccessKey, bucket string) (*Store, error) {
|
||||
return NewWithTransport(endpoint, region, accessKeyID, secretAccessKey, bucket, nil)
|
||||
}
|
||||
|
||||
func NewWithTransport(endpoint, region, accessKeyID, secretAccessKey, bucket string, transport http.RoundTripper) (*Store, error) {
|
||||
host, secure := EndpointHost(endpoint)
|
||||
client, err := minio.New(host, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
|
||||
Region: region,
|
||||
Secure: secure,
|
||||
Transport: transport,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Store{client: client, bucket: bucket, region: region}, nil
|
||||
}
|
||||
|
||||
func (s *Store) EnsureBucket(ctx context.Context) error {
|
||||
exists, err := s.client.BucketExists(ctx, s.bucket)
|
||||
if err != nil {
|
||||
return s.wrap(err)
|
||||
}
|
||||
if exists {
|
||||
return nil
|
||||
}
|
||||
if err := s.client.MakeBucket(ctx, s.bucket, minio.MakeBucketOptions{Region: s.region}); err != nil {
|
||||
switch minio.ToErrorResponse(err).Code {
|
||||
case "BucketAlreadyOwnedByYou", "BucketAlreadyExists":
|
||||
return nil
|
||||
}
|
||||
return s.wrap(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) Ping(ctx context.Context) error {
|
||||
exists, err := s.client.BucketExists(ctx, s.bucket)
|
||||
if err != nil {
|
||||
return s.wrap(err)
|
||||
}
|
||||
if !exists {
|
||||
return fmt.Errorf("%w: bucket is missing", domain.ErrUnavailable)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) Put(ctx context.Context, key string, r io.Reader, size int64, contentType string) error {
|
||||
_, err := s.client.PutObject(ctx, s.bucket, key, r, size, minio.PutObjectOptions{ContentType: contentType})
|
||||
return s.wrap(err)
|
||||
}
|
||||
|
||||
func (s *Store) Get(ctx context.Context, key string) (io.ReadCloser, error) {
|
||||
object, err := s.client.GetObject(ctx, s.bucket, key, minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
return nil, s.wrap(err)
|
||||
}
|
||||
if _, err := object.Stat(); err != nil {
|
||||
object.Close()
|
||||
return nil, s.wrap(err)
|
||||
}
|
||||
return object, nil
|
||||
}
|
||||
|
||||
func (s *Store) Delete(ctx context.Context, key string) error {
|
||||
return s.wrap(s.client.RemoveObject(ctx, s.bucket, key, minio.RemoveObjectOptions{}))
|
||||
}
|
||||
|
||||
func (s *Store) List(ctx context.Context, prefix string, limit int) ([]ObjectInfo, bool, error) {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
objects := make([]ObjectInfo, 0, 64)
|
||||
for info := range s.client.ListObjects(ctx, s.bucket, minio.ListObjectsOptions{Prefix: prefix, Recursive: true}) {
|
||||
if info.Err != nil {
|
||||
return nil, false, s.wrap(info.Err)
|
||||
}
|
||||
if len(objects) >= limit {
|
||||
return objects, true, nil
|
||||
}
|
||||
objects = append(objects, ObjectInfo{Key: info.Key, Size: info.Size, LastModified: info.LastModified})
|
||||
}
|
||||
return objects, false, nil
|
||||
}
|
||||
|
||||
func (s *Store) Buckets(ctx context.Context) ([]string, error) {
|
||||
buckets, err := s.client.ListBuckets(ctx)
|
||||
if err != nil {
|
||||
return nil, s.wrap(err)
|
||||
}
|
||||
names := make([]string, len(buckets))
|
||||
for i, bucket := range buckets {
|
||||
names[i] = bucket.Name
|
||||
}
|
||||
return names, nil
|
||||
}
|
||||
|
||||
func (s *Store) RemoveBucket(ctx context.Context, bucket string) error {
|
||||
return s.wrap(s.client.RemoveBucket(ctx, bucket))
|
||||
}
|
||||
|
||||
func (s *Store) wrap(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
response := minio.ToErrorResponse(err)
|
||||
switch response.Code {
|
||||
case "NoSuchKey", "NotFound":
|
||||
return domain.ErrNotFound
|
||||
}
|
||||
if response.StatusCode == http.StatusNotFound {
|
||||
return domain.ErrNotFound
|
||||
}
|
||||
return fmt.Errorf("%w: %v", domain.ErrUnavailable, err)
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
"messenger/internal/handler"
|
||||
)
|
||||
|
||||
func New(h *handler.Handler, instanceID string) http.Handler {
|
||||
r := chi.NewRouter()
|
||||
r.Use(handler.InstanceID(instanceID), handler.RequestID, h.AccessLog, h.Recoverer)
|
||||
|
||||
r.NotFound(h.NotFound)
|
||||
r.MethodNotAllowed(h.MethodNotAllowed)
|
||||
|
||||
r.Get("/health/live", h.Live)
|
||||
r.Get("/health/ready", h.Ready)
|
||||
|
||||
r.Route("/api/v1", func(r chi.Router) {
|
||||
r.Post("/users", h.RegisterUser)
|
||||
r.Post("/auth/sessions", h.CreateSession)
|
||||
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(h.Authenticate)
|
||||
r.Delete("/auth/sessions/current", h.DeleteSession)
|
||||
r.Get("/users/me", h.CurrentUser)
|
||||
r.Post("/chats", h.CreateChat)
|
||||
r.Get("/chats", h.ListChats)
|
||||
r.Get("/chats/{chat_id}", h.GetChat)
|
||||
r.Post("/chats/{chat_id}/messages", h.SendMessage)
|
||||
r.Get("/chats/{chat_id}/messages", h.ListMessages)
|
||||
r.Post("/chats/{chat_id}/attachments", h.UploadAttachment)
|
||||
r.Get("/attachments/{attachment_id}", h.GetAttachment)
|
||||
r.Get("/attachments/{attachment_id}/content", h.DownloadAttachment)
|
||||
})
|
||||
})
|
||||
return r
|
||||
}
|
||||
@@ -0,0 +1,224 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"image"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
"uuid"
|
||||
|
||||
"messenger/internal/domain"
|
||||
)
|
||||
|
||||
const (
|
||||
originalVariant = "original"
|
||||
maxFilenameRunes = 255
|
||||
sniffBytes = 512
|
||||
)
|
||||
|
||||
type AttachmentService struct {
|
||||
chats *ChatService
|
||||
repo AttachmentRepository
|
||||
store ObjectStore
|
||||
maxBytes int64
|
||||
maxPixels int64
|
||||
uploads chan struct{}
|
||||
decodes chan struct{}
|
||||
}
|
||||
|
||||
func NewAttachmentService(chats *ChatService, repo AttachmentRepository, store ObjectStore,
|
||||
maxBytes, maxPixels int64, maxConcurrentUploads, maxConcurrentDecodes int) *AttachmentService {
|
||||
return &AttachmentService{
|
||||
chats: chats,
|
||||
repo: repo,
|
||||
store: store,
|
||||
maxBytes: maxBytes,
|
||||
maxPixels: maxPixels,
|
||||
uploads: make(chan struct{}, maxConcurrentUploads),
|
||||
decodes: make(chan struct{}, maxConcurrentDecodes),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *AttachmentService) Upload(ctx context.Context, actor domain.User, chatID uuid.UUID, filename, contentType string, body io.Reader) (domain.Attachment, error) {
|
||||
if _, err := s.chats.Get(ctx, actor, chatID); err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
if err := checkFilename(filename); err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
if contentType != domain.ContentPNG && contentType != domain.ContentJPEG {
|
||||
return domain.Attachment{}, fmt.Errorf("%w: content type must be image/png or image/jpeg", domain.ErrUnsupportedMedia)
|
||||
}
|
||||
if err := acquire(ctx, s.uploads); err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
defer release(s.uploads)
|
||||
raw, err := readLimited(body, s.maxBytes)
|
||||
if err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
if detected := detectImageType(raw); detected != contentType {
|
||||
return domain.Attachment{}, fmt.Errorf("%w: body does not match the declared image type", domain.ErrUnsupportedMedia)
|
||||
}
|
||||
if err := checkPixels(raw, s.maxPixels); err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
if err := s.decode(ctx, raw); err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
|
||||
id := uuid.New()
|
||||
sum := sha256.Sum256(raw)
|
||||
createdAt := now()
|
||||
attachment := domain.Attachment{
|
||||
ID: id,
|
||||
ChatID: chatID,
|
||||
OwnerID: actor.ID,
|
||||
Filename: filename,
|
||||
ContentType: contentType,
|
||||
SizeBytes: int64(len(raw)),
|
||||
SHA256: hex.EncodeToString(sum[:]),
|
||||
ObjectKey: "chats/" + chatID.String() + "/" + id.String(),
|
||||
Status: domain.AttachmentReady,
|
||||
CreatedAt: createdAt,
|
||||
UpdatedAt: createdAt,
|
||||
}
|
||||
if err := s.store.Put(ctx, attachment.ObjectKey, bytes.NewReader(raw), attachment.SizeBytes, contentType); err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
if err := s.repo.CreateAttachment(ctx, attachment); err != nil {
|
||||
cleanupCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second)
|
||||
defer cancel()
|
||||
_ = s.store.Delete(cleanupCtx, attachment.ObjectKey)
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
return attachment, nil
|
||||
}
|
||||
|
||||
func (s *AttachmentService) Get(ctx context.Context, actor domain.User, id uuid.UUID) (domain.Attachment, error) {
|
||||
attachment, err := s.repo.GetAttachment(ctx, id)
|
||||
if err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
if _, err := s.chats.Get(ctx, actor, attachment.ChatID); err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
return attachment, nil
|
||||
}
|
||||
|
||||
func (s *AttachmentService) Open(ctx context.Context, actor domain.User, id uuid.UUID, variant string) (domain.Attachment, io.ReadCloser, error) {
|
||||
if variant == "" {
|
||||
variant = originalVariant
|
||||
}
|
||||
if variant != originalVariant {
|
||||
return domain.Attachment{}, nil, fmt.Errorf("%w: only the original variant is available", domain.ErrInvalidInput)
|
||||
}
|
||||
attachment, err := s.repo.GetAttachment(ctx, id)
|
||||
if err != nil {
|
||||
return domain.Attachment{}, nil, err
|
||||
}
|
||||
if _, err := s.chats.Get(ctx, actor, attachment.ChatID); err != nil {
|
||||
return domain.Attachment{}, nil, err
|
||||
}
|
||||
if attachment.Status != domain.AttachmentReady {
|
||||
return domain.Attachment{}, nil, fmt.Errorf("%w: attachment is not ready", domain.ErrConflict)
|
||||
}
|
||||
content, err := s.store.Get(ctx, attachment.ObjectKey)
|
||||
if err != nil {
|
||||
return domain.Attachment{}, nil, err
|
||||
}
|
||||
return attachment, content, nil
|
||||
}
|
||||
|
||||
func (s *AttachmentService) decode(ctx context.Context, raw []byte) error {
|
||||
if err := acquire(ctx, s.decodes); err != nil {
|
||||
return err
|
||||
}
|
||||
defer release(s.decodes)
|
||||
return checkIntegrity(raw)
|
||||
}
|
||||
|
||||
func acquire(ctx context.Context, sem chan struct{}) error {
|
||||
select {
|
||||
case sem <- struct{}{}:
|
||||
return nil
|
||||
case <-ctx.Done():
|
||||
return fmt.Errorf("%w: request cancelled while waiting for capacity", domain.ErrUnavailable)
|
||||
}
|
||||
}
|
||||
|
||||
func release(sem chan struct{}) { <-sem }
|
||||
|
||||
func checkFilename(filename string) error {
|
||||
if strings.TrimSpace(filename) == "" {
|
||||
return fmt.Errorf("%w: filename must not be blank", domain.ErrInvalidInput)
|
||||
}
|
||||
if n := utf8.RuneCountInString(filename); n > maxFilenameRunes {
|
||||
return fmt.Errorf("%w: filename must be 1 to 255 characters", domain.ErrInvalidInput)
|
||||
}
|
||||
for _, r := range filename {
|
||||
if unicode.IsControl(r) || r == '/' || r == '\\' {
|
||||
return fmt.Errorf("%w: filename contains forbidden characters", domain.ErrInvalidInput)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func readLimited(body io.Reader, max int64) ([]byte, error) {
|
||||
raw, err := io.ReadAll(io.LimitReader(body, max+1))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: cannot read the upload body", domain.ErrInvalidInput)
|
||||
}
|
||||
if int64(len(raw)) > max {
|
||||
return nil, fmt.Errorf("%w: upload exceeds %d bytes", domain.ErrTooLarge, max)
|
||||
}
|
||||
if len(raw) == 0 {
|
||||
return nil, fmt.Errorf("%w: upload body is empty", domain.ErrUnsupportedMedia)
|
||||
}
|
||||
return raw, nil
|
||||
}
|
||||
|
||||
func detectImageType(raw []byte) string {
|
||||
sniff := raw
|
||||
if len(sniff) > sniffBytes {
|
||||
sniff = sniff[:sniffBytes]
|
||||
}
|
||||
switch http.DetectContentType(sniff) {
|
||||
case domain.ContentPNG:
|
||||
return domain.ContentPNG
|
||||
case domain.ContentJPEG:
|
||||
return domain.ContentJPEG
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func checkPixels(raw []byte, max int64) error {
|
||||
config, _, err := image.DecodeConfig(bytes.NewReader(raw))
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: image header cannot be decoded", domain.ErrUnsupportedMedia)
|
||||
}
|
||||
if config.Width <= 0 || config.Height <= 0 {
|
||||
return fmt.Errorf("%w: image has invalid dimensions", domain.ErrUnsupportedMedia)
|
||||
}
|
||||
if int64(config.Width) > max/int64(config.Height) {
|
||||
return fmt.Errorf("%w: image exceeds %d pixels", domain.ErrTooLarge, max)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkIntegrity(raw []byte) error {
|
||||
if _, _, err := image.Decode(bytes.NewReader(raw)); err != nil {
|
||||
return fmt.Errorf("%w: image is corrupted", domain.ErrUnsupportedMedia)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"hash/crc32"
|
||||
"image"
|
||||
"image/png"
|
||||
"strings"
|
||||
"testing"
|
||||
"uuid"
|
||||
|
||||
"messenger/internal/domain"
|
||||
)
|
||||
|
||||
func TestCheckFilename(t *testing.T) {
|
||||
valid := []string{"photo.png", "фото 1.jpg", strings.Repeat("a", 255)}
|
||||
for _, name := range valid {
|
||||
if err := checkFilename(name); err != nil {
|
||||
t.Errorf("%q: unexpected error %v", name, err)
|
||||
}
|
||||
}
|
||||
invalid := []string{"", strings.Repeat("a", 256), "dir/photo.png", `dir\photo.png`, "photo\x00.png", "photo\n.png", "photo\x7f.png"}
|
||||
for _, name := range invalid {
|
||||
if err := checkFilename(name); !errors.Is(err, domain.ErrInvalidInput) {
|
||||
t.Errorf("%q: got %v, want ErrInvalidInput", name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectImageType(t *testing.T) {
|
||||
if got := detectImageType(encodePNG(t, 4, 4)); got != domain.ContentPNG {
|
||||
t.Errorf("png: got %q", got)
|
||||
}
|
||||
if got := detectImageType([]byte("not an image at all")); got != "" {
|
||||
t.Errorf("garbage: got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckPixels(t *testing.T) {
|
||||
if err := checkPixels(encodePNG(t, 320, 200), 40000000); err != nil {
|
||||
t.Errorf("small image: unexpected error %v", err)
|
||||
}
|
||||
if err := checkPixels(pngHeader(8000, 5000), 40000000); err != nil {
|
||||
t.Errorf("exact pixel limit: unexpected error %v", err)
|
||||
}
|
||||
if err := checkPixels(pngHeader(20000, 2001), 40000000); !errors.Is(err, domain.ErrTooLarge) {
|
||||
t.Errorf("above pixel limit: got %v, want ErrTooLarge", err)
|
||||
}
|
||||
if err := checkPixels(pngHeader(65536, 65536), 40000000); !errors.Is(err, domain.ErrTooLarge) {
|
||||
t.Errorf("huge dimensions: got %v, want ErrTooLarge", err)
|
||||
}
|
||||
if err := checkPixels([]byte("garbage"), 40000000); !errors.Is(err, domain.ErrUnsupportedMedia) {
|
||||
t.Errorf("garbage: got %v, want ErrUnsupportedMedia", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckIntegrity(t *testing.T) {
|
||||
if err := checkIntegrity(encodePNG(t, 8, 8)); err != nil {
|
||||
t.Errorf("valid png: unexpected error %v", err)
|
||||
}
|
||||
corrupted := encodePNG(t, 8, 8)
|
||||
corrupted = corrupted[:len(corrupted)-20]
|
||||
if err := checkIntegrity(corrupted); !errors.Is(err, domain.ErrUnsupportedMedia) {
|
||||
t.Errorf("truncated png: got %v, want ErrUnsupportedMedia", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckMessageText(t *testing.T) {
|
||||
if err := checkMessageText("", false); !errors.Is(err, domain.ErrInvalidInput) {
|
||||
t.Errorf("empty text without attachments: got %v", err)
|
||||
}
|
||||
if err := checkMessageText(" ", false); !errors.Is(err, domain.ErrInvalidInput) {
|
||||
t.Errorf("blank text without attachments: got %v", err)
|
||||
}
|
||||
if err := checkMessageText("", true); err != nil {
|
||||
t.Errorf("empty text with attachments: unexpected error %v", err)
|
||||
}
|
||||
if err := checkMessageText(strings.Repeat("я", 4000), true); err != nil {
|
||||
t.Errorf("4000 characters: unexpected error %v", err)
|
||||
}
|
||||
if err := checkMessageText(strings.Repeat("я", 4001), true); !errors.Is(err, domain.ErrInvalidInput) {
|
||||
t.Errorf("4001 characters: got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckAttachmentIDs(t *testing.T) {
|
||||
ids := make([]uuid.UUID, 10)
|
||||
for i := range ids {
|
||||
ids[i] = uuid.New()
|
||||
}
|
||||
if err := checkAttachmentIDs(ids); err != nil {
|
||||
t.Errorf("ten unique ids: unexpected error %v", err)
|
||||
}
|
||||
if err := checkAttachmentIDs(append(ids, uuid.New())); !errors.Is(err, domain.ErrInvalidInput) {
|
||||
t.Errorf("eleven ids: got %v", err)
|
||||
}
|
||||
if err := checkAttachmentIDs([]uuid.UUID{ids[0], ids[0]}); !errors.Is(err, domain.ErrInvalidInput) {
|
||||
t.Errorf("duplicate ids: got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMessageHash(t *testing.T) {
|
||||
first, second := uuid.New(), uuid.New()
|
||||
if messageHash("", nil) != messageHash("", []uuid.UUID{}) {
|
||||
t.Error("absent attachment_ids must hash like an empty list")
|
||||
}
|
||||
if messageHash("hi", []uuid.UUID{first, second}) != messageHash("hi", []uuid.UUID{second, first}) {
|
||||
t.Error("attachment order must not change the hash")
|
||||
}
|
||||
if messageHash("hi", nil) == messageHash("hi!", nil) {
|
||||
t.Error("different text must change the hash")
|
||||
}
|
||||
if messageHash("hi", []uuid.UUID{first}) == messageHash("hi", []uuid.UUID{second}) {
|
||||
t.Error("different attachments must change the hash")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadLimited(t *testing.T) {
|
||||
raw, err := readLimited(strings.NewReader("abcd"), 4)
|
||||
if err != nil || string(raw) != "abcd" {
|
||||
t.Fatalf("exact limit: got %q, %v", raw, err)
|
||||
}
|
||||
if _, err := readLimited(strings.NewReader("abcde"), 4); !errors.Is(err, domain.ErrTooLarge) {
|
||||
t.Errorf("over limit: got %v, want ErrTooLarge", err)
|
||||
}
|
||||
if _, err := readLimited(strings.NewReader(""), 4); !errors.Is(err, domain.ErrUnsupportedMedia) {
|
||||
t.Errorf("empty body: got %v, want ErrUnsupportedMedia", err)
|
||||
}
|
||||
}
|
||||
|
||||
func encodePNG(t *testing.T, width, height int) []byte {
|
||||
t.Helper()
|
||||
var buffer bytes.Buffer
|
||||
if err := png.Encode(&buffer, image.NewRGBA(image.Rect(0, 0, width, height))); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return buffer.Bytes()
|
||||
}
|
||||
|
||||
func pngHeader(width, height uint32) []byte {
|
||||
var buffer bytes.Buffer
|
||||
buffer.WriteString("\x89PNG\r\n\x1a\n")
|
||||
header := make([]byte, 13)
|
||||
binary.BigEndian.PutUint32(header[0:4], width)
|
||||
binary.BigEndian.PutUint32(header[4:8], height)
|
||||
header[8], header[9] = 8, 2
|
||||
buffer.Write(binary.BigEndian.AppendUint32(nil, uint32(len(header))))
|
||||
buffer.WriteString("IHDR")
|
||||
buffer.Write(header)
|
||||
checksum := crc32.NewIEEE()
|
||||
checksum.Write([]byte("IHDR"))
|
||||
checksum.Write(header)
|
||||
buffer.Write(binary.BigEndian.AppendUint32(nil, checksum.Sum32()))
|
||||
return buffer.Bytes()
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"messenger/internal/domain"
|
||||
)
|
||||
|
||||
const tokenBytes = 32
|
||||
|
||||
type AuthService struct {
|
||||
users UserRepository
|
||||
sessions SessionStore
|
||||
ttl time.Duration
|
||||
}
|
||||
|
||||
func NewAuthService(users UserRepository, sessions SessionStore, ttl time.Duration) *AuthService {
|
||||
return &AuthService{users: users, sessions: sessions, ttl: ttl}
|
||||
}
|
||||
|
||||
func (s *AuthService) Login(ctx context.Context, username, password string) (domain.Session, error) {
|
||||
user, hash, err := s.users.GetUserByUsername(ctx, username)
|
||||
if errors.Is(err, domain.ErrNotFound) {
|
||||
return domain.Session{}, domain.ErrUnauthorized
|
||||
}
|
||||
if err != nil {
|
||||
return domain.Session{}, err
|
||||
}
|
||||
ok, err := verifyPassword(password, hash)
|
||||
if err != nil {
|
||||
return domain.Session{}, err
|
||||
}
|
||||
if !ok {
|
||||
return domain.Session{}, domain.ErrUnauthorized
|
||||
}
|
||||
|
||||
raw := make([]byte, tokenBytes)
|
||||
if _, err := rand.Read(raw); err != nil {
|
||||
return domain.Session{}, err
|
||||
}
|
||||
token := base64.RawURLEncoding.EncodeToString(raw)
|
||||
expiresAt := now().Add(s.ttl)
|
||||
if err := s.sessions.Create(ctx, HashToken(token), user.ID, expiresAt); err != nil {
|
||||
return domain.Session{}, err
|
||||
}
|
||||
return domain.Session{Token: token, ExpiresAt: expiresAt, User: user}, nil
|
||||
}
|
||||
|
||||
func (s *AuthService) Authenticate(ctx context.Context, token string) (domain.User, error) {
|
||||
userID, err := s.sessions.Get(ctx, HashToken(token))
|
||||
if err != nil {
|
||||
return domain.User{}, err
|
||||
}
|
||||
u, err := s.users.GetUser(ctx, userID)
|
||||
if errors.Is(err, domain.ErrNotFound) {
|
||||
return domain.User{}, domain.ErrUnauthorized
|
||||
}
|
||||
return u, err
|
||||
}
|
||||
|
||||
func (s *AuthService) Logout(ctx context.Context, token string) error {
|
||||
return s.sessions.Delete(ctx, HashToken(token))
|
||||
}
|
||||
|
||||
func HashToken(token string) string {
|
||||
sum := sha256.Sum256([]byte(token))
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"uuid"
|
||||
|
||||
"messenger/internal/domain"
|
||||
)
|
||||
|
||||
type ChatService struct {
|
||||
repo ChatRepository
|
||||
}
|
||||
|
||||
func NewChatService(repo ChatRepository) *ChatService {
|
||||
return &ChatService{repo: repo}
|
||||
}
|
||||
|
||||
func (s *ChatService) Create(ctx context.Context, actor domain.User, title string, memberIDs []uuid.UUID) (domain.Chat, error) {
|
||||
if err := checkText("title", title, 100); err != nil {
|
||||
return domain.Chat{}, err
|
||||
}
|
||||
if len(memberIDs) < 1 || len(memberIDs) > 100 {
|
||||
return domain.Chat{}, fmt.Errorf("%w: member_ids must contain 1 to 100 users", domain.ErrInvalidInput)
|
||||
}
|
||||
seen := make(map[uuid.UUID]struct{}, len(memberIDs))
|
||||
for _, id := range memberIDs {
|
||||
if _, dup := seen[id]; dup {
|
||||
return domain.Chat{}, fmt.Errorf("%w: member_ids must not contain duplicates", domain.ErrInvalidInput)
|
||||
}
|
||||
seen[id] = struct{}{}
|
||||
}
|
||||
if _, ok := seen[actor.ID]; !ok {
|
||||
return domain.Chat{}, fmt.Errorf("%w: member_ids must include the current user", domain.ErrInvalidInput)
|
||||
}
|
||||
|
||||
c := domain.Chat{
|
||||
ID: uuid.New(),
|
||||
Title: title,
|
||||
MemberIDs: memberIDs,
|
||||
CreatedAt: now(),
|
||||
}
|
||||
if err := s.repo.CreateChat(ctx, c); err != nil {
|
||||
if errors.Is(err, domain.ErrNotFound) {
|
||||
return domain.Chat{}, fmt.Errorf("%w: member_ids contains unknown user", domain.ErrInvalidInput)
|
||||
}
|
||||
return domain.Chat{}, err
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (s *ChatService) Get(ctx context.Context, actor domain.User, chatID uuid.UUID) (domain.Chat, error) {
|
||||
c, err := s.repo.GetChat(ctx, chatID)
|
||||
if err != nil {
|
||||
return domain.Chat{}, err
|
||||
}
|
||||
if !c.HasMember(actor.ID) {
|
||||
return domain.Chat{}, domain.ErrNotFound
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (s *ChatService) List(ctx context.Context, actor domain.User, after *domain.Cursor, limit int) (domain.Page[domain.Chat], error) {
|
||||
if err := checkLimit(limit); err != nil {
|
||||
return domain.Page[domain.Chat]{}, err
|
||||
}
|
||||
if after != nil && after.Scope != actor.ID {
|
||||
return domain.Page[domain.Chat]{}, fmt.Errorf("%w: cursor does not belong to this request", domain.ErrInvalidInput)
|
||||
}
|
||||
items, more, err := s.repo.ListUserChats(ctx, actor.ID, after, limit)
|
||||
if err != nil {
|
||||
return domain.Page[domain.Chat]{}, err
|
||||
}
|
||||
page := domain.Page[domain.Chat]{Items: items}
|
||||
if more {
|
||||
last := items[len(items)-1]
|
||||
page.Next = &domain.Cursor{Scope: actor.ID, CreatedAt: last.CreatedAt, ID: last.ID}
|
||||
}
|
||||
return page, nil
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
"uuid"
|
||||
|
||||
"messenger/internal/domain"
|
||||
)
|
||||
|
||||
const maxMessageAttachments = 10
|
||||
|
||||
type MessageService struct {
|
||||
chats *ChatService
|
||||
messages MessageRepository
|
||||
}
|
||||
|
||||
func NewMessageService(chats *ChatService, messages MessageRepository) *MessageService {
|
||||
return &MessageService{chats: chats, messages: messages}
|
||||
}
|
||||
|
||||
func (s *MessageService) Send(ctx context.Context, actor domain.User, chatID uuid.UUID, key, text string, attachmentIDs []uuid.UUID) (domain.Message, error) {
|
||||
if _, err := s.chats.Get(ctx, actor, chatID); err != nil {
|
||||
return domain.Message{}, err
|
||||
}
|
||||
if err := checkMessageText(text, len(attachmentIDs) > 0); err != nil {
|
||||
return domain.Message{}, err
|
||||
}
|
||||
if err := checkAttachmentIDs(attachmentIDs); err != nil {
|
||||
return domain.Message{}, err
|
||||
}
|
||||
m := domain.Message{
|
||||
ID: uuid.New(),
|
||||
ChatID: chatID,
|
||||
SenderID: actor.ID,
|
||||
Text: text,
|
||||
AttachmentIDs: attachmentIDs,
|
||||
CreatedAt: now(),
|
||||
}
|
||||
stored, err := s.messages.CreateMessageIdempotent(ctx, m, key, messageHash(text, attachmentIDs))
|
||||
if errors.Is(err, domain.ErrConflict) {
|
||||
return domain.Message{}, fmt.Errorf("%w: Idempotency-Key was already used with a different body", domain.ErrConflict)
|
||||
}
|
||||
return stored, err
|
||||
}
|
||||
|
||||
func (s *MessageService) History(ctx context.Context, actor domain.User, chatID uuid.UUID, after *domain.Cursor, limit int) (domain.Page[domain.Message], error) {
|
||||
if _, err := s.chats.Get(ctx, actor, chatID); err != nil {
|
||||
return domain.Page[domain.Message]{}, err
|
||||
}
|
||||
if err := checkLimit(limit); err != nil {
|
||||
return domain.Page[domain.Message]{}, err
|
||||
}
|
||||
if after != nil && after.Scope != chatID {
|
||||
return domain.Page[domain.Message]{}, fmt.Errorf("%w: cursor belongs to another chat", domain.ErrInvalidInput)
|
||||
}
|
||||
items, more, err := s.messages.ListMessages(ctx, chatID, after, limit)
|
||||
if err != nil {
|
||||
return domain.Page[domain.Message]{}, err
|
||||
}
|
||||
page := domain.Page[domain.Message]{Items: items}
|
||||
if more {
|
||||
last := items[len(items)-1]
|
||||
page.Next = &domain.Cursor{Scope: chatID, CreatedAt: last.CreatedAt, ID: last.ID}
|
||||
}
|
||||
return page, nil
|
||||
}
|
||||
|
||||
func checkMessageText(text string, hasAttachments bool) error {
|
||||
if utf8.RuneCountInString(text) > 4000 {
|
||||
return fmt.Errorf("%w: text must be at most 4000 characters", domain.ErrInvalidInput)
|
||||
}
|
||||
if !hasAttachments && strings.TrimSpace(text) == "" {
|
||||
return fmt.Errorf("%w: text must not be empty without attachments", domain.ErrInvalidInput)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkAttachmentIDs(ids []uuid.UUID) error {
|
||||
if len(ids) > maxMessageAttachments {
|
||||
return fmt.Errorf("%w: attachment_ids must contain at most %d items", domain.ErrInvalidInput, maxMessageAttachments)
|
||||
}
|
||||
seen := make(map[uuid.UUID]struct{}, len(ids))
|
||||
for _, id := range ids {
|
||||
if _, duplicate := seen[id]; duplicate {
|
||||
return fmt.Errorf("%w: attachment_ids must not contain duplicates", domain.ErrInvalidInput)
|
||||
}
|
||||
seen[id] = struct{}{}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func messageHash(text string, ids []uuid.UUID) string {
|
||||
sorted := make([]string, len(ids))
|
||||
for i, id := range ids {
|
||||
sorted[i] = id.String()
|
||||
}
|
||||
sort.Strings(sorted)
|
||||
sum := sha256.Sum256([]byte(text + "\n" + strings.Join(sorted, ",")))
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/subtle"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/crypto/argon2"
|
||||
)
|
||||
|
||||
const (
|
||||
argonMemory = 19 * 1024
|
||||
argonTime = 2
|
||||
argonThreads = 1
|
||||
argonSaltLen = 16
|
||||
argonKeyLen = 32
|
||||
)
|
||||
|
||||
var errBadHash = errors.New("malformed password hash")
|
||||
|
||||
func hashPassword(password string) (string, error) {
|
||||
salt := make([]byte, argonSaltLen)
|
||||
if _, err := rand.Read(salt); err != nil {
|
||||
return "", err
|
||||
}
|
||||
key := argon2.IDKey([]byte(password), salt, argonTime, argonMemory, argonThreads, argonKeyLen)
|
||||
b64 := base64.RawStdEncoding
|
||||
return fmt.Sprintf("$argon2id$v=%d$m=%d,t=%d,p=%d$%s$%s",
|
||||
argon2.Version, argonMemory, argonTime, argonThreads, b64.EncodeToString(salt), b64.EncodeToString(key)), nil
|
||||
}
|
||||
|
||||
func verifyPassword(password, encoded string) (bool, error) {
|
||||
parts := strings.Split(encoded, "$")
|
||||
if len(parts) != 6 || parts[1] != "argon2id" {
|
||||
return false, errBadHash
|
||||
}
|
||||
var version int
|
||||
var memory, time uint32
|
||||
var threads uint8
|
||||
if _, err := fmt.Sscanf(parts[2], "v=%d", &version); err != nil || version != argon2.Version {
|
||||
return false, errBadHash
|
||||
}
|
||||
if _, err := fmt.Sscanf(parts[3], "m=%d,t=%d,p=%d", &memory, &time, &threads); err != nil {
|
||||
return false, errBadHash
|
||||
}
|
||||
b64 := base64.RawStdEncoding
|
||||
salt, err := b64.DecodeString(parts[4])
|
||||
if err != nil {
|
||||
return false, errBadHash
|
||||
}
|
||||
want, err := b64.DecodeString(parts[5])
|
||||
if err != nil {
|
||||
return false, errBadHash
|
||||
}
|
||||
got := argon2.IDKey([]byte(password), salt, time, memory, threads, uint32(len(want)))
|
||||
return subtle.ConstantTimeCompare(got, want) == 1, nil
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"time"
|
||||
"uuid"
|
||||
|
||||
"messenger/internal/domain"
|
||||
)
|
||||
|
||||
type UserRepository interface {
|
||||
CreateUser(ctx context.Context, u domain.User, passwordHash string) error
|
||||
GetUser(ctx context.Context, id uuid.UUID) (domain.User, error)
|
||||
GetUserByUsername(ctx context.Context, username string) (domain.User, string, error)
|
||||
}
|
||||
|
||||
type ChatRepository interface {
|
||||
CreateChat(ctx context.Context, c domain.Chat) error
|
||||
GetChat(ctx context.Context, id uuid.UUID) (domain.Chat, error)
|
||||
ListUserChats(ctx context.Context, userID uuid.UUID, after *domain.Cursor, limit int) ([]domain.Chat, bool, error)
|
||||
}
|
||||
|
||||
type MessageRepository interface {
|
||||
CreateMessageIdempotent(ctx context.Context, m domain.Message, key, requestHash string) (domain.Message, error)
|
||||
ListMessages(ctx context.Context, chatID uuid.UUID, after *domain.Cursor, limit int) ([]domain.Message, bool, error)
|
||||
}
|
||||
|
||||
type AttachmentRepository interface {
|
||||
CreateAttachment(ctx context.Context, a domain.Attachment) error
|
||||
GetAttachment(ctx context.Context, id uuid.UUID) (domain.Attachment, error)
|
||||
}
|
||||
|
||||
type ObjectStore interface {
|
||||
Put(ctx context.Context, key string, r io.Reader, size int64, contentType string) error
|
||||
Get(ctx context.Context, key string) (io.ReadCloser, error)
|
||||
Delete(ctx context.Context, key string) error
|
||||
Ping(ctx context.Context) error
|
||||
}
|
||||
|
||||
type SessionStore interface {
|
||||
Create(ctx context.Context, tokenHash string, userID uuid.UUID, expiresAt time.Time) error
|
||||
Get(ctx context.Context, tokenHash string) (uuid.UUID, error)
|
||||
Delete(ctx context.Context, tokenHash string) error
|
||||
}
|
||||
|
||||
func now() time.Time {
|
||||
return time.Now().UTC().Truncate(time.Microsecond)
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"unicode/utf8"
|
||||
"uuid"
|
||||
|
||||
"messenger/internal/domain"
|
||||
)
|
||||
|
||||
var usernameRe = regexp.MustCompile(`^[a-z0-9_]{3,32}$`)
|
||||
|
||||
const (
|
||||
minPasswordLen = 12
|
||||
maxPasswordLen = 128
|
||||
)
|
||||
|
||||
type UserService struct {
|
||||
repo UserRepository
|
||||
}
|
||||
|
||||
func NewUserService(repo UserRepository) *UserService {
|
||||
return &UserService{repo: repo}
|
||||
}
|
||||
|
||||
func (s *UserService) Register(ctx context.Context, username, displayName, password string) (domain.User, error) {
|
||||
if !usernameRe.MatchString(username) {
|
||||
return domain.User{}, fmt.Errorf("%w: username must match [a-z0-9_]{3,32}", domain.ErrInvalidInput)
|
||||
}
|
||||
if err := checkText("display_name", displayName, 100); err != nil {
|
||||
return domain.User{}, err
|
||||
}
|
||||
if err := checkText("password", password, maxPasswordLen); err != nil {
|
||||
return domain.User{}, err
|
||||
}
|
||||
if utf8.RuneCountInString(password) < minPasswordLen {
|
||||
return domain.User{}, fmt.Errorf("%w: password must be at least %d characters", domain.ErrInvalidInput, minPasswordLen)
|
||||
}
|
||||
hash, err := hashPassword(password)
|
||||
if err != nil {
|
||||
return domain.User{}, err
|
||||
}
|
||||
u := domain.User{
|
||||
ID: uuid.New(),
|
||||
Username: username,
|
||||
DisplayName: displayName,
|
||||
CreatedAt: now(),
|
||||
}
|
||||
if err := s.repo.CreateUser(ctx, u, hash); err != nil {
|
||||
if errors.Is(err, domain.ErrConflict) {
|
||||
return domain.User{}, fmt.Errorf("%w: username is already taken", domain.ErrConflict)
|
||||
}
|
||||
return domain.User{}, err
|
||||
}
|
||||
return u, nil
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"messenger/internal/domain"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultLimit = 50
|
||||
MaxLimit = 100
|
||||
)
|
||||
|
||||
func checkText(field, value string, maxRunes int) error {
|
||||
if strings.TrimSpace(value) == "" {
|
||||
return fmt.Errorf("%w: %s must not be empty", domain.ErrInvalidInput, field)
|
||||
}
|
||||
if n := utf8.RuneCountInString(value); n > maxRunes {
|
||||
return fmt.Errorf("%w: %s must be at most %d characters", domain.ErrInvalidInput, field, maxRunes)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkLimit(limit int) error {
|
||||
if limit < 1 || limit > MaxLimit {
|
||||
return fmt.Errorf("%w: limit must be between 1 and %d", domain.ErrInvalidInput, MaxLimit)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Executable
+130
@@ -0,0 +1,130 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Attachment contract checks beyond the public smoke; creates synthetic data."""
|
||||
import hashlib
|
||||
import sys
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
||||
from client import Client, check, run
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
PNG = (ROOT / 'tests/fixtures/sample.png').read_bytes()
|
||||
|
||||
|
||||
def upload(c, actor, chat, content=PNG, filename='sample.png', content_type='image/png', expected=201):
|
||||
path = '/api/v1/chats/' + chat['id'] + '/attachments?filename=' + filename
|
||||
return c.call('POST', path, expected, actor=actor, raw=content, headers={'Content-Type': content_type}, schema='Attachment')
|
||||
|
||||
|
||||
def main():
|
||||
c = Client()
|
||||
alice, bob, eve, chat = c.bootstrap()
|
||||
other = c.call('POST', '/api/v1/chats', 201, actor=alice,
|
||||
body={'title': 'Другой чат', 'member_ids': [alice['id'], bob['id']]}, schema='Chat')['json']
|
||||
notes = []
|
||||
|
||||
att = upload(c, alice, chat)['json']
|
||||
notes.append(f"upload 201 ready, sha256={att['sha256']}, size={att['size_bytes']}")
|
||||
check(att['sha256'] == hashlib.sha256(PNG).hexdigest(), 'sha256 mismatch')
|
||||
check(att['status'] == 'ready' and att['variants'] == [] and att['error_code'] is None, 'attachment shape')
|
||||
|
||||
jpeg_path = ROOT / 'scripts/fixtures/sample.jpg'
|
||||
if jpeg_path.is_file():
|
||||
jpeg = jpeg_path.read_bytes()
|
||||
jatt = upload(c, alice, chat, content=jpeg, filename='sample.jpg', content_type='image/jpeg')['json']
|
||||
check(jatt['content_type'] == 'image/jpeg' and jatt['sha256'] == hashlib.sha256(jpeg).hexdigest(),
|
||||
'jpeg metadata mismatch')
|
||||
got = c.call('GET', '/api/v1/attachments/' + jatt['id'] + '/content', 200, actor=bob)
|
||||
check(got['bytes'] == jpeg and got['headers'].get_content_type() == 'image/jpeg', 'jpeg download mismatch')
|
||||
upload(c, alice, chat, content=jpeg, filename='sample.jpg', content_type='image/png', expected=415)
|
||||
notes.append('jpeg upload/download byte-identical; jpeg declared as png -> 415')
|
||||
|
||||
large_path = ROOT / 'scripts/fixtures/large.png'
|
||||
if large_path.is_file():
|
||||
large = large_path.read_bytes()
|
||||
latt = upload(c, alice, chat, content=large, filename='large.png')['json']
|
||||
check(latt['size_bytes'] == len(large) and latt['status'] == 'ready', 'image at the pixel limit was rejected')
|
||||
got = c.call('GET', '/api/v1/attachments/' + latt['id'] + '/content', 200, actor=bob)
|
||||
check(got['bytes'] == large, 'large image download mismatch')
|
||||
notes.append('png with 40,000,000 pixels accepted and downloaded byte-identical')
|
||||
|
||||
upload(c, alice, chat, filename='bad/name.png', expected=400)
|
||||
notes.append('filename with slash -> 400')
|
||||
upload(c, alice, chat, filename='bad%00name.png', expected=400)
|
||||
notes.append('filename with NUL -> 400')
|
||||
upload(c, alice, chat, filename='', expected=400)
|
||||
notes.append('empty filename -> 400')
|
||||
upload(c, alice, chat, content_type='image/jpeg', expected=415)
|
||||
notes.append('declared jpeg with png body -> 415')
|
||||
upload(c, alice, chat, content=PNG[:-30], expected=415)
|
||||
notes.append('truncated png -> 415')
|
||||
upload(c, alice, chat, content=b'\x00' * (10485761), expected=413)
|
||||
notes.append('10485761 bytes -> 413')
|
||||
upload(c, alice, chat, content_type='text/plain', expected=415)
|
||||
notes.append('text/plain -> 415')
|
||||
|
||||
c.call('GET', '/api/v1/attachments/' + att['id'] + '/content?variant=thumbnail', 400, actor=bob)
|
||||
notes.append('unknown variant -> 400')
|
||||
c.call('GET', '/api/v1/attachments/' + att['id'] + '/content?variant=original', 200, actor=bob)
|
||||
notes.append('variant=original -> 200')
|
||||
c.call('GET', '/api/v1/attachments/' + str(uuid.uuid4()), 404, actor=bob)
|
||||
notes.append('unknown attachment id -> 404')
|
||||
c.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=x.png', 404, actor=eve,
|
||||
raw=PNG, headers={'Content-Type': 'image/png'}, schema='Attachment')
|
||||
notes.append('upload by outsider -> 404')
|
||||
|
||||
foreign = upload(c, alice, other)['json']
|
||||
key = uuid.uuid4().hex
|
||||
c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 404, actor=alice,
|
||||
body={'text': '', 'attachment_ids': [foreign['id']]}, headers={'Idempotency-Key': key})
|
||||
notes.append('attachment from another chat -> 404')
|
||||
c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 404, actor=alice,
|
||||
body={'text': '', 'attachment_ids': [str(uuid.uuid4())]}, headers={'Idempotency-Key': uuid.uuid4().hex})
|
||||
notes.append('unknown attachment id in message -> 404')
|
||||
c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 400, actor=alice,
|
||||
body={'text': '', 'attachment_ids': [att['id']] * 2}, headers={'Idempotency-Key': uuid.uuid4().hex})
|
||||
notes.append('duplicate attachment_ids -> 400')
|
||||
c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 400, actor=alice,
|
||||
body={'text': '', 'attachment_ids': [str(uuid.uuid4()) for _ in range(11)]},
|
||||
headers={'Idempotency-Key': uuid.uuid4().hex})
|
||||
notes.append('eleven attachment_ids -> 400')
|
||||
c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 400, actor=alice,
|
||||
body={'text': ''}, headers={'Idempotency-Key': uuid.uuid4().hex})
|
||||
notes.append('empty text without attachments -> 400')
|
||||
|
||||
key = uuid.uuid4().hex
|
||||
first = c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 201, actor=alice,
|
||||
body={'text': 'вложение', 'attachment_ids': [att['id']]}, headers={'Idempotency-Key': key}, schema='Message')['json']
|
||||
replay = c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 201, actor=alice,
|
||||
body={'text': 'вложение', 'attachment_ids': [att['id']]}, headers={'Idempotency-Key': key}, schema='Message')['json']
|
||||
check(first == replay, 'idempotent replay with attachments differs')
|
||||
check(first['attachment_ids'] == [att['id']], 'message lost attachment_ids')
|
||||
notes.append('idempotent replay with same attachment set -> same message')
|
||||
|
||||
empty_key = uuid.uuid4().hex
|
||||
without = c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 201, actor=alice,
|
||||
body={'text': 'без вложений'}, headers={'Idempotency-Key': empty_key}, schema='Message')['json']
|
||||
implicit = c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 201, actor=alice,
|
||||
body={'text': 'без вложений', 'attachment_ids': []}, headers={'Idempotency-Key': empty_key}, schema='Message')['json']
|
||||
check(without == implicit, 'absent attachment_ids must equal an empty list')
|
||||
notes.append('absent attachment_ids equals [] for idempotency')
|
||||
|
||||
c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 409, actor=alice,
|
||||
body={'text': 'вложение', 'attachment_ids': []}, headers={'Idempotency-Key': key})
|
||||
notes.append('same key with a different attachment set -> 409')
|
||||
|
||||
history = c.call('GET', '/api/v1/chats/' + chat['id'] + '/messages?limit=100', 200, actor=bob,
|
||||
schema='MessagePage')['json']['items']
|
||||
check(all(isinstance(m['attachment_ids'], list) for m in history), 'attachment_ids must always be an array')
|
||||
check(any(m['id'] == first['id'] and m['attachment_ids'] == [att['id']] for m in history),
|
||||
'history lost attachments')
|
||||
notes.append('history exposes attachment_ids on every message')
|
||||
|
||||
for line in notes:
|
||||
print('ok:', line)
|
||||
print(f'PASS: {len(notes)} attachment contract checks')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(main)
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 124 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 653 B |
Executable
+56
@@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env python3
|
||||
import sys
|
||||
import threading
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
||||
from client import Client, check
|
||||
|
||||
WORKERS = 20
|
||||
|
||||
|
||||
def main():
|
||||
c = Client()
|
||||
alice, _, _, chat = c.bootstrap()
|
||||
key = uuid.uuid4().hex
|
||||
text = 'Одновременный повтор ' + key
|
||||
barrier = threading.Barrier(WORKERS)
|
||||
results, errors = [], []
|
||||
lock = threading.Lock()
|
||||
|
||||
def send():
|
||||
barrier.wait()
|
||||
try:
|
||||
r = c.message(alice, chat, text, key=key)
|
||||
with lock:
|
||||
results.append((r['json'], r['headers']['X-Instance-Id']))
|
||||
except AssertionError as exc:
|
||||
with lock:
|
||||
errors.append(str(exc))
|
||||
|
||||
threads = [threading.Thread(target=send) for _ in range(WORKERS)]
|
||||
for t in threads:
|
||||
t.start()
|
||||
for t in threads:
|
||||
t.join()
|
||||
|
||||
history = c.call('GET', '/api/v1/chats/' + chat['id'] + '/messages?limit=100', actor=alice, schema='MessagePage')['json']['items']
|
||||
stored = [m for m in history if m['text'] == text]
|
||||
instances = sorted({i for _, i in results})
|
||||
ids = {m['id'] for m, _ in results}
|
||||
print('parallel requests with one key:', WORKERS)
|
||||
print('successful responses (201):', len(results))
|
||||
print('failed requests:', len(errors), errors[:3])
|
||||
print('distinct message ids in responses:', len(ids))
|
||||
print('messages stored in history:', len(stored))
|
||||
print('instances that served requests:', instances)
|
||||
check(not errors, 'some requests failed')
|
||||
check(len(ids) == 1 and len(stored) == 1, 'duplicate message created')
|
||||
check(all(m == results[0][0] for m, _ in results), 'replayed responses differ')
|
||||
check(len(instances) >= 2, 'requests were not spread across replicas')
|
||||
print('OK: exactly one message created')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Executable
+28
@@ -0,0 +1,28 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
mkdir -p secrets
|
||||
chmod 700 secrets
|
||||
|
||||
if [ ! -f secrets/postgres_password ]; then
|
||||
head -c 24 /dev/urandom | od -An -tx1 | tr -d ' \n' > secrets/postgres_password
|
||||
fi
|
||||
password=$(cat secrets/postgres_password)
|
||||
|
||||
[ -f secrets/database_url ] ||
|
||||
printf 'postgres://messenger:%s@postgres:5432/messenger?sslmode=disable\n' "$password" > secrets/database_url
|
||||
[ -f secrets/cache_url ] ||
|
||||
printf 'redis://redis:6379/0\n' > secrets/cache_url
|
||||
[ -f secrets/s3_root_access_key ] ||
|
||||
head -c 10 /dev/urandom | od -An -tx1 | tr -d ' \n' > secrets/s3_root_access_key
|
||||
[ -f secrets/s3_root_secret_key ] ||
|
||||
head -c 24 /dev/urandom | od -An -tx1 | tr -d ' \n' > secrets/s3_root_secret_key
|
||||
[ -f secrets/s3_access_key_id ] ||
|
||||
head -c 10 /dev/urandom | od -An -tx1 | tr -d ' \n' > secrets/s3_access_key_id
|
||||
[ -f secrets/s3_secret_access_key ] ||
|
||||
head -c 24 /dev/urandom | od -An -tx1 | tr -d ' \n' > secrets/s3_secret_access_key
|
||||
|
||||
chmod 644 secrets/postgres_password secrets/database_url secrets/cache_url \
|
||||
secrets/s3_root_access_key secrets/s3_root_secret_key \
|
||||
secrets/s3_access_key_id secrets/s3_secret_access_key
|
||||
echo "secrets ready in ./secrets"
|
||||
Executable
+19
@@ -0,0 +1,19 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
ENGINE=${ENGINE:-$(command -v podman >/dev/null 2>&1 && echo podman || echo docker)}
|
||||
PROJECT=${PROJECT:-messenger-lab4}
|
||||
IMAGE=${IMAGE:-localhost/messenger-api:lab4}
|
||||
|
||||
$ENGINE run --rm --network "${PROJECT}_backend" \
|
||||
--entrypoint /usr/local/bin/reconcile \
|
||||
-v "$PWD/secrets/database_url:/run/secrets/database_url:ro" \
|
||||
-v "$PWD/secrets/s3_access_key_id:/run/secrets/s3_access_key_id:ro" \
|
||||
-v "$PWD/secrets/s3_secret_access_key:/run/secrets/s3_secret_access_key:ro" \
|
||||
-e S3_ENDPOINT="${S3_ENDPOINT:-http://s3:9000}" \
|
||||
-e S3_REGION="${S3_REGION:-us-east-1}" \
|
||||
-e S3_BUCKET="${S3_BUCKET:-messenger-attachments}" \
|
||||
-e DATABASE_URL_FILE=/run/secrets/database_url \
|
||||
-e S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_access_key_id \
|
||||
-e S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret_access_key \
|
||||
"$IMAGE" "$@"
|
||||
Executable
+8
@@ -0,0 +1,8 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
ENGINE=${ENGINE:-$(command -v podman >/dev/null 2>&1 && echo podman || echo docker)}
|
||||
COMPOSE=${COMPOSE:-$([ "$ENGINE" = podman ] && echo podman-compose || echo "docker compose")}
|
||||
|
||||
$COMPOSE stop lb api redis postgres s3
|
||||
$COMPOSE start postgres redis s3 api lb
|
||||
Executable
+37
@@ -0,0 +1,37 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
ENGINE=${ENGINE:-$(command -v podman >/dev/null 2>&1 && echo podman || echo docker)}
|
||||
COMPOSE=${COMPOSE:-$([ "$ENGINE" = podman ] && echo podman-compose || echo "docker compose")}
|
||||
PROJECT=${PROJECT:-messenger-lab4}
|
||||
|
||||
replicas() {
|
||||
$ENGINE ps -aq --no-trunc --filter "label=com.docker.compose.project=$PROJECT" --filter "label=com.docker.compose.service=api" | sort
|
||||
}
|
||||
|
||||
wait_healthy() {
|
||||
for _ in $(seq 1 60); do
|
||||
if [ "$($ENGINE inspect -f '{{.State.Health.Status}}' "$1" 2>/dev/null)" = "healthy" ]; then
|
||||
return 0
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
echo "replica $1 did not become healthy" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
$ENGINE build --build-arg "BUILD_ID=${BUILD_ID:-$(date +%s)}" -t localhost/messenger-api:lab4 .
|
||||
$COMPOSE run --rm migrate
|
||||
|
||||
for old in $(replicas); do
|
||||
before=$(replicas)
|
||||
$ENGINE stop -t 10 "$old"
|
||||
$ENGINE rm "$old"
|
||||
$COMPOSE up -d --no-deps --no-recreate api
|
||||
for id in $(replicas); do
|
||||
if ! echo "$before" | grep -q "$id"; then
|
||||
wait_healthy "$id"
|
||||
fi
|
||||
done
|
||||
sleep 2
|
||||
done
|
||||
Executable
+17
@@ -0,0 +1,17 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
ENGINE=${ENGINE:-$(command -v podman >/dev/null 2>&1 && echo podman || echo docker)}
|
||||
PROJECT=${PROJECT:-messenger-lab4}
|
||||
IMAGE=${IMAGE:-localhost/messenger-api:lab4}
|
||||
|
||||
$ENGINE run --rm -i --network "${PROJECT}_backend" \
|
||||
--entrypoint /usr/local/bin/s3tool \
|
||||
-v "$PWD/secrets/s3_access_key_id:/run/secrets/s3_access_key_id:ro" \
|
||||
-v "$PWD/secrets/s3_secret_access_key:/run/secrets/s3_secret_access_key:ro" \
|
||||
-e S3_ENDPOINT="${S3_ENDPOINT:-http://s3:9000}" \
|
||||
-e S3_REGION="${S3_REGION:-us-east-1}" \
|
||||
-e S3_BUCKET="${S3_BUCKET:-messenger-attachments}" \
|
||||
-e S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_access_key_id \
|
||||
-e S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret_access_key \
|
||||
"$IMAGE" "$@"
|
||||
@@ -0,0 +1,12 @@
|
||||
CREATE OR REPLACE FUNCTION inject_attachment_insert_failure() RETURNS trigger AS $$
|
||||
BEGIN
|
||||
RAISE EXCEPTION 'injected attachment insert failure' USING ERRCODE = '57014';
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
DROP TRIGGER IF EXISTS inject_attachment_insert_failure ON attachments;
|
||||
|
||||
CREATE TRIGGER inject_attachment_insert_failure
|
||||
BEFORE INSERT ON attachments
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION inject_attachment_insert_failure();
|
||||
@@ -0,0 +1,2 @@
|
||||
DROP TRIGGER IF EXISTS inject_attachment_insert_failure ON attachments;
|
||||
DROP FUNCTION IF EXISTS inject_attachment_insert_failure();
|
||||
Executable
+13
@@ -0,0 +1,13 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
ENGINE=${ENGINE:-$(command -v podman >/dev/null 2>&1 && echo podman || echo docker)}
|
||||
PROJECT=${PROJECT:-messenger-lab4}
|
||||
: "${TARGET_INSTANCE_ID:?TARGET_INSTANCE_ID is required}"
|
||||
|
||||
for id in $($ENGINE ps -q --filter "label=com.docker.compose.project=$PROJECT" --filter "label=com.docker.compose.service=api"); do
|
||||
if [ "$($ENGINE inspect -f '{{.Config.Hostname}}' "$id")" = "$TARGET_INSTANCE_ID" ]; then
|
||||
$ENGINE stop -t 10 "$id" >/dev/null
|
||||
exit 0
|
||||
fi
|
||||
done
|
||||
exit 1
|
||||
Executable
+62
@@ -0,0 +1,62 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Reconciler traversal cost for several object counts; creates synthetic objects and removes them."""
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
S3TOOL = ROOT / 'scripts/s3tool.sh'
|
||||
RECONCILE = ROOT / 'scripts/reconcile.sh'
|
||||
PREFIX = 'chats/bench'
|
||||
SIZES = (100, 1000, 5000)
|
||||
|
||||
|
||||
def call(arguments):
|
||||
return subprocess.run(arguments, capture_output=True, text=True, check=False)
|
||||
|
||||
|
||||
def reconcile(*arguments):
|
||||
result = call([str(RECONCILE), *arguments])
|
||||
try:
|
||||
return json.loads(result.stdout)
|
||||
except json.JSONDecodeError:
|
||||
print(result.stdout[-500:], file=sys.stderr)
|
||||
print(result.stderr[-500:], file=sys.stderr)
|
||||
raise SystemExit('reconcile did not return JSON')
|
||||
|
||||
|
||||
def main():
|
||||
measurements = []
|
||||
for count in SIZES:
|
||||
seeded = call([str(S3TOOL), 'seed', PREFIX, str(count), '1024'])
|
||||
if seeded.returncode != 0:
|
||||
raise SystemExit('seed failed: ' + seeded.stderr[-400:])
|
||||
|
||||
scan = reconcile('-prefix', PREFIX + '/', '-grace', '0s')
|
||||
if scan['scanned_objects'] != count:
|
||||
raise SystemExit(f'expected {count} objects, scanned {scan["scanned_objects"]}')
|
||||
if len(scan['orphans']) != count:
|
||||
raise SystemExit(f'expected {count} orphans, got {len(scan["orphans"])}')
|
||||
|
||||
repair = reconcile('-prefix', PREFIX + '/', '-grace', '0s', '-dry-run=false')
|
||||
if repair['deleted_objects'] != count:
|
||||
raise SystemExit(f'expected {count} deletions, got {repair["deleted_objects"]}')
|
||||
|
||||
measurements.append({
|
||||
'objects': count,
|
||||
'scan_ms': scan['duration_ms'],
|
||||
'scan_s3_requests': scan['s3_requests'],
|
||||
'scan_db_queries': scan['db_queries'],
|
||||
'scan_objects_per_second': round(scan['objects_per_second'], 1),
|
||||
'scan_ms_per_object': round(scan['duration_ms'] / count, 4),
|
||||
'repair_ms': repair['duration_ms'],
|
||||
'repair_s3_requests': repair['s3_requests'],
|
||||
'repair_deleted': repair['deleted_objects'],
|
||||
})
|
||||
|
||||
print(json.dumps({'prefix': PREFIX, 'measurements': measurements}, indent=2))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Executable
+130
@@ -0,0 +1,130 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Concurrent upload experiment with API memory measurement; creates synthetic data."""
|
||||
import json
|
||||
import math
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
||||
from client import Client, Failure, arguments, check, run
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def engine():
|
||||
return 'podman' if shutil.which('podman') else 'docker'
|
||||
|
||||
|
||||
def api_containers(project):
|
||||
result = subprocess.run(
|
||||
[engine(), 'ps',
|
||||
'--filter', 'label=com.docker.compose.project=' + project,
|
||||
'--filter', 'label=com.docker.compose.service=api',
|
||||
'--format', '{{.Names}}'],
|
||||
capture_output=True, text=True, check=False)
|
||||
check(result.returncode == 0, 'cannot list api containers: ' + result.stderr.strip())
|
||||
return sorted(line.strip() for line in result.stdout.splitlines() if line.strip())
|
||||
|
||||
|
||||
def peak_bytes(container):
|
||||
for path in ('/sys/fs/cgroup/memory.peak', '/sys/fs/cgroup/memory/memory.max_usage_in_bytes'):
|
||||
result = subprocess.run([engine(), 'exec', container, 'cat', path], capture_output=True, text=True, check=False)
|
||||
if result.returncode == 0 and result.stdout.strip().isdigit():
|
||||
return int(result.stdout.strip())
|
||||
return None
|
||||
|
||||
|
||||
def main():
|
||||
def configure(p):
|
||||
p.add_argument('--concurrency', type=int, default=10)
|
||||
p.add_argument('--file', default=str(ROOT / 'scripts/fixtures/large.png'))
|
||||
p.add_argument('--project', default='messenger-lab4')
|
||||
p.add_argument('--output', default=None)
|
||||
|
||||
args = arguments('Concurrent attachment uploads with peak memory measurement', configure)
|
||||
check(1 <= args.concurrency <= 64, 'concurrency must be 1..64')
|
||||
source = Path(args.file)
|
||||
check(source.is_file(), 'upload fixture not found: ' + str(source))
|
||||
content = source.read_bytes()
|
||||
|
||||
containers = api_containers(args.project)
|
||||
check(bool(containers), 'no api containers found for project ' + args.project)
|
||||
baseline = {name: peak_bytes(name) for name in containers}
|
||||
|
||||
c = Client()
|
||||
alice, _, _, chat = c.bootstrap()
|
||||
actor = {'id': alice['id'], 'token': alice['token']}
|
||||
path = '/api/v1/chats/' + chat['id'] + '/attachments?filename=' + source.name
|
||||
|
||||
barrier = threading.Barrier(args.concurrency)
|
||||
lock = threading.Lock()
|
||||
latencies, failures = [], []
|
||||
|
||||
def worker(index):
|
||||
local = Client(timeout=120)
|
||||
barrier.wait()
|
||||
started = time.monotonic()
|
||||
try:
|
||||
response = local.call('POST', path, 201, actor=actor, raw=content,
|
||||
headers={'Content-Type': 'image/png'}, schema='Attachment')
|
||||
with lock:
|
||||
latencies.append(time.monotonic() - started)
|
||||
check(response['json']['size_bytes'] == len(content), 'uploaded size mismatch')
|
||||
except Failure as exc:
|
||||
with lock:
|
||||
failures.append(str(exc))
|
||||
|
||||
started = time.monotonic()
|
||||
threads = [threading.Thread(target=worker, args=(i,)) for i in range(args.concurrency)]
|
||||
for thread in threads:
|
||||
thread.start()
|
||||
for thread in threads:
|
||||
thread.join()
|
||||
elapsed = time.monotonic() - started
|
||||
|
||||
peaks = {name: peak_bytes(name) for name in containers}
|
||||
peak_delta = {}
|
||||
for name in containers:
|
||||
before = baseline[name]
|
||||
after = peaks[name]
|
||||
if before is None or after is None:
|
||||
peak_delta[name] = None
|
||||
else:
|
||||
peak_delta[name] = after - before
|
||||
|
||||
ordered = sorted(latencies)
|
||||
def percentile(p):
|
||||
return round(1000 * ordered[max(0, math.ceil(p * len(ordered)) - 1)], 1) if ordered else None
|
||||
|
||||
report = {
|
||||
'concurrency': args.concurrency,
|
||||
'uploads': args.concurrency,
|
||||
'fixture': source.name,
|
||||
'fixture_bytes': len(content),
|
||||
'elapsed_seconds': round(elapsed, 3),
|
||||
'successful': len(latencies),
|
||||
'failed': len(failures),
|
||||
'failures': failures[:3],
|
||||
'latency_ms': {'p50': percentile(0.50), 'p95': percentile(0.95), 'p99': percentile(0.99)},
|
||||
'api_containers': containers,
|
||||
'peak_bytes_before': baseline,
|
||||
'peak_bytes_after': peaks,
|
||||
'peak_delta_bytes': peak_delta,
|
||||
'peak_max_mb': round(max(v for v in peaks.values() if v is not None) / (1024 * 1024), 2) if any(v is not None for v in peaks.values()) else None,
|
||||
}
|
||||
text = json.dumps(report, indent=2)
|
||||
if args.output:
|
||||
destination = Path(args.output)
|
||||
destination.parent.mkdir(parents=True, exist_ok=True)
|
||||
destination.write_text(text + '\n')
|
||||
print(text)
|
||||
check(not failures, 'some uploads failed')
|
||||
check(len(latencies) == args.concurrency, 'not all uploads succeeded')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(main)
|
||||
Executable
+39
@@ -0,0 +1,39 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Single binary upload for failure drills; asserts the expected status code."""
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
||||
from client import Client, arguments, check, run
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def main():
|
||||
def configure(p):
|
||||
p.add_argument('--expect', type=int, default=201)
|
||||
p.add_argument('--file', default=str(ROOT / 'scripts/fixtures/sample.jpg'))
|
||||
p.add_argument('--content-type', default='image/jpeg')
|
||||
p.add_argument('--attach', action='store_true', help='also attach the upload to a new message')
|
||||
|
||||
args = arguments('Single upload used by compensation drills; creates synthetic data', configure)
|
||||
source = Path(args.file)
|
||||
check(source.is_file(), 'file not found: ' + str(source))
|
||||
content = source.read_bytes()
|
||||
|
||||
c = Client()
|
||||
alice, _, _, chat = c.bootstrap()
|
||||
response = c.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=' + source.name,
|
||||
args.expect, actor=alice, raw=content,
|
||||
headers={'Content-Type': args.content_type}, schema='Attachment')
|
||||
print(f'status={response["status"]}')
|
||||
if response['status'] == 201:
|
||||
attachment = response['json']
|
||||
print(f"attachment={attachment['id']} size={attachment['size_bytes']}")
|
||||
if args.attach:
|
||||
message = c.message(alice, chat, '', attachments=[attachment['id']])['json']
|
||||
print(f"message={message['id']} attachment_ids={message['attachment_ids']}")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(main)
|
||||
Reference in New Issue
Block a user