Solution/bpi2301 bakulin #160
@@ -18,3 +18,6 @@ __pycache__
|
||||
node_modules
|
||||
evidence
|
||||
.DS_Store
|
||||
compose*.yaml
|
||||
bench-out
|
||||
bin
|
||||
|
||||
+3
-1
@@ -11,10 +11,12 @@ SESSION_STORE=postgres
|
||||
CACHE_URL_FILE=/run/secrets/cache_url
|
||||
API_REPLICAS=2
|
||||
IDEMPOTENCY_TTL_SECONDS=86400
|
||||
S3_ENDPOINT=http://s3:9000
|
||||
S3_ENDPOINT=http://s3:8333
|
||||
S3_REGION=us-east-1
|
||||
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
|
||||
S3_RECONCILE_ACCESS_KEY_ID_FILE=/run/secrets/s3_reconcile_access_key_id
|
||||
S3_RECONCILE_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_reconcile_secret_access_key
|
||||
MAX_UPLOAD_BYTES=10485760
|
||||
MAX_IMAGE_PIXELS=40000000
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
# Files read inside Linux containers or by sh must keep LF on Windows
|
||||
# checkouts with core.autocrlf=true.
|
||||
*.sh text eol=lf
|
||||
*.conf text eol=lf
|
||||
*.cfg text eol=lf
|
||||
*.sql text eol=lf
|
||||
*.cmd text eol=crlf
|
||||
@@ -1,18 +1,26 @@
|
||||
## Лабораторная 4
|
||||
|
||||
Автор / группа: TODO
|
||||
Целевая оценка (3/4/5): TODO
|
||||
Трек 5, если заявлен: TODO
|
||||
Автор указывается при публикации. Группа: БПИ2301.
|
||||
Целевая оценка: **5**.
|
||||
Трек 2: **согласованность двух хранилищ**.
|
||||
|
||||
Что реализовано и что перенесено: TODO
|
||||
Команды запуска из чистого clone: TODO
|
||||
Результаты проверок и ссылка на REPORT.md: TODO
|
||||
Ограничения / известные ошибки: TODO
|
||||
Источники и использование ИИ: TODO
|
||||
Перенесены API/БД/сессии/балансировка/идемпотентность lab3 и её серии экспериментов. Добавлены приватный SeaweedFS, ограниченные S3 identities, потоковые PNG/JPEG upload/download и проверка изображений, content-addressed `files` и отдельные ACL-вложения с UUID, атомарные связи сообщений и reconciler с dry-run/apply/deep.
|
||||
|
||||
- [ ] Весь обязательный минимум текущей и предыдущих работ сохранён с объявленными переходами.
|
||||
- [ ] Контракт и публичные тесты не ослаблены.
|
||||
- [ ] make check и make test выполнены; приложен реальный вывод.
|
||||
- [ ] Сценарии отказов/безопасности своего уровня воспроизведены.
|
||||
- [ ] Все секреты и приватные ключи находятся вне Git; отчёт не содержит tokens.
|
||||
- [ ] README задания сохранён, REPORT.md содержит инструкцию именно моего решения.
|
||||
Повторная загрузка одинаковых байтов создаёт новое логическое вложение, сохраняя единственную постоянную копию содержимого. Сборщик сериализуется с публикацией по хэшу, защищает свежие и используемые данные, обнаруживает missing/orphan и безопасно повторяет исправления.
|
||||
|
||||
Запуск: на Windows/Git Bash `export PYTHONUTF8=1`; затем `cp .env.example .env`, `make secrets`, `make up`, `make test PYTHON=python`, `make check PYTHON=python`, `make unit`, `make vet`. Полные команды, решения, границы согласованности, результаты и ограничения измерений — в [REPORT.md](../REPORT.md), исходные данные — в `evidence/`.
|
||||
|
||||
Изменения базы lab3, выделенные при переносе: обычный shutdown увеличен для активных бинарных запросов; старт ожидает свежие ответы LB. После ревью исправлены оценка памяти progressive JPEG/широкого PNG, дедлайн GET метаданных и классификация обрывов S3. S3 bootstrap ждёт регистрации сохранённых volumes, а не только ответа gateway.
|
||||
|
||||
Ограничения: один узел каждого хранилища и LB; HTTP только локально; нет удаления клиентских вложений и резервного восстановления повреждённых байтов. Дедупликация экономит хранение, не трафик; полная проверка изображения требует ограниченной, но существенной памяти. Числа лабораторного опыта не являются production SLA.
|
||||
|
||||
Источники: контракт репозитория, документация SeaweedFS/minio-go/PostgreSQL/S3, ссылки в REPORT.
|
||||
|
||||
ИИ-ассистент использовался как парный программист: генерация кода, тестов и черновиков документации по моим архитектурным решениям с последующим ревью каждого модуля и переработкой по замечаниям; выбор стека, структура проекта, модель данных, обработка ошибок, логирование и границы модулей определены мной; все проверки воспроизведены мной вручную.
|
||||
|
||||
- [x] Весь обязательный минимум текущей и предыдущих работ сохранён с объявленными переходами.
|
||||
- [x] Контракт и публичные тесты не ослаблены.
|
||||
- [x] make check и make test выполнены; приложен реальный вывод.
|
||||
- [x] Сценарии отказов/безопасности своего уровня воспроизведены.
|
||||
- [x] Все секреты и приватные ключи находятся вне Git; отчёт не содержит tokens.
|
||||
- [x] README задания сохранён, REPORT.md содержит инструкцию именно моего решения.
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
name: Application
|
||||
on: [push, pull_request]
|
||||
permissions:
|
||||
contents: read
|
||||
defaults:
|
||||
run:
|
||||
# Explicit bash turns on `-eo pipefail`, so a failing check behind `| tee` fails the job.
|
||||
shell: bash
|
||||
env:
|
||||
BASE_URL: http://localhost:8080
|
||||
jobs:
|
||||
unit:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: '1.27.1'
|
||||
- run: go vet ./...
|
||||
- run: go test -race -count=1 ./...
|
||||
e2e:
|
||||
runs-on: ubuntu-latest
|
||||
needs: unit
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: '1.27.1'
|
||||
- uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: '3.12'
|
||||
- run: make secrets
|
||||
- run: docker compose up -d --build --wait
|
||||
- run: mkdir -p evidence-ci
|
||||
- name: Public smoke
|
||||
run: make test PYTHON=python BASE_URL="$BASE_URL" | tee evidence-ci/smoke.txt
|
||||
- name: Persistence across a restart of the API and both stores
|
||||
run: make test-persistence PYTHON=python BASE_URL="$BASE_URL" ACTION_SCRIPT=scripts/restart.sh | tee evidence-ci/persistence.txt
|
||||
- name: Failover of one replica behind the balancer
|
||||
run: |
|
||||
make test-failover PYTHON=python BASE_URL="$BASE_URL" ACTION_SCRIPT=scripts/stop-one.sh | tee evidence-ci/failover.txt
|
||||
docker compose start api
|
||||
docker compose up -d --wait
|
||||
- name: Concurrency and shared idempotency across replicas
|
||||
run: go run ./tools/concurrency --base-url "$BASE_URL" --output evidence-ci/concurrency.json
|
||||
- name: Container logs
|
||||
if: always()
|
||||
run: docker compose logs > evidence-ci/compose.log 2>&1 || true
|
||||
- name: Cleanup
|
||||
# The CI stand is disposable, so its volumes go too.
|
||||
if: always()
|
||||
run: docker compose down -v || true
|
||||
- uses: actions/upload-artifact@v4
|
||||
if: always()
|
||||
with:
|
||||
name: evidence
|
||||
path: evidence-ci
|
||||
@@ -30,3 +30,5 @@ coverage/
|
||||
.vscode/
|
||||
# Keep small, sanitized evidence summaries in evidence/ under version control.
|
||||
# Inspect git diff --cached: ignore rules are not a secret scanner.
|
||||
# Raw output of local benchmark runs; curated results go to evidence/.
|
||||
bench-out/
|
||||
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
# Modules in their own layer and a persistent build cache keep the rebuild of
|
||||
# a rolling update short.
|
||||
FROM golang:1.27.1-alpine3.23@sha256:d9e2f2f07b10cc922da3e80e035c3058810b328d5aef82d2c63680967c5e2ec9 AS build
|
||||
WORKDIR /src
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download && go mod verify
|
||||
COPY cmd ./cmd
|
||||
COPY internal ./internal
|
||||
COPY migrations ./migrations
|
||||
RUN --mount=type=cache,target=/root/.cache/go-build \
|
||||
CGO_ENABLED=0 GOFLAGS=-mod=readonly go build -trimpath -ldflags="-s -w" -o /out/ ./cmd/server ./cmd/migrate ./cmd/reconcile ./cmd/s3init
|
||||
|
||||
# The load generator gets an image of its own; the API image does not carry it.
|
||||
FROM build AS loadgen-build
|
||||
COPY tools ./tools
|
||||
RUN --mount=type=cache,target=/root/.cache/go-build \
|
||||
CGO_ENABLED=0 GOFLAGS=-mod=readonly go build -trimpath -ldflags="-s -w" -o /out/loadgen ./tools/loadgen
|
||||
|
||||
FROM alpine:3.23@sha256:fd791d74b68913cbb027c6546007b3f0d3bc45125f797758156952bc2d6daf40 AS loadgen
|
||||
RUN adduser -D -H -u 10001 app
|
||||
COPY --from=loadgen-build /out/loadgen /usr/local/bin/loadgen
|
||||
USER app
|
||||
ENTRYPOINT ["/usr/local/bin/loadgen"]
|
||||
|
||||
# Last, so that `build: .` builds it; it also runs the migrate service.
|
||||
FROM alpine:3.23@sha256:fd791d74b68913cbb027c6546007b3f0d3bc45125f797758156952bc2d6daf40
|
||||
RUN adduser -D -H -u 10001 app
|
||||
COPY --from=build /out/server /out/migrate /out/reconcile /out/s3init /usr/local/bin/
|
||||
USER app
|
||||
ENV APP_HOST=0.0.0.0 \
|
||||
APP_PORT=8080 \
|
||||
APP_ENV=lab
|
||||
EXPOSE 8080
|
||||
# The start interval lets a rolling update see a new replica healthy within
|
||||
# about a second.
|
||||
HEALTHCHECK --interval=5s --timeout=2s --start-period=15s --start-interval=1s --retries=3 \
|
||||
CMD wget -qO- "http://127.0.0.1:${APP_PORT:-8080}/health/ready" >/dev/null || exit 1
|
||||
ENTRYPOINT ["/usr/local/bin/server"]
|
||||
@@ -28,3 +28,85 @@ 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)
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Application targets (Go implementation). The targets above are the skeleton.
|
||||
# ---------------------------------------------------------------------------
|
||||
EVIDENCE_DIR ?= evidence
|
||||
|
||||
.PHONY: build vet unit secrets up down destroy logs concurrency explain instances rollout bench plot failover-load rollout-load
|
||||
|
||||
build:
|
||||
go build -o bin/server ./cmd/server
|
||||
|
||||
vet:
|
||||
go vet ./...
|
||||
|
||||
# The tests start disposable PostgreSQL and Valkey containers: Docker must run.
|
||||
unit:
|
||||
go test -race -count=1 ./...
|
||||
|
||||
secrets:
|
||||
sh scripts/init-secrets.sh
|
||||
|
||||
up:
|
||||
docker compose up -d --build --wait
|
||||
sh scripts/wait-ready.sh
|
||||
|
||||
# Stops and removes the containers; volumes and therefore data are kept.
|
||||
down:
|
||||
docker compose down
|
||||
|
||||
# Deletes the volumes too: every user, chat, message and session of the stand.
|
||||
destroy:
|
||||
docker compose down -v
|
||||
|
||||
logs:
|
||||
docker compose logs
|
||||
|
||||
# Safe default: inspection only; APPLY=1 explicitly enables repairs.
|
||||
APPLY ?= 0
|
||||
DEEP ?= 0
|
||||
.PHONY: reconcile upload-memory reconcile-bench
|
||||
reconcile:
|
||||
docker compose --profile recon run --rm reconcile $(if $(filter 1,$(APPLY)),--apply,) $(if $(filter 1,$(DEEP)),--deep,)
|
||||
|
||||
upload-memory:
|
||||
$(PYTHON) -X utf8 scripts/lab4/upload-memory.py --base-url "$(BASE_URL)" --output "$(EVIDENCE_DIR)/upload-memory.json"
|
||||
|
||||
reconcile-bench:
|
||||
go run ./tools/reconcilebench --output "$(EVIDENCE_DIR)/track2/results.json"
|
||||
|
||||
# Which container answers under which X-Instance-Id.
|
||||
instances:
|
||||
sh scripts/instances.sh
|
||||
|
||||
# Replaces every API replica without downtime; all of them must be running.
|
||||
rollout:
|
||||
sh scripts/rollout.sh
|
||||
|
||||
# Track 1. Every run starts from empty volumes, so the stand's data is lost.
|
||||
# make bench SERIES="scripts/bench/series/A.conf scripts/bench/series/B.conf" RATES=400,800,1200
|
||||
SERIES ?= scripts/bench/series/A.conf
|
||||
bench:
|
||||
sh scripts/bench/run-series.sh $(SERIES)
|
||||
|
||||
# Needs matplotlib from requirements-bench.txt: make plot PYTHON=<venv python>.
|
||||
plot:
|
||||
$(PYTHON) scripts/bench/plot.py evidence/track1/raw evidence/track1
|
||||
|
||||
# Level 4 under load: one replica stopped (KIND=stop) or killed (KIND=kill),
|
||||
# and the rolling update.
|
||||
KIND ?= stop
|
||||
REPLICA ?= 1
|
||||
failover-load:
|
||||
sh scripts/bench/failover.sh $(KIND) $(REPLICA)
|
||||
|
||||
rollout-load:
|
||||
sh scripts/bench/rollout-under-load.sh
|
||||
|
||||
concurrency:
|
||||
go run ./tools/concurrency --base-url "$(BASE_URL)" --output "$(EVIDENCE_DIR)/concurrency.json"
|
||||
|
||||
explain:
|
||||
docker compose exec -T postgres psql -U messenger -d messenger -X -q -v ON_ERROR_STOP=1 -f - < scripts/explain-history.sql
|
||||
|
||||
@@ -1,37 +1,179 @@
|
||||
# Отчёт — лабораторная 4
|
||||
|
||||
- Автор, группа: TODO
|
||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
||||
- Commit/PR: TODO после публикации
|
||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
||||
Цель: **5, трек 2 — согласованность PostgreSQL и S3**. Автор и ссылка на PR указываются при публикации.
|
||||
|
||||
## Запуск из чистого clone
|
||||
|
||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
||||
Нужны Docker Desktop с Compose v2, Git Bash и Make. Для Go-тестов — Go 1.27.1, C-компилятор для `-race` и Docker; для публичных проверок — Python 3.9+. На Windows используйте `PYTHON=python`.
|
||||
|
||||
В Git Bash на Windows сначала выполните `export PYTHONUTF8=1`, чтобы Python читал UTF-8 контракты независимо от системной кодировки. В PowerShell эквивалент — `$env:PYTHONUTF8='1'`.
|
||||
|
||||
```sh
|
||||
cp .env.example .env
|
||||
make secrets
|
||||
make up
|
||||
make test PYTHON=python
|
||||
make check PYTHON=python
|
||||
make unit
|
||||
make vet
|
||||
```
|
||||
|
||||
Полный зафиксированный Go-прогон: `go test -race -p 2 -count=1 ./...` (эквивалент `make unit GOFLAGS=-p=2`). Ограничена только параллельность пакетов с Docker-фикстурами; конкурентные тесты внутри пакетов не отключены.
|
||||
|
||||
LB: `http://localhost:8080`; статистика HAProxy — только `127.0.0.1:8404`. Если порт занят, до запуска и restart-проверок задайте `export PUBLISH=127.0.0.1:18080 BASE_URL=http://localhost:18080`.
|
||||
|
||||
После Compose `make up` и restart-hook дополнительно проверяют свежие readiness-ответы через LB: прежний Docker health status балансировщика может ещё оставаться healthy, пока его active checks выводят новые API из состояния DOWN. Ожидание ограничено 60 пробами, успех — три последовательных ответа.
|
||||
|
||||
`make secrets` создаёт игнорируемый Git каталог `secrets/`; повторный запуск сохраняет существующие значения. Неполный комплект не заменяется молча. Секреты передаются файлами `*_FILE`, не значениями в `.env`. `make down` сохраняет volumes. **`make destroy` безвозвратно удаляет учебные данные PostgreSQL, Valkey и S3**. Удаление файлов секретов при сохранённых volumes не является сбросом паролей.
|
||||
|
||||
## Архитектура и решения
|
||||
|
||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab3, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
||||
Клиент → HAProxy → API × 2 → PostgreSQL (метаданные), Valkey (сессии), SeaweedFS (байты). Перенесены lab3 API, устойчивые сессии, идемпотентность сообщений, drain/rolling update и инструменты исследования. Серии старого трека 1 сохранены, их результаты не выдаются за lab4.
|
||||
|
||||
SeaweedFS **4.53** и остальные Docker-образы закреплены digest в `compose.yaml`; SDK — `minio-go/v7` **7.4.1**. `weed server` хранит и filer-метаданные, и байты в `s3data`. Внутренние master/filer/volume HTTP и gRPC слушают loopback контейнера, исключая сетевой обход S3-авторизации. Наружу не опубликован ни S3, ни административный интерфейс.
|
||||
|
||||
Миграция `00003_attachments.sql` добавляет:
|
||||
|
||||
| Таблица | Назначение |
|
||||
| --- | --- |
|
||||
| `files(hash, size_bytes, mime_type, status, created_at)` | Одна запись на содержимое; PK SHA-256 — 32 байта `bytea`. `created_at` нужен для grace period. |
|
||||
| `attachments(id, chat_id, owner_id, filename, file_hash, created_at)` | Логическое вложение с UUID и доступом через чат; существует до сообщения. |
|
||||
| `message_attachments(chat_id, message_id, attachment_id, ordinal)` | Ссылки сообщения; `ordinal` сохраняет порядок при чтении и идемпотентном повторе. |
|
||||
|
||||
`path` не хранится: ключ — `blobs/<lowercase SHA-256>`. Имя используется только для отображения. Индекс `attachments(file_hash)` нужен для проверки использования общего файла; FK защищают ссылки. Принадлежность вложений чату проверяется одним запросом в транзакции отправки. История возвращает упорядоченные `attachment_ids` одним SQL-запросом.
|
||||
|
||||
Одинаковые байты дают одну строку `files` и один постоянный объект. Каждая загрузка получает свой UUID, владельца, имя и чат: знание хэша не даёт доступа. Дедупликация экономит постоянное хранение, но не клиентский трафик — сервер должен принять тело, чтобы вычислить хэш.
|
||||
|
||||
### Upload и download
|
||||
|
||||
Принимаются сырые PNG/JPEG, **не multipart**, до **10 MiB** и **40 млн пикселей**, с истинным `Content-Type`. Нужен положительный `Content-Length`; неизвестная длина — 400. Имя — 1–255 code points, без разделителей пути и управляющих символов. Чужой объект даёт 404; аутентификация выполняется до разбора параметров.
|
||||
|
||||
1. Поток с жёстким лимитом записывается в `staging/<UUID>`, одновременно считаются размер и SHA-256. Локального оригинала на API нет.
|
||||
2. Проверяются формат и число пикселей **до выделения пиксельного буфера**, затем изображение полностью декодируется для обнаружения повреждённого body.
|
||||
3. Под транзакционной advisory-блокировкой хэша проверяются `files` и общий объект. Готовые байты не копируются повторно; отсутствующий blob восстанавливается из новой загрузки.
|
||||
4. При необходимости серверный Copy публикует `blobs/<hash>`. Одна SQL-транзакция сохраняет готовый файл и новое логическое вложение; 201 возвращается после COMMIT.
|
||||
5. Staging удаляется отдельной ограниченной по времени компенсацией; её неудачу покрывает reconciler.
|
||||
|
||||
Пиксельный буфер 40 млн RGBA64 занимает 320 MB, но это не вся память декодера: progressive JPEG хранит коэффициенты, а PNG — текущую и предыдущую строки. Например, progressive JPEG 4:4:4 8000×5000 требует около 600 MB живых буферов, а однострочный RGBA64 PNG на 40 млн пикселей — около 960 MB. Поэтому допускается **один декодер на реплику**, API ограничен **2 GiB**, `GOMEMLIMIT=1536MiB`. Первоначальная оценка 512 MiB исправлена после независимого ревью. Upload/download остаются потоковыми; проверка только заголовка не используется, поскольку она пропускает повреждённый body.
|
||||
|
||||
Обычный запрос, включая GET метаданных, имеет бюджет 5 s, бинарный — 30 s; socket и соответствующий HAProxy timeout — 35 s. Shutdown допускает 38 s на активные обработчики, Compose stop grace — 45 s с учётом drain 2 s. `MAX_UPLOAD_BYTES` и `MAX_IMAGE_PIXELS` можно уменьшить, но нельзя повысить сверх контракта. Для транзакций с S3 I/O локальный idle timeout увеличен до 35 s. Обрыв чтения S3 при декодировании возвращает 503; повреждённые байты, успешно прочитанные из хранилища, — 415.
|
||||
|
||||
Download сначала авторизует вложение через чат, затем передаёт оригинал через API без redirect. `variants=[]`, `error_code=null`, у неизменяемой логической записи `updated_at=created_at`. Внутренний `failed` отвечает 409 вместо JSON вне обязательного enum lab4.
|
||||
|
||||
S3 намеренно исключён из readiness: при его отказе файловые операции дают 503, текстовые сообщения и доступные метаданные продолжают работать. PostgreSQL и Valkey остаются критическими зависимостями.
|
||||
|
||||
### Согласованность и reconciler
|
||||
|
||||
Общей транзакции PostgreSQL/S3 нет: успешный Copy и неуспешный COMMIT оставляют orphan. При ошибке одной загрузки нельзя удалять общий blob — он может использоваться другими. Компенсируется только её staging.
|
||||
|
||||
Публикация и удаление используют одну `pg_advisory_xact_lock` по первым 8 байтам SHA-256. Коллизия ключа блокировки лишь сериализует разные файлы. Перед изменением reconciler повторно проверяет метаданные, ссылки и возраст текущего объекта под этой блокировкой. Память обхода ограничена страницами до 500 записей.
|
||||
|
||||
Grace period — 1 h, минимум 2 min, выше бюджета upload. Свежие данные и неизвестный возраст не разрешают удаление. По умолчанию CLI выполняет **dry-run без изменений**.
|
||||
|
||||
| Находка после grace | Действие с `--apply` |
|
||||
| --- | --- |
|
||||
| Старый staging | Повторный HEAD, затем удаление. |
|
||||
| Старый blob без `files` | Под блокировкой повторить проверку БД и возраста, удалить объект. |
|
||||
| Старый pending/ready без единого логического вложения | Удалить объект, затем строку; свежие байты защищают даже старую строку. |
|
||||
| Используемый ready без объекта | Пометить файл failed, сохранить вложения, сообщения и связи. |
|
||||
| Неверный размер или SHA-256 (`--deep`) | Только сообщить, не удалять используемые данные. |
|
||||
|
||||
Непривязанное к сообщению логическое вложение не считается мусором — его можно прикрепить позже. Повторный apply безопасен; уже обработанный missing не создаёт нового исправления. Повторная проверенная загрузка восстанавливает отсутствующий blob и файл в failed. Для ready с существующим объектом того же размера действует быстрый Stat-путь. Deep-повреждение такого ready диагностируется и требует отдельного восстановления из достоверных байтов. Простое появление объекта само не меняет failed на ready.
|
||||
|
||||
```sh
|
||||
make reconcile
|
||||
make reconcile APPLY=1
|
||||
make reconcile DEEP=1
|
||||
```
|
||||
|
||||
В каждой API-реплике shallow apply выполняется каждые 10 min с grace 1 h и бюджетом прохода 2 min. Прерывание логируется; ручной CLI позволяет выполнить полный обход. Ошибка S3, запрет доступа или отсутствие bucket не считаются отсутствием отдельного файла.
|
||||
|
||||
Bootstrap — одноразовый `s3-init` с отдельным admin-ключом; приватные статические identities загружаются до приёма запросов. После создания/проверки bucket bootstrap ждёт успешного чтения устойчивого служебного marker и write/read/delete-пробы: доступность S3 gateway сама по себе не означает, что прежние volumes уже зарегистрированы. Marker находится вне `staging/` и `blobs/`. Это барьер запуска, а не прозрачный retry загрузки пользователя. API имеет только Read/Write одного bucket, reconciler — дополнительно List. `S3_RECONCILE_*_FILE` выбирают отдельные ключи фонового обхода. `SESSION_STORE=postgres` и `CACHE_URL_FILE` в исходном skeleton не используются: фактический store сессий — Valkey через `SESSION_STORE_URL_FILE`, отдельного кэша нет.
|
||||
|
||||
## Доказательства критериев
|
||||
|
||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
||||
| --- | --- | --- | --- |
|
||||
| Публичный smoke | make test | TODO | TODO |
|
||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
||||
Обязательные `tests/`, `contracts/openapi.json` и README задания сохранены. Собственные проверки используют настоящий PostgreSQL/SeaweedFS; узкий двойник байтов нужен для детерминированных отказов. Отказ после Copy также внедряется SQL trigger, отвергающим INSERT метаданных.
|
||||
|
||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
||||
| Критерий | Команда / доказательство |
|
||||
| --- | --- |
|
||||
| Smoke: checksum, сообщение, ACL | `make test`; `evidence/smoke.txt` |
|
||||
| Restart БД/S3/сессий с volumes | `make test-persistence ACTION_SCRIPT=scripts/restart.cmd PYTHON=python`; `evidence/persistence.txt` |
|
||||
| Failover загрузившей реплики | `python -X utf8 scripts/lab4/failover.py`; `evidence/failover-uploaders.json`, обе реплики |
|
||||
| Конкурентная запись при отказе | `make test-failover ACTION_SCRIPT=scripts/stop-one.cmd PYTHON=python`; `evidence/failover.txt` |
|
||||
| MIME, повреждение, bytes/pixels, имя, чужой чат | `internal/attachment/validation_test.go`, `internal/app/attachment_test.go` |
|
||||
| Дедупликация, гонка upload/GC, SQL rollback | `internal/attachment/service_test.go` |
|
||||
| Приватность bucket вне API | `evidence/private-bucket.txt` |
|
||||
| Credentials, bootstrap, S3 restart | `internal/platform/s3store/*_test.go`, `internal/testenv/s3_test.go` |
|
||||
| Отказ S3, прерывание upload | `TestAttachmentRealS3RoundTripValidationAndOutage` |
|
||||
| Полный race/vet/check | `make unit`, `make vet`, `make check`; `evidence/tests.txt` |
|
||||
| ≥10 одновременных upload, память API | `make upload-memory PYTHON=python`; `evidence/upload-memory.json` |
|
||||
| Трек 2: faults, dry/apply/repeat, стоимость | `go run ./tools/reconcilebench`; `evidence/track2/` |
|
||||
|
||||
На Linux используйте `.sh` hooks; на Windows `.cmd` вызывает Git Bash `sh`, поскольку Python запускает hook без shell interpolation. Failover-hook оставляет выбранную реплику остановленной: восстановите её до повторного теста.
|
||||
|
||||
Публичная persistence-проверка прошла за 57.838 s: сохранены сообщения, исходный файл и прежняя сессия; `StartedAt` в доказательстве подтверждает перезапуск API, LB, PostgreSQL, Valkey и S3. Собственный failover остановил именно реплику из ответа upload для каждого из двух API: 4.837/4.160 s на остановку, после неё другой API вернул те же байты. Публичный concurrent failover: 130 подтверждённых записей, 0 неудачных попыток, восстановление записи за 4.265 s; дублей и потери подтверждённых данных нет. Остановленные API восстановлены.
|
||||
|
||||
Дополнительный `make validate` требует `requirements-dev.txt`. В этой сессии установка валидатора из PyPI не завершилась из-за сетевых timeout, поэтому локальная полная OpenAPI-валидация не заявляется. Контракт не изменён; offline check и используемое публичными тестами подмножество JSON Schema проверены.
|
||||
|
||||
## Эксперименты и отказы
|
||||
|
||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
||||
Гипотеза трека 2: сериализация по хэшу и повторные проверки позволяют безопасно убирать старые orphan/pending, сохраняя свежие и используемые данные; повторный проход не выполняет повторных исправлений. Deep дополнительно читает байты и потому дороже shallow.
|
||||
|
||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
||||
| --- | --- | --- | --- |
|
||||
| TODO | TODO | TODO | TODO |
|
||||
Машинные исходные данные и методика находятся в `evidence/track2/` и `evidence/upload-memory.json`. Память измеряется у контейнеров API. Семплирование может пропустить короткий пик: это наблюдённый максимум Docker working set, не доказательство абсолютного RSS bound.
|
||||
|
||||
Два burst-прогона по 10 одновременно стартующих загрузок на API × 2, baseline/tail по 3 s, одинаковые байты внутри серии, прогретое хранилище. Дедупликация могла использовать объект предыдущего прогона, но каждое тело полностью передавалось и декодировалось. Во всех 20 запросах — 201 и правильный checksum; последующее скачивание каждой серии совпало побайтно.
|
||||
|
||||
| PNG payload | Успехи | p50, ms | p95/max, ms | Наибольший sampled working set реплики, MiB |
|
||||
| --- | ---: | ---: | ---: | ---: |
|
||||
| 1 MiB, 512×512 RGB | 10/10 | 216.34 | 259.15 | 69.6 |
|
||||
| 10 MiB, 1600×1600 RGB | 10/10 | 1 865.39 | 1 997.06 | 69.6 |
|
||||
|
||||
При 10 наблюдениях nearest-rank p95 совпадает с максимумом, это не устойчивый хвостовой percentile. Маленькая серия короче частоты семплирования; post-tail сохранён, но краткие пики могут быть пропущены. Это эксперимент памяти, не измерение точки насыщения или production throughput.
|
||||
|
||||
Отдельно выполнены одиночные upload/download на границе **40 млн пикселей** с более дорогими путями декодера:
|
||||
|
||||
| Формат | Размер входа, B | Наибольший sampled working set, MiB | Итог |
|
||||
| --- | ---: | ---: | --- |
|
||||
| Progressive JPEG RGB 4:4:4, 8000×5000 | 469 711 | 585.2 | 201, checksum совпал, без OOM/restart |
|
||||
| Progressive JPEG CMYK 4:4:4, 8000×5000 | 939 079 | 945.5 | 201, checksum совпал, без OOM/restart |
|
||||
| PNG RGBA64, 40 000 000×1 | 1 397 354 | 1 290.2 | 201, checksum совпал, без OOM/restart |
|
||||
|
||||
Полные сэмплы, container limits и состояния до/после — `evidence/decoder-memory-*.json`. Старый прогон при 512 MiB оставлен отдельно как историческое сравнение, не как доказательство текущего лимита. В стресс-серии процесс уже был прогрет предыдущими изображениями: baseline может включать не возвращённую ОС память. Три выбранных формата не доказывают абсолютный максимум для всех допустимых файлов и сочетаний нагрузки.
|
||||
|
||||
Для воспроизведения тяжёлых fixtures нужен только дополнительный **Pillow 11.1.0** в своём Python-окружении; основной API и harness от него не зависят. Генератор пишет в игнорируемый `bin/`, а не в обязательные публичные fixtures:
|
||||
|
||||
```sh
|
||||
python -X utf8 scripts/lab4/generate-decoder-fixtures.py
|
||||
python -X utf8 scripts/lab4/decoder-memory.py --file bin/decoder-fixtures/progressive-rgb-444-40mp.jpg --mime image/jpeg --label progressive-rgb --output evidence/decoder-memory-progressive-rgb.json
|
||||
python -X utf8 scripts/lab4/decoder-memory.py --file bin/decoder-fixtures/progressive-cmyk-444-40mp.jpg --mime image/jpeg --label progressive-cmyk --output evidence/decoder-memory-progressive-cmyk.json
|
||||
python -X utf8 scripts/lab4/decoder-memory.py --file bin/decoder-fixtures/wide-rgba64-40mp.png --mime image/png --label wide-rgba64 --output evidence/decoder-memory-wide-rgba64.json
|
||||
```
|
||||
|
||||
Среда: Windows amd64, 16 логических CPU у клиента, Docker VM — 8 CPU и 12 547 280 896 B RAM. Лимиты Compose: API по 1 CPU/2 GiB, PostgreSQL 1 CPU/512 MiB, S3 1 CPU/512 MiB, HAProxy 0.5 CPU/128 MiB, Valkey 0.5 CPU/320 MiB. Данные синтетические; общий host не является выделенным нагрузочным стендом.
|
||||
|
||||
Трек 2: использованы отдельные реальные PostgreSQL/S3 testcontainers, grace 120 s и реальное ожидание 125 s перед очисткой старых объектов. Dry-run нашёл 6 несогласованностей и не изменил данные. Apply выполнил 5 исправлений; сообщение о намеренно повреждённом используемом объекте осталось без удаления. Повторный apply выполнил **0 исправлений**. Проверки сохранили свежие staging/orphan/pending, ссылки сообщений и используемые объекты.
|
||||
|
||||
| Используемых файлов/объектов | Shallow dry-run, ms | SQL statements | Операции порта S3 | Sampled Go HeapAlloc, B |
|
||||
| ---: | ---: | ---: | ---: | ---: |
|
||||
| 100 | 184 | 2 | 103 | 3 875 232 |
|
||||
| 1 000 | 2 160 | 3 | 1 004 | 4 159 720 |
|
||||
| 5 000 | 10 121 | 11 | 5 012 | 5 157 552 |
|
||||
|
||||
Это один прогон на размер на прогретом стенде, без статистических повторов. Измерена память Go-процесса сверки во время Run, **не RSS и не память PostgreSQL/S3**. В стоимости S3 считаются вызовы порта, а не все внутренние HTTP-запросы SDK. Для cost-датасета SQL-метаданные старые, реальные объекты свежие: дополнительная проверка orphan для них ещё не нужна. Отдельный regression на 1 003 старых объектах проверяет пакетное чтение БД. 10k/50k и production SLA не заявляются.
|
||||
|
||||
## Ограничения и следующие шаги
|
||||
|
||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|
||||
- PostgreSQL, Valkey, SeaweedFS, LB и Docker host остаются единичными точками отказа; отказ API-реплики не доказывает HA всего стенда.
|
||||
- SHA-256 используется в принятой модели криптографических коллизий. Дедупликация не позволяет клиенту пропустить отправку тела.
|
||||
- Удаление клиентских вложений и сборка «давно не прикреплённых» логических вложений не входят в контракт.
|
||||
- Повреждение используемого объекта обнаруживается deep-проверкой, но исходные байты нельзя восстановить без новой загрузки или backup.
|
||||
- Между страницами сверки нет snapshot; безопасность изменений обеспечивают повторные проверки под блокировкой.
|
||||
- Периодический обход ограничен двумя минутами и начинает каждый проход заново: для существенно больших наборов нужен ручной полный CLI-проход либо будущий resumable cursor. 5 000 объектов укладываются в измеренный бюджет; автоматическое продвижение на произвольном объёме не доказано.
|
||||
- После аварии bootstrap и одновременно неудачной компенсации может остаться небольшой служебный `.health/check-*`; это не вложение, и attachment-reconciler не обходит служебный namespace. Постоянный `.health/storage-v1` удалять нельзя.
|
||||
- HTTP предназначен для локального изолированного стенда с синтетическими данными; удалённая демонстрация требует TLS.
|
||||
|
||||
## Источники
|
||||
|
||||
Контракт текущего репозитория; [SeaweedFS](https://github.com/seaweedfs/seaweedfs), [minio-go](https://github.com/minio/minio-go), [S3 uploads](https://docs.aws.amazon.com/AmazonS3/latest/userguide/upload-objects.html), [PostgreSQL advisory locks](https://www.postgresql.org/docs/current/explicit-locking.html#ADVISORY-LOCKS).
|
||||
|
||||
ИИ-ассистент использовался как парный программист: генерация кода, тестов и черновиков документации по моим архитектурным решениям с последующим ревью каждого модуля и переработкой по замечаниям; выбор стека, структура проекта, модель данных, обработка ошибок, логирование и границы модулей определены мной; все проверки воспроизведены мной вручную.
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
// Command migrate applies the embedded schema migrations once and exits, so
|
||||
// the API replicas never race each other over the schema.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"messenger/internal/platform/config"
|
||||
"messenger/internal/platform/postgres"
|
||||
)
|
||||
|
||||
// poolSize covers the goose session lock and the migration itself.
|
||||
const poolSize = 2
|
||||
|
||||
func main() {
|
||||
cfg, err := config.LoadMigrate(os.LookupEnv)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "configuration error:", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
log := cfg.NewLogger()
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
|
||||
defer stop()
|
||||
|
||||
if err := run(ctx, log, cfg); err != nil {
|
||||
log.Error("migration failed", "error", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run(ctx context.Context, log *slog.Logger, cfg config.Migrate) error {
|
||||
pool, err := postgres.NewPool(ctx, cfg.DatabaseURL, poolSize)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
applied, err := postgres.Migrate(ctx, pool)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Info("migrations applied", "applied_migrations", applied)
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
// Command reconcile reports attachment inconsistencies as JSON lines. Mutations
|
||||
// require --apply; --deep also streams referenced objects to verify SHA256.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"messenger/internal/attachment"
|
||||
"messenger/internal/platform/config"
|
||||
"messenger/internal/platform/postgres"
|
||||
"messenger/internal/platform/s3store"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
code := run(ctx, os.Args[1:], os.Stdout, os.Stderr)
|
||||
stop()
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
func run(ctx context.Context, args []string, out, errOut io.Writer) int {
|
||||
flags := flag.NewFlagSet("reconcile", flag.ContinueOnError)
|
||||
flags.SetOutput(errOut)
|
||||
apply := flags.Bool("apply", false, "apply repairs (default: read-only dry run)")
|
||||
deep := flags.Bool("deep", false, "stream referenced objects and verify SHA256 and size")
|
||||
grace := flags.Duration("grace", time.Hour, "minimum file/object age; must be at least 2m")
|
||||
if err := flags.Parse(args); err != nil {
|
||||
if errors.Is(err, flag.ErrHelp) {
|
||||
return 0
|
||||
}
|
||||
return 2
|
||||
}
|
||||
if flags.NArg() != 0 || *grace < 2*time.Minute {
|
||||
fmt.Fprintln(errOut, "reconcile: no positional arguments allowed; --grace must be at least 2m")
|
||||
return 2
|
||||
}
|
||||
databaseURL, err := config.LoadDatabaseURL(os.LookupEnv)
|
||||
if err != nil {
|
||||
fmt.Fprintln(errOut, "configuration error:", err)
|
||||
return 2
|
||||
}
|
||||
// Compose mounts the reconciler identity at the standard credential paths.
|
||||
s3Config, err := config.LoadS3(os.LookupEnv)
|
||||
if err != nil {
|
||||
fmt.Fprintln(errOut, "configuration error:", err)
|
||||
return 2
|
||||
}
|
||||
blobs, err := s3store.New(s3Config)
|
||||
if err != nil {
|
||||
fmt.Fprintln(errOut, "configuration error:", err)
|
||||
return 2
|
||||
}
|
||||
db, err := postgres.NewPool(ctx, databaseURL, 10)
|
||||
if err != nil {
|
||||
fmt.Fprintln(errOut, "reconcile:", err)
|
||||
return 1
|
||||
}
|
||||
defer db.Close()
|
||||
r, err := attachment.NewReconciler(db, blobs, *grace)
|
||||
if err != nil {
|
||||
fmt.Fprintln(errOut, "reconcile:", err)
|
||||
return 2
|
||||
}
|
||||
// A broken output stream cancels the scan so repairs cannot continue after
|
||||
// the caller loses the audit trail. Print partial statistics on scan errors.
|
||||
scanCtx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
encoder := json.NewEncoder(out)
|
||||
var outputErr error
|
||||
options := attachment.ReconcileOptions{Apply: *apply, Deep: *deep}
|
||||
stats, runErr := r.Run(scanCtx, options, func(f attachment.Finding) {
|
||||
if outputErr == nil {
|
||||
outputErr = encoder.Encode(f)
|
||||
if outputErr != nil {
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
})
|
||||
if outputErr == nil {
|
||||
outputErr = encoder.Encode(stats)
|
||||
}
|
||||
if outputErr != nil {
|
||||
fmt.Fprintln(errOut, "reconcile output:", outputErr)
|
||||
return 1
|
||||
}
|
||||
if runErr != nil {
|
||||
fmt.Fprintln(errOut, "reconcile:", runErr)
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestReconcileDoesNotValidateUnusedLogging(t *testing.T) {
|
||||
for key, value := range map[string]string{
|
||||
"DATABASE_URL_FILE": "postgres://test:test@127.0.0.1:5432/messenger?sslmode=disable",
|
||||
"S3_ACCESS_KEY_ID_FILE": "test-access",
|
||||
"S3_SECRET_ACCESS_KEY_FILE": "test-secret",
|
||||
} {
|
||||
path := filepath.Join(t.TempDir(), key)
|
||||
if err := os.WriteFile(path, []byte(value), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv(key, path)
|
||||
}
|
||||
t.Setenv("S3_ENDPOINT", "http://127.0.0.1:8333")
|
||||
t.Setenv("S3_REGION", "us-east-1")
|
||||
t.Setenv("S3_BUCKET", "attachments")
|
||||
t.Setenv("APP_LOG_LEVEL", "not-a-log-level")
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel() // Stop before dialing: only configuration relevant to this CLI is checked.
|
||||
var out, errOut bytes.Buffer
|
||||
code := run(ctx, nil, &out, &errOut)
|
||||
if code != 1 || strings.Contains(errOut.String(), "APP_LOG_LEVEL") {
|
||||
t.Fatalf("unused logging prevented the database operation: code=%d error=%s", code, errOut.String())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"messenger/internal/platform/config"
|
||||
"messenger/internal/platform/s3store"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := run(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "bucket bootstrap failed:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
func run() error {
|
||||
cfg, err := config.LoadS3(os.LookupEnv)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s, err := s3store.New(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
return s.Bootstrap(ctx)
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
// Command server starts the messenger API over PostgreSQL, Valkey and S3.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"expvar"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
"os"
|
||||
"os/signal"
|
||||
"runtime"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"messenger/internal/app"
|
||||
"messenger/internal/attachment"
|
||||
"messenger/internal/chat"
|
||||
"messenger/internal/platform/config"
|
||||
"messenger/internal/platform/ident"
|
||||
"messenger/internal/platform/postgres"
|
||||
"messenger/internal/platform/s3store"
|
||||
"messenger/internal/platform/valkey"
|
||||
"messenger/internal/server"
|
||||
)
|
||||
|
||||
const keyCleanupEvery = 10 * time.Minute
|
||||
|
||||
func main() {
|
||||
cfg, err := config.Load(os.LookupEnv)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "configuration error:", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
alias := ident.NewAlias()
|
||||
log := cfg.NewLogger().With("instance", alias)
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
|
||||
defer stop()
|
||||
|
||||
if err := run(ctx, log, cfg, alias); err != nil {
|
||||
log.Error("server stopped with error", "error", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// run connects the dependencies before it listens, so the port accepts
|
||||
// connections only once they answer.
|
||||
func run(ctx context.Context, log *slog.Logger, cfg config.Config, alias string) error {
|
||||
pool, err := postgres.NewPool(ctx, cfg.DatabaseURL, cfg.DBMaxConns)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
sessions, err := valkey.NewClient(ctx, cfg.SessionStoreURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer sessions.Close()
|
||||
blobs, reconciler, err := newAttachmentStorage(pool, cfg.S3, os.LookupEnv)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ln, err := net.Listen("tcp", cfg.Addr())
|
||||
if err != nil {
|
||||
return fmt.Errorf("listen on %s: %w", cfg.Addr(), err)
|
||||
}
|
||||
defer ln.Close()
|
||||
log.Info("starting", "env", cfg.Env, "addr", cfg.Addr(),
|
||||
"gomaxprocs", runtime.GOMAXPROCS(0), "db_max_conns", cfg.DBMaxConns,
|
||||
"drain_delay", cfg.DrainDelay.String(), "shutdown_timeout", cfg.ShutdownTimeout.String(),
|
||||
"session_ttl", cfg.SessionTTL.String(), "idempotency_ttl", cfg.IdempotencyTTL.String())
|
||||
a := app.New(app.Deps{
|
||||
Log: log,
|
||||
DB: pool,
|
||||
Sessions: sessions,
|
||||
SessionTTL: cfg.SessionTTL,
|
||||
MaxBodyBytes: cfg.MaxBodyBytes,
|
||||
InstanceID: alias,
|
||||
IdempotencyTTL: cfg.IdempotencyTTL,
|
||||
Blobs: blobs,
|
||||
AttachmentsCfg: attachment.Config{
|
||||
MaxBytes: cfg.S3.MaxBytes,
|
||||
MaxPixels: cfg.S3.MaxPixels,
|
||||
},
|
||||
})
|
||||
var debugLn net.Listener
|
||||
if cfg.DebugAddr != "" {
|
||||
if debugLn, err = net.Listen("tcp", cfg.DebugAddr); err != nil {
|
||||
return fmt.Errorf("listen on %s: %w", cfg.DebugAddr, err)
|
||||
}
|
||||
publishDebugVars(alias, pool)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
var background sync.WaitGroup
|
||||
background.Go(func() { chat.RunKeyCleanup(ctx, log, a.Chats, keyCleanupEvery) })
|
||||
if reconciler != nil {
|
||||
background.Go(func() {
|
||||
ticker := time.NewTicker(10 * time.Minute)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
scan, cancel := context.WithTimeout(ctx, 2*time.Minute)
|
||||
stats, err := reconciler.Run(scan, attachment.ReconcileOptions{Apply: true}, nil)
|
||||
cancel()
|
||||
if err != nil {
|
||||
log.Warn("attachment cleanup incomplete", "error", err)
|
||||
} else {
|
||||
log.Info("attachment cleanup", "scanned_files", stats.ScannedFiles, "repaired", stats.Repaired)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
if debugLn != nil {
|
||||
background.Go(func() {
|
||||
if err := server.RunDebug(ctx, log, debugLn, expvar.Handler()); err != nil {
|
||||
log.Error("debug listener stopped", "error", err.Error())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
err = server.Run(ctx, log, ln, a.Engine, a.Health, cfg.DrainDelay, cfg.ShutdownTimeout)
|
||||
cancel()
|
||||
background.Wait()
|
||||
return err
|
||||
}
|
||||
|
||||
func newAttachmentStorage(pool *pgxpool.Pool, cfg config.S3, lookup config.Lookup) (attachment.Blobs, *attachment.Reconciler, error) {
|
||||
if cfg.Endpoint == "" {
|
||||
return nil, nil, nil
|
||||
}
|
||||
blobs, err := s3store.New(cfg)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
reconCfg, err := config.LoadS3(config.ReconcileS3(lookup))
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
reconBlobs, err := s3store.New(reconCfg)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
reconciler, err := attachment.NewReconciler(pool, reconBlobs, time.Hour)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return blobs, reconciler, nil
|
||||
}
|
||||
|
||||
// publishDebugVars adds to expvar what the load tests read from a replica.
|
||||
func publishDebugVars(alias string, pool *pgxpool.Pool) {
|
||||
expvar.NewString("instance").Set(alias)
|
||||
expvar.Publish("gomaxprocs", expvar.Func(func() any { return runtime.GOMAXPROCS(0) }))
|
||||
expvar.Publish("pgxpool", expvar.Func(func() any {
|
||||
s := pool.Stat()
|
||||
return map[string]any{
|
||||
"acquire_count": s.AcquireCount(),
|
||||
"acquire_duration_ms": s.AcquireDuration().Milliseconds(),
|
||||
"acquired_conns": s.AcquiredConns(),
|
||||
"idle_conns": s.IdleConns(),
|
||||
"total_conns": s.TotalConns(),
|
||||
"max_conns": s.MaxConns(),
|
||||
"empty_acquire_count": s.EmptyAcquireCount(),
|
||||
"empty_acquire_wait_ms": s.EmptyAcquireWaitTime().Milliseconds(),
|
||||
"canceled_acquire_count": s.CanceledAcquireCount(),
|
||||
"new_conns_count": s.NewConnsCount(),
|
||||
}
|
||||
}))
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/internal/platform/config"
|
||||
"messenger/internal/testenv"
|
||||
)
|
||||
|
||||
var (
|
||||
db *testenv.DB
|
||||
kv *testenv.KV
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
ctx := context.Background()
|
||||
var err error
|
||||
if db, err = testenv.StartPostgres(ctx); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if kv, err = testenv.StartValkey(ctx); err != nil {
|
||||
db.Close()
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
code := m.Run()
|
||||
kv.Close()
|
||||
db.Close()
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
// A start-up failure after the background work began must still end the
|
||||
// process with an error, not leave it waiting for a signal.
|
||||
func TestRunFailsFastWhenTheDebugListenerCannotStart(t *testing.T) {
|
||||
if err := runWithBusyDebugAddr(t, 0); err == nil {
|
||||
t.Fatal("run succeeded on a busy debug address")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFailedStartReleasesTheAPIPort(t *testing.T) {
|
||||
probe, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
port := probe.Addr().(*net.TCPAddr).Port
|
||||
probe.Close()
|
||||
|
||||
if err := runWithBusyDebugAddr(t, port); err == nil {
|
||||
t.Fatal("run succeeded on a busy debug address")
|
||||
}
|
||||
ln, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
|
||||
if err != nil {
|
||||
t.Fatalf("the API port is still taken after the failed start: %v", err)
|
||||
}
|
||||
ln.Close()
|
||||
}
|
||||
|
||||
func runWithBusyDebugAddr(t *testing.T, port int) error {
|
||||
t.Helper()
|
||||
busy, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer busy.Close()
|
||||
cfg := config.Config{
|
||||
Host: "127.0.0.1",
|
||||
Port: port,
|
||||
ShutdownTimeout: time.Second,
|
||||
MaxBodyBytes: 1024,
|
||||
DatabaseURL: db.DSN,
|
||||
SessionStoreURL: kv.URL,
|
||||
SessionTTL: time.Hour,
|
||||
IdempotencyTTL: time.Hour,
|
||||
DBMaxConns: 2,
|
||||
DebugAddr: busy.Addr().String(),
|
||||
}
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
done <- run(context.Background(), slog.New(slog.NewTextHandler(io.Discard, nil)), cfg, "alias")
|
||||
}()
|
||||
select {
|
||||
case err := <-done:
|
||||
return err
|
||||
case <-time.After(10 * time.Second):
|
||||
t.Fatal("run hung instead of returning the listen error")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
# Load-test override of compose.yaml, used only by scripts/bench: every value
|
||||
# an experiment varies, and the load generator. Defaults equal compose.yaml.
|
||||
services:
|
||||
lb:
|
||||
environment:
|
||||
LB_QUIET: ${LB_QUIET:-0}
|
||||
API_MAXCONN: ${API_MAXCONN:-20}
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "${LB_CPUS:-0.5}"
|
||||
|
||||
api:
|
||||
environment:
|
||||
DB_MAX_CONNS: ${DB_MAX_CONNS:-10}
|
||||
GOMEMLIMIT: ${API_GOMEMLIMIT:-230MiB}
|
||||
APP_DEBUG_ADDR: ":9090"
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "${API_CPUS:-1.0}"
|
||||
memory: ${API_MEMORY:-256M}
|
||||
|
||||
postgres:
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "${PG_CPUS:-1.0}"
|
||||
memory: ${PG_MEMORY:-512M}
|
||||
|
||||
loadgen:
|
||||
profiles: ["bench"]
|
||||
build:
|
||||
context: .
|
||||
target: loadgen
|
||||
image: mtusi-messenger-loadgen:lab4
|
||||
environment:
|
||||
GOMEMLIMIT: 900MiB
|
||||
volumes:
|
||||
- ${BENCH_OUT:-./bench-out}:/out
|
||||
networks:
|
||||
- edge
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "${LOADGEN_CPUS:-2.0}"
|
||||
memory: 1G
|
||||
+286
@@ -0,0 +1,286 @@
|
||||
# Local stand: HAProxy in front of interchangeable API replicas over shared
|
||||
# PostgreSQL, Valkey and S3. Only the balancer is published, on loopback; the data
|
||||
# network has no way out. The load-test knobs are in compose.bench.yaml.
|
||||
name: lab4
|
||||
|
||||
services:
|
||||
lb:
|
||||
image: haproxy:3.4.7-alpine3.24@sha256:2caaf8b1a1ef67f3f1ea9b721f2821675d3ba4500fb518e23884fecb93e8dbaa
|
||||
depends_on:
|
||||
api:
|
||||
condition: service_healthy
|
||||
volumes:
|
||||
- ./infra/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
|
||||
ports:
|
||||
- "${PUBLISH:-127.0.0.1:8080}:8080"
|
||||
- "${STATS_PUBLISH:-127.0.0.1:8404}:8404"
|
||||
networks:
|
||||
- edge
|
||||
healthcheck:
|
||||
# Through the balancer: `up --wait` returns once a replica is reachable.
|
||||
test: ["CMD", "wget", "-qO-", "http://127.0.0.1:8080/health/ready"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 3
|
||||
start_period: 20s
|
||||
start_interval: 1s
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.5"
|
||||
memory: 128M
|
||||
|
||||
api:
|
||||
build: .
|
||||
image: mtusi-messenger:lab4
|
||||
env_file:
|
||||
- path: .env
|
||||
required: false
|
||||
environment:
|
||||
DATABASE_URL_FILE: /run/secrets/database_url
|
||||
SESSION_STORE_URL_FILE: /run/secrets/session_store_url
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
|
||||
S3_ENDPOINT: ${S3_ENDPOINT:-http://s3:8333}
|
||||
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
|
||||
# The background reconciler uses a separate identity with List rights.
|
||||
S3_RECONCILE_ACCESS_KEY_ID_FILE: /run/secrets/s3_reconcile_access_key_id
|
||||
S3_RECONCILE_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_reconcile_secret_access_key
|
||||
# One decoder per replica. Progressive JPEG retains coefficient buffers;
|
||||
# a wide RGBA64 PNG also holds two scanlines beside the full pixel buffer.
|
||||
GOMEMLIMIT: 1536MiB
|
||||
secrets:
|
||||
- database_url
|
||||
- session_store_url
|
||||
- s3_access_key_id
|
||||
- s3_secret_access_key
|
||||
- s3_reconcile_access_key_id
|
||||
- s3_reconcile_secret_access_key
|
||||
networks:
|
||||
- edge
|
||||
- data
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
valkey:
|
||||
condition: service_healthy
|
||||
s3-init:
|
||||
condition: service_completed_successfully
|
||||
# Binary deadline 30s plus compensation, shutdown and drain margin.
|
||||
stop_grace_period: 45s
|
||||
deploy:
|
||||
replicas: ${API_REPLICAS:-2}
|
||||
resources:
|
||||
limits:
|
||||
cpus: "1.0"
|
||||
memory: 2G
|
||||
|
||||
migrate:
|
||||
image: mtusi-messenger:lab4
|
||||
entrypoint: ["/usr/local/bin/migrate"]
|
||||
environment:
|
||||
DATABASE_URL_FILE: /run/secrets/database_url
|
||||
secrets:
|
||||
- database_url
|
||||
networks:
|
||||
- data
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
restart: "no"
|
||||
healthcheck:
|
||||
# The image's check probes the API port, which migrate never opens.
|
||||
disable: true
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.5"
|
||||
memory: 128M
|
||||
|
||||
postgres:
|
||||
image: postgres:18.6-alpine3.24@sha256:77f585114c32fbca283dc835b0596f4e52b51b4c6662d7810b2f4084f60a1873
|
||||
# 10 balancer slots x DB_MAX_CONNS 10, plus migrate and psql.
|
||||
command: ["postgres", "-c", "max_connections=150"]
|
||||
environment:
|
||||
POSTGRES_DB: messenger
|
||||
POSTGRES_USER: messenger
|
||||
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
|
||||
secrets:
|
||||
- postgres_password
|
||||
# PostgreSQL 18 keeps PGDATA in a versioned directory below this one.
|
||||
volumes:
|
||||
- pgdata:/var/lib/postgresql
|
||||
networks:
|
||||
- data
|
||||
healthcheck:
|
||||
# Over TCP: the first initialisation runs a temporary socket-only server.
|
||||
test: ["CMD", "pg_isready", "-h", "127.0.0.1", "-U", "messenger", "-d", "messenger"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 10
|
||||
start_period: 10s
|
||||
stop_grace_period: 30s
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "1.0"
|
||||
memory: 512M
|
||||
|
||||
valkey:
|
||||
image: valkey/valkey:9.1.2-alpine3.24@sha256:48332870af354a799964c0012ae1194a0bf2bf894eb508f945810596dc2d8d11
|
||||
command: ["valkey-server", "/usr/local/etc/valkey/valkey.conf"]
|
||||
secrets:
|
||||
- valkey_auth
|
||||
volumes:
|
||||
- ./infra/valkey/valkey.conf:/usr/local/etc/valkey/valkey.conf:ro
|
||||
- valkeydata:/data
|
||||
networks:
|
||||
- data
|
||||
healthcheck:
|
||||
# valkey-cli exits 0 even on NOAUTH, so the reply itself is checked.
|
||||
test: ["CMD-SHELL", "VALKEYCLI_AUTH=\"$$(sed -n 's/^requirepass //p' /run/secrets/valkey_auth)\" valkey-cli ping | grep -q PONG"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 10
|
||||
start_period: 5s
|
||||
stop_grace_period: 10s
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.5"
|
||||
# maxmemory 256mb (valkey.conf) plus the server's own overhead.
|
||||
memory: 320M
|
||||
|
||||
s3:
|
||||
image: chrislusf/seaweedfs:4.53@sha256:ccad23e18639c8b98cee17e7100b95830a06077b56d8e5b006d6dcf8ec5e94a0
|
||||
command: ["server", "-options=/etc/seaweedfs/lab.options"]
|
||||
environment:
|
||||
GOMEMLIMIT: 460MiB
|
||||
secrets:
|
||||
- s3_identities
|
||||
volumes:
|
||||
- ./infra/s3/seaweedfs.options:/etc/seaweedfs/lab.options:ro
|
||||
# Both volume data and the image's /data/filerldb2 metadata live here.
|
||||
- s3data:/data
|
||||
networks:
|
||||
- data
|
||||
healthcheck:
|
||||
# A reachable anonymous request must be refused, including before init.
|
||||
test: ["CMD-SHELL", "wget -S -O /dev/null http://127.0.0.1:8333/ 2>&1 | grep -q ' 403 '"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 12
|
||||
start_period: 15s
|
||||
start_interval: 1s
|
||||
stop_grace_period: 30s
|
||||
pids_limit: 256
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "1.0"
|
||||
memory: 512M
|
||||
pids: 256
|
||||
|
||||
s3-init:
|
||||
build: .
|
||||
image: mtusi-messenger:lab4
|
||||
entrypoint: ["/usr/local/bin/s3init"]
|
||||
environment:
|
||||
S3_ENDPOINT: ${S3_ENDPOINT:-http://s3:8333}
|
||||
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
|
||||
GOMEMLIMIT: 100MiB
|
||||
secrets:
|
||||
- source: s3_admin_access_key_id
|
||||
target: s3_access_key_id
|
||||
- source: s3_admin_secret_access_key
|
||||
target: s3_secret_access_key
|
||||
networks:
|
||||
- data
|
||||
depends_on:
|
||||
s3:
|
||||
condition: service_healthy
|
||||
restart: "no"
|
||||
healthcheck:
|
||||
disable: true
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.5"
|
||||
memory: 128M
|
||||
|
||||
reconcile:
|
||||
profiles: ["recon"]
|
||||
build: .
|
||||
image: mtusi-messenger:lab4
|
||||
entrypoint: ["/usr/local/bin/reconcile"]
|
||||
environment:
|
||||
DATABASE_URL_FILE: /run/secrets/database_url
|
||||
S3_ENDPOINT: ${S3_ENDPOINT:-http://s3:8333}
|
||||
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
|
||||
GOMEMLIMIT: 100MiB
|
||||
secrets:
|
||||
- database_url
|
||||
- source: s3_reconcile_access_key_id
|
||||
target: s3_access_key_id
|
||||
- source: s3_reconcile_secret_access_key
|
||||
target: s3_secret_access_key
|
||||
networks:
|
||||
- data
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
s3-init:
|
||||
condition: service_completed_successfully
|
||||
restart: "no"
|
||||
healthcheck:
|
||||
disable: true
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.5"
|
||||
memory: 128M
|
||||
|
||||
volumes:
|
||||
pgdata:
|
||||
valkeydata:
|
||||
s3data:
|
||||
|
||||
networks:
|
||||
edge: {}
|
||||
data:
|
||||
internal: true
|
||||
|
||||
secrets:
|
||||
database_url:
|
||||
file: ./secrets/database_url
|
||||
session_store_url:
|
||||
file: ./secrets/session_store_url
|
||||
postgres_password:
|
||||
file: ./secrets/postgres_password
|
||||
valkey_auth:
|
||||
file: ./secrets/valkey_auth.conf
|
||||
s3_identities:
|
||||
file: ./secrets/s3_identities.json
|
||||
s3_admin_access_key_id:
|
||||
file: ./secrets/s3_admin_access_key_id
|
||||
s3_admin_secret_access_key:
|
||||
file: ./secrets/s3_admin_secret_access_key
|
||||
s3_access_key_id:
|
||||
file: ./secrets/s3_access_key_id
|
||||
s3_secret_access_key:
|
||||
file: ./secrets/s3_secret_access_key
|
||||
s3_reconcile_access_key_id:
|
||||
file: ./secrets/s3_reconcile_access_key_id
|
||||
s3_reconcile_secret_access_key:
|
||||
file: ./secrets/s3_reconcile_secret_access_key
|
||||
@@ -0,0 +1,764 @@
|
||||
{
|
||||
"schema_version": 1,
|
||||
"name": "progressive-cmyk",
|
||||
"mime": "image/jpeg",
|
||||
"fixture": {
|
||||
"width": 8000,
|
||||
"height": 5000,
|
||||
"bit_depth": 8,
|
||||
"components": 4,
|
||||
"progressive": true,
|
||||
"sampling": [
|
||||
17,
|
||||
17,
|
||||
17,
|
||||
17
|
||||
]
|
||||
},
|
||||
"payload_bytes": 939079,
|
||||
"sha256": "0b38b5e95ca2623f585bae77cdfba9259a6b786971e6b9b8d10d774177023956",
|
||||
"requests": 1,
|
||||
"ok": true,
|
||||
"started_at": "2026-11-16T09:16:29.819614+00:00",
|
||||
"measurement": {
|
||||
"source": "docker stats JSON stream, formatted working-set values",
|
||||
"limitation": "sampled/rounded peaks can miss instantaneous maxima",
|
||||
"baseline_seconds": 3,
|
||||
"tail_seconds": 3
|
||||
},
|
||||
"api_limits": {
|
||||
"e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918": 2147483648,
|
||||
"4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd": 2147483648
|
||||
},
|
||||
"state_before": {
|
||||
"e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918": {
|
||||
"id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"oom_killed": false,
|
||||
"running": true,
|
||||
"restart_count": 0,
|
||||
"exit_code": 0
|
||||
},
|
||||
"4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd": {
|
||||
"id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"oom_killed": false,
|
||||
"running": true,
|
||||
"restart_count": 0,
|
||||
"exit_code": 0
|
||||
}
|
||||
},
|
||||
"duration_s": 2.580977499950677,
|
||||
"memory_samples": [
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 73326920,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 0.549924,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653577421,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 0.549959,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 73326920,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 0.549984,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653577421,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 0.549996,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 73326920,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 1.049104,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653577421,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 1.049131,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 73326920,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 1.049142,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653577421,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 1.049151,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 73190605,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 1.548806,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653472563,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 1.548828,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 73190605,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 1.548839,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653472563,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 1.548847,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 73190605,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 2.049053,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653472563,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 2.049071,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 73190605,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 2.04908,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653472563,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 2.049087,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 73326920,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 2.558386,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653577421,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 2.558406,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 73326920,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 2.558415,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653577421,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 2.558422,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 73326920,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 3.048888,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653577421,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 3.04891,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 73326920,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 3.04892,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653577421,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 3.048928,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 73452749,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 3.548966,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653577421,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 3.54899,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 73452749,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 3.548999,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653577421,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 3.549006,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 73452749,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 4.048986,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653577421,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 4.049008,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 73452749,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 4.049019,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653577421,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 4.049027,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 620652134,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 4.548787,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653577421,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 4.548811,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 620652134,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 4.548823,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653577421,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 4.548832,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 620652134,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 5.049002,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653577421,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 5.049024,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 620652134,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 5.049034,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653577421,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 5.049042,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 792199168,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 5.549043,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653472563,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 5.549064,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 792199168,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 5.549074,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653472563,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 5.549086,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 792199168,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 6.048631,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653472563,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 6.048659,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 792199168,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 6.048671,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653472563,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 6.048682,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 991428608,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 6.549034,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653472563,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 6.549058,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 991428608,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 6.549069,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653472563,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 6.549079,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 991428608,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 7.048895,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653472563,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 7.048919,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 991428608,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 7.048931,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653472563,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 7.048941,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 991323750,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 7.548935,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653472563,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 7.548953,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 991323750,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 7.548963,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653472563,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 7.54897,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 991323750,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 8.048712,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653472563,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 8.048734,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 991323750,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 8.048748,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653472563,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 8.048756,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 991323750,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 8.549399,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653577421,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 8.549422,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 991323750,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 8.549433,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653577421,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 8.549442,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 991323750,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 9.049432,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653577421,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 9.049453,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 991323750,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 9.049463,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 653577421,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 9.049471,
|
||||
"case": "progressive-cmyk",
|
||||
"phase": "load"
|
||||
}
|
||||
],
|
||||
"sampling_error": null,
|
||||
"state_after": {
|
||||
"e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918": {
|
||||
"id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"oom_killed": false,
|
||||
"running": true,
|
||||
"restart_count": 0,
|
||||
"exit_code": 0
|
||||
},
|
||||
"4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd": {
|
||||
"id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"oom_killed": false,
|
||||
"running": true,
|
||||
"restart_count": 0,
|
||||
"exit_code": 0
|
||||
}
|
||||
},
|
||||
"restart_or_oom_detected": false,
|
||||
"memory": {
|
||||
"e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918": {
|
||||
"baseline_bytes": 73452749,
|
||||
"sampled_peak_bytes": 991428608,
|
||||
"delta_bytes": 917975859,
|
||||
"baseline_samples": 14,
|
||||
"load_samples": 22
|
||||
},
|
||||
"4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd": {
|
||||
"baseline_bytes": 653577421,
|
||||
"sampled_peak_bytes": 653577421,
|
||||
"delta_bytes": 0,
|
||||
"baseline_samples": 14,
|
||||
"load_samples": 22
|
||||
}
|
||||
},
|
||||
"observed_max_inflight": 1,
|
||||
"accepted": 1,
|
||||
"status_counts": {
|
||||
"201": 1
|
||||
},
|
||||
"latency": {
|
||||
"p50_ms": 2580.686,
|
||||
"p95_ms": 2580.686,
|
||||
"max_ms": 2580.686
|
||||
},
|
||||
"attempted_bytes": 939079,
|
||||
"successful_bytes": 939079,
|
||||
"results": [
|
||||
{
|
||||
"index": 0,
|
||||
"status": 201,
|
||||
"ok": true,
|
||||
"request_bytes": 939079,
|
||||
"instance_id": "76f24bb2ebbc2a92",
|
||||
"attachment_id": "01a1e3a4-da4a-7ddc-8109-d9ac8efc99b7",
|
||||
"latency_ms": 2580.686,
|
||||
"start_offset_ms": 0.032
|
||||
}
|
||||
],
|
||||
"download": {
|
||||
"ok": true,
|
||||
"status": 200,
|
||||
"bytes": 939079,
|
||||
"sha256": "0b38b5e95ca2623f585bae77cdfba9259a6b786971e6b9b8d10d774177023956"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,655 @@
|
||||
{
|
||||
"schema_version": 1,
|
||||
"name": "progressive-rgb",
|
||||
"mime": "image/jpeg",
|
||||
"fixture": {
|
||||
"width": 8000,
|
||||
"height": 5000,
|
||||
"bit_depth": 8,
|
||||
"components": 3,
|
||||
"progressive": true,
|
||||
"sampling": [
|
||||
17,
|
||||
17,
|
||||
17
|
||||
]
|
||||
},
|
||||
"payload_bytes": 469711,
|
||||
"sha256": "7c01cae2900b154361df76733c19db0737e19fe1c3bd0b9fae82595b7fbf4fad",
|
||||
"requests": 1,
|
||||
"ok": true,
|
||||
"started_at": "2026-11-16T09:16:19.759532+00:00",
|
||||
"measurement": {
|
||||
"source": "docker stats JSON stream, formatted working-set values",
|
||||
"limitation": "sampled/rounded peaks can miss instantaneous maxima",
|
||||
"baseline_seconds": 3,
|
||||
"tail_seconds": 3
|
||||
},
|
||||
"api_limits": {
|
||||
"e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918": 2147483648,
|
||||
"4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd": 2147483648
|
||||
},
|
||||
"state_before": {
|
||||
"e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918": {
|
||||
"id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"oom_killed": false,
|
||||
"running": true,
|
||||
"restart_count": 0,
|
||||
"exit_code": 0
|
||||
},
|
||||
"4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd": {
|
||||
"id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"oom_killed": false,
|
||||
"running": true,
|
||||
"restart_count": 0,
|
||||
"exit_code": 0
|
||||
}
|
||||
},
|
||||
"duration_s": 1.4607699000043795,
|
||||
"memory_samples": [
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 57053020,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 1.547282,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 70894223,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 1.547322,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 57053020,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 1.547345,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 70894223,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 1.547356,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 57053020,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 2.047578,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 70894223,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 2.047641,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 57053020,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 2.047659,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 70894223,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 2.04767,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 57053020,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 2.547018,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 71030538,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 2.547037,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 57053020,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 2.547046,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 71030538,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 2.547053,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 57053020,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 3.046578,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 71030538,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 3.046597,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 57053020,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 3.046605,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 71030538,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 3.046612,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 57053020,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 3.546984,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 71156367,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 3.547006,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 57053020,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 3.547017,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 71156367,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 3.547025,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 57053020,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 4.046531,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 71156367,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 4.046552,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 57053020,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 4.046562,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 71156367,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 4.04657,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 56927191,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 4.546655,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 71187825,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 4.546682,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 56927191,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 4.546693,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 71187825,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 4.546701,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 56927191,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 5.04708,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 71187825,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 5.047106,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 56927191,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 5.047121,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 71187825,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 5.047132,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 56927191,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 5.546886,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 578499379,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 5.546906,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 56927191,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 5.546918,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 578499379,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 5.546926,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 56927191,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 6.047261,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 578499379,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 6.047287,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 56927191,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 6.047298,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 578499379,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 6.047307,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 57053020,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 6.547209,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 613626675,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 6.547231,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 57053020,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 6.547247,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 613626675,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 6.547255,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 57053020,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 7.047074,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 613626675,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 7.047093,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 57053020,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 7.047102,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 613626675,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 7.047109,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 57189335,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 7.546834,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 613626675,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 7.546855,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 57189335,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 7.546864,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 613626675,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 7.546872,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 57189335,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 8.047107,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 613626675,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 8.047126,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 57189335,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 8.047135,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 613626675,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 8.047142,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 57053020,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 8.547593,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 613521818,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 8.547632,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 57053020,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 8.547646,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 613521818,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 8.547655,
|
||||
"case": "progressive-rgb",
|
||||
"phase": "load"
|
||||
}
|
||||
],
|
||||
"sampling_error": null,
|
||||
"state_after": {
|
||||
"e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918": {
|
||||
"id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"oom_killed": false,
|
||||
"running": true,
|
||||
"restart_count": 0,
|
||||
"exit_code": 0
|
||||
},
|
||||
"4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd": {
|
||||
"id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"oom_killed": false,
|
||||
"running": true,
|
||||
"restart_count": 0,
|
||||
"exit_code": 0
|
||||
}
|
||||
},
|
||||
"restart_or_oom_detected": false,
|
||||
"memory": {
|
||||
"e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918": {
|
||||
"baseline_bytes": 56927191,
|
||||
"sampled_peak_bytes": 57189335,
|
||||
"delta_bytes": 262144,
|
||||
"baseline_samples": 14,
|
||||
"load_samples": 16
|
||||
},
|
||||
"4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd": {
|
||||
"baseline_bytes": 71187825,
|
||||
"sampled_peak_bytes": 613626675,
|
||||
"delta_bytes": 542438850,
|
||||
"baseline_samples": 14,
|
||||
"load_samples": 16
|
||||
}
|
||||
},
|
||||
"observed_max_inflight": 1,
|
||||
"accepted": 1,
|
||||
"status_counts": {
|
||||
"201": 1
|
||||
},
|
||||
"latency": {
|
||||
"p50_ms": 1460.381,
|
||||
"p95_ms": 1460.381,
|
||||
"max_ms": 1460.381
|
||||
},
|
||||
"attempted_bytes": 469711,
|
||||
"successful_bytes": 469711,
|
||||
"results": [
|
||||
{
|
||||
"index": 0,
|
||||
"status": 201,
|
||||
"ok": true,
|
||||
"request_bytes": 469711,
|
||||
"instance_id": "acce7ec5a5aab35b",
|
||||
"attachment_id": "01a1e3a4-b65d-7795-aaf3-911e43a58f12",
|
||||
"latency_ms": 1460.381,
|
||||
"start_offset_ms": 0.021
|
||||
}
|
||||
],
|
||||
"download": {
|
||||
"ok": true,
|
||||
"status": 200,
|
||||
"bytes": 469711,
|
||||
"sha256": "7c01cae2900b154361df76733c19db0737e19fe1c3bd0b9fae82595b7fbf4fad"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,613 @@
|
||||
{
|
||||
"schema_version": 1,
|
||||
"name": "wide-rgba64",
|
||||
"mime": "image/png",
|
||||
"fixture": {
|
||||
"width": 40000000,
|
||||
"height": 1,
|
||||
"bit_depth": 16,
|
||||
"color_type": 6
|
||||
},
|
||||
"payload_bytes": 1397354,
|
||||
"sha256": "25d5a4133d85d830fc5d7e5946a694cd71b673bc83acc362ec097f2c601b96e2",
|
||||
"requests": 1,
|
||||
"ok": true,
|
||||
"started_at": "2026-11-16T09:16:40.211157+00:00",
|
||||
"measurement": {
|
||||
"source": "docker stats JSON stream, formatted working-set values",
|
||||
"limitation": "sampled/rounded peaks can miss instantaneous maxima",
|
||||
"baseline_seconds": 3,
|
||||
"tail_seconds": 3
|
||||
},
|
||||
"api_limits": {
|
||||
"e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918": 2147483648,
|
||||
"4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd": 2147483648
|
||||
},
|
||||
"state_before": {
|
||||
"e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918": {
|
||||
"id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"oom_killed": false,
|
||||
"running": true,
|
||||
"restart_count": 0,
|
||||
"exit_code": 0
|
||||
},
|
||||
"4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd": {
|
||||
"id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"oom_killed": false,
|
||||
"running": true,
|
||||
"restart_count": 0,
|
||||
"exit_code": 0
|
||||
}
|
||||
},
|
||||
"duration_s": 0.630827400018461,
|
||||
"memory_samples": [
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 1034420224,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 1.553241,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 713451110,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 1.553274,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 1034420224,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 1.553287,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 713451110,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 1.553295,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 1034420224,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 2.055202,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 713451110,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 2.055222,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 1034420224,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 2.05524,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 713451110,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 2.055258,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 1034525082,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 2.553574,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 713451110,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 2.553593,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 1034525082,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 2.553602,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 713451110,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 2.55365,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 1034525082,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 3.05266,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 713451110,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 3.052681,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 1034525082,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 3.052691,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 713451110,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 3.052699,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 1038090240,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 3.553008,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 713451110,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 3.553026,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 1038090240,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 3.553034,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 713451110,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 3.553041,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 1038090240,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 4.052797,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 713451110,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 4.052817,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 1038090240,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 4.052825,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 713451110,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 4.052832,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 1034525082,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 4.55255,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 713555968,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 4.552569,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 1034525082,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 4.552581,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 713555968,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 4.552588,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "baseline"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 1034525082,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 5.052981,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 713555968,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 5.053002,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 1034525082,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 5.053012,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 713555968,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 5.05302,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 1264867869,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 5.552655,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 713451110,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 5.552674,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 1264867869,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 5.552682,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 713451110,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 5.552688,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 1264867869,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 6.052855,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 713451110,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 6.052874,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 1264867869,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 6.052882,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 713451110,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 6.052889,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 1352914698,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 6.552602,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 713451110,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 6.552622,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 1352914698,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 6.552632,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 713451110,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 6.552644,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 1352914698,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 7.052492,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 713451110,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 7.052511,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 1352914698,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 7.05252,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 713451110,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 7.052528,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 1352914698,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 7.553232,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 713451110,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 7.553251,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 1352914698,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 7.55326,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 713451110,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 7.553268,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 1352914698,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 8.053203,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 713451110,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 8.053222,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"name": "lab4-api-1",
|
||||
"used_bytes": 1352914698,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 8.05323,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "load"
|
||||
},
|
||||
{
|
||||
"container_id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"name": "lab4-api-2",
|
||||
"used_bytes": 713451110,
|
||||
"limit_bytes": 2147483648,
|
||||
"elapsed_s": 8.053237,
|
||||
"case": "wide-rgba64",
|
||||
"phase": "load"
|
||||
}
|
||||
],
|
||||
"sampling_error": null,
|
||||
"state_after": {
|
||||
"e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918": {
|
||||
"id": "e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918",
|
||||
"oom_killed": false,
|
||||
"running": true,
|
||||
"restart_count": 0,
|
||||
"exit_code": 0
|
||||
},
|
||||
"4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd": {
|
||||
"id": "4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd",
|
||||
"oom_killed": false,
|
||||
"running": true,
|
||||
"restart_count": 0,
|
||||
"exit_code": 0
|
||||
}
|
||||
},
|
||||
"restart_or_oom_detected": false,
|
||||
"memory": {
|
||||
"e0755982c2d34c1849f3c6aed91fc3c6fb659d9114f6cab52ad1908fafffa918": {
|
||||
"baseline_bytes": 1034525082,
|
||||
"sampled_peak_bytes": 1352914698,
|
||||
"delta_bytes": 318389616,
|
||||
"baseline_samples": 14,
|
||||
"load_samples": 14
|
||||
},
|
||||
"4705eb662d229d90626175762a8d73a46410d642a14f75aa80285442335032bd": {
|
||||
"baseline_bytes": 713555968,
|
||||
"sampled_peak_bytes": 713555968,
|
||||
"delta_bytes": 0,
|
||||
"baseline_samples": 14,
|
||||
"load_samples": 14
|
||||
}
|
||||
},
|
||||
"observed_max_inflight": 1,
|
||||
"accepted": 1,
|
||||
"status_counts": {
|
||||
"201": 1
|
||||
},
|
||||
"latency": {
|
||||
"p50_ms": 630.558,
|
||||
"p95_ms": 630.558,
|
||||
"max_ms": 630.558
|
||||
},
|
||||
"attempted_bytes": 1397354,
|
||||
"successful_bytes": 1397354,
|
||||
"results": [
|
||||
{
|
||||
"index": 0,
|
||||
"status": 201,
|
||||
"ok": true,
|
||||
"request_bytes": 1397354,
|
||||
"instance_id": "76f24bb2ebbc2a92",
|
||||
"attachment_id": "01a1e3a5-0702-7b0a-8650-57d4e66f7c31",
|
||||
"latency_ms": 630.558,
|
||||
"start_offset_ms": 0.02
|
||||
}
|
||||
],
|
||||
"download": {
|
||||
"ok": true,
|
||||
"status": 200,
|
||||
"bytes": 1397354,
|
||||
"sha256": "25d5a4133d85d830fc5d7e5946a694cd71b673bc83acc362ec097f2c601b96e2"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"collected_at": "2026-11-16T09:38:59.955076+00:00",
|
||||
"source_code_commit": "29f45ca0eb0ca060f77a8ada1e20aeca4d7e0785",
|
||||
"go": "go version go1.27.1 windows/amd64",
|
||||
"docker_version": "28.5.1",
|
||||
"docker_cpus": 8,
|
||||
"docker_memory_bytes": 12547280896,
|
||||
"driver_system": "Windows",
|
||||
"driver_arch": "AMD64",
|
||||
"driver_logical_cpus": 16,
|
||||
"python": "3.13.1",
|
||||
"api_image": "sha256:5f84776e9db8de84b736cf9a58332830042618c6ea4a2097640a9242cc5a660e",
|
||||
"running_services": [
|
||||
"lab4-api-1 running healthy",
|
||||
"lab4-api-2 running healthy",
|
||||
"lab4-lb-1 running healthy",
|
||||
"lab4-postgres-1 running healthy",
|
||||
"lab4-s3-1 running healthy",
|
||||
"lab4-valkey-1 running healthy"
|
||||
],
|
||||
"scope": "final source snapshot; individual experiment inputs, dates and metric definitions are in their own evidence files"
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
{
|
||||
"scenario": "stop actual upload-serving replica; download via survivor",
|
||||
"cases": [
|
||||
{
|
||||
"uploader": {
|
||||
"container": "e0755982c2d3",
|
||||
"replica": "lab4-api-1",
|
||||
"state": "running",
|
||||
"alias": "1326e1bfb6754455",
|
||||
"image": "5f84776e9db8"
|
||||
},
|
||||
"download_instance": "4cfce35bab4eba13",
|
||||
"stop_seconds": 4.837,
|
||||
"sha256": "99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75",
|
||||
"same_bytes": true,
|
||||
"before": [
|
||||
{
|
||||
"container": "e0755982c2d3",
|
||||
"replica": "lab4-api-1",
|
||||
"state": "running",
|
||||
"alias": "1326e1bfb6754455",
|
||||
"image": "5f84776e9db8"
|
||||
},
|
||||
{
|
||||
"container": "4705eb662d22",
|
||||
"replica": "lab4-api-2",
|
||||
"state": "running",
|
||||
"alias": "4cfce35bab4eba13",
|
||||
"image": "5f84776e9db8"
|
||||
}
|
||||
],
|
||||
"after": [
|
||||
{
|
||||
"container": "e0755982c2d3",
|
||||
"replica": "lab4-api-1",
|
||||
"state": "exited",
|
||||
"alias": "-",
|
||||
"image": "5f84776e9db8"
|
||||
},
|
||||
{
|
||||
"container": "4705eb662d22",
|
||||
"replica": "lab4-api-2",
|
||||
"state": "running",
|
||||
"alias": "4cfce35bab4eba13",
|
||||
"image": "5f84776e9db8"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"uploader": {
|
||||
"container": "4705eb662d22",
|
||||
"replica": "lab4-api-2",
|
||||
"state": "running",
|
||||
"alias": "4cfce35bab4eba13",
|
||||
"image": "5f84776e9db8"
|
||||
},
|
||||
"download_instance": "1caa70dbe06803f5",
|
||||
"stop_seconds": 4.16,
|
||||
"sha256": "99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75",
|
||||
"same_bytes": true,
|
||||
"before": [
|
||||
{
|
||||
"container": "e0755982c2d3",
|
||||
"replica": "lab4-api-1",
|
||||
"state": "running",
|
||||
"alias": "1caa70dbe06803f5",
|
||||
"image": "5f84776e9db8"
|
||||
},
|
||||
{
|
||||
"container": "4705eb662d22",
|
||||
"replica": "lab4-api-2",
|
||||
"state": "running",
|
||||
"alias": "4cfce35bab4eba13",
|
||||
"image": "5f84776e9db8"
|
||||
}
|
||||
],
|
||||
"after": [
|
||||
{
|
||||
"container": "e0755982c2d3",
|
||||
"replica": "lab4-api-1",
|
||||
"state": "running",
|
||||
"alias": "1caa70dbe06803f5",
|
||||
"image": "5f84776e9db8"
|
||||
},
|
||||
{
|
||||
"container": "4705eb662d22",
|
||||
"replica": "lab4-api-2",
|
||||
"state": "exited",
|
||||
"alias": "-",
|
||||
"image": "5f84776e9db8"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"ok": true
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
Public lab4 failover with concurrent idempotent messages and retained attachment.
|
||||
Before:
|
||||
lab4-api-1 running
|
||||
lab4-api-2 running
|
||||
lab4-lb-1 running
|
||||
lab4-migrate-1 exited
|
||||
lab4-postgres-1 running
|
||||
lab4-s3-1 running
|
||||
lab4-s3-init-1 exited
|
||||
lab4-valkey-1 running
|
||||
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
|
||||
{"observed_instances_before": 2, "successful_writes": 130, "failed_attempts": 0, "first_recovery_seconds": 4.265}
|
||||
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||
After:
|
||||
lab4-api-1 running
|
||||
lab4-api-2 exited
|
||||
lab4-lb-1 running
|
||||
lab4-migrate-1 exited
|
||||
lab4-postgres-1 running
|
||||
lab4-s3-1 running
|
||||
lab4-s3-init-1 exited
|
||||
lab4-valkey-1 running
|
||||
Elapsed seconds: 16.326
|
||||
@@ -0,0 +1,25 @@
|
||||
Command: python -X utf8 tests/resilience.py --mode persistence --action-script scripts/restart.cmd
|
||||
Before:
|
||||
/lab4-api-1 started=2026-11-16T09:12:00.227764693Z status=running
|
||||
/lab4-api-2 started=2026-11-16T09:12:00.497951153Z status=running
|
||||
/lab4-lb-1 started=2026-11-16T08:30:34.254973105Z status=running
|
||||
/lab4-migrate-1 started=2026-11-16T09:11:59.49568462Z status=exited
|
||||
/lab4-postgres-1 started=2026-11-16T08:30:34.24518074Z status=running
|
||||
/lab4-s3-1 started=2026-11-16T08:30:34.241553958Z status=running
|
||||
/lab4-s3-init-1 started=2026-11-16T09:11:59.509084337Z status=exited
|
||||
/lab4-valkey-1 started=2026-11-16T08:30:34.223057181Z status=running
|
||||
|
||||
Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.
|
||||
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||
|
||||
After:
|
||||
/lab4-api-1 started=2026-11-16T09:31:00.646703947Z status=running
|
||||
/lab4-api-2 started=2026-11-16T09:31:00.927490539Z status=running
|
||||
/lab4-lb-1 started=2026-11-16T09:30:59.396418626Z status=running
|
||||
/lab4-migrate-1 started=2026-11-16T09:30:59.901158645Z status=exited
|
||||
/lab4-postgres-1 started=2026-11-16T09:30:30.351009425Z status=running
|
||||
/lab4-s3-1 started=2026-11-16T09:30:30.357773314Z status=running
|
||||
/lab4-s3-init-1 started=2026-11-16T09:30:59.909286202Z status=exited
|
||||
/lab4-valkey-1 started=2026-11-16T09:30:30.342892671Z status=running
|
||||
|
||||
Elapsed seconds: 57.838
|
||||
@@ -0,0 +1,6 @@
|
||||
Direct unauthenticated GET of existing private object from API container:
|
||||
Connecting to s3:8333 (172.18.0.3:8333)
|
||||
HTTP/1.1 403 Forbidden
|
||||
wget: server returned error: HTTP/1.1 403 Forbidden
|
||||
|
||||
PASS: no bytes available without S3 credentials.
|
||||
@@ -0,0 +1,23 @@
|
||||
Technical review completed after implementation and a scoped fix verification.
|
||||
|
||||
Resolved:
|
||||
- Maximum-pixel progressive JPEG / wide RGBA64 PNG required more than the
|
||||
original 512 MiB estimate. API limit is 2 GiB, GOMEMLIMIT 1536MiB, one
|
||||
decoder per replica. Three valid 40MP formats were accepted without OOM.
|
||||
- Shutdown default 38s and Compose grace 45s cover active binary handlers.
|
||||
Real upload and download continued for 11.5s across shutdown; race tests pass.
|
||||
- JSON attachment metadata uses the normal 5s deadline, not the binary 30s
|
||||
group. Connection-pool contention regression passes.
|
||||
- Mid-stream S3 reader faults preserve dependency-error classification (503),
|
||||
while malformed bytes successfully read from storage retain 415.
|
||||
- S3 bootstrap waits for retained-byte routing and a write/read/delete probe.
|
||||
Three consecutive container replacements preserve exact bytes.
|
||||
|
||||
No P1/P2 blockers remain in the reviewed feature scope. Complete Go race,
|
||||
vet/check, public smoke, full-stack persistence and both actual uploader
|
||||
replica failures have saved passing evidence.
|
||||
|
||||
Declared nonblocking limits: tiny abandoned startup probes may remain after
|
||||
failed cleanup; the two-minute periodic pass does not prove progress through
|
||||
arbitrarily large data. Use the full CLI scan for larger datasets. Working-set
|
||||
and Go-heap measurements are sampled, distinct metrics, not universal maxima.
|
||||
@@ -0,0 +1,2 @@
|
||||
Command: python -X utf8 tests/smoke.py --base-url http://localhost:8080
|
||||
PASS: lab4 public API scenario; infrastructure and grading evidence remain separate.
|
||||
@@ -0,0 +1,34 @@
|
||||
Command: go test -race -p 2 -count=1 ./...
|
||||
? messenger/cmd/migrate [no test files]
|
||||
? messenger/cmd/reconcile [no test files]
|
||||
? messenger/cmd/s3init [no test files]
|
||||
ok messenger/cmd/server 6.787s
|
||||
ok messenger/internal/app 45.607s
|
||||
ok messenger/internal/attachment 49.580s
|
||||
? messenger/internal/attachment/store [no test files]
|
||||
ok messenger/internal/auth 7.745s
|
||||
? messenger/internal/auth/store [no test files]
|
||||
ok messenger/internal/chat 8.943s
|
||||
ok messenger/internal/chat/store 8.784s
|
||||
? messenger/internal/health [no test files]
|
||||
ok messenger/internal/platform/apperr 1.550s
|
||||
ok messenger/internal/platform/config 1.721s
|
||||
ok messenger/internal/platform/httpx 1.220s
|
||||
ok messenger/internal/platform/ident 1.543s
|
||||
ok messenger/internal/platform/pagination 1.622s
|
||||
ok messenger/internal/platform/password 2.356s
|
||||
ok messenger/internal/platform/postgres 5.178s
|
||||
ok messenger/internal/platform/s3store 205.312s
|
||||
ok messenger/internal/platform/valkey 2.276s
|
||||
ok messenger/internal/server 18.093s
|
||||
ok messenger/internal/testenv 2.536s
|
||||
ok messenger/internal/user 4.805s
|
||||
? messenger/internal/user/store [no test files]
|
||||
? messenger/migrations [no test files]
|
||||
ok messenger/tools/concurrency 6.197s
|
||||
ok messenger/tools/loadgen 10.987s
|
||||
ok messenger/tools/reconcilebench 1.205s
|
||||
PASS: go vet ./...
|
||||
PASS: python -X utf8 scripts/check_contract.py
|
||||
PASS: 13 Python experiment harness tests
|
||||
PASS: regenerated 40MP fixtures match measured SHA-256 values
|
||||
@@ -0,0 +1 @@
|
||||
{"elapsed_ms":10,"sql_queries":8,"s3_calls":4,"scanned_files":6,"scanned_objects":6,"findings":0,"repaired":0}
|
||||
@@ -0,0 +1 @@
|
||||
{"elapsed_ms":10,"sql_queries":3,"s3_calls":4,"scanned_files":6,"scanned_objects":6,"findings":0,"repaired":0}
|
||||
@@ -0,0 +1 @@
|
||||
{"elapsed_ms":14,"sql_queries":8,"s3_calls":4,"scanned_files":6,"scanned_objects":6,"findings":0,"repaired":0}
|
||||
@@ -0,0 +1,193 @@
|
||||
[
|
||||
{
|
||||
"grace_ms": 120000,
|
||||
"memory_metric": "sampled Go process HeapAlloc bytes during Run; not RSS; excludes PostgreSQL and S3",
|
||||
"phase": "environment",
|
||||
"postgres_image": "postgres:18.6-alpine3.24@sha256:77f585114c32fbca283dc835b0596f4e52b51b4c6662d7810b2f4084f60a1873",
|
||||
"s3_image": "chrislusf/seaweedfs:4.53@sha256:ccad23e18639c8b98cee17e7100b95830a06077b56d8e5b006d6dcf8ec5e94a0",
|
||||
"wait_ms": 125000
|
||||
},
|
||||
{
|
||||
"phase": "fault",
|
||||
"mode": "dry",
|
||||
"target_files": 7,
|
||||
"go_heap_peak_bytes": 2621072,
|
||||
"stats": {
|
||||
"elapsed_ms": 27,
|
||||
"sql_queries": 3,
|
||||
"s3_calls": 12,
|
||||
"scanned_files": 7,
|
||||
"scanned_objects": 9,
|
||||
"findings": 6,
|
||||
"repaired": 0
|
||||
},
|
||||
"findings": [
|
||||
{
|
||||
"kind": "stale_staging",
|
||||
"key": "staging/old",
|
||||
"action": "delete_object",
|
||||
"applied": false
|
||||
},
|
||||
{
|
||||
"kind": "orphan_blob",
|
||||
"key": "blobs/ffea09ba14e194c3863fd5338b4994073275de7f1cfb799a4b82cf27340753b3",
|
||||
"action": "delete_object",
|
||||
"applied": false
|
||||
},
|
||||
{
|
||||
"kind": "unreferenced_ready",
|
||||
"key": "blobs/0827f495ee64051e55f98e1d89fa78f6e85df2a36c08745dd73b5b760fefb35d",
|
||||
"action": "delete_file",
|
||||
"applied": false
|
||||
},
|
||||
{
|
||||
"kind": "unreferenced_pending",
|
||||
"key": "blobs/3a7b538ee81c67aa0bfd549349a001bbbe727ec8b158dcf8525b0e86e44f2cc8",
|
||||
"action": "delete_file",
|
||||
"applied": false
|
||||
},
|
||||
{
|
||||
"kind": "hash_mismatch",
|
||||
"key": "blobs/86d4cd5182d5620e915db1e3363c153bd1b619ef09be6a6918fc194e67c92dff",
|
||||
"action": "report",
|
||||
"applied": false
|
||||
},
|
||||
{
|
||||
"kind": "missing_blob",
|
||||
"key": "blobs/e18d9f81534696cf0ce0143f1c2c4b540873bc7f39e08cbbfe584d6b5212723e",
|
||||
"action": "mark_failed",
|
||||
"applied": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"phase": "fault",
|
||||
"mode": "apply",
|
||||
"target_files": 7,
|
||||
"go_heap_peak_bytes": 3289520,
|
||||
"stats": {
|
||||
"elapsed_ms": 62,
|
||||
"sql_queries": 41,
|
||||
"s3_calls": 18,
|
||||
"scanned_files": 7,
|
||||
"scanned_objects": 9,
|
||||
"findings": 6,
|
||||
"repaired": 5
|
||||
},
|
||||
"findings": [
|
||||
{
|
||||
"kind": "stale_staging",
|
||||
"key": "staging/old",
|
||||
"action": "delete_object",
|
||||
"applied": true
|
||||
},
|
||||
{
|
||||
"kind": "orphan_blob",
|
||||
"key": "blobs/ffea09ba14e194c3863fd5338b4994073275de7f1cfb799a4b82cf27340753b3",
|
||||
"action": "delete_object",
|
||||
"applied": true
|
||||
},
|
||||
{
|
||||
"kind": "unreferenced_ready",
|
||||
"key": "blobs/0827f495ee64051e55f98e1d89fa78f6e85df2a36c08745dd73b5b760fefb35d",
|
||||
"action": "delete_file",
|
||||
"applied": true
|
||||
},
|
||||
{
|
||||
"kind": "unreferenced_pending",
|
||||
"key": "blobs/3a7b538ee81c67aa0bfd549349a001bbbe727ec8b158dcf8525b0e86e44f2cc8",
|
||||
"action": "delete_file",
|
||||
"applied": true
|
||||
},
|
||||
{
|
||||
"kind": "hash_mismatch",
|
||||
"key": "blobs/86d4cd5182d5620e915db1e3363c153bd1b619ef09be6a6918fc194e67c92dff",
|
||||
"action": "report",
|
||||
"applied": false
|
||||
},
|
||||
{
|
||||
"kind": "missing_blob",
|
||||
"key": "blobs/e18d9f81534696cf0ce0143f1c2c4b540873bc7f39e08cbbfe584d6b5212723e",
|
||||
"action": "mark_failed",
|
||||
"applied": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"phase": "fault",
|
||||
"mode": "repeat",
|
||||
"target_files": 7,
|
||||
"go_heap_peak_bytes": 2437032,
|
||||
"stats": {
|
||||
"elapsed_ms": 37,
|
||||
"sql_queries": 23,
|
||||
"s3_calls": 10,
|
||||
"scanned_files": 5,
|
||||
"scanned_objects": 5,
|
||||
"findings": 1,
|
||||
"repaired": 0
|
||||
},
|
||||
"findings": [
|
||||
{
|
||||
"kind": "hash_mismatch",
|
||||
"key": "blobs/86d4cd5182d5620e915db1e3363c153bd1b619ef09be6a6918fc194e67c92dff",
|
||||
"action": "report",
|
||||
"applied": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"phase": "cost",
|
||||
"mode": "dry",
|
||||
"target_files": 100,
|
||||
"go_heap_peak_bytes": 3875232,
|
||||
"stats": {
|
||||
"elapsed_ms": 184,
|
||||
"sql_queries": 2,
|
||||
"s3_calls": 103,
|
||||
"scanned_files": 100,
|
||||
"scanned_objects": 100,
|
||||
"findings": 0,
|
||||
"repaired": 0
|
||||
}
|
||||
},
|
||||
{
|
||||
"phase": "cost",
|
||||
"mode": "dry",
|
||||
"target_files": 1000,
|
||||
"go_heap_peak_bytes": 4159720,
|
||||
"stats": {
|
||||
"elapsed_ms": 2160,
|
||||
"sql_queries": 3,
|
||||
"s3_calls": 1004,
|
||||
"scanned_files": 1000,
|
||||
"scanned_objects": 1000,
|
||||
"findings": 0,
|
||||
"repaired": 0
|
||||
}
|
||||
},
|
||||
{
|
||||
"phase": "cost",
|
||||
"mode": "dry",
|
||||
"target_files": 5000,
|
||||
"go_heap_peak_bytes": 5157552,
|
||||
"stats": {
|
||||
"elapsed_ms": 10121,
|
||||
"sql_queries": 11,
|
||||
"s3_calls": 5012,
|
||||
"scanned_files": 5000,
|
||||
"scanned_objects": 5000,
|
||||
"findings": 0,
|
||||
"repaired": 0
|
||||
}
|
||||
},
|
||||
{
|
||||
"fault_checks": "passed",
|
||||
"phase": "complete",
|
||||
"sizes": [
|
||||
100,
|
||||
1000,
|
||||
5000
|
||||
]
|
||||
}
|
||||
]
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,118 @@
|
||||
module messenger
|
||||
|
||||
go 1.27.1
|
||||
|
||||
require (
|
||||
github.com/docker/docker v28.3.3+incompatible
|
||||
github.com/docker/go-connections v0.6.0
|
||||
github.com/gin-gonic/gin v1.12.0
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/jackc/pgx/v5 v5.11.0
|
||||
github.com/minio/minio-go/v7 v7.4.1
|
||||
github.com/pressly/goose/v3 v3.27.0
|
||||
github.com/redis/go-redis/v9 v9.23.0
|
||||
github.com/testcontainers/testcontainers-go v0.39.0
|
||||
github.com/testcontainers/testcontainers-go/modules/postgres v0.39.0
|
||||
github.com/testcontainers/testcontainers-go/modules/valkey v0.39.0
|
||||
golang.org/x/crypto v0.57.0
|
||||
google.golang.org/grpc v1.84.0
|
||||
google.golang.org/protobuf v1.36.12
|
||||
)
|
||||
|
||||
require (
|
||||
dario.cat/mergo v1.0.2 // indirect
|
||||
github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c // indirect
|
||||
github.com/Microsoft/go-winio v0.6.2 // indirect
|
||||
github.com/bytedance/gopkg v0.1.3 // indirect
|
||||
github.com/bytedance/sonic v1.15.0 // indirect
|
||||
github.com/bytedance/sonic/loader v0.5.0 // indirect
|
||||
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/cloudwego/base64x v0.1.6 // indirect
|
||||
github.com/containerd/errdefs v1.0.0 // indirect
|
||||
github.com/containerd/errdefs/pkg v0.3.0 // indirect
|
||||
github.com/containerd/log v0.1.0 // indirect
|
||||
github.com/containerd/platforms v0.2.1 // indirect
|
||||
github.com/cpuguy83/dockercfg v0.3.2 // indirect
|
||||
github.com/creack/pty v1.1.24 // indirect
|
||||
github.com/distribution/reference v0.6.0 // indirect
|
||||
github.com/docker/go-units v0.5.0 // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/ebitengine/purego v0.10.1 // indirect
|
||||
github.com/felixge/httpsnoop v1.1.0 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.12 // indirect
|
||||
github.com/gin-contrib/sse v1.1.0 // indirect
|
||||
github.com/go-logr/logr v1.4.4 // indirect
|
||||
github.com/go-logr/stdr v1.2.2 // indirect
|
||||
github.com/go-ole/go-ole v1.3.0 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-playground/validator/v10 v10.30.1 // indirect
|
||||
github.com/goccy/go-json v0.10.5 // indirect
|
||||
github.com/goccy/go-yaml v1.19.2 // indirect
|
||||
github.com/gogo/protobuf v1.3.2 // indirect
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.31.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/json-iterator/go v1.1.12 // 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/leodido/go-urn v1.4.0 // indirect
|
||||
github.com/lufia/plan9stats v0.0.0-20260330125221-c963978e514e // indirect
|
||||
github.com/magiconair/properties v1.8.10 // indirect
|
||||
github.com/mattn/go-isatty v0.0.24 // indirect
|
||||
github.com/mdelapenya/tlscert v0.2.0 // indirect
|
||||
github.com/mfridman/interpolate v0.0.2 // indirect
|
||||
github.com/minio/crc64nvme v1.1.1 // indirect
|
||||
github.com/minio/md5-simd v1.1.2 // indirect
|
||||
github.com/moby/docker-image-spec v1.3.1 // indirect
|
||||
github.com/moby/go-archive v0.2.0 // indirect
|
||||
github.com/moby/patternmatcher v0.6.1 // indirect
|
||||
github.com/moby/sys/sequential v0.7.0 // indirect
|
||||
github.com/moby/sys/user v0.4.0 // indirect
|
||||
github.com/moby/sys/userns v0.1.0 // indirect
|
||||
github.com/moby/term v0.5.2 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/morikuni/aec v1.0.0 // indirect
|
||||
github.com/opencontainers/go-digest v1.0.0 // indirect
|
||||
github.com/opencontainers/image-spec v1.1.1 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.4.3 // indirect
|
||||
github.com/philhofer/fwd v1.2.0 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
|
||||
github.com/quic-go/qpack v0.6.0 // indirect
|
||||
github.com/quic-go/quic-go v0.59.0 // indirect
|
||||
github.com/rs/xid v1.6.0 // indirect
|
||||
github.com/sethvargo/go-retry v0.4.0 // indirect
|
||||
github.com/shirou/gopsutil/v4 v4.26.6 // indirect
|
||||
github.com/sirupsen/logrus v1.10.2 // indirect
|
||||
github.com/stretchr/testify v1.12.1 // indirect
|
||||
github.com/tinylib/msgp v1.6.4 // indirect
|
||||
github.com/tklauser/go-sysconf v0.4.0 // indirect
|
||||
github.com/tklauser/numcpus v0.12.0 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/ugorji/go/codec v1.3.1 // indirect
|
||||
github.com/yusufpapurcu/wmi v1.2.4 // indirect
|
||||
github.com/zeebo/xxh3 v1.1.0 // indirect
|
||||
go.mongodb.org/mongo-driver/v2 v2.5.0 // indirect
|
||||
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
|
||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.71.0 // indirect
|
||||
go.opentelemetry.io/otel v1.46.0 // indirect
|
||||
go.opentelemetry.io/otel/metric v1.46.0 // indirect
|
||||
go.opentelemetry.io/otel/trace v1.46.0 // indirect
|
||||
go.uber.org/atomic v1.12.0 // indirect
|
||||
go.uber.org/multierr v1.11.0 // indirect
|
||||
go.yaml.in/yaml/v3 v3.0.5 // indirect
|
||||
golang.org/x/arch v0.22.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20260908205506-85c1c2202aba // indirect
|
||||
golang.org/x/net v0.60.0 // indirect
|
||||
golang.org/x/sync v0.23.0 // indirect
|
||||
golang.org/x/sys v0.48.0 // indirect
|
||||
golang.org/x/text v0.42.0 // indirect
|
||||
golang.org/x/time v0.11.0 // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260918162117-cecb64721679 // indirect
|
||||
gopkg.in/ini.v1 v1.67.3 // indirect
|
||||
)
|
||||
@@ -0,0 +1,328 @@
|
||||
dario.cat/mergo v1.0.2 h1:85+piFYR1tMbRrLcDwR18y4UKJ3aH1Tbzi24VRW1TK8=
|
||||
dario.cat/mergo v1.0.2/go.mod h1:E/hbnu0NxMFBjpMIE34DRGLWqDy0g5FuKDhCb31ngxA=
|
||||
github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6 h1:He8afgbRMd7mFxO99hRNu+6tazq8nFF9lIwo9JFroBk=
|
||||
github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6/go.mod h1:8o94RPi1/7XTJvwPpRSzSUedZrtlirdB3r9Z20bi2f8=
|
||||
github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c h1:udKWzYgxTojEKWjV8V+WSxDXJ4NFATAsZjh8iIbsQIg=
|
||||
github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
|
||||
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
|
||||
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
|
||||
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/bytedance/gopkg v0.1.3 h1:TPBSwH8RsouGCBcMBktLt1AymVo2TVsBVCY4b6TnZ/M=
|
||||
github.com/bytedance/gopkg v0.1.3/go.mod h1:576VvJ+eJgyCzdjS+c4+77QF3p7ubbtiKARP3TxducM=
|
||||
github.com/bytedance/sonic v1.15.0 h1:/PXeWFaR5ElNcVE84U0dOHjiMHQOwNIx3K4ymzh/uSE=
|
||||
github.com/bytedance/sonic v1.15.0/go.mod h1:tFkWrPz0/CUCLEF4ri4UkHekCIcdnkqXw9VduqpJh0k=
|
||||
github.com/bytedance/sonic/loader v0.5.0 h1:gXH3KVnatgY7loH5/TkeVyXPfESoqSBSBEiDd5VjlgE=
|
||||
github.com/bytedance/sonic/loader v0.5.0/go.mod h1:AR4NYCk5DdzZizZ5djGqQ92eEhCCcdf5x77udYiSJRo=
|
||||
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
|
||||
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
|
||||
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/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M=
|
||||
github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU=
|
||||
github.com/containerd/errdefs v1.0.0 h1:tg5yIfIlQIrxYtu9ajqY42W3lpS19XqdxRQeEwYG8PI=
|
||||
github.com/containerd/errdefs v1.0.0/go.mod h1:+YBYIdtsnF4Iw6nWZhJcqGSg/dwvV7tyJ/kCkyJ2k+M=
|
||||
github.com/containerd/errdefs/pkg v0.3.0 h1:9IKJ06FvyNlexW690DXuQNx2KA2cUJXx151Xdx3ZPPE=
|
||||
github.com/containerd/errdefs/pkg v0.3.0/go.mod h1:NJw6s9HwNuRhnjJhM7pylWwMyAkmCQvQ4GpJHEqRLVk=
|
||||
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
|
||||
github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo=
|
||||
github.com/containerd/platforms v0.2.1 h1:zvwtM3rz2YHPQsF2CHYM8+KtB5dvhISiXh5ZpSBQv6A=
|
||||
github.com/containerd/platforms v0.2.1/go.mod h1:XHCb+2/hzowdiut9rkudds9bE5yJ7npe7dG/wG+uFPw=
|
||||
github.com/cpuguy83/dockercfg v0.3.2 h1:DlJTyZGBDlXqUZ2Dk2Q3xHs/FtnooJJVaad2S9GKorA=
|
||||
github.com/cpuguy83/dockercfg v0.3.2/go.mod h1:sugsbF4//dDlL/i+S+rtpIWp+5h0BHJHfjj5/jFyUJc=
|
||||
github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s=
|
||||
github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk=
|
||||
github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
|
||||
github.com/docker/docker v28.3.3+incompatible h1:Dypm25kh4rmk49v1eiVbsAtpAsYURjYkaKubwuBdxEI=
|
||||
github.com/docker/docker v28.3.3+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
|
||||
github.com/docker/go-connections v0.6.0 h1:LlMG9azAe1TqfR7sO+NJttz1gy6KO7VJBh+pMmjSD94=
|
||||
github.com/docker/go-connections v0.6.0/go.mod h1:AahvXYshr6JgfUJGdDCs2b5EZG/vmaMAntpSFH5BFKE=
|
||||
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
|
||||
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
|
||||
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/ebitengine/purego v0.10.1 h1:dewVBCBT2GaMu1SrNTYxQhgQBethzfhiwvZiLGP/qyY=
|
||||
github.com/ebitengine/purego v0.10.1/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
|
||||
github.com/felixge/httpsnoop v1.1.0 h1:3YtUj32ZZkqZtt3sZZsClsymw/QDuVfpNhoA31zeORc=
|
||||
github.com/felixge/httpsnoop v1.1.0/go.mod h1:Zqxgdd+1Rkcz8euOqdr7lqgCRJztwr5hp9vDSi5UZCE=
|
||||
github.com/gabriel-vasile/mimetype v1.4.12 h1:e9hWvmLYvtp846tLHam2o++qitpguFiYCKbn0w9jyqw=
|
||||
github.com/gabriel-vasile/mimetype v1.4.12/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s=
|
||||
github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w=
|
||||
github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM=
|
||||
github.com/gin-gonic/gin v1.12.0 h1:b3YAbrZtnf8N//yjKeU2+MQsh2mY5htkZidOM7O0wG8=
|
||||
github.com/gin-gonic/gin v1.12.0/go.mod h1:VxccKfsSllpKshkBWgVgRniFFAzFb9csfngsqANjnLc=
|
||||
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-logr/logr v1.4.4 h1:tG4xh9yMsRCAiodLVTxyrkzSZ9+o0L1Kg/+cPVcbP/8=
|
||||
github.com/go-logr/logr v1.4.4/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
|
||||
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
|
||||
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
|
||||
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/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
|
||||
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
||||
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
||||
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
||||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||
github.com/go-playground/validator/v10 v10.30.1 h1:f3zDSN/zOma+w6+1Wswgd9fLkdwy06ntQJp0BBvFG0w=
|
||||
github.com/go-playground/validator/v10 v10.30.1/go.mod h1:oSuBIQzuJxL//3MelwSLD5hc2Tu889bF0Idm9Dg26cM=
|
||||
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
|
||||
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||
github.com/goccy/go-yaml v1.19.2 h1:PmFC1S6h8ljIz6gMRBopkjP1TVT7xuwrButHID66PoM=
|
||||
github.com/goccy/go-yaml v1.19.2/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
|
||||
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
|
||||
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
|
||||
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/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
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/grpc-ecosystem/grpc-gateway/v2 v2.31.0 h1:Bd7KaOxzULLxtZ/K5s1aLbWhR0+5RToO65TXHsf3bqQ=
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.31.0/go.mod h1:nN7ts3dFXKtCZWc//yfkpcQNKJABg16/uDVAZpLDalo=
|
||||
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/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
|
||||
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
||||
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/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
||||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/lufia/plan9stats v0.0.0-20260330125221-c963978e514e h1:Q6MvJtQK/iRcRtzAscm/zF23XxJlbECiGPyRicsX+Ak=
|
||||
github.com/lufia/plan9stats v0.0.0-20260330125221-c963978e514e/go.mod h1:autxFIvghDt3jPTLoqZ9OZ7s9qTGNAWmYCjVFWPX/zg=
|
||||
github.com/magiconair/properties v1.8.10 h1:s31yESBquKXCV9a/ScB3ESkOjUYYv+X0rg8SYxI99mE=
|
||||
github.com/magiconair/properties v1.8.10/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
|
||||
github.com/mattn/go-isatty v0.0.24 h1:tGZZoVgT/KiqK1c8ocVLeDS8BSWMRd47J3Lbz7vsReI=
|
||||
github.com/mattn/go-isatty v0.0.24/go.mod h1:nMCL3Zebbrt45jsMDgnfIwz6ydEQApk5oEI3HqDio6A=
|
||||
github.com/mdelapenya/tlscert v0.2.0 h1:7H81W6Z/4weDvZBNOfQte5GpIMo0lGYEeWbkGp5LJHI=
|
||||
github.com/mdelapenya/tlscert v0.2.0/go.mod h1:O4njj3ELLnJjGdkN7M/vIVCpZ+Cf0L6muqOG4tLSl8o=
|
||||
github.com/mfridman/interpolate v0.0.2 h1:pnuTK7MQIxxFz1Gr+rjSIx9u7qVjf5VOoM/u6BbAxPY=
|
||||
github.com/mfridman/interpolate v0.0.2/go.mod h1:p+7uk6oE07mpE/Ik1b8EckO0O4ZXiGAfshKBWLUM9Xg=
|
||||
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/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/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0=
|
||||
github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo=
|
||||
github.com/moby/go-archive v0.2.0 h1:zg5QDUM2mi0JIM9fdQZWC7U8+2ZfixfTYoHL7rWUcP8=
|
||||
github.com/moby/go-archive v0.2.0/go.mod h1:mNeivT14o8xU+5q1YnNrkQVpK+dnNe/K6fHqnTg4qPU=
|
||||
github.com/moby/patternmatcher v0.6.1 h1:qlhtafmr6kgMIJjKJMDmMWq7WLkKIo23hsrpR3x084U=
|
||||
github.com/moby/patternmatcher v0.6.1/go.mod h1:hDPoyOpDY7OrrMDLaYoY3hf52gNCR/YOUYxkhApJIxc=
|
||||
github.com/moby/sys/atomicwriter v0.1.0 h1:kw5D/EqkBwsBFi0ss9v1VG3wIkVhzGvLklJ+w3A14Sw=
|
||||
github.com/moby/sys/atomicwriter v0.1.0/go.mod h1:Ul8oqv2ZMNHOceF643P6FKPXeCmYtlQMvpizfsSoaWs=
|
||||
github.com/moby/sys/sequential v0.7.0 h1:ASQNGNROJSuOO6LL6bPHbKvuZu6NU8P4ldPWk31zj/8=
|
||||
github.com/moby/sys/sequential v0.7.0/go.mod h1:NfSTAp6V3fw4tmkD62PEcOKeZKquXT8VKCkf7aVR79o=
|
||||
github.com/moby/sys/user v0.4.0 h1:jhcMKit7SA80hivmFJcbB1vqmw//wU61Zdui2eQXuMs=
|
||||
github.com/moby/sys/user v0.4.0/go.mod h1:bG+tYYYJgaMtRKgEmuueC0hJEAZWwtIbZTB+85uoHjs=
|
||||
github.com/moby/sys/userns v0.1.0 h1:tVLXkFOxVu9A64/yh59slHVv9ahO9UIev4JZusOLG/g=
|
||||
github.com/moby/sys/userns v0.1.0/go.mod h1:IHUYgu/kao6N8YZlp9Cf444ySSvCmDlmzUcYfDHOl28=
|
||||
github.com/moby/term v0.5.2 h1:6qk3FJAFDs6i/q3W/pQ97SX192qKfZgGjCQqfCJkgzQ=
|
||||
github.com/moby/term v0.5.2/go.mod h1:d3djjFCrjnB+fl8NJux+EJzu0msscUP+f8it8hPkFLc=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A=
|
||||
github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc=
|
||||
github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w=
|
||||
github.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
|
||||
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
|
||||
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
|
||||
github.com/opencontainers/image-spec v1.1.1 h1:y0fUlFfIZhPF1W537XOLg0/fcx6zcHCJwooC2xJA040=
|
||||
github.com/opencontainers/image-spec v1.1.1/go.mod h1:qpqAh3Dmcf36wStyyWU+kCeDgrGnAve2nCC8+7h8Q0M=
|
||||
github.com/pelletier/go-toml/v2 v2.4.3 h1:GTRvJQutkOSftxIFD5xw9aepkYNuPWmVJpffdDPYVpY=
|
||||
github.com/pelletier/go-toml/v2 v2.4.3/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
|
||||
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/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
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/pressly/goose/v3 v3.27.0 h1:/D30gVTuQhu0WsNZYbJi4DMOsx1lNq+6SkLe+Wp59BM=
|
||||
github.com/pressly/goose/v3 v3.27.0/go.mod h1:3ZBeCXqzkgIRvrEMDkYh1guvtoJTU5oMMuDdkutoM78=
|
||||
github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8=
|
||||
github.com/quic-go/qpack v0.6.0/go.mod h1:lUpLKChi8njB4ty2bFLX2x4gzDqXwUpaO1DP9qMDZII=
|
||||
github.com/quic-go/quic-go v0.59.0 h1:OLJkp1Mlm/aS7dpKgTc6cnpynnD2Xg7C1pwL6vy/SAw=
|
||||
github.com/quic-go/quic-go v0.59.0/go.mod h1:upnsH4Ju1YkqpLXC305eW3yDZ4NfnNbmQRCMWS58IKU=
|
||||
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/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
|
||||
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
|
||||
github.com/sethvargo/go-retry v0.4.0 h1:9qy1OoIAxBL+gBYnkTnTnWle5wlfsXQlwRzIbbpdqPw=
|
||||
github.com/sethvargo/go-retry v0.4.0/go.mod h1:tvsjdKG6xfiCx4LSiUZ06kcv38xvdVQwv8R6/VnnVWg=
|
||||
github.com/shirou/gopsutil/v4 v4.26.6 h1:Mzr/npDtQC/xpeEuQKHZt8Zo9CmPvhTj8nkR8w5TLDs=
|
||||
github.com/shirou/gopsutil/v4 v4.26.6/go.mod h1:LZ6ewCSkBqUpvSOf+LsTGnRinC6iaNUNMGBtDkJBaLQ=
|
||||
github.com/sirupsen/logrus v1.10.2 h1:G2SED73/qrAu6YwbdxOD6peLkCBI3z7L+ykJFTXJBBo=
|
||||
github.com/sirupsen/logrus v1.10.2/go.mod h1:SLEg8TqYulVKKfIGHldVp2K2aYz2DKSVBq4g/H5bR7Q=
|
||||
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/objx v0.5.3 h1:jmXUvGomnU1o3W/V5h2VEradbpJDwGrzugQQvL0POH4=
|
||||
github.com/stretchr/objx v0.5.3/go.mod h1:rDQraq+vQZU7Fde9LOZLr8Tax6zZvy4kuNKF+QYS+U0=
|
||||
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.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
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/testcontainers/testcontainers-go v0.39.0 h1:uCUJ5tA+fcxbFAB0uP3pIK3EJ2IjjDUHFSZ1H1UxAts=
|
||||
github.com/testcontainers/testcontainers-go v0.39.0/go.mod h1:qmHpkG7H5uPf/EvOORKvS6EuDkBUPE3zpVGaH9NL7f8=
|
||||
github.com/testcontainers/testcontainers-go/modules/postgres v0.39.0 h1:REJz+XwNpGC/dCgTfYvM4SKqobNqDBfvhq74s2oHTUM=
|
||||
github.com/testcontainers/testcontainers-go/modules/postgres v0.39.0/go.mod h1:4K2OhtHEeT+JSIFX4V8DkGKsyLa96Y2vLdd3xsxD5HE=
|
||||
github.com/testcontainers/testcontainers-go/modules/valkey v0.39.0 h1:b5BwugyCMHrFfZYCSFKY9IHMhoWtR+REcN2ddcCQv6g=
|
||||
github.com/testcontainers/testcontainers-go/modules/valkey v0.39.0/go.mod h1:lgNiE/W5RPjOu1S8fZ9Z9e63JU8wyaxNQ43Umv6kroQ=
|
||||
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.4.0 h1:7H0uAN+7RkwWRaxhYXDLqa5V3LPrJeV8wmD9dRUgPQU=
|
||||
github.com/tklauser/go-sysconf v0.4.0/go.mod h1:8mTNWyog7H+MpKijp4VmKJAd2bbYQ2zuUwkYRbUArPI=
|
||||
github.com/tklauser/numcpus v0.12.0 h1:NR85qdvHA9pFse3x3weVZ0r0ST8R6l5RHbZrlRaqob4=
|
||||
github.com/tklauser/numcpus v0.12.0/go.mod h1:ABHeXzJnr/qqwguhClkZKT1/8VABcYrsyUiUGobwWJg=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||
github.com/ugorji/go/codec v1.3.1 h1:waO7eEiFDwidsBN6agj1vJQ4AG7lh2yqXyOXqhgQuyY=
|
||||
github.com/ugorji/go/codec v1.3.1/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4=
|
||||
github.com/valkey-io/valkey-go v1.0.59 h1:W67Z0UY+Qqk3k8NKkFCFlM3X4yQUniixl7dSJAch2Qo=
|
||||
github.com/valkey-io/valkey-go v1.0.59/go.mod h1:bHmwjIEOrGq/ubOJfh5uMRs7Xj6mV3mQ/ZXUbmqpjqY=
|
||||
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
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.mongodb.org/mongo-driver/v2 v2.5.0 h1:yXUhImUjjAInNcpTcAlPHiT7bIXhshCTL3jVBkF3xaE=
|
||||
go.mongodb.org/mongo-driver/v2 v2.5.0/go.mod h1:yOI9kBsufol30iFsl1slpdq1I0eHPzybRWdyYUs8K/0=
|
||||
go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=
|
||||
go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y=
|
||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.71.0 h1:3g7B90UzBltIDKq1/5mrTGxTnOFDV0ICOhLoxiZ8jlg=
|
||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.71.0/go.mod h1:Ef8SuTh59BT7+ofpDxN9z+yOlc4t2GjLmKDgYNJL/NU=
|
||||
go.opentelemetry.io/otel v1.46.0 h1:FHt5/CDyVxi/8IM1CH7VE/rRgq3kLHa2mSTVMO8AWyc=
|
||||
go.opentelemetry.io/otel v1.46.0/go.mod h1:Gj3SEScelsNC45tp4nSxRYlS+f5iez7W8XPMCt905kE=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0 h1:Mne5On7VWdx7omSrSSZvM4Kw7cS7NQkOOmLcgscI51U=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0/go.mod h1:IPtUMKL4O3tH5y+iXVyAXqpAwMuzC1IrxVS81rummfE=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0 h1:IeMeyr1aBvBiPVYihXIaeIZba6b8E1bYp7lbdxK8CQg=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0/go.mod h1:oVdCUtjq9MK9BlS7TtucsQwUcXcymNiEDjgDD2jMtZU=
|
||||
go.opentelemetry.io/otel/metric v1.46.0 h1:yBnkXvgV7AXFILZc5K6IZe/CBFF3OS7BJ8ov6/lj0K8=
|
||||
go.opentelemetry.io/otel/metric v1.46.0/go.mod h1:iPmdWqifKUdzziPkvvzIJXITl56fQx2mGM/DHLB3/2o=
|
||||
go.opentelemetry.io/otel/sdk v1.46.0 h1:h5CNQQjEbuQXY/JfZtgt3i7HVFV3aHPO2OAwO2eTYPI=
|
||||
go.opentelemetry.io/otel/sdk v1.46.0/go.mod h1:GAERFXFt5SYCEB+YiKUbMBeza6UaDH7GmGOZEfh2gSM=
|
||||
go.opentelemetry.io/otel/sdk/metric v1.46.0 h1:0piZ26EG4RBfebb2jhDH6ERCYHoVWduc3kLgPCwSnSE=
|
||||
go.opentelemetry.io/otel/sdk/metric v1.46.0/go.mod h1:I1PbKrdVc8Qu8HYVDNtqVIwLwjNrhsV/uFuxfwg8mO4=
|
||||
go.opentelemetry.io/otel/trace v1.46.0 h1:OULy7ccdJnZtJ0UDYFOIGaCmiWzJ8Vi2G/Rsu60qs1c=
|
||||
go.opentelemetry.io/otel/trace v1.46.0/go.mod h1:J7GAXweO77XSFkB/rmAqk9D6ihszhFjLU+d9WuUxDLI=
|
||||
go.opentelemetry.io/proto/otlp v1.0.0 h1:T0TX0tmXU8a3CbNXzEKGeU5mIVOdf0oykP+u2lIVU/I=
|
||||
go.opentelemetry.io/proto/otlp v1.0.0/go.mod h1:Sy6pihPLfYHkr3NkUbEhGHFhINUSI/v80hjKIs5JXpM=
|
||||
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.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y=
|
||||
go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU=
|
||||
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
|
||||
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
|
||||
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/arch v0.22.0 h1:c/Zle32i5ttqRXjdLyyHZESLD/bB90DCU1g9l/0YBDI=
|
||||
golang.org/x/arch v0.22.0/go.mod h1:dNHoOeKiyja7GTvF9NJS1l3Z2yntpQNzgrjh1cU103A=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M=
|
||||
golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA=
|
||||
golang.org/x/exp v0.0.0-20260908205506-85c1c2202aba h1:Ck8QetSgk912qxWLMCKxd0in+aiyBQyDSMae6e/xmpU=
|
||||
golang.org/x/exp v0.0.0-20260908205506-85c1c2202aba/go.mod h1:50RgIsmK7OwqzTTeqcSXQW8SswW0o8fRcDxmqGluJ8E=
|
||||
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
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-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.23.0 h1:KameEIfc1IkluZyXWLn39Wd4tURc6GbCiISGiZm2bQk=
|
||||
golang.org/x/sync v0.23.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-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo=
|
||||
golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
|
||||
golang.org/x/term v0.46.0 h1:3+OXuTbaKDgwk8jTi3aSLHRlmWqHEUDUtxnbFigO4YE=
|
||||
golang.org/x/term v0.46.0/go.mod h1:+K02xbkittuwc0Am4abfA3Fc+XRGXkvBXNO88NCXPoc=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.42.0 h1:JbOZXgfeCPU9gacVtYliJqOhD+zhrEqK4LfdpmlUZqI=
|
||||
golang.org/x/text v0.42.0/go.mod h1:ojzP1Z+2QtioaF8DTtO8K5q7JWVVYwZKenzujK0Zd0E=
|
||||
golang.org/x/time v0.11.0 h1:/bpjEDfN9tkoN/ryeYHnv5hcMlc8ncjMcM4XBk5NWV0=
|
||||
golang.org/x/time v0.11.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
|
||||
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gonum.org/v1/gonum v0.17.0 h1:VbpOemQlsSMrYmn7T2OUvQ4dqxQXU+ouZFQsZOx50z4=
|
||||
gonum.org/v1/gonum v0.17.0/go.mod h1:El3tOrEuMpv2UdMrbNlKEh9vd86bmQ6vqIcDwxEOc1E=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20260921155816-b14227669459 h1:GS9OIt/j7c8bvBjYNgnKQysVfmV7e4jM0H8ZK95G4t8=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20260921155816-b14227669459/go.mod h1:PX5/4vemwVoXtwEcRDWwcR1/r0qrosfx3qoVADMwnVE=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260918162117-cecb64721679 h1:KmqdJU4vrNcxy/6qdg3JduZtalEXrJLspVltnR1cE+8=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260918162117-cecb64721679/go.mod h1:OaIUM3+LpYcK2GXM4FTmhWoIq371Owdr+Cc7/BsYHHc=
|
||||
google.golang.org/grpc v1.84.0 h1:soMyaPJ8pAak5PIQ0DGBUir0XRo2fRoMqhNWMLlLxO0=
|
||||
google.golang.org/grpc v1.84.0/go.mod h1:ljCht0DrxQrXBDRTZp52Qxh3Ffk8CdYm2sj4O2QN2C0=
|
||||
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=
|
||||
gotest.tools/v3 v3.5.2 h1:7koQfIKdy+I8UTetycgUqXWSDwpgv193Ka+qRsmBY8Q=
|
||||
gotest.tools/v3 v3.5.2/go.mod h1:LtdLGcnqToBH83WByAAi/wiwSFCArdFIUV/xxN4pcjA=
|
||||
modernc.org/libc v1.68.0 h1:PJ5ikFOV5pwpW+VqCK1hKJuEWsonkIJhhIXyuF/91pQ=
|
||||
modernc.org/libc v1.68.0/go.mod h1:NnKCYeoYgsEqnY3PgvNgAeaJnso968ygU8Z0DxjoEc0=
|
||||
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
|
||||
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
|
||||
modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI=
|
||||
modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
|
||||
modernc.org/sqlite v1.46.1 h1:eFJ2ShBLIEnUWlLy12raN0Z1plqmFX9Qe3rjQTKt6sU=
|
||||
modernc.org/sqlite v1.46.1/go.mod h1:CzbrU2lSB1DKUusvwGz7rqEKIq+NUd8GWuBBZDs9/nA=
|
||||
@@ -0,0 +1,76 @@
|
||||
# The single entry point: round-robin over the API replicas found through
|
||||
# Docker DNS, with active readiness checks and no client stickiness.
|
||||
|
||||
global
|
||||
log stdout format raw local0 info
|
||||
maxconn 20000
|
||||
# The container has at most one CPU; by default HAProxy starts a thread
|
||||
# per host CPU and fights the quota.
|
||||
nbthread 1
|
||||
|
||||
defaults
|
||||
mode http
|
||||
log global
|
||||
option dontlognull
|
||||
timeout connect 1s
|
||||
timeout client 30s
|
||||
# Above the API's 5 s request deadline: the API answers a slow request
|
||||
# itself, this only cuts off a hung replica.
|
||||
timeout server 7s
|
||||
timeout http-request 10s
|
||||
timeout http-keep-alive 10s
|
||||
timeout check 2s
|
||||
# Below the 500 ms latency objective: excess load gets a fast 503.
|
||||
timeout queue 300ms
|
||||
# Re-sent only when nothing reached the API: a timeout or an empty
|
||||
# response may follow a committed write.
|
||||
retries 2
|
||||
option redispatch 1
|
||||
retry-on conn-failure
|
||||
# Errors produced by the balancer itself use the contract's envelope.
|
||||
http-error status 502 content-type application/json lf-string '{"error":{"code":"unavailable","message":"the API replica failed to answer","request_id":"%[unique-id]"}}' hdr X-Request-Id "%[unique-id]"
|
||||
http-error status 503 content-type application/json lf-string '{"error":{"code":"unavailable","message":"no API replica is available","request_id":"%[unique-id]"}}' hdr X-Request-Id "%[unique-id]"
|
||||
http-error status 504 content-type application/json lf-string '{"error":{"code":"unavailable","message":"the API replica did not answer in time","request_id":"%[unique-id]"}}' hdr X-Request-Id "%[unique-id]"
|
||||
|
||||
# `api` resolves to every running replica, re-resolved every second; an
|
||||
# address that disappears leaves at once.
|
||||
resolvers docker
|
||||
nameserver dns 127.0.0.11:53
|
||||
resolve_retries 3
|
||||
timeout resolve 1s
|
||||
timeout retry 1s
|
||||
hold valid 10s
|
||||
hold obsolete 0s
|
||||
accepted_payload_size 8192
|
||||
|
||||
frontend entry
|
||||
bind :8080
|
||||
option httplog
|
||||
unique-id-format %[uuid(4),regsub(-,,g)]
|
||||
# Replaces any client value; the API keeps it, so one id spans both logs.
|
||||
http-request set-header X-Request-Id %[unique-id]
|
||||
capture response header X-Instance-Id len 32
|
||||
capture response header X-Request-Id len 40
|
||||
http-after-response set-log-level silent if { status lt 500 } { env(LB_QUIET) -m str 1 }
|
||||
default_backend replicas
|
||||
|
||||
backend replicas
|
||||
# Binary handlers have a 30s budget, ordinary API handlers retain 5s.
|
||||
http-request set-timeout server 35s if { path_reg ^/api/v1/chats/[^/]+/attachments$|^/api/v1/attachments/[^/]+/content$ }
|
||||
balance roundrobin
|
||||
option httpchk
|
||||
http-check send meth GET uri /health/ready
|
||||
http-check expect status 200
|
||||
# Out within 1.5 s (inter + fastinter), less than the API's 2 s drain delay.
|
||||
# maxconn 20: the replica's 10 pooled database connections plus requests
|
||||
# that need no database; the rest waits in the queue.
|
||||
default-server maxconn "${API_MAXCONN-20}" inter 1s fastinter 500ms downinter 1s fall 2 rise 2 check resolvers docker init-addr none resolve-prefer ipv4
|
||||
# Up to five replicas, doubled while docker rollout replaces them.
|
||||
server-template api 10 api:8080
|
||||
|
||||
listen stats
|
||||
bind :8404
|
||||
no log
|
||||
stats enable
|
||||
stats uri /
|
||||
stats refresh 2s
|
||||
@@ -0,0 +1,77 @@
|
||||
# Private object storage
|
||||
|
||||
`s3` runs SeaweedFS 4.53 in one process. The image is pinned by digest in
|
||||
`compose.yaml` and `internal/testenv/s3.go`. `seaweedfs.options` is shared by
|
||||
the stand and the disposable integration-test server.
|
||||
|
||||
S3 HTTP (8333) and SeaweedFS's companion gRPC (18333) listen on the private
|
||||
container network. This image has no flag to disable the companion listener;
|
||||
its identity-cache mutations reject remote peers, which the integration test
|
||||
checks. Master, volume and filer HTTP/gRPC endpoints bind to loopback.
|
||||
No host port is published. `/data`
|
||||
contains both object volumes and the image's LevelDB filer metadata; the
|
||||
named `s3data` volume must be retained together with PostgreSQL data.
|
||||
|
||||
Run `sh scripts/init-secrets.sh` before starting the stand. It preserves the
|
||||
existing PostgreSQL and Valkey credentials and creates three independent S3
|
||||
identities plus the static server configuration. Credential values are never
|
||||
printed. Repeated initialization keeps all existing keys. Set `S3_BUCKET` in
|
||||
the environment or the local `.env` before the first initialization; changing
|
||||
it later requires a planned migration of identities and retained data.
|
||||
|
||||
The server loads its static identity secret before serving requests. There is
|
||||
no anonymous identity. The bootstrap identity has administrative rights and
|
||||
its individual key files are mounted only in `s3-init`. The API identity has
|
||||
`Read:<bucket>` and `Write:<bucket>`; it cannot list objects or administer
|
||||
buckets. The reconciler identity adds `List:<bucket>`. All three identities
|
||||
are necessarily present in the S3 server's own identity configuration secret.
|
||||
|
||||
`s3-init` creates the bucket idempotently and waits for storage before API
|
||||
replicas start. An anonymous 403 and bucket metadata alone do not prove that
|
||||
retained volume locations have registered with the elected master. Bootstrap
|
||||
first reads a persistent `.health/storage-v1` marker (creating it only when
|
||||
absent), then verifies a fresh write/read/delete. This startup-only barrier
|
||||
uses the caller's deadline, capped at 60 seconds; ordinary S3 operations and
|
||||
API readiness do not retry through it. The marker is outside the reconciler's
|
||||
`staging/` and `blobs/` namespaces. `scripts/restart.sh` already runs this
|
||||
bootstrap before restoring API replicas.
|
||||
|
||||
The bootstrap uses `/usr/local/bin/s3init`. The optional `recon` profile runs
|
||||
`/usr/local/bin/reconcile`, with reconciler credentials mapped to the standard
|
||||
`S3_ACCESS_KEY_ID_FILE` and `S3_SECRET_ACCESS_KEY_FILE` paths:
|
||||
|
||||
```sh
|
||||
docker compose --profile recon run --rm reconcile
|
||||
docker compose --profile recon run --rm reconcile --apply
|
||||
```
|
||||
|
||||
The API receives its own standard S3 key files. Its background reconciliation
|
||||
worker must construct a separate store from
|
||||
`S3_RECONCILE_ACCESS_KEY_ID_FILE` and `S3_RECONCILE_SECRET_ACCESS_KEY_FILE`;
|
||||
listing with the API identity is denied. S3 is intentionally absent from API
|
||||
readiness so text traffic can remain available during an object-store outage.
|
||||
|
||||
The S3 cgroup is limited to one CPU, 512 MiB, and 256 processes. Upload limits
|
||||
are four concurrent requests and 64 MiB in flight; reader buffers have a
|
||||
32 MiB budget and the chunk cache is disabled. Disk allocation is capped at
|
||||
64 volumes of approximately 128 MiB each (with normal volume rollover
|
||||
overhead); this is a lab capacity limit, not a filesystem quota.
|
||||
|
||||
Each API replica has 2 GiB and `GOMEMLIMIT=1536MiB`, with one image decoder.
|
||||
Pixels alone are not the full budget: progressive JPEG retains coefficients,
|
||||
and a wide RGBA64 PNG keeps two large scanlines beside its pixel buffer.
|
||||
The 40 MP RGB/CMYK progressive JPEG and wide-PNG measurements are recorded in
|
||||
`evidence/decoder-memory-*.json`; this does not change the S3 service's 512 MiB limit.
|
||||
|
||||
Verify the adapter and real server with Docker running:
|
||||
|
||||
```sh
|
||||
go test -race -count=1 ./internal/platform/s3store
|
||||
```
|
||||
|
||||
`testenv.StartS3(ctx)` returns `Store` with reconciler rights, `APIStore`,
|
||||
`Admin`, endpoint and credentials. `Recreate(ctx)` replaces only its own
|
||||
container while keeping its named volume and does not recreate the bucket.
|
||||
It waits for the same retained-marker and write/read/delete barrier before
|
||||
returning. `Start(ctx)` does so as well. Always call `Close()` to remove the
|
||||
fixture's container and volume.
|
||||
@@ -0,0 +1,31 @@
|
||||
dir=/data
|
||||
ip=127.0.0.1
|
||||
ip.bind=127.0.0.1
|
||||
master.telemetry=false
|
||||
master.volumeSizeLimitMB=128
|
||||
volume.port=9340
|
||||
volume.max=64
|
||||
volume.index=leveldb
|
||||
volume.fileSizeLimitMB=10
|
||||
volume.concurrentUploadLimitMB=64
|
||||
volume.concurrentDownloadLimitMB=64
|
||||
volume.readBufferSizeMB=1
|
||||
volume.preStopSeconds=1
|
||||
filer=true
|
||||
filer.exposeDirectoryData=false
|
||||
filer.disableDirListing=true
|
||||
filer.concurrentFileUploadLimit=4
|
||||
filer.concurrentUploadLimitMB=64
|
||||
s3=true
|
||||
s3.ip.bind=0.0.0.0
|
||||
s3.port=8333
|
||||
s3.config=/run/secrets/s3_identities
|
||||
s3.autoCreateBucket=false
|
||||
s3.iam=false
|
||||
s3.port.iceberg=0
|
||||
s3.port.lance=0
|
||||
s3.concurrentFileUploadLimit=4
|
||||
s3.concurrentUploadLimitMB=64
|
||||
s3.readerCacheSizeMB=32
|
||||
s3.cacheCapacityMB=0
|
||||
s3.idleTimeout=35
|
||||
@@ -0,0 +1,19 @@
|
||||
# Valkey is the only store of user sessions, so it is their source of truth.
|
||||
# Every write is fsynced before the reply: an acknowledged login survives a
|
||||
# crash or a restart. Only logins and logouts write; session lookups are reads
|
||||
# and pay nothing for this.
|
||||
appendonly yes
|
||||
appendfsync always
|
||||
dir /data
|
||||
|
||||
# The append-only file already persists everything; RDB snapshots are off.
|
||||
save ""
|
||||
|
||||
# Sessions are never evicted to free memory: once maxmemory is reached the
|
||||
# store refuses new sessions (login answers 503) instead of silently logging
|
||||
# users out. A session key takes about a hundred bytes.
|
||||
maxmemory 256mb
|
||||
maxmemory-policy noeviction
|
||||
|
||||
# requirepass comes from the compose secret, never from the repository.
|
||||
include /run/secrets/valkey_auth
|
||||
@@ -0,0 +1,99 @@
|
||||
// Package app is the composition root: it builds every module with its
|
||||
// stores and mounts them on one HTTP engine. Both the server binary and the
|
||||
// integration tests start from here.
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"messenger/internal/attachment"
|
||||
attachmentstore "messenger/internal/attachment/store"
|
||||
"messenger/internal/auth"
|
||||
authstore "messenger/internal/auth/store"
|
||||
"messenger/internal/chat"
|
||||
chatstore "messenger/internal/chat/store"
|
||||
"messenger/internal/health"
|
||||
"messenger/internal/platform/httpx"
|
||||
"messenger/internal/user"
|
||||
userstore "messenger/internal/user/store"
|
||||
)
|
||||
|
||||
type App struct {
|
||||
Engine *gin.Engine
|
||||
Health *health.Handler
|
||||
// Chats is exposed for the background cleanup of idempotency keys.
|
||||
Chats *chat.Service
|
||||
}
|
||||
|
||||
// Deps are the shared resources the modules are built on; their lifetime is
|
||||
// owned by the caller.
|
||||
type Deps struct {
|
||||
Log *slog.Logger
|
||||
DB *pgxpool.Pool
|
||||
Sessions *redis.Client
|
||||
SessionTTL time.Duration
|
||||
MaxBodyBytes int64
|
||||
// Zero timeouts use the defaults below.
|
||||
RequestTimeout time.Duration
|
||||
BinaryRequestTimeout time.Duration
|
||||
InstanceID string
|
||||
// IdempotencyTTL zero means the contract's 24 hours.
|
||||
IdempotencyTTL time.Duration
|
||||
Blobs attachment.Blobs
|
||||
AttachmentsCfg attachment.Config
|
||||
}
|
||||
|
||||
// defaultRequestTimeout stays below the server's write timeout, so a
|
||||
// request that gives up on a dependency can still write its 503.
|
||||
const defaultRequestTimeout = 5 * time.Second
|
||||
|
||||
// Binary transfers and decoding share this budget, below the 35s socket timeout.
|
||||
const defaultBinaryRequestTimeout = 30 * time.Second
|
||||
|
||||
const defaultIdempotencyTTL = 24 * time.Hour
|
||||
|
||||
func New(d Deps) *App {
|
||||
users := user.NewService(userstore.NewUsers(d.DB))
|
||||
keyTTL := d.IdempotencyTTL
|
||||
if keyTTL == 0 {
|
||||
keyTTL = defaultIdempotencyTTL
|
||||
}
|
||||
chatRepository := chatstore.NewChats(d.DB)
|
||||
chats := chat.NewService(chatRepository, chatstore.NewMessages(d.DB), users, keyTTL)
|
||||
|
||||
engine := httpx.NewEngine(d.Log, d.MaxBodyBytes, d.InstanceID)
|
||||
probes := health.New(
|
||||
health.Check{Name: "postgres", Probe: d.DB.Ping},
|
||||
health.Check{Name: "valkey", Probe: func(ctx context.Context) error { return d.Sessions.Ping(ctx).Err() }},
|
||||
)
|
||||
probes.Register(engine)
|
||||
|
||||
sessions := auth.NewService(users, authstore.NewSessions(d.Sessions), d.SessionTTL)
|
||||
|
||||
timeout := d.RequestTimeout
|
||||
if timeout == 0 {
|
||||
timeout = defaultRequestTimeout
|
||||
}
|
||||
public := engine.Group("/api/v1", httpx.Timeout(timeout))
|
||||
protected := public.Group("", auth.RequireSession(sessions))
|
||||
auth.NewHandler(sessions).Register(public, protected)
|
||||
user.NewHandler(users).Register(public, protected)
|
||||
chat.NewHandler(chats).Register(protected)
|
||||
attachments := attachment.NewService(
|
||||
attachmentstore.NewAttachments(d.DB), chatRepository, d.Blobs, d.AttachmentsCfg,
|
||||
)
|
||||
binaryTimeout := d.BinaryRequestTimeout
|
||||
if binaryTimeout == 0 {
|
||||
binaryTimeout = defaultBinaryRequestTimeout
|
||||
}
|
||||
binaryProtected := engine.Group("/api/v1", httpx.Timeout(binaryTimeout), auth.RequireSession(sessions))
|
||||
attachment.NewHandler(attachments).Register(protected, binaryProtected)
|
||||
|
||||
return &App{Engine: engine, Health: probes, Chats: chats}
|
||||
}
|
||||
@@ -0,0 +1,523 @@
|
||||
package app_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/app"
|
||||
"messenger/internal/chat"
|
||||
"messenger/internal/platform/pagination"
|
||||
"messenger/internal/testenv"
|
||||
"messenger/internal/user"
|
||||
)
|
||||
|
||||
var (
|
||||
db *testenv.DB
|
||||
kv *testenv.KV
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
ctx := context.Background()
|
||||
var err error
|
||||
if db, err = testenv.StartPostgres(ctx); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if kv, err = testenv.StartValkey(ctx); err != nil {
|
||||
db.Close()
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
code := m.Run()
|
||||
kv.Close()
|
||||
db.Close()
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
const testPassword = "correct-horse-battery"
|
||||
|
||||
const testInstance = "00000000000000a1"
|
||||
|
||||
// errorBody mirrors the contract error envelope.
|
||||
type errorBody struct {
|
||||
Error struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
RequestID string `json:"request_id"`
|
||||
} `json:"error"`
|
||||
}
|
||||
|
||||
type response struct {
|
||||
status int
|
||||
requestID string
|
||||
challenge string
|
||||
raw []byte
|
||||
}
|
||||
|
||||
// tester drives the assembled application in-process.
|
||||
type tester struct {
|
||||
t *testing.T
|
||||
a *app.App
|
||||
}
|
||||
|
||||
// account is a registered user with a live session.
|
||||
type account struct {
|
||||
user.Response
|
||||
token string
|
||||
}
|
||||
|
||||
// sessionBody mirrors the contract Session schema.
|
||||
type sessionBody struct {
|
||||
Token string `json:"token"`
|
||||
TokenType string `json:"token_type"`
|
||||
ExpiresAt string `json:"expires_at"`
|
||||
User user.Response `json:"user"`
|
||||
}
|
||||
|
||||
func newTester(t *testing.T) *tester {
|
||||
t.Helper()
|
||||
return newTesterWithTTL(t, time.Hour)
|
||||
}
|
||||
|
||||
// newTesterWithTTL assembles the application over empty stores.
|
||||
func newTesterWithTTL(t *testing.T, ttl time.Duration) *tester {
|
||||
t.Helper()
|
||||
db.Reset(t)
|
||||
kv.Reset(t)
|
||||
log := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
return &tester{t: t, a: app.New(app.Deps{
|
||||
Log: log,
|
||||
DB: db.Pool,
|
||||
Sessions: kv.Client,
|
||||
SessionTTL: ttl,
|
||||
MaxBodyBytes: 64 * 1024,
|
||||
InstanceID: testInstance,
|
||||
})}
|
||||
}
|
||||
|
||||
// do sends one request, authenticated with the bearer token when it is set.
|
||||
// A string body is sent verbatim, anything else is JSON-encoded; both get a
|
||||
// JSON content type. Every response is checked for X-Request-Id, error
|
||||
// bodies for the contract envelope, and every 401 for the challenge of the
|
||||
// scheme the route expects.
|
||||
func (ts *tester) do(method, path, token string, body any, headers map[string]string) response {
|
||||
ts.t.Helper()
|
||||
req := httptest.NewRequest(method, path, encode(ts.t, body))
|
||||
if body != nil {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
if token != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
}
|
||||
for k, v := range headers {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
rec := httptest.NewRecorder()
|
||||
ts.a.Engine.ServeHTTP(rec, req)
|
||||
|
||||
res := response{
|
||||
status: rec.Code,
|
||||
requestID: rec.Header().Get("X-Request-Id"),
|
||||
challenge: rec.Header().Get("WWW-Authenticate"),
|
||||
raw: rec.Body.Bytes(),
|
||||
}
|
||||
if res.requestID == "" {
|
||||
ts.t.Fatalf("%s %s: missing X-Request-Id", method, path)
|
||||
}
|
||||
if got := rec.Header().Get("X-Instance-Id"); got != testInstance {
|
||||
ts.t.Fatalf("%s %s: X-Instance-Id = %q, want %q", method, path, got, testInstance)
|
||||
}
|
||||
if res.status >= 400 {
|
||||
var eb errorBody
|
||||
if err := json.Unmarshal(res.raw, &eb); err != nil {
|
||||
ts.t.Fatalf("%s %s: error body is not the contract envelope: %v: %s", method, path, err, res.raw)
|
||||
}
|
||||
if eb.Error.RequestID != res.requestID || eb.Error.Code == "" || eb.Error.Message == "" {
|
||||
ts.t.Fatalf("%s %s: bad error envelope: %s", method, path, res.raw)
|
||||
}
|
||||
}
|
||||
if res.status == http.StatusUnauthorized {
|
||||
scheme := "Bearer "
|
||||
if path == "/api/v1/auth/sessions" {
|
||||
scheme = "Basic "
|
||||
}
|
||||
if !strings.HasPrefix(res.challenge, scheme) {
|
||||
ts.t.Fatalf("%s %s: 401 challenge = %q, want scheme %q", method, path, res.challenge, scheme)
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func encode(t *testing.T, body any) io.Reader {
|
||||
t.Helper()
|
||||
switch b := body.(type) {
|
||||
case nil:
|
||||
return nil
|
||||
case string:
|
||||
return strings.NewReader(b)
|
||||
default:
|
||||
raw, err := json.Marshal(b)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return bytes.NewReader(raw)
|
||||
}
|
||||
}
|
||||
|
||||
// expect asserts the status and, for errors, the contract code.
|
||||
func (ts *tester) expect(res response, status int, code string) {
|
||||
ts.t.Helper()
|
||||
if res.status != status {
|
||||
ts.t.Fatalf("status = %d, want %d; body %s", res.status, status, res.raw)
|
||||
}
|
||||
if code != "" {
|
||||
var eb errorBody
|
||||
_ = json.Unmarshal(res.raw, &eb)
|
||||
if eb.Error.Code != code {
|
||||
ts.t.Fatalf("code = %q, want %q; body %s", eb.Error.Code, code, res.raw)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// decode parses a success body into a typed DTO and rejects unknown fields,
|
||||
// which enforces the additionalProperties=false rule of the contract.
|
||||
func decode[T any](t *testing.T, res response) T {
|
||||
t.Helper()
|
||||
var out T
|
||||
dec := json.NewDecoder(bytes.NewReader(res.raw))
|
||||
dec.DisallowUnknownFields()
|
||||
if err := dec.Decode(&out); err != nil {
|
||||
t.Fatalf("body does not match %T: %v: %s", out, err, res.raw)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// user registers a user and logs it in.
|
||||
func (ts *tester) user(username, display string) account {
|
||||
ts.t.Helper()
|
||||
body := map[string]string{"username": username, "display_name": display, "password": testPassword}
|
||||
res := ts.do(http.MethodPost, "/api/v1/users", "", body, nil)
|
||||
ts.expect(res, http.StatusCreated, "")
|
||||
registered := decode[user.Response](ts.t, res)
|
||||
return account{Response: registered, token: ts.login(username, testPassword).Token}
|
||||
}
|
||||
|
||||
func basic(username, password string) map[string]string {
|
||||
raw := base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
|
||||
return map[string]string{"Authorization": "Basic " + raw}
|
||||
}
|
||||
|
||||
func (ts *tester) login(username, password string) sessionBody {
|
||||
ts.t.Helper()
|
||||
res := ts.do(http.MethodPost, "/api/v1/auth/sessions", "", nil, basic(username, password))
|
||||
ts.expect(res, http.StatusCreated, "")
|
||||
return decode[sessionBody](ts.t, res)
|
||||
}
|
||||
|
||||
func (ts *tester) chat(token, title string, members ...string) chat.ChatResponse {
|
||||
ts.t.Helper()
|
||||
res := ts.do(http.MethodPost, "/api/v1/chats", token, map[string]any{"title": title, "member_ids": members}, nil)
|
||||
ts.expect(res, http.StatusCreated, "")
|
||||
return decode[chat.ChatResponse](ts.t, res)
|
||||
}
|
||||
|
||||
func (ts *tester) message(token, chatID, text string) chat.MessageResponse {
|
||||
ts.t.Helper()
|
||||
res := ts.do(http.MethodPost, "/api/v1/chats/"+chatID+"/messages", token, map[string]string{"text": text}, freshKey())
|
||||
ts.expect(res, http.StatusCreated, "")
|
||||
return decode[chat.MessageResponse](ts.t, res)
|
||||
}
|
||||
|
||||
func freshKey() map[string]string {
|
||||
return map[string]string{"Idempotency-Key": uuid.NewString()}
|
||||
}
|
||||
|
||||
func (ts *tester) chats(token, query string) pagination.PageResponse[chat.ChatResponse] {
|
||||
ts.t.Helper()
|
||||
res := ts.do(http.MethodGet, "/api/v1/chats"+query, token, nil, nil)
|
||||
ts.expect(res, http.StatusOK, "")
|
||||
return decode[pagination.PageResponse[chat.ChatResponse]](ts.t, res)
|
||||
}
|
||||
|
||||
func (ts *tester) history(token, chatID, query string) pagination.PageResponse[chat.MessageResponse] {
|
||||
ts.t.Helper()
|
||||
res := ts.do(http.MethodGet, "/api/v1/chats/"+chatID+"/messages"+query, token, nil, nil)
|
||||
ts.expect(res, http.StatusOK, "")
|
||||
return decode[pagination.PageResponse[chat.MessageResponse]](ts.t, res)
|
||||
}
|
||||
|
||||
func cursorQuery(limit int, cursor *string) string {
|
||||
q := fmt.Sprintf("?limit=%d", limit)
|
||||
if cursor != nil {
|
||||
q += "&cursor=" + url.QueryEscape(*cursor)
|
||||
}
|
||||
return q
|
||||
}
|
||||
|
||||
func TestHealthEndpoints(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
for _, path := range []string{"/health/live", "/health/ready"} {
|
||||
res := ts.do(http.MethodGet, path, "", nil, nil)
|
||||
ts.expect(res, http.StatusOK, "")
|
||||
if strings.TrimSpace(string(res.raw)) != `{"status":"ok"}` {
|
||||
t.Fatalf("%s body = %s", path, res.raw)
|
||||
}
|
||||
}
|
||||
ts.a.Health.SetReady(false)
|
||||
ts.expect(ts.do(http.MethodGet, "/health/ready", "", nil, nil), http.StatusServiceUnavailable, "unavailable")
|
||||
ts.expect(ts.do(http.MethodGet, "/health/live", "", nil, nil), http.StatusOK, "")
|
||||
}
|
||||
|
||||
func TestRegisterUserShapeAndConflicts(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
alice := ts.user("alice", "Алиса")
|
||||
if alice.ID == "" || alice.Username != "alice" || alice.DisplayName != "Алиса" {
|
||||
t.Fatalf("user = %+v", alice)
|
||||
}
|
||||
if !strings.HasSuffix(alice.CreatedAt, "Z") || len(alice.CreatedAt) != len("2026-09-01T12:00:00.000Z") {
|
||||
t.Fatalf("created_at = %q", alice.CreatedAt)
|
||||
}
|
||||
post := func(body any) response { return ts.do(http.MethodPost, "/api/v1/users", "", body, nil) }
|
||||
ts.expect(post(map[string]string{"username": "alice", "display_name": "Other", "password": testPassword}), http.StatusConflict, "conflict")
|
||||
ts.expect(post(map[string]string{"username": "bob", "display_name": "Bob"}), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(post(map[string]string{"username": "Alice", "display_name": "x"}), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(post(map[string]string{"username": "bob", "display_name": " "}), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(post(map[string]any{"username": "bob", "display_name": "x", "password": "no"}), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(post(`{"username":`), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(post(map[string]any{"username": 5, "display_name": "x"}), http.StatusBadRequest, "invalid_request")
|
||||
}
|
||||
|
||||
func TestCurrentUserIsTheTokenOwner(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
alice := ts.user("alice", "Алиса")
|
||||
ts.user("bob", "Боб")
|
||||
res := ts.do(http.MethodGet, "/api/v1/users/me", alice.token, nil, nil)
|
||||
ts.expect(res, http.StatusOK, "")
|
||||
if got := decode[user.Response](t, res); got != alice.Response {
|
||||
t.Fatalf("me = %+v, want %+v", got, alice.Response)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatCreationRules(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
alice, bob := ts.user("alice", "A"), ts.user("bob", "B")
|
||||
created := ts.chat(alice.token, "Проект по ВНП", alice.ID, bob.ID)
|
||||
if created.Title != "Проект по ВНП" || len(created.MemberIDs) != 2 || created.MemberIDs[0] != alice.ID || created.MemberIDs[1] != bob.ID {
|
||||
t.Fatalf("chat = %+v", created)
|
||||
}
|
||||
unknown := "22222222-2222-4222-8222-222222222222"
|
||||
invalid := map[string]any{
|
||||
"creator missing": map[string]any{"title": "t", "member_ids": []string{bob.ID}},
|
||||
"duplicate": map[string]any{"title": "t", "member_ids": []string{alice.ID, alice.ID}},
|
||||
"unknown member": map[string]any{"title": "t", "member_ids": []string{alice.ID, unknown}},
|
||||
"malformed uuid": map[string]any{"title": "t", "member_ids": []string{alice.ID, "not-a-uuid"}},
|
||||
"empty members": map[string]any{"title": "t", "member_ids": []string{}},
|
||||
"blank title": map[string]any{"title": "", "member_ids": []string{alice.ID}},
|
||||
"long title": map[string]any{"title": strings.Repeat("я", 101), "member_ids": []string{alice.ID}},
|
||||
"missing members": map[string]any{"title": "t"},
|
||||
"extra field": map[string]any{"title": "t", "member_ids": []string{alice.ID}, "owner": alice.ID},
|
||||
}
|
||||
for name, body := range invalid {
|
||||
res := ts.do(http.MethodPost, "/api/v1/chats", alice.token, body, nil)
|
||||
if res.status != http.StatusBadRequest {
|
||||
t.Errorf("%s: status = %d, want 400; body %s", name, res.status, res.raw)
|
||||
}
|
||||
}
|
||||
ts.expect(ts.do(http.MethodPost, "/api/v1/chats", "", map[string]any{"title": "t", "member_ids": []string{alice.ID}}, nil), http.StatusUnauthorized, "unauthorized")
|
||||
}
|
||||
|
||||
func TestChatVisibilityAndListing(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
alice, bob, eve := ts.user("alice", "A"), ts.user("bob", "B"), ts.user("eve", "E")
|
||||
created := ts.chat(alice.token, "Контрактный тест", alice.ID, bob.ID)
|
||||
path := "/api/v1/chats/" + created.ID
|
||||
|
||||
res := ts.do(http.MethodGet, path, bob.token, nil, nil)
|
||||
ts.expect(res, http.StatusOK, "")
|
||||
if got := decode[chat.ChatResponse](t, res); got.ID != created.ID || len(got.MemberIDs) != 2 {
|
||||
t.Fatalf("GET chat = %+v", got)
|
||||
}
|
||||
ts.expect(ts.do(http.MethodGet, path, eve.token, nil, nil), http.StatusNotFound, "not_found")
|
||||
ts.expect(ts.do(http.MethodGet, "/api/v1/chats/33333333-3333-4333-8333-333333333333", alice.token, nil, nil), http.StatusNotFound, "not_found")
|
||||
ts.expect(ts.do(http.MethodGet, "/api/v1/chats/not-a-uuid", alice.token, nil, nil), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(ts.do(http.MethodGet, path, "", nil, nil), http.StatusUnauthorized, "unauthorized")
|
||||
|
||||
listed := ts.chats(bob.token, "?limit=100")
|
||||
if len(listed.Items) != 1 || listed.Items[0].ID != created.ID || listed.NextCursor != nil {
|
||||
t.Fatalf("bob list = %+v", listed)
|
||||
}
|
||||
hidden := ts.do(http.MethodGet, "/api/v1/chats?limit=100", eve.token, nil, nil)
|
||||
ts.expect(hidden, http.StatusOK, "")
|
||||
if !strings.Contains(string(hidden.raw), `"items":[]`) || !strings.Contains(string(hidden.raw), `"next_cursor":null`) {
|
||||
t.Fatalf("empty list must serialize items as [] and next_cursor as null: %s", hidden.raw)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatListPagination(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
alice := ts.user("alice", "A")
|
||||
var expected []string
|
||||
for i := 0; i < 5; i++ {
|
||||
expected = append(expected, ts.chat(alice.token, fmt.Sprintf("chat %d", i), alice.ID).ID)
|
||||
}
|
||||
var walked []string
|
||||
var cursor *string
|
||||
for pages := 0; pages < 10; pages++ {
|
||||
page := ts.chats(alice.token, cursorQuery(2, cursor))
|
||||
for _, c := range page.Items {
|
||||
walked = append(walked, c.ID)
|
||||
}
|
||||
if page.NextCursor == nil {
|
||||
break
|
||||
}
|
||||
cursor = page.NextCursor
|
||||
}
|
||||
if strings.Join(walked, ",") != strings.Join(expected, ",") {
|
||||
t.Fatalf("walked %v, want %v", walked, expected)
|
||||
}
|
||||
|
||||
bob := ts.user("bob", "B")
|
||||
foreign := ts.chats(alice.token, "?limit=1").NextCursor
|
||||
ts.expect(ts.do(http.MethodGet, "/api/v1/chats"+cursorQuery(50, foreign), bob.token, nil, nil), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(ts.do(http.MethodGet, "/api/v1/chats?cursor=", alice.token, nil, nil), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(ts.do(http.MethodGet, "/api/v1/chats?limit=abc", alice.token, nil, nil), http.StatusBadRequest, "invalid_request")
|
||||
}
|
||||
|
||||
func TestMessagesEndToEnd(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
alice, bob, eve := ts.user("alice", "A"), ts.user("bob", "B"), ts.user("eve", "E")
|
||||
room := ts.chat(alice.token, "Контрактный тест", alice.ID, bob.ID)
|
||||
path := "/api/v1/chats/" + room.ID + "/messages"
|
||||
|
||||
first := ts.message(alice.token, room.ID, "Привет, мир 👋")
|
||||
second := ts.message(bob.token, room.ID, "Сообщение Боба")
|
||||
if first.SenderID != alice.ID || second.SenderID != bob.ID {
|
||||
t.Fatalf("sender must come from the bearer token: %+v %+v", first, second)
|
||||
}
|
||||
if first.ChatID != room.ID || first.Text != "Привет, мир 👋" {
|
||||
t.Fatalf("message = %+v", first)
|
||||
}
|
||||
|
||||
page1 := ts.history(bob.token, room.ID, "?limit=1")
|
||||
if len(page1.Items) != 1 || page1.NextCursor == nil || !reflect.DeepEqual(page1.Items[0], first) {
|
||||
t.Fatalf("page1 = %+v", page1)
|
||||
}
|
||||
page2 := ts.history(alice.token, room.ID, cursorQuery(1, page1.NextCursor))
|
||||
if len(page2.Items) != 1 || page2.NextCursor != nil || !reflect.DeepEqual(page2.Items[0], second) {
|
||||
t.Fatalf("page2 = %+v", page2)
|
||||
}
|
||||
|
||||
post := func(actor string, body any) response { return ts.do(http.MethodPost, path, actor, body, freshKey()) }
|
||||
ts.expect(post(alice.token, map[string]string{"text": ""}), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(post(alice.token, map[string]string{"text": " "}), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(post(alice.token, map[string]string{"text": strings.Repeat("я", 4001)}), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(post(alice.token, map[string]string{"text": "x", "sender_id": bob.ID}), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(post(alice.token, `{"text":`), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(post(eve.token, map[string]string{"text": "чужое"}), http.StatusNotFound, "not_found")
|
||||
ts.expect(post(eve.token, map[string]string{"text": ""}), http.StatusNotFound, "not_found")
|
||||
ts.expect(post("", map[string]string{"text": "x"}), http.StatusUnauthorized, "unauthorized")
|
||||
ts.expect(post(alice.token, map[string]string{"text": strings.Repeat("я", 4000)}), http.StatusCreated, "")
|
||||
|
||||
get := func(actor, query string) response { return ts.do(http.MethodGet, path+query, actor, nil, nil) }
|
||||
ts.expect(get(eve.token, ""), http.StatusNotFound, "not_found")
|
||||
ts.expect(get(alice.token, "?limit=0"), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(get(alice.token, "?limit=101"), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(get(alice.token, "?cursor=garbage"), http.StatusBadRequest, "invalid_request")
|
||||
|
||||
other := ts.chat(alice.token, "другой", alice.ID)
|
||||
ts.expect(ts.do(http.MethodGet, "/api/v1/chats/"+other.ID+"/messages"+cursorQuery(50, page1.NextCursor), alice.token, nil, nil), http.StatusBadRequest, "invalid_request")
|
||||
}
|
||||
|
||||
func TestMessageHistoryWalkAcrossPages(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
alice := ts.user("alice", "A")
|
||||
room := ts.chat(alice.token, "t", alice.ID)
|
||||
var expected []string
|
||||
for i := 0; i < 23; i++ {
|
||||
expected = append(expected, ts.message(alice.token, room.ID, fmt.Sprintf("m%d", i)).ID)
|
||||
}
|
||||
var walked []string
|
||||
var cursor *string
|
||||
for {
|
||||
page := ts.history(alice.token, room.ID, cursorQuery(5, cursor))
|
||||
for _, m := range page.Items {
|
||||
walked = append(walked, m.ID)
|
||||
}
|
||||
if page.NextCursor == nil {
|
||||
break
|
||||
}
|
||||
cursor = page.NextCursor
|
||||
}
|
||||
if strings.Join(walked, ",") != strings.Join(expected, ",") {
|
||||
t.Fatalf("walk mismatch:\n%v\n%v", walked, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConcurrentSendsProduceExactlyUniqueMessages(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
const clients, perClient = 10, 10
|
||||
clientsList := make([]account, 0, clients)
|
||||
members := make([]string, 0, clients)
|
||||
for i := 0; i < clients; i++ {
|
||||
c := ts.user(fmt.Sprintf("client_%d", i), "C")
|
||||
clientsList = append(clientsList, c)
|
||||
members = append(members, c.ID)
|
||||
}
|
||||
room := ts.chat(clientsList[0].token, "нагрузка", members...)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for n, c := range clientsList {
|
||||
wg.Go(func() {
|
||||
for i := 0; i < perClient; i++ {
|
||||
res := ts.do(http.MethodPost, "/api/v1/chats/"+room.ID+"/messages", c.token, map[string]string{"text": fmt.Sprintf("c%d-m%d", n, i)}, freshKey())
|
||||
if res.status != http.StatusCreated {
|
||||
t.Errorf("client %d: status %d body %s", n, res.status, res.raw)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
page := ts.history(clientsList[0].token, room.ID, "?limit=100")
|
||||
if len(page.Items) != clients*perClient || page.NextCursor != nil {
|
||||
t.Fatalf("got %d messages, next=%v; want %d and null", len(page.Items), page.NextCursor, clients*perClient)
|
||||
}
|
||||
ids := make([]string, 0, len(page.Items))
|
||||
unique := make(map[string]struct{}, len(page.Items))
|
||||
for _, m := range page.Items {
|
||||
ids = append(ids, m.ID)
|
||||
unique[m.ID] = struct{}{}
|
||||
}
|
||||
if !sort.StringsAreSorted(ids) {
|
||||
t.Fatal("history is not ordered by id")
|
||||
}
|
||||
if len(unique) != clients*perClient {
|
||||
t.Fatalf("%d unique ids, want %d", len(unique), clients*perClient)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnknownRouteAndMethodAreContractErrors(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
ts.expect(ts.do(http.MethodGet, "/api/v1/nothing", "", nil, nil), http.StatusNotFound, "not_found")
|
||||
ts.expect(ts.do(http.MethodDelete, "/api/v1/users", "", nil, nil), http.StatusMethodNotAllowed, "invalid_request")
|
||||
ts.expect(ts.do(http.MethodPost, "/api/v1/users", "", "{}", map[string]string{"Content-Type": "text/plain"}), http.StatusUnsupportedMediaType, "unsupported_media_type")
|
||||
big := map[string]string{"username": "big", "display_name": strings.Repeat("x", 70*1024)}
|
||||
ts.expect(ts.do(http.MethodPost, "/api/v1/users", "", big, nil), http.StatusRequestEntityTooLarge, "payload_too_large")
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
package app_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/jpeg"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"messenger/internal/app"
|
||||
"messenger/internal/attachment"
|
||||
"messenger/internal/chat"
|
||||
"messenger/internal/testenv"
|
||||
)
|
||||
|
||||
func TestAttachmentUploadAuthenticatesBeforeParsing(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
ts.expect(ts.do(http.MethodPost, "/api/v1/chats/not-a-uuid/attachments?filename=x.png", "", "invalid image", map[string]string{"Content-Type": "image/png"}), http.StatusUnauthorized, "unauthorized")
|
||||
}
|
||||
|
||||
func TestAttachmentRoutesUseConfiguredDeadlines(t *testing.T) {
|
||||
for _, binary := range []bool{false, true} {
|
||||
name := "metadata"
|
||||
requestTimeout := 40 * time.Millisecond
|
||||
var binaryTimeout time.Duration
|
||||
if binary {
|
||||
name = "upload"
|
||||
requestTimeout = time.Second
|
||||
binaryTimeout = 40 * time.Millisecond
|
||||
}
|
||||
t.Run(name, func(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
actor := ts.user("deadline_user", "User")
|
||||
ts.a = app.New(app.Deps{
|
||||
Log: slog.New(slog.NewTextHandler(io.Discard, nil)), DB: db.Pool, Sessions: kv.Client,
|
||||
SessionTTL: time.Hour, MaxBodyBytes: 65536, InstanceID: testInstance,
|
||||
RequestTimeout: requestTimeout, BinaryRequestTimeout: binaryTimeout,
|
||||
})
|
||||
var held []*pgxpool.Conn
|
||||
for range db.Pool.Config().MaxConns {
|
||||
conn, err := db.Pool.Acquire(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
held = append(held, conn)
|
||||
}
|
||||
defer func() {
|
||||
for _, conn := range held {
|
||||
conn.Release()
|
||||
}
|
||||
}()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 400*time.Millisecond)
|
||||
defer cancel()
|
||||
path := "/api/v1/attachments/11111111-1111-4111-8111-111111111111"
|
||||
req := httptest.NewRequest("GET", path, nil).WithContext(ctx)
|
||||
if binary {
|
||||
path = "/api/v1/chats/11111111-1111-4111-8111-111111111111/attachments?filename=x.png"
|
||||
req = httptest.NewRequest("POST", path, strings.NewReader("body")).WithContext(ctx)
|
||||
req.Header.Set("Content-Type", "image/png")
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+actor.token)
|
||||
rec := httptest.NewRecorder()
|
||||
started := time.Now()
|
||||
ts.a.Engine.ServeHTTP(rec, req)
|
||||
if rec.Code != 503 || time.Since(started) > 200*time.Millisecond {
|
||||
t.Fatalf("route exceeded configured budget: status%d elapsed%v body%s", rec.Code, time.Since(started), rec.Body.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (ts *tester) binary(method, path, token, mime string, data []byte, length int64) response {
|
||||
ts.t.Helper()
|
||||
req := httptest.NewRequest(method, path, bytes.NewReader(data))
|
||||
req.ContentLength = length
|
||||
if token != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
}
|
||||
if mime != "" {
|
||||
req.Header.Set("Content-Type", mime)
|
||||
}
|
||||
rec := httptest.NewRecorder()
|
||||
ts.a.Engine.ServeHTTP(rec, req)
|
||||
res := response{status: rec.Code, requestID: rec.Header().Get("X-Request-Id"), challenge: rec.Header().Get("WWW-Authenticate"), raw: rec.Body.Bytes()}
|
||||
if res.requestID == "" || rec.Header().Get("X-Instance-Id") == "" {
|
||||
ts.t.Fatal("binary response missing correlation headers")
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func TestAttachmentRealS3RoundTripValidationAndOutage(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s3, err := testenv.StartS3(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer s3.Close()
|
||||
ts := newTester(t)
|
||||
ts.a = app.New(app.Deps{Log: slog.New(slog.NewTextHandler(io.Discard, nil)), DB: db.Pool, Sessions: kv.Client, SessionTTL: time.Hour, MaxBodyBytes: 65536, InstanceID: testInstance, Blobs: s3.APIStore})
|
||||
alice, bob, eve := ts.user("alice", "A"), ts.user("bob", "B"), ts.user("eve", "E")
|
||||
room := ts.chat(alice.token, "files", alice.ID, bob.ID)
|
||||
path := "/api/v1/chats/" + room.ID + "/attachments?filename=" + url.QueryEscape("фото.png")
|
||||
data, err := os.ReadFile("../../tests/fixtures/sample.png")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
res := ts.binary("POST", path, alice.token, "image/png", data, int64(len(data)))
|
||||
ts.expect(res, 201, "")
|
||||
a := decode[attachment.Response](t, res)
|
||||
digest := sha256.Sum256(data)
|
||||
if a.SHA256 != hex.EncodeToString(digest[:]) || a.SizeBytes != int64(len(data)) || a.Filename != "фото.png" || a.Status != "ready" || a.ErrorCode != nil || len(a.Variants) != 0 {
|
||||
t.Fatalf("attachment: %+v", a)
|
||||
}
|
||||
base := "/api/v1/attachments/" + a.ID
|
||||
for _, suffix := range []string{"", "/content"} {
|
||||
ts.expect(ts.do("GET", base+suffix, eve.token, nil, nil), 404, "not_found")
|
||||
}
|
||||
ts.expect(ts.do("GET", base, bob.token, nil, nil), 200, "")
|
||||
got := ts.binary("GET", base+"/content", bob.token, "", nil, 0)
|
||||
ts.expect(got, 200, "")
|
||||
if !bytes.Equal(got.raw, data) {
|
||||
t.Fatal("download bytes changed")
|
||||
}
|
||||
ts.expect(ts.do("GET", base+"/content?variant=thumbnail", bob.token, nil, nil), 400, "invalid_request")
|
||||
msg := ts.do("POST", "/api/v1/chats/"+room.ID+"/messages", bob.token, map[string]any{"text": "", "attachment_ids": []string{a.ID}}, freshKey())
|
||||
ts.expect(msg, 201, "")
|
||||
if m := decode[chat.MessageResponse](t, msg); len(m.AttachmentIDs) != 1 || m.AttachmentIDs[0] != a.ID {
|
||||
t.Fatal("lost attachment on message")
|
||||
}
|
||||
other := ts.chat(alice.token, "other", alice.ID)
|
||||
ts.expect(ts.do("POST", "/api/v1/chats/"+other.ID+"/messages", alice.token, map[string]any{"text": "", "attachment_ids": []string{a.ID}}, freshKey()), 404, "not_found")
|
||||
for _, tc := range []struct {
|
||||
name, mime string
|
||||
data []byte
|
||||
length int64
|
||||
want int
|
||||
}{
|
||||
{"MIME spoof", "image/jpeg", data, int64(len(data)), 415},
|
||||
{"broken body", "image/png", data[:len(data)/2], int64(len(data) / 2), 415},
|
||||
{"unsupported", "image/gif", data, int64(len(data)), 415},
|
||||
{"oversize", "image/png", nil, 10485761, 413},
|
||||
{"unknown length", "image/png", data, -1, 400},
|
||||
{"empty", "image/png", nil, 0, 400},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
r := ts.binary("POST", path, alice.token, tc.mime, tc.data, tc.length)
|
||||
if r.status != tc.want {
|
||||
t.Fatalf("got %d want %d: %s", r.status, tc.want, r.raw)
|
||||
}
|
||||
})
|
||||
}
|
||||
for _, name := range []string{"../x.png", `a\b.png`, "bad\nname", strings.Repeat("я", 256)} {
|
||||
ts.expect(ts.binary("POST", "/api/v1/chats/"+room.ID+"/attachments?filename="+url.QueryEscape(name), alice.token, "image/png", data, int64(len(data))), 400, "invalid_request")
|
||||
}
|
||||
var j bytes.Buffer
|
||||
if err := jpeg.Encode(&j, image.NewRGBA(image.Rect(0, 0, 5, 3)), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ts.expect(ts.binary("POST", path, bob.token, "image/jpeg", j.Bytes(), int64(j.Len())), 201, "")
|
||||
ts.expect(ts.binary("POST", path, eve.token, "image/png", data, int64(len(data))), 404, "not_found")
|
||||
// An interrupted read is never a 500 or a successfully published identity.
|
||||
before := 0
|
||||
if err := db.Pool.QueryRow(ctx, `SELECT count(*) FROM attachments`).Scan(&before); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
short := ts.binary("POST", path, alice.token, "image/png", data[:10], int64(len(data)))
|
||||
if short.status != 400 && short.status != 503 {
|
||||
t.Fatalf("interrupted body: %d %s", short.status, short.raw)
|
||||
}
|
||||
after := 0
|
||||
if err := db.Pool.QueryRow(ctx, `SELECT count(*) FROM attachments`).Scan(&after); err != nil || before != after {
|
||||
t.Fatalf("interruption published identity: %d -> %d (%v)", before, after, err)
|
||||
}
|
||||
if err := s3.Stop(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ts.expect(ts.binary("POST", path, alice.token, "image/png", data, int64(len(data))), 503, "unavailable")
|
||||
ts.expect(ts.do("GET", base+"/content", bob.token, nil, nil), 503, "unavailable")
|
||||
ts.expect(ts.do("GET", "/health/ready", "", nil, nil), 200, "")
|
||||
ts.message(alice.token, room.ID, fmt.Sprintf("text works with S3 down %d", time.Now().UnixNano()))
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
package app_test
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/internal/user"
|
||||
)
|
||||
|
||||
const (
|
||||
basicChallenge = `Basic realm="messenger", charset="UTF-8"`
|
||||
bearerChallenge = `Bearer realm="messenger"`
|
||||
)
|
||||
|
||||
func TestLoginReturnsBearerSession(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
alice := ts.user("alice", "Алиса")
|
||||
before := time.Now()
|
||||
|
||||
res := ts.do(http.MethodPost, "/api/v1/auth/sessions", "", nil, basic("alice", testPassword))
|
||||
ts.expect(res, http.StatusCreated, "")
|
||||
s := decode[sessionBody](t, res)
|
||||
|
||||
if s.TokenType != "Bearer" || len(s.Token) != 43 || s.Token == alice.token {
|
||||
t.Fatalf("session = %+v; want a fresh 43-character Bearer token", s)
|
||||
}
|
||||
if s.User != alice.Response {
|
||||
t.Fatalf("user = %+v, want %+v", s.User, alice.Response)
|
||||
}
|
||||
expires, err := time.Parse(time.RFC3339, s.ExpiresAt)
|
||||
if err != nil || !strings.HasSuffix(s.ExpiresAt, "Z") {
|
||||
t.Fatalf("expires_at = %q: %v", s.ExpiresAt, err)
|
||||
}
|
||||
if ttl := expires.Sub(before); ttl < 59*time.Minute || ttl > 61*time.Minute {
|
||||
t.Fatalf("expires_at is %v after login, want the 1h TTL", ttl)
|
||||
}
|
||||
if strings.Contains(string(res.raw), "password") || strings.Contains(string(res.raw), "argon2") {
|
||||
t.Fatalf("session response leaks password data: %s", res.raw)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoginFailuresAreUniformBasicChallenges(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
ts.user("alice", "Алиса")
|
||||
encoded := func(s string) string { return base64.StdEncoding.EncodeToString([]byte(s)) }
|
||||
cases := map[string]map[string]string{
|
||||
"wrong password": basic("alice", testPassword+"!"),
|
||||
"unknown user": basic("mallory", testPassword),
|
||||
"no header": nil,
|
||||
"bearer scheme": {"Authorization": "Bearer " + strings.Repeat("A", 43)},
|
||||
"not base64": {"Authorization": "Basic !!!"},
|
||||
"no colon": {"Authorization": "Basic " + encoded("alice")},
|
||||
"nul username": basic("a\x00b", testPassword),
|
||||
"non-utf-8 name": basic("\xff\xfe", testPassword),
|
||||
}
|
||||
messages := map[string]string{}
|
||||
for name, headers := range cases {
|
||||
res := ts.do(http.MethodPost, "/api/v1/auth/sessions", "", nil, headers)
|
||||
ts.expect(res, http.StatusUnauthorized, "unauthorized")
|
||||
if res.challenge != basicChallenge {
|
||||
t.Errorf("%s: WWW-Authenticate = %q, want %q", name, res.challenge, basicChallenge)
|
||||
}
|
||||
var eb errorBody
|
||||
_ = json.Unmarshal(res.raw, &eb)
|
||||
messages[name] = eb.Error.Message
|
||||
}
|
||||
if messages["wrong password"] != messages["unknown user"] {
|
||||
t.Fatalf("wrong password and unknown user must look the same: %q vs %q", messages["wrong password"], messages["unknown user"])
|
||||
}
|
||||
}
|
||||
|
||||
// The password is UTF-8 and may contain colons; only the first colon of the
|
||||
// Basic credentials separates the username (contracts/README.md).
|
||||
func TestLoginAcceptsColonAndUnicodePasswords(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
const password = "пароль:с:двоеточием 👋"
|
||||
body := map[string]string{"username": "alice", "display_name": "Алиса", "password": password}
|
||||
ts.expect(ts.do(http.MethodPost, "/api/v1/users", "", body, nil), http.StatusCreated, "")
|
||||
s := ts.login("alice", password)
|
||||
if s.User.Username != "alice" {
|
||||
t.Fatalf("logged in as %+v", s.User)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProtectedRoutesAcceptOnlyBearer(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
alice, eve := ts.user("alice", "A"), ts.user("eve", "E")
|
||||
me := func(headers map[string]string) response {
|
||||
return ts.do(http.MethodGet, "/api/v1/users/me", "", nil, headers)
|
||||
}
|
||||
rejected := map[string]map[string]string{
|
||||
"no header": nil,
|
||||
"basic with valid credentials": basic("alice", testPassword),
|
||||
"x-user-id alone": {"X-User-Id": alice.ID},
|
||||
"unknown token": {"Authorization": "Bearer " + strings.Repeat("A", 43)},
|
||||
"empty token": {"Authorization": "Bearer "},
|
||||
"scheme only": {"Authorization": "Bearer"},
|
||||
"other scheme": {"Authorization": "Token " + alice.token},
|
||||
"two tokens": {"Authorization": "Bearer " + alice.token + " " + eve.token},
|
||||
}
|
||||
for name, headers := range rejected {
|
||||
res := me(headers)
|
||||
ts.expect(res, http.StatusUnauthorized, "unauthorized")
|
||||
if res.challenge != bearerChallenge {
|
||||
t.Errorf("%s: WWW-Authenticate = %q, want %q", name, res.challenge, bearerChallenge)
|
||||
}
|
||||
}
|
||||
accepted := map[string]map[string]string{
|
||||
"lowercase scheme": {"Authorization": "bearer " + alice.token},
|
||||
"several spaces": {"Authorization": "Bearer " + alice.token},
|
||||
"x-user-id is ignored": {"Authorization": "Bearer " + alice.token, "X-User-Id": eve.ID},
|
||||
}
|
||||
for name, headers := range accepted {
|
||||
res := me(headers)
|
||||
ts.expect(res, http.StatusOK, "")
|
||||
if got := decode[user.Response](t, res); got.ID != alice.ID {
|
||||
t.Errorf("%s: caller = %s, want alice", name, got.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogoutRevokesOnlyTheCurrentToken(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
alice := ts.user("alice", "A")
|
||||
other := ts.login("alice", testPassword)
|
||||
|
||||
res := ts.do(http.MethodDelete, "/api/v1/auth/sessions/current", alice.token, nil, nil)
|
||||
ts.expect(res, http.StatusNoContent, "")
|
||||
if len(res.raw) != 0 {
|
||||
t.Fatalf("204 must have no body, got %q", res.raw)
|
||||
}
|
||||
|
||||
for _, again := range []response{
|
||||
ts.do(http.MethodGet, "/api/v1/users/me", alice.token, nil, nil),
|
||||
ts.do(http.MethodDelete, "/api/v1/auth/sessions/current", alice.token, nil, nil),
|
||||
} {
|
||||
ts.expect(again, http.StatusUnauthorized, "unauthorized")
|
||||
if again.challenge != bearerChallenge {
|
||||
t.Fatalf("revoked token: WWW-Authenticate = %q, want %q", again.challenge, bearerChallenge)
|
||||
}
|
||||
}
|
||||
ts.expect(ts.do(http.MethodGet, "/api/v1/users/me", other.Token, nil, nil), http.StatusOK, "")
|
||||
}
|
||||
|
||||
// Valkey enforces the expiry by the Docker VM clock, which may lag this host
|
||||
// by a fraction of a second, so the 401 is awaited with a tolerance.
|
||||
func TestSessionExpiresAfterTTL(t *testing.T) {
|
||||
const ttl, clockTolerance = 2 * time.Second, 3 * time.Second
|
||||
ts := newTesterWithTTL(t, ttl)
|
||||
alice := ts.user("alice", "A")
|
||||
ts.expect(ts.do(http.MethodGet, "/api/v1/users/me", alice.token, nil, nil), http.StatusOK, "")
|
||||
time.Sleep(ttl)
|
||||
deadline := time.Now().Add(clockTolerance)
|
||||
for {
|
||||
res := ts.do(http.MethodGet, "/api/v1/users/me", alice.token, nil, nil)
|
||||
if res.status == http.StatusUnauthorized {
|
||||
ts.expect(res, http.StatusUnauthorized, "unauthorized")
|
||||
return
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatalf("session still valid %v after its TTL of %v", clockTolerance, ttl)
|
||||
}
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistrationIsPublicAndIgnoresAuthorization(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
body := map[string]string{"username": "alice", "display_name": "A", "password": testPassword}
|
||||
headers := map[string]string{"Authorization": "Bearer garbage"}
|
||||
ts.expect(ts.do(http.MethodPost, "/api/v1/users", "", body, headers), http.StatusCreated, "")
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package app_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/internal/app"
|
||||
"messenger/internal/testenv"
|
||||
)
|
||||
|
||||
// newIsolatedTester assembles the application over containers of its own,
|
||||
// so a test can stop or pause them without disturbing the shared ones.
|
||||
func newIsolatedTester(t *testing.T, requestTimeout time.Duration) (*tester, *testenv.DB, *testenv.KV) {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
pg, err := testenv.StartPostgres(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(pg.Close)
|
||||
vk, err := testenv.StartValkey(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(vk.Close)
|
||||
log := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
a := app.New(app.Deps{
|
||||
Log: log,
|
||||
DB: pg.Pool,
|
||||
Sessions: vk.Client,
|
||||
SessionTTL: time.Hour,
|
||||
MaxBodyBytes: 64 * 1024,
|
||||
RequestTimeout: requestTimeout,
|
||||
InstanceID: testInstance,
|
||||
})
|
||||
return &tester{t: t, a: a}, pg, vk
|
||||
}
|
||||
|
||||
// Without the session store no caller can be authenticated: the API answers
|
||||
// 503, never 401 (the token is not at fault) and never as an anonymous user.
|
||||
// Registration does not need the store and keeps working.
|
||||
func TestValkeyOutageAnswers503(t *testing.T) {
|
||||
ts, _, vk := newIsolatedTester(t, 5*time.Second)
|
||||
alice := ts.user("alice", "Алиса")
|
||||
room := ts.chat(alice.token, "t", alice.ID)
|
||||
|
||||
if err := vk.Stop(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
ts.expect(ts.do(http.MethodGet, "/api/v1/users/me", alice.token, nil, nil), http.StatusServiceUnavailable, "unavailable")
|
||||
ts.expect(ts.do(http.MethodGet, "/api/v1/chats/"+room.ID, alice.token, nil, nil), http.StatusServiceUnavailable, "unavailable")
|
||||
ts.expect(ts.do(http.MethodPost, "/api/v1/auth/sessions", "", nil, basic("alice", testPassword)), http.StatusServiceUnavailable, "unavailable")
|
||||
ts.expect(ts.do(http.MethodDelete, "/api/v1/auth/sessions/current", alice.token, nil, nil), http.StatusServiceUnavailable, "unavailable")
|
||||
ts.expect(ts.do(http.MethodGet, "/health/ready", "", nil, nil), http.StatusServiceUnavailable, "unavailable")
|
||||
ts.expect(ts.do(http.MethodGet, "/health/live", "", nil, nil), http.StatusOK, "")
|
||||
|
||||
body := map[string]string{"username": "bob", "display_name": "Bob", "password": testPassword}
|
||||
ts.expect(ts.do(http.MethodPost, "/api/v1/users", "", body, nil), http.StatusCreated, "")
|
||||
}
|
||||
|
||||
func TestPostgresOutageAnswers503(t *testing.T) {
|
||||
ts, pg, _ := newIsolatedTester(t, 5*time.Second)
|
||||
alice := ts.user("alice", "Алиса")
|
||||
|
||||
if err := pg.Stop(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
ts.expect(ts.do(http.MethodGet, "/api/v1/chats", alice.token, nil, nil), http.StatusServiceUnavailable, "unavailable")
|
||||
ts.expect(ts.do(http.MethodGet, "/api/v1/users/me", alice.token, nil, nil), http.StatusServiceUnavailable, "unavailable")
|
||||
ts.expect(ts.do(http.MethodPost, "/api/v1/auth/sessions", "", nil, basic("alice", testPassword)), http.StatusServiceUnavailable, "unavailable")
|
||||
body := map[string]string{"username": "bob", "display_name": "Bob", "password": testPassword}
|
||||
ts.expect(ts.do(http.MethodPost, "/api/v1/users", "", body, nil), http.StatusServiceUnavailable, "unavailable")
|
||||
ts.expect(ts.do(http.MethodGet, "/health/ready", "", nil, nil), http.StatusServiceUnavailable, "unavailable")
|
||||
}
|
||||
|
||||
// A database that accepts connections but never answers must not hold the
|
||||
// request: the request deadline turns it into a 503.
|
||||
func TestHungPostgresAnswers503WithinTheRequestTimeout(t *testing.T) {
|
||||
ts, pg, _ := newIsolatedTester(t, 300*time.Millisecond)
|
||||
alice := ts.user("alice", "Алиса")
|
||||
|
||||
ctx := context.Background()
|
||||
if err := pg.Pause(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = pg.Unpause(ctx) })
|
||||
|
||||
for _, path := range []string{"/api/v1/chats", "/health/ready"} {
|
||||
start := time.Now()
|
||||
ts.expect(ts.do(http.MethodGet, path, alice.token, nil, nil), http.StatusServiceUnavailable, "unavailable")
|
||||
if elapsed := time.Since(start); elapsed > 2*time.Second {
|
||||
t.Fatalf("%s took %v with a hung database, want it bounded", path, elapsed)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
package app_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/internal/app"
|
||||
)
|
||||
|
||||
func (ts *tester) send(token, chatID, key string, body any) response {
|
||||
ts.t.Helper()
|
||||
return ts.do(http.MethodPost, "/api/v1/chats/"+chatID+"/messages", token, body, map[string]string{"Idempotency-Key": key})
|
||||
}
|
||||
|
||||
func TestSendIdempotencyKeyValidation(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
alice := ts.user("alice", "A")
|
||||
room := ts.chat(alice.token, "ключи", alice.ID)
|
||||
path := "/api/v1/chats/" + room.ID + "/messages"
|
||||
body := map[string]string{"text": "x"}
|
||||
|
||||
ts.expect(ts.do(http.MethodPost, path, alice.token, body, nil), http.StatusBadRequest, "invalid_request")
|
||||
for name, key := range map[string]string{
|
||||
"empty": "",
|
||||
"too long": strings.Repeat("a", 129),
|
||||
"inner space": "a b",
|
||||
"non-ascii": "ключ",
|
||||
"slash": "a/b",
|
||||
} {
|
||||
res := ts.send(alice.token, room.ID, key, body)
|
||||
if res.status != http.StatusBadRequest {
|
||||
t.Errorf("%s key: status %d, want 400", name, res.status)
|
||||
}
|
||||
}
|
||||
ts.expect(ts.send(alice.token, room.ID, strings.Repeat("a", 128), body), http.StatusCreated, "")
|
||||
ts.expect(ts.send(alice.token, room.ID, "Az09._:-", body), http.StatusCreated, "")
|
||||
|
||||
// Two Idempotency-Key headers are ambiguous: which one names the attempt?
|
||||
req := httptest.NewRequest(http.MethodPost, path, strings.NewReader(`{"text":"x"}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+alice.token)
|
||||
req.Header.Add("Idempotency-Key", "one")
|
||||
req.Header.Add("Idempotency-Key", "two")
|
||||
rec := httptest.NewRecorder()
|
||||
ts.a.Engine.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("two keys: status %d, want 400", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReplayReturnsIdenticalBody(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
alice := ts.user("alice", "A")
|
||||
room := ts.chat(alice.token, "повторы", alice.ID)
|
||||
first := ts.send(alice.token, room.ID, "retry-1", map[string]string{"text": "Привет 👋"})
|
||||
ts.expect(first, http.StatusCreated, "")
|
||||
// The same JSON with other spacing is the same request.
|
||||
again := ts.send(alice.token, room.ID, "retry-1", ` { "text" : "Привет 👋" } `)
|
||||
ts.expect(again, http.StatusCreated, "")
|
||||
if !bytes.Equal(first.raw, again.raw) {
|
||||
t.Fatalf("replay body differs:\n%s\n%s", first.raw, again.raw)
|
||||
}
|
||||
if page := ts.history(alice.token, room.ID, ""); len(page.Items) != 1 {
|
||||
t.Fatalf("history has %d messages, want 1", len(page.Items))
|
||||
}
|
||||
}
|
||||
|
||||
func TestReplayWithDifferentBodyIs409(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
alice := ts.user("alice", "A")
|
||||
room := ts.chat(alice.token, "конфликт", alice.ID)
|
||||
ts.expect(ts.send(alice.token, room.ID, "k", map[string]string{"text": "первое"}), http.StatusCreated, "")
|
||||
ts.expect(ts.send(alice.token, room.ID, "k", map[string]string{"text": "второе"}), http.StatusConflict, "conflict")
|
||||
}
|
||||
|
||||
// A replay is a new request: a revoked session cannot fetch the stored result.
|
||||
func TestReplayRechecksSession(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
alice := ts.user("alice", "A")
|
||||
room := ts.chat(alice.token, "сессия", alice.ID)
|
||||
ts.expect(ts.send(alice.token, room.ID, "k", map[string]string{"text": "x"}), http.StatusCreated, "")
|
||||
ts.expect(ts.do(http.MethodDelete, "/api/v1/auth/sessions/current", alice.token, nil, nil), http.StatusNoContent, "")
|
||||
ts.expect(ts.send(alice.token, room.ID, "k", map[string]string{"text": "x"}), http.StatusUnauthorized, "unauthorized")
|
||||
}
|
||||
|
||||
// The header is checked before access, like the JSON syntax: the answer does
|
||||
// not depend on the chat, so it reveals nothing about a foreign one.
|
||||
func TestOutsiderWithoutKeyGets400AndWithKey404(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
alice, eve := ts.user("alice", "A"), ts.user("eve", "E")
|
||||
room := ts.chat(alice.token, "чужой", alice.ID)
|
||||
path := "/api/v1/chats/" + room.ID + "/messages"
|
||||
ts.expect(ts.do(http.MethodPost, path, eve.token, map[string]string{"text": "x"}, nil), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(ts.send(eve.token, room.ID, "k", map[string]string{"text": "x"}), http.StatusNotFound, "not_found")
|
||||
}
|
||||
|
||||
// Two applications over the same stores stand for two replicas: retries of one
|
||||
// key alternate between them and still create one message.
|
||||
func TestConcurrentReplaysAcrossTwoAppsCreateOneMessage(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
alice := ts.user("alice", "A")
|
||||
room := ts.chat(alice.token, "реплики", alice.ID)
|
||||
other := app.New(app.Deps{
|
||||
Log: slog.New(slog.NewTextHandler(io.Discard, nil)),
|
||||
DB: db.Pool,
|
||||
Sessions: kv.Client,
|
||||
SessionTTL: time.Hour,
|
||||
MaxBodyBytes: 64 * 1024,
|
||||
InstanceID: "00000000000000b2",
|
||||
})
|
||||
engines := []http.Handler{ts.a.Engine, other.Engine}
|
||||
|
||||
const attempts = 10
|
||||
type result struct {
|
||||
status int
|
||||
body string
|
||||
instance string
|
||||
}
|
||||
results := make([]result, attempts)
|
||||
var wg sync.WaitGroup
|
||||
for i := range attempts {
|
||||
wg.Go(func() {
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/chats/"+room.ID+"/messages", strings.NewReader(`{"text":"один раз"}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+alice.token)
|
||||
req.Header.Set("Idempotency-Key", "shared")
|
||||
rec := httptest.NewRecorder()
|
||||
engines[i%2].ServeHTTP(rec, req)
|
||||
results[i] = result{rec.Code, rec.Body.String(), rec.Header().Get("X-Instance-Id")}
|
||||
})
|
||||
}
|
||||
wg.Wait()
|
||||
instances := map[string]bool{}
|
||||
for i, r := range results {
|
||||
if r.status != http.StatusCreated || r.body != results[0].body {
|
||||
t.Fatalf("attempt %d: %d %s; want 201 %s", i, r.status, r.body, results[0].body)
|
||||
}
|
||||
instances[r.instance] = true
|
||||
}
|
||||
if len(instances) != 2 {
|
||||
t.Fatalf("instances %v, want both replicas", instances)
|
||||
}
|
||||
if page := ts.history(alice.token, room.ID, ""); len(page.Items) != 1 {
|
||||
t.Fatalf("history has %d messages, want 1: %+v", len(page.Items), page.Items)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package attachment
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type FileStatus string
|
||||
|
||||
const (
|
||||
FilePending FileStatus = "pending"
|
||||
FileReady FileStatus = "ready"
|
||||
FileFailed FileStatus = "failed"
|
||||
)
|
||||
|
||||
type Attachment struct {
|
||||
ID, ChatID, OwnerID uuid.UUID
|
||||
Filename string
|
||||
FileHash []byte
|
||||
SizeBytes int64
|
||||
MIMEType string
|
||||
Status FileStatus
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type UploadInput struct {
|
||||
ChatID uuid.UUID
|
||||
Filename string
|
||||
MIMEType string
|
||||
SizeBytes int64
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package attachment
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ErrObjectMissing = errors.New("object missing")
|
||||
|
||||
type Object struct {
|
||||
Key string
|
||||
Size int64
|
||||
Modified time.Time
|
||||
}
|
||||
|
||||
type Blobs interface {
|
||||
Put(ctx context.Context, key string, body io.Reader, size int64, mimeType string) error
|
||||
Open(ctx context.Context, key string) (io.ReadCloser, error)
|
||||
Stat(ctx context.Context, key string) (Object, error)
|
||||
Copy(ctx context.Context, sourceKey, destinationKey string) error
|
||||
Delete(ctx context.Context, key string) error
|
||||
List(ctx context.Context, prefix, after string, limit int) ([]Object, error)
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
package attachment
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"io"
|
||||
"log/slog"
|
||||
"mime"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/httpx"
|
||||
"messenger/internal/platform/ident"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Handler struct{ s *Service }
|
||||
|
||||
func NewHandler(s *Service) *Handler { return &Handler{s} }
|
||||
func (h *Handler) Register(protected, binaryProtected gin.IRouter) {
|
||||
binaryProtected.POST("/chats/:chat_id/attachments", httpx.WithUser(h.upload))
|
||||
protected.GET("/attachments/:attachment_id", httpx.WithUser(h.metadata))
|
||||
binaryProtected.GET("/attachments/:attachment_id/content", httpx.WithUser(h.content))
|
||||
}
|
||||
|
||||
type Response 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"`
|
||||
// Lab4's shared response contract requires variants=[], even without processing.
|
||||
Variants []any `json:"variants"`
|
||||
ErrorCode *string `json:"error_code"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
|
||||
func toResponse(a Attachment) Response {
|
||||
return Response{
|
||||
ID: a.ID.String(),
|
||||
ChatID: a.ChatID.String(),
|
||||
OwnerID: a.OwnerID.String(),
|
||||
Filename: a.Filename,
|
||||
ContentType: a.MIMEType,
|
||||
SizeBytes: a.SizeBytes,
|
||||
SHA256: hex.EncodeToString(a.FileHash),
|
||||
Status: string(a.Status),
|
||||
Variants: []any{},
|
||||
CreatedAt: httpx.Timestamp(a.CreatedAt),
|
||||
UpdatedAt: httpx.Timestamp(a.CreatedAt),
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) upload(c *gin.Context, actor uuid.UUID) {
|
||||
id, err := ident.Parse(c.Param("chat_id"))
|
||||
if err != nil {
|
||||
httpx.Error(c, apperr.Invalid("chat_id must be a UUID"))
|
||||
return
|
||||
}
|
||||
names := c.Request.URL.Query()["filename"]
|
||||
if len(names) != 1 {
|
||||
httpx.Error(c, apperr.Invalid("one filename is required"))
|
||||
return
|
||||
}
|
||||
if err := ValidateFilename(names[0]); err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
media, params, err := mime.ParseMediaType(c.GetHeader("Content-Type"))
|
||||
if err != nil || len(params) > 0 {
|
||||
httpx.Error(c, apperr.UnsupportedMedia("Content-Type must be image/png or image/jpeg"))
|
||||
return
|
||||
}
|
||||
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, h.s.config.MaxBytes)
|
||||
input := UploadInput{
|
||||
ChatID: id,
|
||||
Filename: names[0],
|
||||
MIMEType: media,
|
||||
SizeBytes: c.Request.ContentLength,
|
||||
}
|
||||
a, err := h.s.Upload(c.Request.Context(), actor, input, c.Request.Body)
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
httpx.JSON(c, http.StatusCreated, toResponse(a))
|
||||
}
|
||||
func attachmentID(c *gin.Context) (uuid.UUID, error) {
|
||||
id, err := ident.Parse(c.Param("attachment_id"))
|
||||
if err != nil {
|
||||
return uuid.Nil, apperr.Invalid("attachment_id must be a UUID")
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
func (h *Handler) metadata(c *gin.Context, actor uuid.UUID) {
|
||||
id, err := attachmentID(c)
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
a, err := h.s.Get(c.Request.Context(), actor, id)
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
httpx.JSON(c, http.StatusOK, toResponse(a))
|
||||
}
|
||||
func (h *Handler) content(c *gin.Context, actor uuid.UUID) {
|
||||
id, err := attachmentID(c)
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
variant, present := c.GetQuery("variant")
|
||||
if present && variant != "original" {
|
||||
httpx.Error(c, apperr.Invalid("variant must be original"))
|
||||
return
|
||||
}
|
||||
a, r, err := h.s.Content(c.Request.Context(), actor, id)
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
if err := r.Close(); err != nil {
|
||||
slog.Warn("download reader close failed", "request_id", httpx.RequestID(c), "error", err)
|
||||
}
|
||||
}()
|
||||
c.Header("Content-Disposition", mime.FormatMediaType("attachment", map[string]string{"filename": a.Filename}))
|
||||
c.Header("X-Content-Type-Options", "nosniff")
|
||||
c.Header("Content-Type", a.MIMEType)
|
||||
c.Header("Content-Length", strconv.FormatInt(a.SizeBytes, 10))
|
||||
c.Status(http.StatusOK)
|
||||
if _, err := io.CopyN(c.Writer, r, a.SizeBytes); err != nil {
|
||||
slog.Error("download interrupted", "request_id", httpx.RequestID(c), "error", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package attachment
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
// LockHash serializes CAS publication and collection until the transaction ends.
|
||||
// A collision in the signed first eight digest bytes only adds serialization.
|
||||
func LockHash(ctx context.Context, tx pgx.Tx, hash []byte) error {
|
||||
if len(hash) != sha256.Size {
|
||||
return errors.New("attachment: lock requires a SHA256 hash")
|
||||
}
|
||||
_, err := tx.Exec(ctx, `SELECT pg_advisory_xact_lock($1)`, int64(binary.BigEndian.Uint64(hash[:8])))
|
||||
return err
|
||||
}
|
||||
|
||||
func BlobKey(hash []byte) string { return "blobs/" + hex.EncodeToString(hash) }
|
||||
@@ -0,0 +1,504 @@
|
||||
package attachment
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
const (
|
||||
reconcilePageSize = 500
|
||||
reconcileOperationTimeout = 30 * time.Second
|
||||
)
|
||||
|
||||
type Finding struct {
|
||||
Kind string `json:"kind"`
|
||||
Key string `json:"key"`
|
||||
Action string `json:"action"`
|
||||
Applied bool `json:"applied"`
|
||||
}
|
||||
|
||||
// SQLQueries counts SQL statements, including transaction control and locks.
|
||||
// S3Calls counts calls to the Blobs port, including each list page and deep GET.
|
||||
type Stats struct {
|
||||
ElapsedMS int64 `json:"elapsed_ms"`
|
||||
SQLQueries int64 `json:"sql_queries"`
|
||||
S3Calls int64 `json:"s3_calls"`
|
||||
ScannedFiles int64 `json:"scanned_files"`
|
||||
ScannedObjects int64 `json:"scanned_objects"`
|
||||
Findings int64 `json:"findings"`
|
||||
Repaired int64 `json:"repaired"`
|
||||
}
|
||||
|
||||
type Reconciler struct {
|
||||
db *pgxpool.Pool
|
||||
blobs Blobs
|
||||
grace time.Duration
|
||||
}
|
||||
|
||||
type ReconcileOptions struct {
|
||||
Apply bool
|
||||
Deep bool
|
||||
}
|
||||
|
||||
func NewReconciler(db *pgxpool.Pool, blobs Blobs, grace time.Duration) (*Reconciler, error) {
|
||||
if db == nil || blobs == nil {
|
||||
return nil, errors.New("reconcile: database and blobs are required")
|
||||
}
|
||||
if grace == 0 {
|
||||
grace = time.Hour
|
||||
}
|
||||
if grace < 2*time.Minute {
|
||||
return nil, errors.New("reconcile: grace must be at least two minutes")
|
||||
}
|
||||
return &Reconciler{db: db, blobs: blobs, grace: grace}, nil
|
||||
}
|
||||
|
||||
// Run streams findings and keeps at most one page of metadata in memory.
|
||||
// Dry runs only read storage. Each apply candidate is checked again under the
|
||||
// publisher's transaction lock; no scan snapshot authorizes a later mutation.
|
||||
func (r *Reconciler) Run(ctx context.Context, options ReconcileOptions, emit func(Finding)) (stats Stats, err error) {
|
||||
started := time.Now()
|
||||
defer func() { stats.ElapsedMS = time.Since(started).Milliseconds() }()
|
||||
run := reconcileRun{
|
||||
r: r,
|
||||
ctx: ctx,
|
||||
apply: options.Apply,
|
||||
deep: options.Deep,
|
||||
emit: emit,
|
||||
stats: &stats,
|
||||
cutoff: started.Add(-r.grace),
|
||||
}
|
||||
for _, prefix := range []string{"staging/", "blobs/"} {
|
||||
if err = run.objects(prefix); err != nil {
|
||||
return stats, err
|
||||
}
|
||||
}
|
||||
err = run.files()
|
||||
return stats, err
|
||||
}
|
||||
|
||||
type reconcileRun struct {
|
||||
r *Reconciler
|
||||
ctx context.Context
|
||||
apply, deep bool
|
||||
emit func(Finding)
|
||||
stats *Stats
|
||||
cutoff time.Time
|
||||
}
|
||||
|
||||
func (s *reconcileRun) note(f *Finding) {
|
||||
if f == nil {
|
||||
return
|
||||
}
|
||||
s.stats.Findings++
|
||||
if f.Applied {
|
||||
s.stats.Repaired++
|
||||
}
|
||||
if s.emit != nil {
|
||||
s.emit(*f)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *reconcileRun) old(modified time.Time) bool {
|
||||
// Unknown timestamps must never authorize collection.
|
||||
return !modified.IsZero() && modified.Before(s.cutoff)
|
||||
}
|
||||
|
||||
func (s *reconcileRun) objects(prefix string) error {
|
||||
after := ""
|
||||
for {
|
||||
if err := s.ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
s.stats.S3Calls++
|
||||
opCtx, cancel := context.WithTimeout(s.ctx, reconcileOperationTimeout)
|
||||
page, err := s.r.blobs.List(opCtx, prefix, after, reconcilePageSize)
|
||||
cancel()
|
||||
if err != nil {
|
||||
return fmt.Errorf("reconcile list %s: %w", prefix, err)
|
||||
}
|
||||
if len(page) == 0 {
|
||||
return nil
|
||||
}
|
||||
var known map[string]bool
|
||||
if prefix == "blobs/" {
|
||||
known, err = s.knownFiles(page)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, obj := range page {
|
||||
if obj.Key <= after {
|
||||
return errors.New("reconcile: object page did not advance")
|
||||
}
|
||||
after = obj.Key
|
||||
s.stats.ScannedObjects++
|
||||
if !s.old(obj.Modified) {
|
||||
continue
|
||||
}
|
||||
var finding *Finding
|
||||
if prefix == "staging/" {
|
||||
finding, err = s.staging(obj)
|
||||
} else {
|
||||
finding, err = s.orphan(obj, known[obj.Key])
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("reconcile %s: %w", obj.Key, err)
|
||||
}
|
||||
s.note(finding)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *reconcileRun) stat(key string) (Object, error) {
|
||||
s.stats.S3Calls++
|
||||
opCtx, cancel := context.WithTimeout(s.ctx, reconcileOperationTimeout)
|
||||
defer cancel()
|
||||
return s.r.blobs.Stat(opCtx, key)
|
||||
}
|
||||
|
||||
func (s *reconcileRun) delete(key string) error {
|
||||
s.stats.S3Calls++
|
||||
opCtx, cancel := context.WithTimeout(s.ctx, reconcileOperationTimeout)
|
||||
defer cancel()
|
||||
err := s.r.blobs.Delete(opCtx, key)
|
||||
if errors.Is(err, ErrObjectMissing) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *reconcileRun) staging(obj Object) (*Finding, error) {
|
||||
f := &Finding{Kind: "stale_staging", Key: obj.Key, Action: "delete_object"}
|
||||
if !s.apply {
|
||||
return f, nil
|
||||
}
|
||||
current, err := s.stat(obj.Key)
|
||||
if errors.Is(err, ErrObjectMissing) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !s.old(current.Modified) {
|
||||
return nil, nil
|
||||
}
|
||||
if err = s.delete(obj.Key); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
f.Applied = true
|
||||
return f, nil
|
||||
}
|
||||
|
||||
func (s *reconcileRun) knownFiles(page []Object) (map[string]bool, error) {
|
||||
hashes := make([][]byte, 0, len(page))
|
||||
for _, obj := range page {
|
||||
if !s.old(obj.Modified) {
|
||||
continue
|
||||
}
|
||||
hash, err := hex.DecodeString(obj.Key[len("blobs/"):])
|
||||
if err == nil && len(hash) == sha256.Size && BlobKey(hash) == obj.Key {
|
||||
hashes = append(hashes, hash)
|
||||
}
|
||||
}
|
||||
known := make(map[string]bool, len(hashes))
|
||||
if len(hashes) == 0 {
|
||||
return known, nil
|
||||
}
|
||||
s.stats.SQLQueries++
|
||||
rows, err := s.r.db.Query(s.ctx, `
|
||||
SELECT hash FROM files WHERE hash = ANY($1::bytea[])`, hashes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var hash []byte
|
||||
if err := rows.Scan(&hash); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
known[BlobKey(hash)] = true
|
||||
}
|
||||
return known, rows.Err()
|
||||
}
|
||||
|
||||
func (s *reconcileRun) orphan(obj Object, known bool) (*Finding, error) {
|
||||
hash, err := hex.DecodeString(obj.Key[len("blobs/"):])
|
||||
if err != nil || len(hash) != sha256.Size || BlobKey(hash) != obj.Key {
|
||||
return &Finding{Kind: "invalid_blob_key", Key: obj.Key, Action: "report"}, nil
|
||||
}
|
||||
if known {
|
||||
return nil, nil
|
||||
}
|
||||
f := &Finding{Kind: "orphan_blob", Key: obj.Key, Action: "delete_object"}
|
||||
if !s.apply {
|
||||
return f, nil
|
||||
}
|
||||
return s.locked(hash, func(opCtx context.Context, tx pgx.Tx) (*Finding, error) {
|
||||
op := *s
|
||||
op.ctx = opCtx
|
||||
var exists bool
|
||||
s.stats.SQLQueries++
|
||||
if err := tx.QueryRow(opCtx, `
|
||||
SELECT EXISTS (SELECT 1 FROM files WHERE hash = $1)`, hash).Scan(&exists); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if exists {
|
||||
return nil, nil
|
||||
}
|
||||
current, err := op.stat(obj.Key)
|
||||
if errors.Is(err, ErrObjectMissing) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !s.old(current.Modified) {
|
||||
return nil, nil
|
||||
}
|
||||
if err = op.delete(obj.Key); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
f.Applied = true
|
||||
return f, nil
|
||||
})
|
||||
}
|
||||
|
||||
// locked also covers hashes without a files row: a publisher and an orphan
|
||||
// collector therefore cannot pass each other's existence check concurrently.
|
||||
func (s *reconcileRun) locked(hash []byte, fn func(context.Context, pgx.Tx) (*Finding, error)) (*Finding, error) {
|
||||
opCtx, cancel := context.WithTimeout(s.ctx, reconcileOperationTimeout)
|
||||
defer cancel()
|
||||
s.stats.SQLQueries++
|
||||
tx, err := s.r.db.Begin(opCtx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
committed := false
|
||||
defer func() {
|
||||
if !committed {
|
||||
cleanup, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
s.stats.SQLQueries++
|
||||
_ = tx.Rollback(cleanup)
|
||||
}
|
||||
}()
|
||||
// Pool defaults cover ordinary requests; object I/O can outlive those
|
||||
// idle limits. The operation deadline bounds the entire locked candidate.
|
||||
s.stats.SQLQueries++
|
||||
if _, err = tx.Exec(opCtx, `SET LOCAL idle_in_transaction_session_timeout='35s'`); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.stats.SQLQueries++
|
||||
if err = LockHash(opCtx, tx, hash); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
f, err := fn(opCtx, tx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.stats.SQLQueries++
|
||||
err = tx.Commit(opCtx)
|
||||
committed = true // pgx closes the transaction even when COMMIT fails.
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
|
||||
type reconcileFile struct {
|
||||
hash []byte
|
||||
size int64
|
||||
status FileStatus
|
||||
created time.Time
|
||||
referenced bool
|
||||
}
|
||||
|
||||
func (s *reconcileRun) files() error {
|
||||
after := []byte{}
|
||||
for {
|
||||
if err := s.ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
s.stats.SQLQueries++
|
||||
rows, err := s.r.db.Query(s.ctx, `
|
||||
SELECT f.hash, f.size_bytes, f.status, f.created_at,
|
||||
EXISTS (SELECT 1 FROM attachments a WHERE a.file_hash = f.hash)
|
||||
FROM files f
|
||||
WHERE f.hash > $1
|
||||
ORDER BY f.hash
|
||||
LIMIT $2`, after, reconcilePageSize)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
page := make([]reconcileFile, 0, reconcilePageSize)
|
||||
for rows.Next() {
|
||||
var f reconcileFile
|
||||
if err = rows.Scan(&f.hash, &f.size, &f.status, &f.created, &f.referenced); err != nil {
|
||||
rows.Close()
|
||||
return err
|
||||
}
|
||||
page = append(page, f)
|
||||
}
|
||||
err = rows.Err()
|
||||
rows.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(page) == 0 {
|
||||
return nil
|
||||
}
|
||||
// Close the page's SQL rows before acquiring a transaction connection.
|
||||
for _, f := range page {
|
||||
after = f.hash
|
||||
s.stats.ScannedFiles++
|
||||
if !s.candidate(f) {
|
||||
continue
|
||||
}
|
||||
finding, err := s.inspect(f)
|
||||
if err == nil && s.apply && finding != nil && finding.Action != "report" {
|
||||
finding, err = s.repairFile(f.hash)
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("reconcile file %s: %w", BlobKey(f.hash), err)
|
||||
}
|
||||
s.note(finding)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *reconcileRun) repairFile(hash []byte) (*Finding, error) {
|
||||
return s.locked(hash, func(opCtx context.Context, tx pgx.Tx) (*Finding, error) {
|
||||
op := *s
|
||||
op.ctx = opCtx
|
||||
var current reconcileFile
|
||||
s.stats.SQLQueries++
|
||||
err := tx.QueryRow(opCtx, `
|
||||
SELECT f.hash, f.size_bytes, f.status, f.created_at,
|
||||
EXISTS (SELECT 1 FROM attachments a WHERE a.file_hash = f.hash)
|
||||
FROM files f
|
||||
WHERE f.hash = $1
|
||||
FOR UPDATE OF f`, hash).
|
||||
Scan(¤t.hash, ¤t.size, ¤t.status, ¤t.created, ¤t.referenced)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !op.candidate(current) {
|
||||
return nil, nil
|
||||
}
|
||||
finding, err := op.inspect(current)
|
||||
if err != nil || finding == nil {
|
||||
return finding, err
|
||||
}
|
||||
switch finding.Action {
|
||||
case "delete_file":
|
||||
// Delete bytes first. A failed SQL commit leaves an unreferenced
|
||||
// missing row that the next pass can safely finish collecting.
|
||||
if err := op.delete(finding.Key); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.stats.SQLQueries++
|
||||
_, err = tx.Exec(opCtx, `DELETE FROM files WHERE hash = $1`, hash)
|
||||
case "mark_failed":
|
||||
s.stats.SQLQueries++
|
||||
_, err = tx.Exec(opCtx, `UPDATE files SET status = $2 WHERE hash = $1`, hash, FileFailed)
|
||||
default:
|
||||
return finding, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
finding.Applied = true
|
||||
return finding, nil
|
||||
})
|
||||
}
|
||||
|
||||
func (s *reconcileRun) candidate(f reconcileFile) bool {
|
||||
if !s.old(f.created) {
|
||||
return false
|
||||
}
|
||||
if !f.referenced {
|
||||
return f.status == FilePending || f.status == FileReady
|
||||
}
|
||||
return f.status == FileReady || f.status == FileFailed
|
||||
}
|
||||
|
||||
func (s *reconcileRun) inspect(f reconcileFile) (*Finding, error) {
|
||||
key := BlobKey(f.hash)
|
||||
if !f.referenced {
|
||||
// A publication that rolled back can leave fresh bytes behind an old
|
||||
// pending row. Both the metadata and the current object must be old.
|
||||
obj, err := s.stat(key)
|
||||
if err != nil && !errors.Is(err, ErrObjectMissing) {
|
||||
return nil, err
|
||||
}
|
||||
if err == nil && !s.old(obj.Modified) {
|
||||
return nil, nil
|
||||
}
|
||||
return &Finding{Kind: "unreferenced_" + string(f.status), Key: key, Action: "delete_file"}, nil
|
||||
}
|
||||
obj, err := s.stat(key)
|
||||
if errors.Is(err, ErrObjectMissing) {
|
||||
if f.status == FileFailed {
|
||||
return nil, nil
|
||||
}
|
||||
return &Finding{Kind: "missing_blob", Key: key, Action: "mark_failed"}, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if obj.Size != f.size {
|
||||
return &Finding{Kind: "size_mismatch", Key: key, Action: "report"}, nil
|
||||
}
|
||||
if s.deep {
|
||||
match, err := s.verify(f)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !match {
|
||||
return &Finding{Kind: "hash_mismatch", Key: key, Action: "report"}, nil
|
||||
}
|
||||
}
|
||||
// A failed file stays failed even when the key reappears. Recovery requires
|
||||
// an explicit publisher; a shallow HEAD cannot prove content integrity.
|
||||
if f.status == FileFailed {
|
||||
return &Finding{Kind: "failed_present", Key: key, Action: "report"}, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *reconcileRun) verify(f reconcileFile) (bool, error) {
|
||||
if f.size < 0 || f.size == math.MaxInt64 {
|
||||
return false, errors.New("reconcile: invalid file size")
|
||||
}
|
||||
s.stats.S3Calls++
|
||||
opCtx, cancel := context.WithTimeout(s.ctx, reconcileOperationTimeout)
|
||||
defer cancel()
|
||||
body, err := s.r.blobs.Open(opCtx, BlobKey(f.hash))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
hash := sha256.New()
|
||||
n, readErr := io.Copy(hash, io.LimitReader(body, f.size+1))
|
||||
closeErr := body.Close()
|
||||
if readErr != nil {
|
||||
return false, readErr
|
||||
}
|
||||
if closeErr != nil {
|
||||
return false, closeErr
|
||||
}
|
||||
return n == f.size && bytes.Equal(hash.Sum(nil), f.hash), nil
|
||||
}
|
||||
@@ -0,0 +1,731 @@
|
||||
package attachment_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"messenger/internal/attachment"
|
||||
"messenger/internal/testenv"
|
||||
)
|
||||
|
||||
func TestReconcileBlobKey(t *testing.T) {
|
||||
hash, _ := hex.DecodeString("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
|
||||
if got := attachment.BlobKey(hash); got != "blobs/e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" {
|
||||
t.Fatalf("blob key: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
// Only object storage is doubled: SQL, transactions, FKs and advisory locks are real.
|
||||
type reconcileBlobs struct {
|
||||
mu sync.Mutex
|
||||
objects map[string]attachment.Object
|
||||
bodies map[string][]byte
|
||||
statErr, deleteErr, openErr error
|
||||
listed func(string)
|
||||
stating func(string)
|
||||
opening func(string) io.ReadCloser
|
||||
writes, readBytes int
|
||||
limits []int
|
||||
}
|
||||
|
||||
func newReconcileBlobs() *reconcileBlobs {
|
||||
return &reconcileBlobs{objects: make(map[string]attachment.Object), bodies: make(map[string][]byte)}
|
||||
}
|
||||
func (b *reconcileBlobs) add(key string, body []byte, modified time.Time) {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
b.objects[key] = attachment.Object{Key: key, Size: int64(len(body)), Modified: modified}
|
||||
b.bodies[key] = bytes.Clone(body)
|
||||
}
|
||||
func (b *reconcileBlobs) has(key string) bool {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
_, ok := b.objects[key]
|
||||
return ok
|
||||
}
|
||||
func (b *reconcileBlobs) Put(_ context.Context, key string, r io.Reader, size int64, _ string) error {
|
||||
body, err := io.ReadAll(io.LimitReader(r, size+1))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if int64(len(body)) != size {
|
||||
return io.ErrUnexpectedEOF
|
||||
}
|
||||
b.add(key, body, time.Now())
|
||||
b.mu.Lock()
|
||||
b.writes++
|
||||
b.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
func (b *reconcileBlobs) Copy(_ context.Context, source, dest string) error {
|
||||
b.mu.Lock()
|
||||
body, ok := b.bodies[source]
|
||||
b.mu.Unlock()
|
||||
if !ok {
|
||||
return attachment.ErrObjectMissing
|
||||
}
|
||||
b.add(dest, body, time.Now())
|
||||
b.mu.Lock()
|
||||
b.writes++
|
||||
b.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
func (b *reconcileBlobs) Open(_ context.Context, key string) (io.ReadCloser, error) {
|
||||
if b.openErr != nil {
|
||||
return nil, b.openErr
|
||||
}
|
||||
if b.opening != nil {
|
||||
return b.opening(key), nil
|
||||
}
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
body, ok := b.bodies[key]
|
||||
if !ok {
|
||||
return nil, attachment.ErrObjectMissing
|
||||
}
|
||||
return io.NopCloser(bytes.NewReader(body)), nil
|
||||
}
|
||||
func (b *reconcileBlobs) Stat(_ context.Context, key string) (attachment.Object, error) {
|
||||
if b.stating != nil {
|
||||
b.stating(key)
|
||||
}
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
if b.statErr != nil {
|
||||
return attachment.Object{}, b.statErr
|
||||
}
|
||||
obj, ok := b.objects[key]
|
||||
if !ok {
|
||||
return attachment.Object{}, fmt.Errorf("head: %w", attachment.ErrObjectMissing)
|
||||
}
|
||||
return obj, nil
|
||||
}
|
||||
func (b *reconcileBlobs) Delete(_ context.Context, key string) error {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
if b.deleteErr != nil {
|
||||
return b.deleteErr
|
||||
}
|
||||
delete(b.objects, key)
|
||||
delete(b.bodies, key)
|
||||
b.writes++
|
||||
return nil
|
||||
}
|
||||
func (b *reconcileBlobs) List(_ context.Context, prefix, after string, limit int) ([]attachment.Object, error) {
|
||||
b.mu.Lock()
|
||||
b.limits = append(b.limits, limit)
|
||||
if limit < 1 || limit > 500 {
|
||||
b.mu.Unlock()
|
||||
return nil, errors.New("unbounded page")
|
||||
}
|
||||
var page []attachment.Object
|
||||
for key, obj := range b.objects {
|
||||
if strings.HasPrefix(key, prefix) && key > after {
|
||||
page = append(page, obj)
|
||||
}
|
||||
}
|
||||
sort.Slice(page, func(i, j int) bool { return page[i].Key < page[j].Key })
|
||||
if len(page) > limit {
|
||||
page = page[:limit]
|
||||
}
|
||||
b.mu.Unlock()
|
||||
if b.listed != nil {
|
||||
b.listed(prefix)
|
||||
}
|
||||
return page, nil
|
||||
}
|
||||
|
||||
type reconcileFixture struct {
|
||||
t *testing.T
|
||||
db *pgxpool.Pool
|
||||
b *reconcileBlobs
|
||||
user, chat, message uuid.UUID
|
||||
}
|
||||
|
||||
func reconcileSetup(t *testing.T, db *testenv.DB) *reconcileFixture {
|
||||
t.Helper()
|
||||
db.Reset(t)
|
||||
f := &reconcileFixture{t: t, db: db.Pool, b: newReconcileBlobs(), user: uuid.New(), chat: uuid.New(), message: uuid.New()}
|
||||
f.exec(`INSERT INTO users(id,username,display_name,password_hash,created_at) VALUES($1,'reconcile','Reconcile','unused',now())`, f.user)
|
||||
f.exec(`INSERT INTO chats(id,title,created_at) VALUES($1,'reconcile',now())`, f.chat)
|
||||
f.exec(`INSERT INTO messages(chat_id,id,sender_id,text,created_at) VALUES($1,$2,$3,'preserve',now())`, f.chat, f.message, f.user)
|
||||
return f
|
||||
}
|
||||
func (f *reconcileFixture) exec(sql string, args ...any) {
|
||||
f.t.Helper()
|
||||
if _, err := f.db.Exec(context.Background(), sql, args...); err != nil {
|
||||
f.t.Fatal(err)
|
||||
}
|
||||
}
|
||||
func (f *reconcileFixture) file(body, status string, age time.Duration, referenced, present bool) []byte {
|
||||
f.t.Helper()
|
||||
hash := sha256.Sum256([]byte(body))
|
||||
f.exec(`INSERT INTO files(hash,size_bytes,mime_type,status,created_at) VALUES($1,$2,'image/png',$3,$4)`, hash[:], len(body), status, time.Now().Add(-age))
|
||||
if referenced {
|
||||
id := uuid.New()
|
||||
f.exec(`INSERT INTO attachments(id,chat_id,owner_id,filename,file_hash,created_at) VALUES($1,$2,$3,'image.png',$4,now())`, id, f.chat, f.user, hash[:])
|
||||
f.exec(`INSERT INTO message_attachments(chat_id,message_id,attachment_id,ordinal)
|
||||
VALUES($1,$2,$3,(SELECT count(*) FROM message_attachments WHERE chat_id=$1 AND message_id=$2))`, f.chat, f.message, id)
|
||||
}
|
||||
if present {
|
||||
f.b.add(attachment.BlobKey(hash[:]), []byte(body), time.Now().Add(-age))
|
||||
}
|
||||
return hash[:]
|
||||
}
|
||||
func (f *reconcileFixture) status(hash []byte) string {
|
||||
f.t.Helper()
|
||||
var s string
|
||||
err := f.db.QueryRow(context.Background(), `SELECT status FROM files WHERE hash=$1`, hash).Scan(&s)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ""
|
||||
}
|
||||
if err != nil {
|
||||
f.t.Fatal(err)
|
||||
}
|
||||
return s
|
||||
}
|
||||
func (f *reconcileFixture) run(apply, deep bool) (attachment.Stats, []attachment.Finding, error) {
|
||||
f.t.Helper()
|
||||
r, err := attachment.NewReconciler(f.db, f.b, 0)
|
||||
if err != nil {
|
||||
f.t.Fatal(err)
|
||||
}
|
||||
var found []attachment.Finding
|
||||
options := attachment.ReconcileOptions{Apply: apply, Deep: deep}
|
||||
stats, err := r.Run(context.Background(), options, func(v attachment.Finding) { found = append(found, v) })
|
||||
return stats, found, err
|
||||
}
|
||||
|
||||
func TestReconciler(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
|
||||
defer cancel()
|
||||
db, err := testenv.StartPostgres(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
t.Run("healthy apply does not block publication", func(t *testing.T) {
|
||||
f := reconcileSetup(t, db)
|
||||
hash := f.file("healthy", "ready", 2*time.Hour, true, true)
|
||||
tx, err := db.Pool.Begin(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
if err := attachment.LockHash(ctx, tx, hash); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r, err := attachment.NewReconciler(f.db, f.b, time.Hour)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
scanCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
||||
defer cancel()
|
||||
stats, err := r.Run(scanCtx, attachment.ReconcileOptions{Apply: true, Deep: true}, nil)
|
||||
if err != nil || stats.Findings != 0 || stats.Repaired != 0 {
|
||||
t.Fatalf("healthy inspection waited for publication: %+v %v", stats, err)
|
||||
}
|
||||
})
|
||||
t.Run("constructor validates and defaults grace", func(t *testing.T) {
|
||||
f := reconcileSetup(t, db)
|
||||
for _, grace := range []time.Duration{-time.Second, time.Minute} {
|
||||
if _, err := attachment.NewReconciler(f.db, f.b, grace); err == nil {
|
||||
t.Fatalf("accepted grace %v", grace)
|
||||
}
|
||||
}
|
||||
if _, err := attachment.NewReconciler(nil, f.b, 0); err == nil {
|
||||
t.Fatal("accepted nil DB")
|
||||
}
|
||||
if _, err := attachment.NewReconciler(f.db, nil, 0); err == nil {
|
||||
t.Fatal("accepted nil blobs")
|
||||
}
|
||||
if _, err := attachment.NewReconciler(f.db, f.b, 2*time.Minute); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fresh := f.file("half hour", "ready", 30*time.Minute, false, true)
|
||||
stats, _, err := f.run(true, false)
|
||||
if err != nil || stats.Repaired != 0 || f.status(fresh) != "ready" {
|
||||
t.Fatalf("default grace: %+v %v", stats, err)
|
||||
}
|
||||
})
|
||||
t.Run("dry run then apply then repeat preserves fresh referenced and message links", func(t *testing.T) {
|
||||
f := reconcileSetup(t, db)
|
||||
old := 2 * time.Hour
|
||||
pending := f.file("pending", "pending", old, false, true)
|
||||
unused := f.file("unused", "ready", old, false, true)
|
||||
used := f.file("used", "ready", old, true, true)
|
||||
fresh := f.file("fresh", "pending", time.Minute, false, true)
|
||||
missing := f.file("missing", "ready", old, true, false)
|
||||
f.b.add("staging/old", []byte("old"), time.Now().Add(-old))
|
||||
f.b.add("staging/fresh", []byte("new"), time.Now())
|
||||
orphan := sha256.Sum256([]byte("orphan"))
|
||||
orphanKey := attachment.BlobKey(orphan[:])
|
||||
f.b.add(orphanKey, []byte("orphan"), time.Now().Add(-old))
|
||||
stats, findings, err := f.run(false, false)
|
||||
if err != nil || stats.Repaired != 0 || stats.Findings != 5 || len(findings) != 5 {
|
||||
t.Fatalf("dry: %+v %v %+v", stats, err, findings)
|
||||
}
|
||||
if f.b.writes != 0 || f.status(pending) != "pending" || f.status(missing) != "ready" {
|
||||
t.Fatal("dry run mutated storage")
|
||||
}
|
||||
for _, finding := range findings {
|
||||
if finding.Applied {
|
||||
t.Fatal("dry finding marked applied")
|
||||
}
|
||||
}
|
||||
stats, findings, err = f.run(true, false)
|
||||
if err != nil || stats.Repaired != 5 {
|
||||
t.Fatalf("apply: %+v %v %+v", stats, err, findings)
|
||||
}
|
||||
if f.status(pending) != "" || f.status(unused) != "" || f.status(missing) != "failed" {
|
||||
t.Fatal("metadata repair incorrect")
|
||||
}
|
||||
if f.b.has(orphanKey) || f.b.has("staging/old") || !f.b.has("staging/fresh") || !f.b.has(attachment.BlobKey(used)) || !f.b.has(attachment.BlobKey(fresh)) {
|
||||
t.Fatal("incorrect object collection")
|
||||
}
|
||||
var links int
|
||||
if err := f.db.QueryRow(ctx, `SELECT count(*) FROM message_attachments`).Scan(&links); err != nil || links != 2 {
|
||||
t.Fatalf("message links lost: %d %v", links, err)
|
||||
}
|
||||
stats, _, err = f.run(true, false)
|
||||
if err != nil || stats.Repaired != 0 {
|
||||
t.Fatalf("repeat: %+v %v", stats, err)
|
||||
}
|
||||
})
|
||||
t.Run("outage is not missing and failed delete keeps row", func(t *testing.T) {
|
||||
f := reconcileSetup(t, db)
|
||||
used := f.file("used", "ready", 2*time.Hour, true, true)
|
||||
outage := errors.New("injected S3 outage")
|
||||
f.b.statErr = outage
|
||||
stats, _, err := f.run(true, false)
|
||||
if !errors.Is(err, outage) || stats.Repaired != 0 || f.status(used) != "ready" {
|
||||
t.Fatalf("outage: %+v %v", stats, err)
|
||||
}
|
||||
f.b.statErr = nil
|
||||
unused := f.file("unused", "pending", 2*time.Hour, false, true)
|
||||
f.b.deleteErr = outage
|
||||
_, _, err = f.run(true, false)
|
||||
if !errors.Is(err, outage) || f.status(unused) != "pending" {
|
||||
t.Fatalf("delete failure: %v", err)
|
||||
}
|
||||
})
|
||||
t.Run("deep reports corruption and never revives failed without verification", func(t *testing.T) {
|
||||
f := reconcileSetup(t, db)
|
||||
hash := f.file("good", "ready", 2*time.Hour, true, true)
|
||||
f.b.add(attachment.BlobKey(hash), []byte("evil"), time.Now().Add(-2*time.Hour))
|
||||
stats, found, err := f.run(true, true)
|
||||
if err != nil || stats.Findings != 1 || stats.Repaired != 0 || len(found) != 1 || found[0].Kind != "hash_mismatch" || f.status(hash) != "ready" || !f.b.has(attachment.BlobKey(hash)) {
|
||||
t.Fatalf("deep: %+v %v %+v", stats, err, found)
|
||||
}
|
||||
f.exec(`UPDATE files SET status='failed' WHERE hash=$1`, hash)
|
||||
stats, _, err = f.run(true, false)
|
||||
if err != nil || stats.Repaired != 0 || f.status(hash) != "failed" {
|
||||
t.Fatal("shallow revived failed file")
|
||||
}
|
||||
stats, _, err = f.run(true, true)
|
||||
if err != nil || stats.Repaired != 0 || f.status(hash) != "failed" {
|
||||
t.Fatal("corrupt file revived")
|
||||
}
|
||||
})
|
||||
t.Run("wrong size reports only and fresh missing stays ready", func(t *testing.T) {
|
||||
f := reconcileSetup(t, db)
|
||||
hash := f.file("sized", "ready", 2*time.Hour, true, true)
|
||||
f.b.add(attachment.BlobKey(hash), []byte("longer body"), time.Now().Add(-2*time.Hour))
|
||||
fresh := f.file("new missing", "ready", time.Minute, true, false)
|
||||
stats, found, err := f.run(true, false)
|
||||
if err != nil || stats.Repaired != 0 || stats.Findings != 1 || found[0].Kind != "size_mismatch" || f.status(fresh) != "ready" {
|
||||
t.Fatalf("size/fresh: %+v %v %+v", stats, err, found)
|
||||
}
|
||||
})
|
||||
t.Run("orphan rechecks current object age", func(t *testing.T) {
|
||||
f := reconcileSetup(t, db)
|
||||
hash := sha256.Sum256([]byte("orphan"))
|
||||
key := attachment.BlobKey(hash[:])
|
||||
f.b.add(key, []byte("orphan"), time.Now().Add(-2*time.Hour))
|
||||
f.b.listed = func(prefix string) {
|
||||
if prefix == "blobs/" {
|
||||
f.b.add(key, []byte("republished"), time.Now())
|
||||
}
|
||||
}
|
||||
stats, _, err := f.run(true, false)
|
||||
if err != nil || stats.Repaired != 0 || !f.b.has(key) {
|
||||
t.Fatalf("fresh replacement deleted: %+v %v", stats, err)
|
||||
}
|
||||
})
|
||||
t.Run("row rechecks references after taking lock", func(t *testing.T) {
|
||||
f := reconcileSetup(t, db)
|
||||
hash := f.file("race refs", "ready", 2*time.Hour, false, true)
|
||||
tx, err := f.db.Begin(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
if err = attachment.LockHash(ctx, tx, hash); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
r, _ := attachment.NewReconciler(f.db, f.b, 0)
|
||||
_, err := r.Run(ctx, attachment.ReconcileOptions{Apply: true}, nil)
|
||||
done <- err
|
||||
}()
|
||||
waitReconcileLock(t, ctx, f.db)
|
||||
_, err = tx.Exec(ctx, `INSERT INTO attachments(id,chat_id,owner_id,filename,file_hash,created_at) VALUES($1,$2,$3,'race.png',$4,now())`, uuid.New(), f.chat, f.user, hash)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = <-done; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if f.status(hash) != "ready" || !f.b.has(attachment.BlobKey(hash)) {
|
||||
t.Fatal("new reference collected")
|
||||
}
|
||||
})
|
||||
t.Run("concurrent upload lock prevents orphan collection", func(t *testing.T) {
|
||||
f := reconcileSetup(t, db)
|
||||
hash := sha256.Sum256([]byte("publishing"))
|
||||
key := attachment.BlobKey(hash[:])
|
||||
f.b.add(key, []byte("publishing"), time.Now().Add(-2*time.Hour))
|
||||
tx, err := f.db.Begin(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
if err = attachment.LockHash(ctx, tx, hash[:]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
r, _ := attachment.NewReconciler(f.db, f.b, 0)
|
||||
_, err := r.Run(ctx, attachment.ReconcileOptions{Apply: true}, nil)
|
||||
done <- err
|
||||
}()
|
||||
waitReconcileLock(t, ctx, f.db)
|
||||
if _, err = tx.Exec(ctx, `INSERT INTO files(hash,size_bytes,mime_type,status) VALUES($1,10,'image/png','ready')`, hash[:]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = <-done; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !f.b.has(key) || f.status(hash[:]) != "ready" {
|
||||
t.Fatal("published blob collected")
|
||||
}
|
||||
})
|
||||
t.Run("signed big endian lock key matches publisher", func(t *testing.T) {
|
||||
f := reconcileSetup(t, db)
|
||||
hash := make([]byte, 32)
|
||||
for i := 0; i < 8; i++ {
|
||||
hash[i] = 255
|
||||
}
|
||||
hash[7] = 254
|
||||
tx, err := f.db.Begin(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
if err = attachment.LockHash(ctx, tx, hash); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
other, err := f.db.Begin(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer other.Rollback(ctx)
|
||||
var acquired bool
|
||||
if err = other.QueryRow(ctx, `SELECT pg_try_advisory_xact_lock(-2::bigint)`).Scan(&acquired); err != nil || acquired {
|
||||
t.Fatalf("lock mismatch: %v %v", acquired, err)
|
||||
}
|
||||
})
|
||||
t.Run("keyset pages tolerate deletes and report costs", func(t *testing.T) {
|
||||
f := reconcileSetup(t, db)
|
||||
for i := 0; i < 1003; i++ {
|
||||
f.b.add(fmt.Sprintf("staging/%04d", i), []byte("x"), time.Now().Add(-2*time.Hour))
|
||||
}
|
||||
f.exec(`INSERT INTO files(hash,size_bytes,mime_type,status,created_at) SELECT decode(lpad(to_hex(i),64,'0'),'hex'),1,'image/png','pending',now()-interval '2 hour' FROM generate_series(1,1003) AS i`)
|
||||
stats, _, err := f.run(true, false)
|
||||
if err != nil || stats.ScannedFiles != 1003 || stats.ScannedObjects != 1003 || stats.Repaired != 2006 {
|
||||
t.Fatalf("pages: %+v %v", stats, err)
|
||||
}
|
||||
var n int
|
||||
if err := f.db.QueryRow(ctx, `SELECT count(*) FROM files`).Scan(&n); err != nil || n != 0 {
|
||||
t.Fatalf("skipped rows: %d %v", n, err)
|
||||
}
|
||||
if len(f.b.limits) < 4 || stats.SQLQueries == 0 || stats.S3Calls == 0 {
|
||||
t.Fatalf("missing paging/costs: %+v", stats)
|
||||
}
|
||||
raw, err := json.Marshal(stats)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, key := range []string{"elapsed_ms", "sql_queries", "s3_calls", "scanned_files", "scanned_objects", "findings", "repaired"} {
|
||||
if !bytes.Contains(raw, []byte(`"`+key+`":`)) {
|
||||
t.Fatalf("missing JSON key %s: %s", key, raw)
|
||||
}
|
||||
}
|
||||
})
|
||||
t.Run("old blob existence checks are batched by page", func(t *testing.T) {
|
||||
f := reconcileSetup(t, db)
|
||||
f.exec(`INSERT INTO files(hash,size_bytes,mime_type,status,created_at) SELECT decode(lpad(to_hex(i),64,'0'),'hex'),1,'image/png','ready',now()-interval '2 hour' FROM generate_series(1,1003) AS i`)
|
||||
f.exec(`INSERT INTO attachments(id,chat_id,owner_id,filename,file_hash,created_at) SELECT gen_random_uuid(),$1,$2,'batch.png',hash,now() FROM files`, f.chat, f.user)
|
||||
for i := 1; i <= 1003; i++ {
|
||||
f.b.add(fmt.Sprintf("blobs/%064x", i), []byte("x"), time.Now().Add(-2*time.Hour))
|
||||
}
|
||||
stats, _, err := f.run(false, false)
|
||||
if err != nil || stats.SQLQueries > 8 || stats.SQLQueries == 0 || stats.ScannedFiles != 1003 || stats.Findings != 0 {
|
||||
t.Fatalf("per-object SQL lookup: %+v %v", stats, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func waitReconcileLock(t *testing.T, ctx context.Context, db *pgxpool.Pool) {
|
||||
t.Helper()
|
||||
deadline := time.NewTimer(5 * time.Second)
|
||||
defer deadline.Stop()
|
||||
tick := time.NewTicker(10 * time.Millisecond)
|
||||
defer tick.Stop()
|
||||
for {
|
||||
var waiting bool
|
||||
if err := db.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM pg_locks WHERE locktype='advisory' AND NOT granted)`).Scan(&waiting); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if waiting {
|
||||
return
|
||||
}
|
||||
select {
|
||||
case <-deadline.C:
|
||||
t.Fatal("reconciler did not wait on publisher advisory lock")
|
||||
case <-tick.C:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type reconcileEndless struct {
|
||||
read int64
|
||||
closed bool
|
||||
}
|
||||
|
||||
func (r *reconcileEndless) Read(p []byte) (int, error) {
|
||||
for i := range p {
|
||||
p[i] = 'x'
|
||||
}
|
||||
r.read += int64(len(p))
|
||||
return len(p), nil
|
||||
}
|
||||
func (r *reconcileEndless) Close() error { r.closed = true; return nil }
|
||||
|
||||
func TestReconcilerFailureBoundaries(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
||||
defer cancel()
|
||||
db, err := testenv.StartPostgres(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
t.Run("slow object call survives pool idle transaction timeout", func(t *testing.T) {
|
||||
f := reconcileSetup(t, db)
|
||||
hash := f.file("slow object", "pending", 2*time.Hour, false, true)
|
||||
cfg := f.db.Config()
|
||||
cfg.ConnConfig.RuntimeParams["idle_in_transaction_session_timeout"] = "100ms"
|
||||
pool, err := pgxpool.NewWithConfig(ctx, cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer pool.Close()
|
||||
f.db = pool
|
||||
f.b.stating = func(string) { time.Sleep(250 * time.Millisecond) }
|
||||
stats, _, err := f.run(true, false)
|
||||
if err != nil || stats.Repaired != 1 || f.status(hash) != "" {
|
||||
t.Fatalf("slow S3 transaction rolled back: %+v %v", stats, err)
|
||||
}
|
||||
})
|
||||
t.Run("fresh object with old unused metadata is preserved", func(t *testing.T) {
|
||||
f := reconcileSetup(t, db)
|
||||
hash := f.file("republished", "pending", 2*time.Hour, false, true)
|
||||
f.b.add(attachment.BlobKey(hash), []byte("republished"), time.Now())
|
||||
stats, _, err := f.run(true, false)
|
||||
if err != nil || stats.Repaired != 0 || f.status(hash) != "pending" || !f.b.has(attachment.BlobKey(hash)) {
|
||||
t.Fatalf("fresh blob lost: %+v %v", stats, err)
|
||||
}
|
||||
})
|
||||
t.Run("deep read stops at size plus one and closes body", func(t *testing.T) {
|
||||
f := reconcileSetup(t, db)
|
||||
hash := f.file("good", "ready", 2*time.Hour, true, true)
|
||||
body := &reconcileEndless{}
|
||||
f.b.opening = func(string) io.ReadCloser { return body }
|
||||
stats, found, err := f.run(true, true)
|
||||
if err != nil || body.read != 5 || !body.closed || stats.Repaired != 0 || len(found) != 1 || found[0].Kind != "hash_mismatch" || f.status(hash) != "ready" {
|
||||
t.Fatalf("unbounded deep read: read=%d closed=%v %+v %v", body.read, body.closed, stats, err)
|
||||
}
|
||||
})
|
||||
t.Run("deep GET outage preserves referenced state", func(t *testing.T) {
|
||||
f := reconcileSetup(t, db)
|
||||
hash := f.file("good", "ready", 2*time.Hour, true, true)
|
||||
outage := errors.New("injected GET outage")
|
||||
f.b.openErr = outage
|
||||
stats, _, err := f.run(true, true)
|
||||
if !errors.Is(err, outage) || stats.Repaired != 0 || f.status(hash) != "ready" {
|
||||
t.Fatalf("GET outage: %+v %v", stats, err)
|
||||
}
|
||||
})
|
||||
t.Run("valid failed object remains stable even deep", func(t *testing.T) {
|
||||
f := reconcileSetup(t, db)
|
||||
hash := f.file("good", "failed", 2*time.Hour, true, true)
|
||||
for _, deep := range []bool{false, true} {
|
||||
stats, found, err := f.run(true, deep)
|
||||
if err != nil || stats.Repaired != 0 || len(found) != 1 || found[0].Kind != "failed_present" || f.status(hash) != "failed" {
|
||||
t.Fatalf("failed state unstable: %+v %v %+v", stats, err, found)
|
||||
}
|
||||
}
|
||||
})
|
||||
t.Run("dry run works with a read only PostgreSQL session", func(t *testing.T) {
|
||||
f := reconcileSetup(t, db)
|
||||
hash := f.file("missing", "ready", 2*time.Hour, true, false)
|
||||
cfg := f.db.Config()
|
||||
cfg.ConnConfig.RuntimeParams["default_transaction_read_only"] = "on"
|
||||
readOnly, err := pgxpool.NewWithConfig(ctx, cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer readOnly.Close()
|
||||
r, err := attachment.NewReconciler(readOnly, f.b, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stats, err := r.Run(ctx, attachment.ReconcileOptions{Deep: true}, nil)
|
||||
if err != nil || stats.Findings != 1 || stats.Repaired != 0 || f.status(hash) != "ready" {
|
||||
t.Fatalf("read only dry run: %+v %v", stats, err)
|
||||
}
|
||||
})
|
||||
t.Run("failed SQL commit after blob deletion is retryable", func(t *testing.T) {
|
||||
f := reconcileSetup(t, db)
|
||||
hash := f.file("unused", "pending", 2*time.Hour, false, true)
|
||||
f.exec(`CREATE FUNCTION reconcile_reject_delete() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'injected commit failure'; END $$`)
|
||||
f.exec(`CREATE CONSTRAINT TRIGGER reconcile_reject_delete AFTER DELETE ON files DEFERRABLE INITIALLY DEFERRED FOR EACH ROW EXECUTE FUNCTION reconcile_reject_delete()`)
|
||||
t.Cleanup(func() {
|
||||
f.exec(`DROP TRIGGER IF EXISTS reconcile_reject_delete ON files`)
|
||||
f.exec(`DROP FUNCTION IF EXISTS reconcile_reject_delete()`)
|
||||
})
|
||||
stats, _, err := f.run(true, false)
|
||||
if err == nil || stats.Repaired != 0 || f.status(hash) != "pending" || f.b.has(attachment.BlobKey(hash)) {
|
||||
t.Fatalf("failed commit: %+v %v", stats, err)
|
||||
}
|
||||
f.exec(`DROP TRIGGER reconcile_reject_delete ON files`)
|
||||
stats, _, err = f.run(true, false)
|
||||
if err != nil || stats.Repaired != 1 || f.status(hash) != "" {
|
||||
t.Fatalf("retry: %+v %v", stats, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestReconcilerS3(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
|
||||
defer cancel()
|
||||
db, err := testenv.StartPostgres(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
s3, err := testenv.StartS3(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer s3.Close()
|
||||
f := reconcileSetup(t, db)
|
||||
hash := f.file("good", "ready", 2*time.Hour, true, false)
|
||||
missing := f.file("missing", "ready", 2*time.Hour, true, false)
|
||||
key := attachment.BlobKey(hash)
|
||||
if err = s3.Store.Put(ctx, key, strings.NewReader("evil"), 4, "image/png"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = s3.Store.Put(ctx, "staging/fresh", strings.NewReader("x"), 1, "image/png"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r, err := attachment.NewReconciler(f.db, s3.Store, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Exercise the command with file-based configuration and the real listing
|
||||
// identity. Its default must remain read-only, including missing metadata.
|
||||
secretDir := t.TempDir()
|
||||
cliEnv := os.Environ()
|
||||
for name, value := range map[string]string{
|
||||
"DATABASE_URL_FILE": db.DSN,
|
||||
"S3_ACCESS_KEY_ID_FILE": s3.AccessKey,
|
||||
"S3_SECRET_ACCESS_KEY_FILE": s3.SecretKey,
|
||||
} {
|
||||
path := filepath.Join(secretDir, name)
|
||||
if err := os.WriteFile(path, []byte(value), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cliEnv = append(cliEnv, name+"="+path)
|
||||
}
|
||||
cliEnv = append(cliEnv, "S3_ENDPOINT="+s3.Endpoint, "S3_REGION="+s3.Region, "S3_BUCKET="+s3.Bucket)
|
||||
cmd := exec.CommandContext(ctx, "go", "run", "../../cmd/reconcile", "--deep")
|
||||
cmd.Env = cliEnv
|
||||
var stdout, stderr bytes.Buffer
|
||||
cmd.Stdout = &stdout
|
||||
cmd.Stderr = &stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
t.Fatalf("CLI dry run: %v: %s", err, stderr.String())
|
||||
}
|
||||
decoder := json.NewDecoder(&stdout)
|
||||
var cliStats attachment.Stats
|
||||
for i := 0; i < 2; i++ {
|
||||
var finding attachment.Finding
|
||||
if err := decoder.Decode(&finding); err != nil || finding.Applied || finding.Kind == "" || finding.Key == "" || finding.Action == "" {
|
||||
t.Fatalf("CLI finding: %+v %v", finding, err)
|
||||
}
|
||||
}
|
||||
if err := decoder.Decode(&cliStats); err != nil || cliStats.Findings != 2 || cliStats.Repaired != 0 || f.status(missing) != "ready" {
|
||||
t.Fatalf("CLI mutated by default: %+v %v", cliStats, err)
|
||||
}
|
||||
var found []attachment.Finding
|
||||
stats, err := r.Run(ctx, attachment.ReconcileOptions{Deep: true}, func(v attachment.Finding) { found = append(found, v) })
|
||||
if err != nil || stats.Repaired != 0 || stats.Findings != 2 || f.status(missing) != "ready" {
|
||||
t.Fatalf("S3 dry: %+v %v %+v", stats, err, found)
|
||||
}
|
||||
stats, err = r.Run(ctx, attachment.ReconcileOptions{Apply: true, Deep: true}, nil)
|
||||
if err != nil || stats.Repaired != 1 || f.status(missing) != "failed" || f.status(hash) != "ready" {
|
||||
t.Fatalf("S3 apply: %+v %v", stats, err)
|
||||
}
|
||||
if _, err = s3.Store.Stat(ctx, key); err != nil {
|
||||
t.Fatal("referenced corrupt object deleted", err)
|
||||
}
|
||||
if _, err = s3.Store.Stat(ctx, "staging/fresh"); err != nil {
|
||||
t.Fatal("fresh staging deleted", err)
|
||||
}
|
||||
stats, err = r.Run(ctx, attachment.ReconcileOptions{Apply: true, Deep: true}, nil)
|
||||
if err != nil || stats.Repaired != 0 {
|
||||
t.Fatalf("S3 repeat: %+v %v", stats, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReconcileLockRejectsMalformedHash(t *testing.T) {
|
||||
if err := attachment.LockHash(context.Background(), nil, []byte("bad")); err == nil {
|
||||
t.Fatal("malformed hash accepted")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package attachment
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type AttachmentRepository interface {
|
||||
// WithPublication holds the file's hash lock through the callback and commit.
|
||||
// Publication is valid only inside the callback. Any callback error rolls back.
|
||||
WithPublication(ctx context.Context, attachment Attachment, fn func(Publication) error) error
|
||||
GetForMember(ctx context.Context, attachmentID, actorID uuid.UUID) (Attachment, error)
|
||||
}
|
||||
|
||||
type Publication interface {
|
||||
FileReady() bool
|
||||
SaveReady(ctx context.Context) error
|
||||
}
|
||||
|
||||
type ChatMembership interface {
|
||||
IsMember(ctx context.Context, chatID, actorID uuid.UUID) (bool, error)
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package attachment_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"messenger/internal/testenv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFilesSchemaRejectsMalformedHashes(t *testing.T) {
|
||||
db, err := testenv.StartPostgres(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
_, err = db.Pool.Exec(context.Background(), `INSERT INTO files(hash,size_bytes,mime_type,status) VALUES (decode(repeat('ab',32),'hex'),12,'image/png','ready')`)
|
||||
if err != nil {
|
||||
t.Fatalf("valid content-addressed file: %v", err)
|
||||
}
|
||||
_, err = db.Pool.Exec(context.Background(), `INSERT INTO files(hash,size_bytes,mime_type,status) VALUES ('bad',12,'image/png','ready')`)
|
||||
if err == nil {
|
||||
t.Fatal("accepted a malformed SHA256")
|
||||
}
|
||||
_, err = db.Pool.Exec(context.Background(), `INSERT INTO files(hash,size_bytes,mime_type,status) VALUES (decode(repeat('ab',32),'hex'),12,'image/png','ready')`)
|
||||
if err == nil {
|
||||
t.Fatal("accepted duplicate content hash")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,230 @@
|
||||
package attachment
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"errors"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/ident"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
attachments AttachmentRepository
|
||||
chats ChatMembership
|
||||
blobs Blobs
|
||||
config Config
|
||||
decode chan struct{}
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
MaxBytes int64
|
||||
MaxPixels int64
|
||||
}
|
||||
|
||||
// A nil blobs dependency allows the text API to run without configured S3.
|
||||
// Binary operations report unavailability in that mode.
|
||||
func NewService(attachments AttachmentRepository, chats ChatMembership, blobs Blobs, cfg Config) *Service {
|
||||
if cfg.MaxBytes == 0 {
|
||||
cfg.MaxBytes = MaxUploadBytes
|
||||
}
|
||||
if cfg.MaxPixels == 0 {
|
||||
cfg.MaxPixels = MaxImagePixels
|
||||
}
|
||||
return &Service{
|
||||
attachments: attachments,
|
||||
chats: chats,
|
||||
blobs: blobs,
|
||||
config: cfg,
|
||||
decode: make(chan struct{}, 1),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) Upload(ctx context.Context, actor uuid.UUID, input UploadInput, body io.Reader) (Attachment, error) {
|
||||
if err := ValidateFilename(input.Filename); err != nil {
|
||||
return Attachment{}, err
|
||||
}
|
||||
if _, accepted := imageFormats[input.MIMEType]; !accepted {
|
||||
return Attachment{}, apperr.UnsupportedMedia("only image/png and image/jpeg are accepted")
|
||||
}
|
||||
ok, err := s.chats.IsMember(ctx, input.ChatID, actor)
|
||||
if err != nil {
|
||||
return Attachment{}, err
|
||||
}
|
||||
if !ok {
|
||||
return Attachment{}, apperr.NotFound("chat not found")
|
||||
}
|
||||
if input.SizeBytes > s.config.MaxBytes {
|
||||
return Attachment{}, apperr.PayloadTooLarge("file exceeds byte limit")
|
||||
}
|
||||
if input.SizeBytes <= 0 {
|
||||
return Attachment{}, apperr.Invalid("a nonempty body with Content-Length is required")
|
||||
}
|
||||
if s.blobs == nil {
|
||||
return Attachment{}, apperr.Unavailable("object storage is unavailable")
|
||||
}
|
||||
id := ident.New()
|
||||
stage := "staging/" + id.String()
|
||||
defer func() {
|
||||
cleanup, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
// A failed cleanup leaves a staging object for the reconciler's grace-based collection.
|
||||
if err := s.blobs.Delete(cleanup, stage); err != nil {
|
||||
slog.Warn("staging cleanup deferred", "key", stage, "error", err)
|
||||
}
|
||||
}()
|
||||
hash := sha256.New()
|
||||
counter := &countReader{r: io.LimitReader(body, s.config.MaxBytes+1)}
|
||||
err = s.blobs.Put(ctx, stage, io.TeeReader(counter, hash), input.SizeBytes, input.MIMEType)
|
||||
if err != nil {
|
||||
_, tooLarge := errors.AsType[*http.MaxBytesError](err)
|
||||
if counter.n > s.config.MaxBytes || tooLarge {
|
||||
return Attachment{}, apperr.PayloadTooLarge("file exceeds byte limit")
|
||||
}
|
||||
if errors.Is(err, io.ErrUnexpectedEOF) || errors.Is(err, io.EOF) {
|
||||
return Attachment{}, apperr.Invalid("incomplete upload")
|
||||
}
|
||||
return Attachment{}, unavailable(err)
|
||||
}
|
||||
var extra [1]byte
|
||||
n, readErr := counter.Read(extra[:])
|
||||
if counter.n > s.config.MaxBytes {
|
||||
return Attachment{}, apperr.PayloadTooLarge("file exceeds byte limit")
|
||||
}
|
||||
if counter.n != input.SizeBytes || n != 0 || !errors.Is(readErr, io.EOF) {
|
||||
return Attachment{}, apperr.Invalid("body length does not match Content-Length")
|
||||
}
|
||||
// Full decoding is bounded by dimensions and one decoder per replica. Body
|
||||
// transfer itself stays streaming and does not occupy the decoder slot.
|
||||
select {
|
||||
case s.decode <- struct{}{}:
|
||||
case <-ctx.Done():
|
||||
return Attachment{}, unavailable(ctx.Err())
|
||||
}
|
||||
err = s.validate(ctx, stage, input.MIMEType)
|
||||
<-s.decode
|
||||
if err != nil {
|
||||
return Attachment{}, err
|
||||
}
|
||||
a := Attachment{
|
||||
ID: id,
|
||||
ChatID: input.ChatID,
|
||||
OwnerID: actor,
|
||||
Filename: input.Filename,
|
||||
FileHash: hash.Sum(nil),
|
||||
SizeBytes: input.SizeBytes,
|
||||
MIMEType: input.MIMEType,
|
||||
Status: FileReady,
|
||||
CreatedAt: ident.TimeOf(id),
|
||||
}
|
||||
if err := s.attachments.WithPublication(ctx, a, func(p Publication) error {
|
||||
if err := s.publishBlob(ctx, a, stage, p.FileReady()); err != nil {
|
||||
return err
|
||||
}
|
||||
return p.SaveReady(ctx)
|
||||
}); err != nil {
|
||||
return Attachment{}, err
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
func (s *Service) publishBlob(ctx context.Context, a Attachment, stagingKey string, fileReady bool) error {
|
||||
blobKey := BlobKey(a.FileHash)
|
||||
if fileReady {
|
||||
obj, err := s.blobs.Stat(ctx, blobKey)
|
||||
if err == nil && obj.Size == a.SizeBytes {
|
||||
return nil
|
||||
}
|
||||
if err != nil && !errors.Is(err, ErrObjectMissing) {
|
||||
return unavailable(err)
|
||||
}
|
||||
}
|
||||
// The hash is known after streaming. Copy the validated staging object to
|
||||
// its permanent CAS key while the repository's publication lock is held.
|
||||
if err := s.blobs.Copy(ctx, stagingKey, blobKey); err != nil {
|
||||
return unavailable(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) validate(ctx context.Context, stage, mime string) (err error) {
|
||||
r, err := s.blobs.Open(ctx, stage)
|
||||
if err != nil {
|
||||
return unavailable(err)
|
||||
}
|
||||
defer func() {
|
||||
if closeErr := r.Close(); closeErr != nil && err == nil {
|
||||
err = unavailable(closeErr)
|
||||
}
|
||||
}()
|
||||
tracked := &storageReader{reader: r}
|
||||
err = ValidateImage(tracked, mime, s.config.MaxPixels)
|
||||
if tracked.err != nil {
|
||||
return unavailable(tracked.err)
|
||||
}
|
||||
if ctx.Err() != nil {
|
||||
return unavailable(ctx.Err())
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Image sniffing can replace a reader failure with ErrFormat. Preserve the
|
||||
// storage failure independently, while ordinary EOF still means malformed bytes.
|
||||
type storageReader struct {
|
||||
reader io.Reader
|
||||
err error
|
||||
}
|
||||
|
||||
func (r *storageReader) Read(p []byte) (int, error) {
|
||||
n, err := r.reader.Read(p)
|
||||
if err != nil && !errors.Is(err, io.EOF) {
|
||||
r.err = err
|
||||
}
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (s *Service) Get(ctx context.Context, actor, id uuid.UUID) (Attachment, error) {
|
||||
a, err := s.attachments.GetForMember(ctx, id, actor)
|
||||
if err != nil {
|
||||
return Attachment{}, err
|
||||
}
|
||||
if a.Status != FileReady {
|
||||
return Attachment{}, apperr.Conflict("attachment is not ready")
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
func (s *Service) Content(ctx context.Context, actor, id uuid.UUID) (Attachment, io.ReadCloser, error) {
|
||||
a, err := s.Get(ctx, actor, id)
|
||||
if err != nil {
|
||||
return Attachment{}, nil, err
|
||||
}
|
||||
if s.blobs == nil {
|
||||
return Attachment{}, nil, apperr.Unavailable("object storage is unavailable")
|
||||
}
|
||||
r, err := s.blobs.Open(ctx, BlobKey(a.FileHash))
|
||||
if err != nil {
|
||||
return Attachment{}, nil, unavailable(err)
|
||||
}
|
||||
return a, r, nil
|
||||
}
|
||||
|
||||
type countReader struct {
|
||||
r io.Reader
|
||||
n int64
|
||||
}
|
||||
|
||||
func (c *countReader) Read(b []byte) (int, error) {
|
||||
n, err := c.r.Read(b)
|
||||
c.n += int64(n)
|
||||
return n, err
|
||||
}
|
||||
|
||||
func unavailable(err error) error {
|
||||
return apperr.Wrap(apperr.KindUnavailable, "object storage is unavailable", err)
|
||||
}
|
||||
@@ -0,0 +1,276 @@
|
||||
package attachment_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"messenger/internal/attachment"
|
||||
"messenger/internal/attachment/store"
|
||||
chatstore "messenger/internal/chat/store"
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/ident"
|
||||
"messenger/internal/testenv"
|
||||
)
|
||||
|
||||
type testBlobs struct {
|
||||
mu sync.Mutex
|
||||
data map[string][]byte
|
||||
copies int
|
||||
failCopy bool
|
||||
}
|
||||
|
||||
type blockingPublication struct {
|
||||
*testBlobs
|
||||
entered, resume chan struct{}
|
||||
}
|
||||
|
||||
func (b *blockingPublication) Copy(ctx context.Context, from, to string) error {
|
||||
close(b.entered)
|
||||
select {
|
||||
case <-b.resume:
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
return b.testBlobs.Copy(ctx, from, to)
|
||||
}
|
||||
func (b *blockingPublication) Stat(ctx context.Context, key string) (attachment.Object, error) {
|
||||
o, e := b.testBlobs.Stat(ctx, key)
|
||||
o.Modified = time.Now().Add(-2 * time.Hour)
|
||||
return o, e
|
||||
}
|
||||
|
||||
func TestCollectorRechecksReferencesAfterConcurrentPublication(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
db, err := testenv.StartPostgres(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
actor, room := ident.New(), ident.New()
|
||||
if _, err = db.Pool.Exec(ctx, `INSERT INTO users VALUES($1,'race_user','User','hash',now())`, actor); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = db.Pool.Exec(ctx, `INSERT INTO chats VALUES($1,'race',now())`, room); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = db.Pool.Exec(ctx, `INSERT INTO chat_members VALUES($1,$2)`, room, actor); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data, err := os.ReadFile("../../tests/fixtures/sample.png")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
hash := sha256.Sum256(data)
|
||||
if _, err = db.Pool.Exec(ctx, `INSERT INTO files(hash,size_bytes,mime_type,status,created_at) VALUES($1,$2,'image/png','pending',now()-interval '2 hours')`, hash[:], len(data)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b := &blockingPublication{testBlobs: &testBlobs{data: map[string][]byte{}}, entered: make(chan struct{}), resume: make(chan struct{})}
|
||||
s := attachment.NewService(store.NewAttachments(db.Pool), chatstore.NewChats(db.Pool), b, attachment.Config{})
|
||||
uploaded := make(chan error, 1)
|
||||
go func() {
|
||||
input := attachment.UploadInput{ChatID: room, Filename: "race.png", MIMEType: "image/png", SizeBytes: int64(len(data))}
|
||||
_, e := s.Upload(ctx, actor, input, bytes.NewReader(data))
|
||||
uploaded <- e
|
||||
}()
|
||||
select {
|
||||
case <-b.entered:
|
||||
case <-ctx.Done():
|
||||
t.Fatal("publisher never reached copy")
|
||||
}
|
||||
r, err := attachment.NewReconciler(db.Pool, b, time.Hour)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
collected := make(chan error, 1)
|
||||
go func() {
|
||||
stats, e := r.Run(ctx, attachment.ReconcileOptions{Apply: true}, nil)
|
||||
if e == nil && stats.Repaired != 0 {
|
||||
e = errors.New("collector removed a newly referenced file")
|
||||
}
|
||||
collected <- e
|
||||
}()
|
||||
// Observe a real waiting advisory lock, rather than guessing which goroutine won.
|
||||
deadline := time.Now().Add(3 * time.Second)
|
||||
waiting := false
|
||||
for time.Now().Before(deadline) {
|
||||
var n int
|
||||
if err := db.Pool.QueryRow(ctx, `SELECT count(*) FROM pg_locks WHERE locktype='advisory' AND NOT granted`).Scan(&n); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n > 0 {
|
||||
waiting = true
|
||||
break
|
||||
}
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
}
|
||||
close(b.resume)
|
||||
if !waiting {
|
||||
t.Fatal("collector did not serialize with the publisher")
|
||||
}
|
||||
if err := <-uploaded; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := <-collected; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := b.Stat(ctx, attachment.BlobKey(hash[:])); err != nil {
|
||||
t.Fatal("collector deleted published data", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (b *testBlobs) Put(_ context.Context, k string, r io.Reader, n int64, m string) error {
|
||||
data, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
b.data[k] = data
|
||||
return nil
|
||||
}
|
||||
func (b *testBlobs) Open(_ context.Context, k string) (io.ReadCloser, error) {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
data, ok := b.data[k]
|
||||
if !ok {
|
||||
return nil, attachment.ErrObjectMissing
|
||||
}
|
||||
return io.NopCloser(bytes.NewReader(data)), nil
|
||||
}
|
||||
func (b *testBlobs) Stat(_ context.Context, k string) (attachment.Object, error) {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
data, ok := b.data[k]
|
||||
if !ok {
|
||||
return attachment.Object{}, attachment.ErrObjectMissing
|
||||
}
|
||||
return attachment.Object{Key: k, Size: int64(len(data)), Modified: time.Now()}, nil
|
||||
}
|
||||
func (b *testBlobs) Copy(_ context.Context, from, to string) error {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
if b.failCopy {
|
||||
return errors.New("copy outage")
|
||||
}
|
||||
b.copies++
|
||||
b.data[to] = bytes.Clone(b.data[from])
|
||||
return nil
|
||||
}
|
||||
func (b *testBlobs) Delete(_ context.Context, k string) error {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
delete(b.data, k)
|
||||
return nil
|
||||
}
|
||||
func (b *testBlobs) List(context.Context, string, string, int) ([]attachment.Object, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func TestUploadDeduplicatesContentButKeepsChatScopedIdentities(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
db, err := testenv.StartPostgres(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
actor, c1, c2 := ident.New(), ident.New(), ident.New()
|
||||
_, err = db.Pool.Exec(ctx, `INSERT INTO users VALUES($1,'alice','Alice','hash',now());`, actor)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, c := range []uuid.UUID{c1, c2} {
|
||||
_, err = db.Pool.Exec(ctx, `INSERT INTO chats VALUES($1,'room',now())`, c)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = db.Pool.Exec(ctx, `INSERT INTO chat_members VALUES($1,$2)`, c, actor)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
b := &testBlobs{data: map[string][]byte{}}
|
||||
s := attachment.NewService(store.NewAttachments(db.Pool), chatstore.NewChats(db.Pool), b, attachment.Config{})
|
||||
data, err := os.ReadFile("../../tests/fixtures/sample.png")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var results [10]attachment.Attachment
|
||||
var wg sync.WaitGroup
|
||||
for i := range results {
|
||||
wg.Go(func() {
|
||||
c := c1
|
||||
if i%2 == 1 {
|
||||
c = c2
|
||||
}
|
||||
var e error
|
||||
input := attachment.UploadInput{ChatID: c, Filename: "photo.png", MIMEType: "image/png", SizeBytes: int64(len(data))}
|
||||
results[i], e = s.Upload(ctx, actor, input, bytes.NewReader(data))
|
||||
if e != nil {
|
||||
t.Error(e)
|
||||
}
|
||||
})
|
||||
}
|
||||
wg.Wait()
|
||||
var files, atts int
|
||||
err = db.Pool.QueryRow(ctx, `SELECT (SELECT count(*) FROM files),(SELECT count(*) FROM attachments)`).Scan(&files, &atts)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if files != 1 || atts != 10 || b.copies != 1 || len(b.data) != 1 {
|
||||
t.Fatalf("files=%d attachments=%d copies=%d objects=%d", files, atts, b.copies, len(b.data))
|
||||
}
|
||||
if results[0].ID == results[1].ID {
|
||||
t.Fatal("deduplication merged chat ACL identity")
|
||||
}
|
||||
if _, err := s.Get(ctx, ident.New(), results[0].ID); apperr.KindOf(err) != apperr.KindNotFound {
|
||||
t.Fatalf("outsider got %v", err)
|
||||
}
|
||||
// A removed blob is republished from the new upload, preserving old references.
|
||||
b.mu.Lock()
|
||||
clear(b.data)
|
||||
b.mu.Unlock()
|
||||
input := attachment.UploadInput{ChatID: c1, Filename: "again.png", MIMEType: "image/png", SizeBytes: int64(len(data))}
|
||||
if _, err := s.Upload(ctx, actor, input, bytes.NewReader(data)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if b.copies != 2 {
|
||||
t.Fatalf("missing ready blob was not restored: copies=%d", b.copies)
|
||||
}
|
||||
b.failCopy = true
|
||||
other := bytes.Clone(data)
|
||||
other = append(other, 0)
|
||||
input.Filename, input.SizeBytes = "failed.png", int64(len(other))
|
||||
if _, err := s.Upload(ctx, actor, input, bytes.NewReader(other)); apperr.KindOf(err) != apperr.KindUnavailable {
|
||||
t.Fatalf("copy outage: %v", err)
|
||||
}
|
||||
if len(b.data) != 1 {
|
||||
t.Fatalf("failed publication left staging: %d objects", len(b.data))
|
||||
}
|
||||
b.failCopy = false
|
||||
// S3 publication succeeds, then PostgreSQL refuses the logical attachment.
|
||||
// Compensation may remove staging but must leave shared CAS data intact.
|
||||
_, err = db.Pool.Exec(ctx, `CREATE FUNCTION reject_attachment() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'injected insert failure'; END $$;
|
||||
CREATE TRIGGER reject_attachment BEFORE INSERT ON attachments FOR EACH ROW EXECUTE FUNCTION reject_attachment()`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
input.Filename = "rollback.png"
|
||||
if _, err := s.Upload(ctx, actor, input, bytes.NewReader(other)); err == nil {
|
||||
t.Fatal("publication survived rejected metadata")
|
||||
}
|
||||
if len(b.data) != 2 {
|
||||
t.Fatalf("expected original and recoverable orphan, got %d objects", len(b.data))
|
||||
}
|
||||
if err := db.Pool.QueryRow(ctx, `SELECT count(*) FROM files`).Scan(&files); err != nil || files != 1 {
|
||||
t.Fatalf("failed DB insert committed file metadata: %d %v", files, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"messenger/internal/attachment"
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/postgres"
|
||||
)
|
||||
|
||||
type Attachments struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
var _ attachment.AttachmentRepository = (*Attachments)(nil)
|
||||
|
||||
func NewAttachments(pool *pgxpool.Pool) *Attachments {
|
||||
return &Attachments{pool: pool}
|
||||
}
|
||||
|
||||
// Publication and garbage collection use the same hash lock. The transaction
|
||||
// spans only publication, never client transfer or image decoding.
|
||||
func (r *Attachments) WithPublication(ctx context.Context, a attachment.Attachment, fn func(attachment.Publication) error) error {
|
||||
return postgres.Classify(pgx.BeginFunc(ctx, r.pool, func(tx pgx.Tx) error {
|
||||
if _, err := tx.Exec(ctx, `SET LOCAL idle_in_transaction_session_timeout = '35s'`); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := attachment.LockHash(ctx, tx, a.FileHash); err != nil {
|
||||
return err
|
||||
}
|
||||
var status attachment.FileStatus
|
||||
err := tx.QueryRow(ctx, `SELECT status FROM files WHERE hash = $1 FOR UPDATE`, a.FileHash).Scan(&status)
|
||||
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
|
||||
return err
|
||||
}
|
||||
return fn(publication{tx: tx, attachment: a, ready: status == attachment.FileReady})
|
||||
}))
|
||||
}
|
||||
|
||||
type publication struct {
|
||||
tx pgx.Tx
|
||||
attachment attachment.Attachment
|
||||
ready bool
|
||||
}
|
||||
|
||||
func (p publication) FileReady() bool { return p.ready }
|
||||
|
||||
func (p publication) SaveReady(ctx context.Context) error {
|
||||
a := p.attachment
|
||||
_, err := p.tx.Exec(ctx, `
|
||||
INSERT INTO files (hash, size_bytes, mime_type, status)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (hash) DO UPDATE
|
||||
SET size_bytes = excluded.size_bytes,
|
||||
mime_type = excluded.mime_type,
|
||||
status = excluded.status`,
|
||||
a.FileHash, a.SizeBytes, a.MIMEType, attachment.FileReady)
|
||||
if err != nil {
|
||||
return fmt.Errorf("publish file: %w", err)
|
||||
}
|
||||
_, err = p.tx.Exec(ctx, `
|
||||
INSERT INTO attachments (id, chat_id, owner_id, filename, file_hash, created_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)`,
|
||||
a.ID, a.ChatID, a.OwnerID, a.Filename, a.FileHash, a.CreatedAt)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *Attachments) GetForMember(ctx context.Context, id, actor uuid.UUID) (attachment.Attachment, error) {
|
||||
var a attachment.Attachment
|
||||
err := r.pool.QueryRow(ctx, `
|
||||
SELECT a.id, a.chat_id, a.owner_id, a.filename, a.file_hash,
|
||||
f.size_bytes, f.mime_type, f.status, a.created_at
|
||||
FROM attachments a
|
||||
JOIN files f ON f.hash = a.file_hash
|
||||
WHERE a.id = $1 AND EXISTS (
|
||||
SELECT 1 FROM chat_members m
|
||||
WHERE m.chat_id = a.chat_id AND m.user_id = $2
|
||||
)`, id, actor).
|
||||
Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.FileHash,
|
||||
&a.SizeBytes, &a.MIMEType, &a.Status, &a.CreatedAt)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return attachment.Attachment{}, apperr.NotFound("attachment not found")
|
||||
}
|
||||
return a, postgres.Classify(err)
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package attachment
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"image"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"io"
|
||||
"strings"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
"messenger/internal/platform/apperr"
|
||||
)
|
||||
|
||||
const MaxUploadBytes int64 = 10 * 1024 * 1024
|
||||
const MaxImagePixels int64 = 40_000_000
|
||||
|
||||
var imageFormats = map[string]string{
|
||||
"image/png": "png",
|
||||
"image/jpeg": "jpeg",
|
||||
}
|
||||
|
||||
func ValidateFilename(name string) error {
|
||||
if !utf8.ValidString(name) || strings.TrimSpace(name) == "" || utf8.RuneCountInString(name) > 255 {
|
||||
return apperr.Invalid("filename must contain 1-255 characters")
|
||||
}
|
||||
for _, r := range name {
|
||||
if r == '/' || r == '\\' || unicode.IsControl(r) {
|
||||
return apperr.Invalid("filename contains a forbidden character")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// The small header is replayed into the decoder; pixels are allocated only
|
||||
// after checking dimensions. The caller serializes decoding per replica.
|
||||
func ValidateImage(r io.Reader, mime string, maxPixels int64) error {
|
||||
limited := io.LimitReader(r, MaxUploadBytes+1)
|
||||
var header bytes.Buffer
|
||||
cfg, format, err := image.DecodeConfig(io.TeeReader(limited, &header))
|
||||
if err != nil {
|
||||
return apperr.Wrap(apperr.KindUnsupportedMedia, "invalid image", err)
|
||||
}
|
||||
if expectedFormat, accepted := imageFormats[mime]; !accepted || format != expectedFormat {
|
||||
return apperr.UnsupportedMedia("image does not match Content-Type")
|
||||
}
|
||||
if cfg.Width <= 0 || cfg.Height <= 0 || int64(cfg.Width) > maxPixels/int64(cfg.Height) {
|
||||
return apperr.PayloadTooLarge("image exceeds pixel limit")
|
||||
}
|
||||
_, _, err = image.Decode(io.MultiReader(bytes.NewReader(header.Bytes()), limited))
|
||||
if err != nil {
|
||||
return apperr.Wrap(apperr.KindUnsupportedMedia, "invalid image", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
package attachment
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/png"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/platform/apperr"
|
||||
)
|
||||
|
||||
type chatMember struct {
|
||||
chatID, userID uuid.UUID
|
||||
}
|
||||
|
||||
func (m chatMember) IsMember(_ context.Context, chatID, userID uuid.UUID) (bool, error) {
|
||||
return chatID == m.chatID && userID == m.userID, nil
|
||||
}
|
||||
|
||||
func TestUploadWithoutStoragePreservesChatAccessCheck(t *testing.T) {
|
||||
member := chatMember{chatID: uuid.New(), userID: uuid.New()}
|
||||
s := NewService(nil, member, nil, Config{})
|
||||
input := UploadInput{ChatID: member.chatID, Filename: "photo.png", MIMEType: "image/png", SizeBytes: 4}
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
actor uuid.UUID
|
||||
kind apperr.Kind
|
||||
}{
|
||||
{name: "member", actor: member.userID, kind: apperr.KindUnavailable},
|
||||
{name: "outsider", actor: uuid.New(), kind: apperr.KindNotFound},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
_, err := s.Upload(context.Background(), tc.actor, input, strings.NewReader("data"))
|
||||
if apperr.KindOf(err) != tc.kind {
|
||||
t.Fatalf("got %v, want %v", err, tc.kind)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateFilename(t *testing.T) {
|
||||
for _, name := range []string{"", " ", "../a.png", `a\b.jpg`, "a\x00b", "a\nb", strings.Repeat("я", 256)} {
|
||||
if err := ValidateFilename(name); apperr.KindOf(err) != apperr.KindInvalid {
|
||||
t.Errorf("%q: %v", name, err)
|
||||
}
|
||||
}
|
||||
if err := ValidateFilename("фото 👋.png"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
type failingRead struct{ prefix []byte }
|
||||
|
||||
func (r *failingRead) Read(p []byte) (int, error) {
|
||||
if len(r.prefix) > 0 {
|
||||
n := copy(p, r.prefix)
|
||||
r.prefix = r.prefix[n:]
|
||||
return n, nil
|
||||
}
|
||||
return 0, errors.New("injected storage read outage")
|
||||
}
|
||||
|
||||
type openedBlob struct {
|
||||
Blobs
|
||||
reader io.Reader
|
||||
}
|
||||
|
||||
func (b openedBlob) Open(context.Context, string) (io.ReadCloser, error) {
|
||||
if reader, ok := b.reader.(io.ReadCloser); ok {
|
||||
return reader, nil
|
||||
}
|
||||
return io.NopCloser(b.reader), nil
|
||||
}
|
||||
|
||||
type closeErrorBody struct{ io.Reader }
|
||||
|
||||
func (closeErrorBody) Close() error { return errors.New("injected close failure") }
|
||||
|
||||
func TestStagingCloseFailurePreventsPublication(t *testing.T) {
|
||||
data, err := os.ReadFile("../../tests/fixtures/sample.png")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
blobs := openedBlob{reader: closeErrorBody{Reader: bytes.NewReader(data)}}
|
||||
s := NewService(nil, nil, blobs, Config{})
|
||||
if err := s.validate(context.Background(), "staging/test", "image/png"); apperr.KindOf(err) != apperr.KindUnavailable {
|
||||
t.Fatalf("close failure classified as %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStagingReadOutageIsUnavailableBeforeAndAfterHeader(t *testing.T) {
|
||||
var pngData bytes.Buffer
|
||||
if err := png.Encode(&pngData, image.NewRGBA(image.Rect(0, 0, 2, 2))); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, prefix := range [][]byte{nil, pngData.Bytes()[:33]} {
|
||||
s := NewService(nil, nil, openedBlob{reader: &failingRead{prefix: prefix}}, Config{})
|
||||
err := s.validate(context.Background(), "staging/test", "image/png")
|
||||
if apperr.KindOf(err) != apperr.KindUnavailable {
|
||||
t.Fatalf("storage read error classified as %v: %v", apperr.KindOf(err), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateImageRejectsCorruptionAndLimitsPixelsBeforeDecode(t *testing.T) {
|
||||
im := image.NewNRGBA(image.Rect(0, 0, 3, 2))
|
||||
im.Set(1, 1, color.NRGBA{R: 200, A: 255})
|
||||
var b bytes.Buffer
|
||||
if err := png.Encode(&b, im); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
name, mime string
|
||||
data []byte
|
||||
pixels int64
|
||||
kind apperr.Kind
|
||||
}{
|
||||
{"valid", "image/png", b.Bytes(), 6, apperr.KindInternal},
|
||||
{"spoof", "image/jpeg", b.Bytes(), 6, apperr.KindUnsupportedMedia},
|
||||
{"truncated", "image/png", b.Bytes()[:len(b.Bytes())-20], 6, apperr.KindUnsupportedMedia},
|
||||
{"pixels", "image/png", b.Bytes(), 5, apperr.KindPayloadTooLarge},
|
||||
{"garbage", "image/png", []byte("not a picture"), 6, apperr.KindUnsupportedMedia},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := ValidateImage(bytes.NewReader(tc.data), tc.mime, tc.pixels)
|
||||
if tc.name == "valid" {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err == nil || apperr.KindOf(err) != tc.kind {
|
||||
t.Fatalf("got %v, want kind %v", err, tc.kind)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/httpx"
|
||||
"messenger/internal/user"
|
||||
)
|
||||
|
||||
// Challenges sent with every 401: Basic on the login endpoint, Bearer on
|
||||
// every protected one.
|
||||
const (
|
||||
basicChallenge = `Basic realm="messenger", charset="UTF-8"`
|
||||
bearerChallenge = `Bearer realm="messenger"`
|
||||
)
|
||||
|
||||
var errCredentialsRequired = apperr.Unauthorized("basic credentials are required")
|
||||
|
||||
type Handler struct {
|
||||
sessions *Service
|
||||
}
|
||||
|
||||
func NewHandler(sessions *Service) *Handler {
|
||||
return &Handler{sessions: sessions}
|
||||
}
|
||||
|
||||
// Register mounts login on the public router and logout on the router
|
||||
// guarded by RequireSession.
|
||||
func (h *Handler) Register(public, protected gin.IRouter) {
|
||||
public.POST("/auth/sessions", h.login)
|
||||
protected.DELETE("/auth/sessions/current", h.logout)
|
||||
}
|
||||
|
||||
type sessionResponse struct {
|
||||
Token string `json:"token"`
|
||||
TokenType string `json:"token_type"`
|
||||
ExpiresAt string `json:"expires_at"`
|
||||
User user.Response `json:"user"`
|
||||
}
|
||||
|
||||
// login accepts only the Authorization header; the body is not read.
|
||||
// BasicAuth splits at the first colon, so a password may contain colons.
|
||||
func (h *Handler) login(c *gin.Context) {
|
||||
username, password, ok := c.Request.BasicAuth()
|
||||
if !ok {
|
||||
unauthorized(c, basicChallenge, errCredentialsRequired)
|
||||
return
|
||||
}
|
||||
issued, err := h.sessions.Login(c.Request.Context(), username, password)
|
||||
if err != nil {
|
||||
unauthorized(c, basicChallenge, err)
|
||||
return
|
||||
}
|
||||
httpx.JSON(c, http.StatusCreated, sessionResponse{
|
||||
Token: issued.Token,
|
||||
TokenType: "Bearer",
|
||||
ExpiresAt: httpx.Timestamp(issued.ExpiresAt),
|
||||
User: user.ToResponse(issued.User),
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handler) logout(c *gin.Context) {
|
||||
token, ok := c.Get(keyToken)
|
||||
if !ok {
|
||||
httpx.Error(c, errors.New("logout route is missing the RequireSession middleware"))
|
||||
return
|
||||
}
|
||||
if err := h.sessions.Logout(c.Request.Context(), token.(string)); err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
// Status alone is buffered by gin; writing the header now sends the 204
|
||||
// with an empty body.
|
||||
c.Status(http.StatusNoContent)
|
||||
c.Writer.WriteHeaderNow()
|
||||
}
|
||||
|
||||
// unauthorized adds the challenge only to a 401: an unavailable store is a
|
||||
// 503 and must not invite the client to retry its credentials.
|
||||
func unauthorized(c *gin.Context, challenge string, err error) {
|
||||
if apperr.KindOf(err) == apperr.KindUnauthorized {
|
||||
c.Header("WWW-Authenticate", challenge)
|
||||
}
|
||||
httpx.Error(c, err)
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package auth_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"messenger/internal/testenv"
|
||||
)
|
||||
|
||||
var (
|
||||
db *testenv.DB
|
||||
kv *testenv.KV
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
ctx := context.Background()
|
||||
var err error
|
||||
if db, err = testenv.StartPostgres(ctx); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if kv, err = testenv.StartValkey(ctx); err != nil {
|
||||
db.Close()
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
code := m.Run()
|
||||
kv.Close()
|
||||
db.Close()
|
||||
os.Exit(code)
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"messenger/internal/platform/httpx"
|
||||
)
|
||||
|
||||
const keyToken = "messenger.session_token"
|
||||
|
||||
// RequireSession admits a request only with Authorization: Bearer <token> of
|
||||
// a live session and records its owner for the handlers behind it. Any other
|
||||
// identification, including Basic credentials and X-User-Id, is ignored, so
|
||||
// it can neither grant access nor change the caller.
|
||||
func RequireSession(sessions *Service) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
token, ok := bearerToken(c.GetHeader("Authorization"))
|
||||
if !ok {
|
||||
unauthorized(c, bearerChallenge, ErrUnauthenticated)
|
||||
return
|
||||
}
|
||||
userID, err := sessions.Authenticate(c.Request.Context(), token)
|
||||
if err != nil {
|
||||
unauthorized(c, bearerChallenge, err)
|
||||
return
|
||||
}
|
||||
httpx.SetUserID(c, userID)
|
||||
c.Set(keyToken, token)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// bearerToken parses "Bearer <token>": the scheme is case-insensitive and
|
||||
// may be followed by several spaces (RFC 7235); the token itself never
|
||||
// contains whitespace.
|
||||
func bearerToken(header string) (string, bool) {
|
||||
scheme, rest, found := strings.Cut(header, " ")
|
||||
if !found || !strings.EqualFold(scheme, "Bearer") {
|
||||
return "", false
|
||||
}
|
||||
token := strings.TrimLeft(rest, " ")
|
||||
if token == "" || strings.ContainsAny(token, " \t") {
|
||||
return "", false
|
||||
}
|
||||
return token, true
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
// Create stores the session until its absolute ExpiresAt.
|
||||
Create(ctx context.Context, s Session) error
|
||||
// UserID returns the owner of a live session, or ErrUnauthenticated when
|
||||
// the token is unknown, expired or revoked: the store cannot tell these
|
||||
// apart, and the client must not either.
|
||||
UserID(ctx context.Context, tokenHash string) (uuid.UUID, error)
|
||||
// Delete revokes the session; deleting a missing one is not an error.
|
||||
Delete(ctx context.Context, tokenHash string) error
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/user"
|
||||
)
|
||||
|
||||
var ErrUnauthenticated = apperr.Unauthorized("a valid bearer token is required")
|
||||
|
||||
// Credentials is what this module needs from the users module: the user
|
||||
// whose password matches, or user.ErrInvalidCredentials.
|
||||
type Credentials interface {
|
||||
Authenticate(ctx context.Context, username, password string) (user.User, error)
|
||||
}
|
||||
|
||||
// Issued is a new session as the client sees it; Token is shown only here.
|
||||
type Issued struct {
|
||||
Token string
|
||||
ExpiresAt time.Time
|
||||
User user.User
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
users Credentials
|
||||
sessions Repository
|
||||
ttl time.Duration
|
||||
}
|
||||
|
||||
func NewService(users Credentials, sessions Repository, ttl time.Duration) *Service {
|
||||
return &Service{users: users, sessions: sessions, ttl: ttl}
|
||||
}
|
||||
|
||||
// Login checks the credentials first, so a store outage never hands out a
|
||||
// token without a stored session. The expiry is fixed here and never
|
||||
// extended; it is truncated to milliseconds, the precision of both the API
|
||||
// timestamps and the store.
|
||||
func (s *Service) Login(ctx context.Context, username, password string) (Issued, error) {
|
||||
u, err := s.users.Authenticate(ctx, username, password)
|
||||
if err != nil {
|
||||
return Issued{}, err
|
||||
}
|
||||
token := newToken()
|
||||
expiresAt := time.Now().Add(s.ttl).Truncate(time.Millisecond).UTC()
|
||||
if err := s.sessions.Create(ctx, Session{TokenHash: HashToken(token), UserID: u.ID, ExpiresAt: expiresAt}); err != nil {
|
||||
return Issued{}, err
|
||||
}
|
||||
return Issued{Token: token, ExpiresAt: expiresAt, User: u}, nil
|
||||
}
|
||||
|
||||
// Authenticate returns the owner of the token's live session or
|
||||
// ErrUnauthenticated. A store failure stays an unavailability, never
|
||||
// ErrUnauthenticated: an outage must not look like a bad token to the client.
|
||||
func (s *Service) Authenticate(ctx context.Context, token string) (uuid.UUID, error) {
|
||||
return s.sessions.UserID(ctx, HashToken(token))
|
||||
}
|
||||
|
||||
func (s *Service) Logout(ctx context.Context, token string) error {
|
||||
return s.sessions.Delete(ctx, HashToken(token))
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
package auth_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/internal/auth"
|
||||
authstore "messenger/internal/auth/store"
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/user"
|
||||
userstore "messenger/internal/user/store"
|
||||
)
|
||||
|
||||
const pw = "correct-horse-battery"
|
||||
|
||||
type fixture struct {
|
||||
ctx context.Context
|
||||
svc *auth.Service
|
||||
alice user.User
|
||||
}
|
||||
|
||||
// newFixture wires the real users service and session store over empty
|
||||
// databases and registers alice.
|
||||
func newFixture(t *testing.T, ttl time.Duration) fixture {
|
||||
t.Helper()
|
||||
db.Reset(t)
|
||||
kv.Reset(t)
|
||||
ctx := context.Background()
|
||||
users := user.NewService(userstore.NewUsers(db.Pool))
|
||||
alice, err := users.Register(ctx, "alice", "Алиса", pw)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return fixture{ctx: ctx, svc: auth.NewService(users, authstore.NewSessions(kv.Client), ttl), alice: alice}
|
||||
}
|
||||
|
||||
func TestLoginIssuesOpaqueTokenBackedByStoredSession(t *testing.T) {
|
||||
f := newFixture(t, time.Hour)
|
||||
before := time.Now()
|
||||
issued, err := f.svc.Login(f.ctx, "alice", pw)
|
||||
after := time.Now()
|
||||
if err != nil {
|
||||
t.Fatalf("Login: %v", err)
|
||||
}
|
||||
raw, err := base64.RawURLEncoding.DecodeString(issued.Token)
|
||||
if err != nil || len(raw) != 32 {
|
||||
t.Fatalf("token %q must be 32 random bytes in base64url, got %d bytes, %v", issued.Token, len(raw), err)
|
||||
}
|
||||
if strings.Contains(issued.Token, f.alice.ID.String()) || strings.Contains(issued.Token, "alice") {
|
||||
t.Fatal("token must not carry the identity")
|
||||
}
|
||||
if issued.User != f.alice {
|
||||
t.Fatalf("User = %+v, want alice", issued.User)
|
||||
}
|
||||
if issued.ExpiresAt.Before(before.Add(time.Hour).Add(-time.Millisecond)) || issued.ExpiresAt.After(after.Add(time.Hour)) {
|
||||
t.Fatalf("ExpiresAt = %v, want login time + 1h", issued.ExpiresAt)
|
||||
}
|
||||
|
||||
key := "session:" + auth.HashToken(issued.Token)
|
||||
owner, err := kv.Client.Get(f.ctx, key).Result()
|
||||
if err != nil || owner != f.alice.ID.String() {
|
||||
t.Fatalf("stored owner = %q, %v; want alice", owner, err)
|
||||
}
|
||||
expireAt, err := kv.Client.PExpireTime(f.ctx, key).Result()
|
||||
if err != nil || expireAt != time.Duration(issued.ExpiresAt.UnixMilli())*time.Millisecond {
|
||||
t.Fatalf("store expiry = %v, %v; want the absolute ExpiresAt %d ms", expireAt, err, issued.ExpiresAt.UnixMilli())
|
||||
}
|
||||
keys, err := kv.Client.Keys(f.ctx, "*").Result()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, k := range keys {
|
||||
if strings.Contains(k, issued.Token) {
|
||||
t.Fatal("the raw token must never be stored")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoginWithBadCredentialsCreatesNoSession(t *testing.T) {
|
||||
f := newFixture(t, time.Hour)
|
||||
for name, in := range map[string][2]string{
|
||||
"wrong password": {"alice", pw + "!"},
|
||||
"unknown user": {"mallory", pw},
|
||||
} {
|
||||
if _, err := f.svc.Login(f.ctx, in[0], in[1]); !errors.Is(err, user.ErrInvalidCredentials) {
|
||||
t.Errorf("%s: err = %v, want ErrInvalidCredentials", name, err)
|
||||
}
|
||||
}
|
||||
if n, err := kv.Client.DBSize(f.ctx).Result(); err != nil || n != 0 {
|
||||
t.Fatalf("sessions = %d, %v; want none", n, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthenticateResolvesTheOwner(t *testing.T) {
|
||||
f := newFixture(t, time.Hour)
|
||||
first, _ := f.svc.Login(f.ctx, "alice", pw)
|
||||
second, _ := f.svc.Login(f.ctx, "alice", pw)
|
||||
if first.Token == second.Token {
|
||||
t.Fatal("every login must get its own token")
|
||||
}
|
||||
for _, token := range []string{first.Token, second.Token} {
|
||||
id, err := f.svc.Authenticate(f.ctx, token)
|
||||
if err != nil || id != f.alice.ID {
|
||||
t.Fatalf("Authenticate = %v, %v; want alice", id, err)
|
||||
}
|
||||
}
|
||||
_, err := f.svc.Authenticate(f.ctx, "unknown-token")
|
||||
if !errors.Is(err, auth.ErrUnauthenticated) || apperr.KindOf(err) != apperr.KindUnauthorized {
|
||||
t.Fatalf("unknown token: err = %v, want ErrUnauthenticated", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogoutRevokesOnlyThatSession(t *testing.T) {
|
||||
f := newFixture(t, time.Hour)
|
||||
first, _ := f.svc.Login(f.ctx, "alice", pw)
|
||||
second, _ := f.svc.Login(f.ctx, "alice", pw)
|
||||
if err := f.svc.Logout(f.ctx, first.Token); err != nil {
|
||||
t.Fatalf("Logout: %v", err)
|
||||
}
|
||||
if _, err := f.svc.Authenticate(f.ctx, first.Token); !errors.Is(err, auth.ErrUnauthenticated) {
|
||||
t.Fatalf("revoked token: err = %v, want ErrUnauthenticated", err)
|
||||
}
|
||||
if id, err := f.svc.Authenticate(f.ctx, second.Token); err != nil || id != f.alice.ID {
|
||||
t.Fatalf("other session: %v, %v; want still valid", id, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Valkey enforces the expiry by the Docker VM clock, which may lag this host
|
||||
// by a fraction of a second, so the expiry is awaited with a tolerance.
|
||||
func TestSessionExpiresAfterTTL(t *testing.T) {
|
||||
const clockTolerance = 3 * time.Second
|
||||
f := newFixture(t, time.Second)
|
||||
issued, err := f.svc.Login(f.ctx, "alice", pw)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := f.svc.Authenticate(f.ctx, issued.Token); err != nil {
|
||||
t.Fatalf("fresh session: %v", err)
|
||||
}
|
||||
time.Sleep(time.Until(issued.ExpiresAt))
|
||||
deadline := time.Now().Add(clockTolerance)
|
||||
for {
|
||||
_, err := f.svc.Authenticate(f.ctx, issued.Token)
|
||||
if errors.Is(err, auth.ErrUnauthenticated) {
|
||||
return
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatalf("expired session: err = %v %v after ExpiresAt, want ErrUnauthenticated", err, clockTolerance)
|
||||
}
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// Package auth authenticates callers.
|
||||
//
|
||||
// A login with Basic credentials creates a server-side session and returns an
|
||||
// opaque bearer token; every protected request presents the token, and logout
|
||||
// deletes the session, which revokes the token at once. The store keeps only
|
||||
// the SHA-256 of a token: a token is a random 256-bit value, so a fast digest
|
||||
// cannot be reversed by guessing, and a leaked store yields no usable tokens.
|
||||
// Passwords need the opposite treatment (a slow KDF), see platform/password.
|
||||
package auth
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Session struct {
|
||||
TokenHash string
|
||||
UserID uuid.UUID
|
||||
ExpiresAt time.Time
|
||||
}
|
||||
|
||||
// newToken returns 32 random bytes in base64url: 43 characters, 256 bits.
|
||||
func newToken() string {
|
||||
raw := make([]byte, 32)
|
||||
// Since Go 1.24 crypto/rand.Read never returns an error.
|
||||
_, _ = rand.Read(raw)
|
||||
return base64.RawURLEncoding.EncodeToString(raw)
|
||||
}
|
||||
|
||||
// HashToken is the key under which a token's session is stored.
|
||||
func HashToken(token string) string {
|
||||
sum := sha256.Sum256([]byte(token))
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
// Package store holds the session store implementations.
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"messenger/internal/auth"
|
||||
"messenger/internal/platform/apperr"
|
||||
)
|
||||
|
||||
const keyPrefix = "session:"
|
||||
|
||||
// Sessions keeps one key per session, session:<sha256 of token>, holding the
|
||||
// owner's id. Valkey expires the key itself, so no cleanup job is needed.
|
||||
type Sessions struct {
|
||||
client *redis.Client
|
||||
}
|
||||
|
||||
var _ auth.Repository = (*Sessions)(nil)
|
||||
|
||||
func NewSessions(client *redis.Client) *Sessions {
|
||||
return &Sessions{client: client}
|
||||
}
|
||||
|
||||
// Create sets an absolute expiry (EXAT/PXAT): a restart of the store replays
|
||||
// it from the append-only file and does not extend the session.
|
||||
func (r *Sessions) Create(ctx context.Context, s auth.Session) error {
|
||||
err := r.client.SetArgs(ctx, keyPrefix+s.TokenHash, s.UserID.String(), redis.SetArgs{ExpireAt: s.ExpiresAt}).Err()
|
||||
if err != nil {
|
||||
return unavailable(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Sessions) UserID(ctx context.Context, tokenHash string) (uuid.UUID, error) {
|
||||
raw, err := r.client.Get(ctx, keyPrefix+tokenHash).Result()
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return uuid.Nil, auth.ErrUnauthenticated
|
||||
}
|
||||
if err != nil {
|
||||
return uuid.Nil, unavailable(err)
|
||||
}
|
||||
id, err := uuid.Parse(raw)
|
||||
if err != nil {
|
||||
return uuid.Nil, fmt.Errorf("session holds a malformed user id: %w", err)
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (r *Sessions) Delete(ctx context.Context, tokenHash string) error {
|
||||
if err := r.client.Del(ctx, keyPrefix+tokenHash).Err(); err != nil {
|
||||
return unavailable(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func unavailable(err error) error {
|
||||
return apperr.Wrap(apperr.KindUnavailable, "session store unavailable", err)
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package chat_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/chat"
|
||||
"messenger/internal/platform/httpx"
|
||||
)
|
||||
|
||||
func messageRouter(f fixture, actor uuid.UUID) *gin.Engine {
|
||||
r := gin.New()
|
||||
r.Use(func(c *gin.Context) { httpx.SetUserID(c, actor) })
|
||||
chat.NewHandler(f.svc).Register(r)
|
||||
return r
|
||||
}
|
||||
|
||||
func sendJSON(r http.Handler, chatID uuid.UUID, key, body string) *httptest.ResponseRecorder {
|
||||
req := httptest.NewRequest(http.MethodPost, "/chats/"+chatID.String()+"/messages", strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Idempotency-Key", key)
|
||||
rec := httptest.NewRecorder()
|
||||
r.ServeHTTP(rec, req)
|
||||
return rec
|
||||
}
|
||||
|
||||
func TestMessageAttachmentJSONEmptyArrayAndOmitted(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
c := f.newChat(t, f.alice)
|
||||
r := messageRouter(f, f.alice)
|
||||
for i, body := range []string{`{"text":"hello","attachment_ids":[]}`, `{"text":"hello"}`} {
|
||||
rec := sendJSON(r, c, fmt.Sprint(i), body)
|
||||
if rec.Code != http.StatusCreated {
|
||||
t.Fatalf("send %s: %d %s", body, rec.Code, rec.Body)
|
||||
}
|
||||
var fields map[string]json.RawMessage
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &fields); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(fields["attachment_ids"]) != "[]" {
|
||||
t.Fatalf("attachment_ids = %s, want []", fields["attachment_ids"])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMessageAttachmentJSONRejectsInvalidArrays(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
c := f.newChat(t, f.alice)
|
||||
r := messageRouter(f, f.alice)
|
||||
id := uuid.NewString()
|
||||
for _, raw := range []string{
|
||||
"null", `"not an array"`, "{}", "1", "[null]", "[1]", `["bad"]`,
|
||||
`["00000000-0000-0000-0000-000000000000"]`,
|
||||
fmt.Sprintf("[%q]", strings.ReplaceAll(id, "-", "")),
|
||||
fmt.Sprintf("[%q,%q]", id, strings.ToUpper(id)),
|
||||
"[" + strings.TrimSuffix(strings.Repeat(fmt.Sprintf("%q,", id), 11), ",") + "]",
|
||||
} {
|
||||
t.Run(raw, func(t *testing.T) {
|
||||
rec := sendJSON(r, c, "invalid", `{"text":"hello","attachment_ids":`+raw+"}")
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("send: %d %s", rec.Code, rec.Body)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package chat_test
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/chat"
|
||||
"messenger/internal/platform/ident"
|
||||
"messenger/internal/platform/pagination"
|
||||
)
|
||||
|
||||
func (f fixture) attachment(t *testing.T, chatID, owner uuid.UUID, status string) uuid.UUID {
|
||||
t.Helper()
|
||||
id := ident.New()
|
||||
hash := sha256.Sum256([]byte(id.String()))
|
||||
_, err := db.Pool.Exec(f.ctx, `
|
||||
WITH file AS (
|
||||
INSERT INTO files (hash, size_bytes, mime_type, status, created_at)
|
||||
VALUES ($1, 7, 'image/png', $2, now()) RETURNING hash)
|
||||
INSERT INTO attachments (id, chat_id, owner_id, filename, file_hash, created_at)
|
||||
SELECT $3, $4, $5, 'image.png', hash, now() FROM file`, hash[:], status, id, chatID, owner)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
func TestAttachmentHTTPRoundTripReplayAndOrderedConflict(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
c := f.newChat(t, f.alice, f.bob)
|
||||
a, b := f.attachment(t, c, f.bob, "ready"), f.attachment(t, c, f.alice, "ready")
|
||||
r := messageRouter(f, f.alice)
|
||||
body := fmt.Sprintf(`{"text":"","attachment_ids":[%q,%q]}`, b, a)
|
||||
first := sendJSON(r, c, "attachments", body)
|
||||
if first.Code != http.StatusCreated {
|
||||
t.Fatalf("attachment-only send: %d %s", first.Code, first.Body)
|
||||
}
|
||||
var message chat.MessageResponse
|
||||
if err := json.Unmarshal(first.Body.Bytes(), &message); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if message.Text != "" || !reflect.DeepEqual(message.AttachmentIDs, []string{b.String(), a.String()}) {
|
||||
t.Fatalf("message = %+v", message)
|
||||
}
|
||||
if again := sendJSON(r, c, "attachments", body); again.Code != http.StatusCreated || again.Body.String() != first.Body.String() {
|
||||
t.Fatalf("replay: %d %s, want %s", again.Code, again.Body, first.Body)
|
||||
}
|
||||
for _, changed := range []string{
|
||||
fmt.Sprintf(`{"text":"","attachment_ids":[%q,%q]}`, a, b),
|
||||
fmt.Sprintf(`{"text":"","attachment_ids":[%q]}`, b),
|
||||
fmt.Sprintf(`{"text":"different","attachment_ids":[%q,%q]}`, b, a),
|
||||
} {
|
||||
if rec := sendJSON(r, c, "attachments", changed); rec.Code != http.StatusConflict {
|
||||
t.Fatalf("changed payload: %d %s", rec.Code, rec.Body)
|
||||
}
|
||||
}
|
||||
rec := httptest.NewRecorder()
|
||||
r.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/chats/"+c.String()+"/messages", nil))
|
||||
var page pagination.PageResponse[chat.MessageResponse]
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("history: %d %s", rec.Code, rec.Body)
|
||||
}
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &page); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(page.Items) != 1 || !reflect.DeepEqual(page.Items[0], message) {
|
||||
t.Fatalf("history = %+v, want %+v", page, message)
|
||||
}
|
||||
without := sendJSON(r, c, "empty", `{"text":"text-only"}`)
|
||||
empty := sendJSON(r, c, "empty", `{"attachment_ids":[],"text":"text-only"}`)
|
||||
if without.Code != http.StatusCreated || empty.Code != http.StatusCreated || without.Body.String() != empty.Body.String() {
|
||||
t.Fatalf("omitted vs empty replay: %d %s; %d %s", without.Code, without.Body, empty.Code, empty.Body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttachmentHTTPStatusAndChatAccess(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
c := f.newChat(t, f.alice, f.bob)
|
||||
other := f.newChat(t, f.alice)
|
||||
ready := f.attachment(t, c, f.bob, "ready")
|
||||
foreign := f.attachment(t, other, f.alice, "ready")
|
||||
failed := f.attachment(t, c, f.alice, "failed")
|
||||
pending := f.attachment(t, c, f.alice, "pending")
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
actor, attachment uuid.UUID
|
||||
want int
|
||||
}{
|
||||
{"unknown", f.alice, ident.New(), http.StatusNotFound},
|
||||
{"foreign", f.alice, foreign, http.StatusNotFound},
|
||||
{"failed", f.alice, failed, http.StatusConflict},
|
||||
{"pending", f.alice, pending, http.StatusConflict},
|
||||
{"outsider", f.eve, ready, http.StatusNotFound},
|
||||
{"other-owner", f.alice, ready, http.StatusCreated},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
body := fmt.Sprintf(`{"text":"","attachment_ids":[%q]}`, tc.attachment)
|
||||
rec := sendJSON(messageRouter(f, tc.actor), c, "same", body)
|
||||
if rec.Code != tc.want {
|
||||
t.Fatalf("send: %d %s, want %d", rec.Code, rec.Body, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMessageJSONRequiresTextString(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
c := f.newChat(t, f.alice)
|
||||
r := messageRouter(f, f.alice)
|
||||
for _, body := range []string{"null", `{}`, `{"text":null}`, `{"text":1}`, `{"text":[]}`, `{"text":"","attachment_ids":[]}`} {
|
||||
if rec := sendJSON(r, c, "text", body); rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("send %s: %d %s", body, rec.Code, rec.Body)
|
||||
}
|
||||
}
|
||||
if rec := sendJSON(r, c, "text", `{"text":"valid"}`); rec.Code != http.StatusCreated {
|
||||
t.Fatalf("valid payload after rejected requests: %d %s", rec.Code, rec.Body)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package chat_test
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/chat"
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/ident"
|
||||
)
|
||||
|
||||
func TestAttachmentMessageValidatesAndOwnsOrderedIDs(t *testing.T) {
|
||||
a, b := ident.New(), ident.New()
|
||||
ids := []uuid.UUID{b, a}
|
||||
m, err := chat.NewMessage(ident.New(), ident.New(), ident.New(), "", ids...)
|
||||
if err != nil || m.Text != "" || !reflect.DeepEqual(m.AttachmentIDs, ids) {
|
||||
t.Fatalf("attachment-only message = %+v, %v", m, err)
|
||||
}
|
||||
ids[0] = a
|
||||
if m.AttachmentIDs[0] != b {
|
||||
t.Fatal("message aliases caller's attachment slice")
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
text string
|
||||
ids []uuid.UUID
|
||||
}{
|
||||
{"ok", []uuid.UUID{a, a}},
|
||||
{"ok", []uuid.UUID{uuid.Nil}},
|
||||
{"ok", make([]uuid.UUID, 11)},
|
||||
{strings.Repeat("x", 4001), []uuid.UUID{a}},
|
||||
{"nul\x00", []uuid.UUID{a}},
|
||||
} {
|
||||
_, err := chat.NewMessage(ident.New(), ident.New(), ident.New(), tc.text, tc.ids...)
|
||||
if err == nil || apperr.KindOf(err) != apperr.KindInvalid {
|
||||
t.Fatalf("invalid message accepted: %v", err)
|
||||
}
|
||||
}
|
||||
ten := make([]uuid.UUID, 10)
|
||||
for i := range ten {
|
||||
ten[i] = ident.New()
|
||||
}
|
||||
if _, err := chat.NewMessage(ident.New(), ident.New(), ident.New(), " \n", ten...); err != nil {
|
||||
t.Fatalf("ten attachments: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSameContentIncludesOrderedAttachmentsAndTreatsNilAsEmpty(t *testing.T) {
|
||||
a, b := ident.New(), ident.New()
|
||||
m := chat.Message{Text: "hello", AttachmentIDs: []uuid.UUID{b, a}}
|
||||
for _, tc := range []struct {
|
||||
ids []uuid.UUID
|
||||
equal bool
|
||||
}{
|
||||
{[]uuid.UUID{b, a}, true}, {[]uuid.UUID{a, b}, false}, {[]uuid.UUID{a}, false}, {nil, false},
|
||||
} {
|
||||
if got := m.SameContent(chat.Message{Text: "hello", AttachmentIDs: tc.ids}); got != tc.equal {
|
||||
t.Fatalf("attachments %v: equality=%v, want %v", tc.ids, got, tc.equal)
|
||||
}
|
||||
}
|
||||
if !(chat.Message{Text: "hello"}).SameContent(chat.Message{Text: "hello", AttachmentIDs: []uuid.UUID{}}) {
|
||||
t.Fatal("nil attachments must equal empty attachments")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
// Package chat is the chats module. Entities mirror table rows and
|
||||
// carry no behaviour; membership is a query rather than a field, and
|
||||
// ChatWithMembers is the projection the API renders. Both entities derive
|
||||
// CreatedAt from their UUIDv7 id, so the history order (created_at, id) is
|
||||
// plain id order.
|
||||
package chat
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/ident"
|
||||
)
|
||||
|
||||
const (
|
||||
MaxTitleCodePoints = 100
|
||||
MaxMembers = 100
|
||||
)
|
||||
|
||||
// ErrNotFound covers both a missing chat and a chat the caller is not a
|
||||
// member of, so outsiders cannot probe for existence.
|
||||
var ErrNotFound = apperr.NotFound("chat not found")
|
||||
|
||||
// ErrKeyReused answers a send whose Idempotency-Key is already bound to a
|
||||
// message with a different text.
|
||||
var ErrKeyReused = apperr.Conflict("Idempotency-Key was already used with a different message")
|
||||
|
||||
type Chat struct {
|
||||
ID uuid.UUID
|
||||
Title string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type ChatWithMembers struct {
|
||||
Chat
|
||||
MemberIDs []uuid.UUID
|
||||
}
|
||||
|
||||
func NewChat(id uuid.UUID, title string) (Chat, error) {
|
||||
if strings.TrimSpace(title) == "" {
|
||||
return Chat{}, apperr.Invalid("title must not be blank")
|
||||
}
|
||||
if utf8.RuneCountInString(title) > MaxTitleCodePoints {
|
||||
return Chat{}, apperr.Invalid(fmt.Sprintf("title must be at most %d characters", MaxTitleCodePoints))
|
||||
}
|
||||
// PostgreSQL text cannot hold U+0000.
|
||||
if strings.ContainsRune(title, 0) {
|
||||
return Chat{}, apperr.Invalid("title must not contain the NUL character")
|
||||
}
|
||||
return Chat{ID: id, Title: title, CreatedAt: ident.TimeOf(id)}, nil
|
||||
}
|
||||
|
||||
// ValidateMembers checks the member list of a new chat and returns its own
|
||||
// copy ordered by id: members are a set, and this is the one order in which
|
||||
// every read returns them.
|
||||
func ValidateMembers(memberIDs []uuid.UUID, creator uuid.UUID) ([]uuid.UUID, error) {
|
||||
if len(memberIDs) == 0 {
|
||||
return nil, apperr.Invalid("member_ids must not be empty")
|
||||
}
|
||||
if len(memberIDs) > MaxMembers {
|
||||
return nil, apperr.Invalid(fmt.Sprintf("member_ids must have at most %d entries", MaxMembers))
|
||||
}
|
||||
seen := make(map[uuid.UUID]struct{}, len(memberIDs))
|
||||
for _, m := range memberIDs {
|
||||
if m == uuid.Nil {
|
||||
return nil, apperr.Invalid("member_ids must not contain the nil uuid")
|
||||
}
|
||||
if _, dup := seen[m]; dup {
|
||||
return nil, apperr.Invalid("member_ids must be unique")
|
||||
}
|
||||
seen[m] = struct{}{}
|
||||
}
|
||||
if _, ok := seen[creator]; !ok {
|
||||
return nil, apperr.Invalid("member_ids must include the creator")
|
||||
}
|
||||
members := slices.Clone(memberIDs)
|
||||
slices.SortFunc(members, func(a, b uuid.UUID) int { return bytes.Compare(a[:], b[:]) })
|
||||
return members, nil
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package chat_test
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/chat"
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/ident"
|
||||
)
|
||||
|
||||
func TestNewChatDerivesCreatedAtFromID(t *testing.T) {
|
||||
id := ident.New()
|
||||
c, err := chat.NewChat(id, "Проект")
|
||||
if err != nil {
|
||||
t.Fatalf("NewChat error: %v", err)
|
||||
}
|
||||
if c.ID != id || c.Title != "Проект" || !c.CreatedAt.Equal(ident.TimeOf(id)) {
|
||||
t.Fatalf("unexpected chat %+v", c)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewChatValidatesTitle(t *testing.T) {
|
||||
// PostgreSQL text cannot hold U+0000, so a title with it is invalid input,
|
||||
// not a storage failure.
|
||||
for _, title := range []string{"", " ", strings.Repeat("я", 101), "nul\x00inside"} {
|
||||
_, err := chat.NewChat(ident.New(), title)
|
||||
if apperr.KindOf(err) != apperr.KindInvalid {
|
||||
t.Errorf("title %q must be rejected as invalid, got %v", title, err)
|
||||
}
|
||||
}
|
||||
if _, err := chat.NewChat(ident.New(), strings.Repeat("я", 100)); err != nil {
|
||||
t.Errorf("100 code points must be accepted: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Members come back in one canonical order (by id) whatever order the client
|
||||
// sent, so the created chat and every later read of it agree.
|
||||
func TestValidateMembersCopiesInCanonicalOrder(t *testing.T) {
|
||||
alice, bob := ident.New(), ident.New()
|
||||
input := []uuid.UUID{bob, alice}
|
||||
members, err := chat.ValidateMembers(input, alice)
|
||||
if err != nil {
|
||||
t.Fatalf("ValidateMembers error: %v", err)
|
||||
}
|
||||
if len(members) != 2 || members[0] != alice || members[1] != bob {
|
||||
t.Fatalf("members must be ordered by id, got %v", members)
|
||||
}
|
||||
input[1] = ident.New()
|
||||
if members[0] != alice {
|
||||
t.Fatal("result must not alias the input slice")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateMembersRejectsInvalidLists(t *testing.T) {
|
||||
alice, bob := ident.New(), ident.New()
|
||||
cases := map[string][]uuid.UUID{
|
||||
"empty": {},
|
||||
"nil": nil,
|
||||
"missing creator": {bob},
|
||||
"duplicate": {alice, bob, bob},
|
||||
"duplicate self": {alice, alice},
|
||||
"too many": append([]uuid.UUID{alice}, many(100)...),
|
||||
"nil uuid member": {alice, uuid.Nil},
|
||||
}
|
||||
for name, members := range cases {
|
||||
_, err := chat.ValidateMembers(members, alice)
|
||||
if apperr.KindOf(err) != apperr.KindInvalid {
|
||||
t.Errorf("%s: want invalid, got %v", name, err)
|
||||
}
|
||||
if err != nil && !strings.Contains(err.Error(), "member_ids") {
|
||||
t.Errorf("%s: message must name the field: %v", name, err)
|
||||
}
|
||||
}
|
||||
if _, err := chat.ValidateMembers(append([]uuid.UUID{alice}, many(99)...), alice); err != nil {
|
||||
t.Errorf("100 members must be accepted: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func many(n int) []uuid.UUID {
|
||||
out := make([]uuid.UUID, n)
|
||||
for i := range out {
|
||||
out[i] = ident.New()
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"time"
|
||||
)
|
||||
|
||||
// RunKeyCleanup purges expired Idempotency-Keys every period until ctx is done.
|
||||
func RunKeyCleanup(ctx context.Context, log *slog.Logger, svc *Service, every time.Duration) {
|
||||
ticker := time.NewTicker(every)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
}
|
||||
deleted, err := svc.PurgeExpiredKeys(ctx)
|
||||
if err != nil {
|
||||
if ctx.Err() == nil {
|
||||
log.Warn("idempotency key cleanup failed", "deleted", deleted, "error", err.Error())
|
||||
}
|
||||
continue
|
||||
}
|
||||
if deleted > 0 {
|
||||
log.Info("idempotency keys purged", "deleted", deleted)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,253 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"regexp"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/httpx"
|
||||
"messenger/internal/platform/ident"
|
||||
"messenger/internal/platform/pagination"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
chats *Service
|
||||
}
|
||||
|
||||
func NewHandler(chats *Service) *Handler {
|
||||
return &Handler{chats: chats}
|
||||
}
|
||||
|
||||
// Register mounts the chat endpoints on a router guarded by the authentication middleware.
|
||||
func (h *Handler) Register(protected gin.IRouter) {
|
||||
protected.POST("/chats", httpx.WithUser(h.create))
|
||||
protected.GET("/chats", httpx.WithUser(h.list))
|
||||
protected.GET("/chats/:chat_id", httpx.WithUser(h.get))
|
||||
protected.POST("/chats/:chat_id/messages", httpx.WithUser(h.send))
|
||||
protected.GET("/chats/:chat_id/messages", httpx.WithUser(h.history))
|
||||
}
|
||||
|
||||
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 ChatResponse struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
MemberIDs []string `json:"member_ids"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
}
|
||||
|
||||
type MessageResponse struct {
|
||||
AttachmentIDs []string `json:"attachment_ids"`
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
SenderID string `json:"sender_id"`
|
||||
Text string `json:"text"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
}
|
||||
|
||||
func toChatResponse(c ChatWithMembers) ChatResponse {
|
||||
members := make([]string, 0, len(c.MemberIDs))
|
||||
for _, m := range c.MemberIDs {
|
||||
members = append(members, m.String())
|
||||
}
|
||||
return ChatResponse{
|
||||
ID: c.ID.String(),
|
||||
Title: c.Title,
|
||||
MemberIDs: members,
|
||||
CreatedAt: httpx.Timestamp(c.CreatedAt),
|
||||
}
|
||||
}
|
||||
|
||||
func toMessageResponse(m Message) MessageResponse {
|
||||
ids := make([]string, 0, len(m.AttachmentIDs))
|
||||
for _, id := range m.AttachmentIDs {
|
||||
ids = append(ids, id.String())
|
||||
}
|
||||
return MessageResponse{
|
||||
AttachmentIDs: ids,
|
||||
ID: m.ID.String(),
|
||||
ChatID: m.ChatID.String(),
|
||||
SenderID: m.SenderID.String(),
|
||||
Text: m.Text,
|
||||
CreatedAt: httpx.Timestamp(m.CreatedAt),
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) create(c *gin.Context, actor uuid.UUID) {
|
||||
var req createChatRequest
|
||||
if err := httpx.DecodeJSON(c, &req); err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
members := make([]uuid.UUID, 0, len(req.MemberIDs))
|
||||
for _, raw := range req.MemberIDs {
|
||||
id, err := ident.Parse(raw)
|
||||
if err != nil {
|
||||
httpx.Error(c, apperr.Invalid("member_ids must contain valid UUIDs"))
|
||||
return
|
||||
}
|
||||
members = append(members, id)
|
||||
}
|
||||
chat, err := h.chats.Create(c.Request.Context(), actor, req.Title, members)
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
httpx.JSON(c, http.StatusCreated, toChatResponse(chat))
|
||||
}
|
||||
|
||||
func (h *Handler) list(c *gin.Context, actor uuid.UUID) {
|
||||
limit, after, err := listing(c, actor)
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
page, err := h.chats.List(c.Request.Context(), actor, after, limit)
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
httpx.JSON(c, http.StatusOK, pagination.ToResponse(page, actor, toChatResponse))
|
||||
}
|
||||
|
||||
func (h *Handler) get(c *gin.Context, actor uuid.UUID) {
|
||||
chatID, err := chatParam(c)
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
chat, err := h.chats.Get(c.Request.Context(), actor, chatID)
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
httpx.JSON(c, http.StatusOK, toChatResponse(chat))
|
||||
}
|
||||
|
||||
func (h *Handler) send(c *gin.Context, actor uuid.UUID) {
|
||||
chatID, err := chatParam(c)
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
key, err := idempotencyKey(c)
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
var req createMessageRequest
|
||||
if err := httpx.DecodeJSON(c, &req); err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
if req.Text == nil {
|
||||
httpx.Error(c, apperr.Invalid("text must be a string"))
|
||||
return
|
||||
}
|
||||
attachments, err := decodeAttachmentIDs(req.AttachmentIDs)
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
message, err := h.chats.Send(c.Request.Context(), actor, MessagePayload{ChatID: chatID, IdempotencyKey: key, Text: *req.Text, AttachmentIDs: attachments})
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
httpx.JSON(c, http.StatusCreated, toMessageResponse(message))
|
||||
}
|
||||
|
||||
func (h *Handler) history(c *gin.Context, actor uuid.UUID) {
|
||||
chatID, err := chatParam(c)
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
limit, after, err := listing(c, chatID)
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
page, err := h.chats.History(c.Request.Context(), actor, chatID, after, limit)
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
httpx.JSON(c, http.StatusOK, pagination.ToResponse(page, chatID, toMessageResponse))
|
||||
}
|
||||
|
||||
var keyPattern = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`)
|
||||
|
||||
// idempotencyKey rejects two values of the header as ambiguous.
|
||||
func idempotencyKey(c *gin.Context) (string, error) {
|
||||
values := c.Request.Header.Values("Idempotency-Key")
|
||||
if len(values) != 1 || !keyPattern.MatchString(values[0]) {
|
||||
return "", apperr.Invalid("Idempotency-Key header is required: one value of 1-128 characters A-Z a-z 0-9 . _ : -")
|
||||
}
|
||||
return values[0], nil
|
||||
}
|
||||
|
||||
func chatParam(c *gin.Context) (uuid.UUID, error) {
|
||||
id, err := ident.Parse(c.Param("chat_id"))
|
||||
if err != nil {
|
||||
return uuid.Nil, apperr.Invalid("chat_id must be a UUID")
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// listing parses limit and cursor; the cursor must belong to scope, and a
|
||||
// present but empty cursor is rejected like a malformed one.
|
||||
func listing(c *gin.Context, scope uuid.UUID) (int, *uuid.UUID, error) {
|
||||
limit, err := pagination.ParseLimit(c.Query("limit"))
|
||||
if err != nil {
|
||||
return 0, nil, apperr.Wrap(apperr.KindInvalid, err.Error(), err)
|
||||
}
|
||||
token, present := c.GetQuery("cursor")
|
||||
if !present {
|
||||
return limit, nil, nil
|
||||
}
|
||||
last, err := pagination.DecodeCursor(token, scope)
|
||||
if err != nil {
|
||||
return 0, nil, apperr.Wrap(apperr.KindInvalid, "cursor is invalid for this resource", err)
|
||||
}
|
||||
return limit, &last, nil
|
||||
}
|
||||
|
||||
// RawMessage distinguishes an omitted optional array from an explicit null.
|
||||
func decodeAttachmentIDs(raw json.RawMessage) ([]uuid.UUID, error) {
|
||||
if raw == nil {
|
||||
return nil, nil
|
||||
}
|
||||
if bytes.Equal(bytes.TrimSpace(raw), []byte("null")) {
|
||||
return nil, apperr.Invalid("attachment_ids must be an array")
|
||||
}
|
||||
var values []string
|
||||
if err := json.Unmarshal(raw, &values); err != nil {
|
||||
return nil, apperr.Invalid("attachment_ids must be an array of UUIDs")
|
||||
}
|
||||
if len(values) > MaxAttachments {
|
||||
return nil, apperr.Invalid("attachment_ids must have at most 10 entries")
|
||||
}
|
||||
ids := make([]uuid.UUID, 0, len(values))
|
||||
for _, value := range values {
|
||||
id, err := ident.Parse(value)
|
||||
if err != nil {
|
||||
return nil, apperr.Invalid("attachment_ids must contain valid UUIDs")
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
return ids, nil
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/ident"
|
||||
)
|
||||
|
||||
const (
|
||||
MaxTextCodePoints = 4000
|
||||
MaxAttachments = 10
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
ID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
SenderID uuid.UUID
|
||||
Text string
|
||||
CreatedAt time.Time
|
||||
AttachmentIDs []uuid.UUID
|
||||
}
|
||||
|
||||
// NewMessage keeps text and attachment order verbatim; it owns its attachment slice.
|
||||
func NewMessage(id, chatID, senderID uuid.UUID, text string, attachmentIDs ...uuid.UUID) (Message, error) {
|
||||
if len(attachmentIDs) > MaxAttachments {
|
||||
return Message{}, apperr.Invalid("attachment_ids must have at most 10 entries")
|
||||
}
|
||||
seen := make(map[uuid.UUID]struct{}, len(attachmentIDs))
|
||||
for _, id := range attachmentIDs {
|
||||
if id == uuid.Nil {
|
||||
return Message{}, apperr.Invalid("attachment_ids must contain non-nil UUIDs")
|
||||
}
|
||||
if _, duplicate := seen[id]; duplicate {
|
||||
return Message{}, apperr.Invalid("attachment_ids must be unique")
|
||||
}
|
||||
seen[id] = struct{}{}
|
||||
}
|
||||
if strings.TrimSpace(text) == "" && len(attachmentIDs) == 0 {
|
||||
return Message{}, apperr.Invalid("text must not be blank")
|
||||
}
|
||||
if utf8.RuneCountInString(text) > MaxTextCodePoints {
|
||||
return Message{}, apperr.Invalid(fmt.Sprintf("text must be at most %d characters", MaxTextCodePoints))
|
||||
}
|
||||
// PostgreSQL text cannot hold U+0000.
|
||||
if strings.ContainsRune(text, 0) {
|
||||
return Message{}, apperr.Invalid("text must not contain the NUL character")
|
||||
}
|
||||
return Message{
|
||||
ID: id,
|
||||
ChatID: chatID,
|
||||
SenderID: senderID,
|
||||
Text: text,
|
||||
CreatedAt: ident.TimeOf(id),
|
||||
AttachmentIDs: slices.Clone(attachmentIDs),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// SameContent reports whether two messages say the same thing, whatever their
|
||||
// ids and times.
|
||||
func (m Message) SameContent(other Message) bool {
|
||||
return m.Text == other.Text && slices.Equal(m.AttachmentIDs, other.AttachmentIDs)
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package chat_test
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"messenger/internal/chat"
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/ident"
|
||||
)
|
||||
|
||||
func TestNewMessageDerivesCreatedAt(t *testing.T) {
|
||||
id, chatID, alice := ident.New(), ident.New(), ident.New()
|
||||
m, err := chat.NewMessage(id, chatID, alice, "Привет, мир 👋")
|
||||
if err != nil {
|
||||
t.Fatalf("NewMessage error: %v", err)
|
||||
}
|
||||
if m.ID != id || m.ChatID != chatID || m.SenderID != alice || m.Text != "Привет, мир 👋" {
|
||||
t.Fatalf("unexpected message %+v", m)
|
||||
}
|
||||
if !m.CreatedAt.Equal(ident.TimeOf(id)) {
|
||||
t.Fatalf("CreatedAt = %v, want %v", m.CreatedAt, ident.TimeOf(id))
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewMessageValidatesTextByCodePoints(t *testing.T) {
|
||||
chatID, alice := ident.New(), ident.New()
|
||||
if _, err := chat.NewMessage(ident.New(), chatID, alice, strings.Repeat("я", 4000)); err != nil {
|
||||
t.Errorf("4000 code points must be accepted: %v", err)
|
||||
}
|
||||
for _, text := range []string{"", " ", "\n\t ", strings.Repeat("я", 4001), "nul\x00inside"} {
|
||||
_, err := chat.NewMessage(ident.New(), chatID, alice, text)
|
||||
if apperr.KindOf(err) != apperr.KindInvalid {
|
||||
t.Errorf("text %q must be rejected as invalid, got %v", text, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewMessageKeepsSurroundingWhitespace(t *testing.T) {
|
||||
m, err := chat.NewMessage(ident.New(), ident.New(), ident.New(), " hi ")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if m.Text != " hi " {
|
||||
t.Fatalf("text must be stored verbatim, got %q", m.Text)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSameContentComparesWhatTheSenderWrote(t *testing.T) {
|
||||
chatID, alice := ident.New(), ident.New()
|
||||
first, _ := chat.NewMessage(ident.New(), chatID, alice, "привет")
|
||||
retry, _ := chat.NewMessage(ident.New(), chatID, alice, "привет")
|
||||
other, _ := chat.NewMessage(ident.New(), chatID, alice, "привет!")
|
||||
if !first.SameContent(retry) {
|
||||
t.Fatal("messages with the same text must have the same content")
|
||||
}
|
||||
if first.SameContent(other) {
|
||||
t.Fatal("messages with different text must differ")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// ChatRepository stores chats and their membership. Membership is answered
|
||||
// by queries; the projection with members is assembled by the store in one
|
||||
// query, never by loading a member list into the entity. Members are always
|
||||
// returned ordered by id.
|
||||
type ChatRepository interface {
|
||||
// Create stores the chat row and its members atomically.
|
||||
Create(ctx context.Context, chat Chat, memberIDs []uuid.UUID) error
|
||||
// GetForMember returns the chat with members, or ErrNotFound when the
|
||||
// chat does not exist or the user is not a member.
|
||||
GetForMember(ctx context.Context, chatID, userID uuid.UUID) (ChatWithMembers, error)
|
||||
IsMember(ctx context.Context, chatID, userID uuid.UUID) (bool, error)
|
||||
// ListByMember returns up to limit chats of the user ordered by
|
||||
// (created_at, id), strictly after the chat identified by after when set.
|
||||
ListByMember(ctx context.Context, userID uuid.UUID, after *uuid.UUID, limit int) ([]ChatWithMembers, error)
|
||||
}
|
||||
|
||||
type MessageRepository interface {
|
||||
// AppendOnce stores message under the sender's key unless the key was
|
||||
// used in this chat within ttl, and returns the message the key is bound
|
||||
// to: the given one or the earlier one.
|
||||
AppendOnce(ctx context.Context, message Message, key string, ttl time.Duration) (Message, error)
|
||||
DeleteExpiredKeys(ctx context.Context, olderThan time.Duration, limit int) (int64, error)
|
||||
// ListByChat returns up to limit messages ordered by (created_at, id),
|
||||
// strictly after the message identified by after when set. An unknown
|
||||
// chat yields an empty history.
|
||||
ListByChat(ctx context.Context, chatID uuid.UUID, after *uuid.UUID, limit int) ([]Message, error)
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/ident"
|
||||
"messenger/internal/platform/pagination"
|
||||
)
|
||||
|
||||
const (
|
||||
keyPurgeBatch = 1000
|
||||
// keyPurgeGrace keeps a key past its TTL, so a key that a request has just
|
||||
// found valid cannot vanish before the request reads its message back.
|
||||
keyPurgeGrace = time.Minute
|
||||
)
|
||||
|
||||
// UserLookup is what this module needs from the users module: one batched
|
||||
// existence check. It is declared here, on the consumer side, so this
|
||||
// package never imports the users package and tests substitute a map.
|
||||
type UserLookup interface {
|
||||
ExistingIDs(ctx context.Context, ids []uuid.UUID) ([]uuid.UUID, error)
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
chats ChatRepository
|
||||
messages MessageRepository
|
||||
users UserLookup
|
||||
keyTTL time.Duration
|
||||
}
|
||||
|
||||
// NewService takes keyTTL, how long an Idempotency-Key stays bound to its
|
||||
// message.
|
||||
func NewService(chats ChatRepository, messages MessageRepository, users UserLookup, keyTTL time.Duration) *Service {
|
||||
return &Service{chats: chats, messages: messages, users: users, keyTTL: keyTTL}
|
||||
}
|
||||
|
||||
func (s *Service) Create(ctx context.Context, creator uuid.UUID, title string, memberIDs []uuid.UUID) (ChatWithMembers, error) {
|
||||
chat, err := NewChat(ident.New(), title)
|
||||
if err != nil {
|
||||
return ChatWithMembers{}, err
|
||||
}
|
||||
members, err := ValidateMembers(memberIDs, creator)
|
||||
if err != nil {
|
||||
return ChatWithMembers{}, err
|
||||
}
|
||||
found, err := s.users.ExistingIDs(ctx, members)
|
||||
if err != nil {
|
||||
return ChatWithMembers{}, fmt.Errorf("check members: %w", err)
|
||||
}
|
||||
if len(found) != len(members) {
|
||||
return ChatWithMembers{}, apperr.Invalid("member_ids must reference registered users")
|
||||
}
|
||||
if err := s.chats.Create(ctx, chat, members); err != nil {
|
||||
return ChatWithMembers{}, err
|
||||
}
|
||||
return ChatWithMembers{Chat: chat, MemberIDs: members}, nil
|
||||
}
|
||||
|
||||
func (s *Service) Get(ctx context.Context, actor, chatID uuid.UUID) (ChatWithMembers, error) {
|
||||
return s.chats.GetForMember(ctx, chatID, actor)
|
||||
}
|
||||
|
||||
func (s *Service) List(ctx context.Context, actor uuid.UUID, after *uuid.UUID, limit int) (pagination.Page[ChatWithMembers], error) {
|
||||
items, err := s.chats.ListByMember(ctx, actor, after, limit+1)
|
||||
if err != nil {
|
||||
return pagination.Page[ChatWithMembers]{}, err
|
||||
}
|
||||
return pagination.Cut(items, limit, func(c ChatWithMembers) uuid.UUID { return c.ID }), nil
|
||||
}
|
||||
|
||||
// MessagePayload is one send attempt: the message and its Idempotency-Key.
|
||||
type MessagePayload struct {
|
||||
ChatID uuid.UUID
|
||||
IdempotencyKey string
|
||||
Text string
|
||||
AttachmentIDs []uuid.UUID
|
||||
}
|
||||
|
||||
// Send checks access and content on every attempt, a replay included, as the
|
||||
// contract requires of a repeat.
|
||||
func (s *Service) Send(ctx context.Context, actor uuid.UUID, p MessagePayload) (Message, error) {
|
||||
if err := s.requireMember(ctx, actor, p.ChatID); err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
message, err := NewMessage(ident.New(), p.ChatID, actor, p.Text, p.AttachmentIDs...)
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
stored, err := s.messages.AppendOnce(ctx, message, p.IdempotencyKey, s.keyTTL)
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
if !stored.SameContent(message) {
|
||||
return Message{}, ErrKeyReused
|
||||
}
|
||||
return stored, nil
|
||||
}
|
||||
|
||||
func (s *Service) PurgeExpiredKeys(ctx context.Context) (int64, error) {
|
||||
var total int64
|
||||
for {
|
||||
n, err := s.messages.DeleteExpiredKeys(ctx, s.keyTTL+keyPurgeGrace, keyPurgeBatch)
|
||||
total += n
|
||||
if err != nil {
|
||||
return total, err
|
||||
}
|
||||
if n < keyPurgeBatch {
|
||||
return total, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) History(ctx context.Context, actor, chatID uuid.UUID, after *uuid.UUID, limit int) (pagination.Page[Message], error) {
|
||||
if err := s.requireMember(ctx, actor, chatID); err != nil {
|
||||
return pagination.Page[Message]{}, err
|
||||
}
|
||||
items, err := s.messages.ListByChat(ctx, chatID, after, limit+1)
|
||||
if err != nil {
|
||||
return pagination.Page[Message]{}, err
|
||||
}
|
||||
return pagination.Cut(items, limit, func(m Message) uuid.UUID { return m.ID }), nil
|
||||
}
|
||||
|
||||
// requireMember runs before any field validation, so an outsider gets the
|
||||
// same answer for a foreign chat as for a missing one.
|
||||
func (s *Service) requireMember(ctx context.Context, actor, chatID uuid.UUID) error {
|
||||
ok, err := s.chats.IsMember(ctx, chatID, actor)
|
||||
if err != nil {
|
||||
return fmt.Errorf("check membership: %w", err)
|
||||
}
|
||||
if !ok {
|
||||
return ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,392 @@
|
||||
package chat_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/chat"
|
||||
"messenger/internal/chat/store"
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/ident"
|
||||
"messenger/internal/testenv"
|
||||
"messenger/internal/user"
|
||||
userstore "messenger/internal/user/store"
|
||||
)
|
||||
|
||||
var db *testenv.DB
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
var err error
|
||||
db, err = testenv.StartPostgres(context.Background())
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
code := m.Run()
|
||||
db.Close()
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
// countingUsers passes through to the real users service and counts the
|
||||
// lookups, which proves that member checks are batched.
|
||||
type countingUsers struct {
|
||||
*user.Service
|
||||
calls atomic.Int32
|
||||
}
|
||||
|
||||
func (c *countingUsers) ExistingIDs(ctx context.Context, ids []uuid.UUID) ([]uuid.UUID, error) {
|
||||
c.calls.Add(1)
|
||||
return c.Service.ExistingIDs(ctx, ids)
|
||||
}
|
||||
|
||||
type fixture struct {
|
||||
svc *chat.Service
|
||||
users *countingUsers
|
||||
alice, bob uuid.UUID
|
||||
eve uuid.UUID
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func newFixture(t *testing.T) fixture {
|
||||
t.Helper()
|
||||
db.Reset(t)
|
||||
ctx := context.Background()
|
||||
users := &countingUsers{Service: user.NewService(userstore.NewUsers(db.Pool))}
|
||||
register := func(name string) uuid.UUID {
|
||||
u, err := users.Register(ctx, name, name, "correct-horse-battery")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return u.ID
|
||||
}
|
||||
return fixture{
|
||||
svc: chat.NewService(store.NewChats(db.Pool), store.NewMessages(db.Pool), users, time.Hour),
|
||||
users: users,
|
||||
alice: register("alice"),
|
||||
bob: register("bob"),
|
||||
eve: register("eve"),
|
||||
ctx: ctx,
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateChatChecksMembersInOneCall(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
c, err := f.svc.Create(f.ctx, f.alice, "Проект", []uuid.UUID{f.eve, f.alice, f.bob})
|
||||
if err != nil {
|
||||
t.Fatalf("Create error: %v", err)
|
||||
}
|
||||
if c.Title != "Проект" || len(c.MemberIDs) != 3 {
|
||||
t.Fatalf("unexpected chat %+v", c)
|
||||
}
|
||||
if got := f.users.calls.Load(); got != 1 {
|
||||
t.Fatalf("member existence must be checked with one batch call, got %d", got)
|
||||
}
|
||||
got, err := f.svc.Get(f.ctx, f.bob, c.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("member Get error: %v", err)
|
||||
}
|
||||
if fmt.Sprint(got.MemberIDs) != fmt.Sprint(c.MemberIDs) || !got.CreatedAt.Equal(c.CreatedAt) || got.Title != c.Title {
|
||||
t.Fatalf("Get = %+v, want what Create returned %+v", got, c)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateChatRejectsUnknownMember(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
_, err := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, ident.New()})
|
||||
if apperr.KindOf(err) != apperr.KindInvalid {
|
||||
t.Fatalf("kind = %v, want KindInvalid (%v)", apperr.KindOf(err), err)
|
||||
}
|
||||
if !strings.Contains(err.Error(), "member_ids") {
|
||||
t.Fatalf("message must name the field: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateChatRejectsInvalidInputBeforeAnyLookup(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
cases := map[string]struct {
|
||||
title string
|
||||
members []uuid.UUID
|
||||
}{
|
||||
"blank title": {" ", []uuid.UUID{f.alice}},
|
||||
"missing creator": {"t", []uuid.UUID{f.bob}},
|
||||
"duplicate": {"t", []uuid.UUID{f.alice, f.alice}},
|
||||
}
|
||||
for name, tc := range cases {
|
||||
_, err := f.svc.Create(f.ctx, f.alice, tc.title, tc.members)
|
||||
if apperr.KindOf(err) != apperr.KindInvalid {
|
||||
t.Errorf("%s: kind = %v, want KindInvalid (%v)", name, apperr.KindOf(err), err)
|
||||
}
|
||||
}
|
||||
if got := f.users.calls.Load(); got != 0 {
|
||||
t.Fatalf("invalid input must be rejected before any user lookup, got %d calls", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetHidesForeignAndUnknownChatsAlike(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
c, err := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, f.bob})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, foreign := f.svc.Get(f.ctx, f.eve, c.ID)
|
||||
_, unknown := f.svc.Get(f.ctx, f.eve, ident.New())
|
||||
if !errors.Is(foreign, chat.ErrNotFound) || !errors.Is(unknown, chat.ErrNotFound) {
|
||||
t.Fatalf("foreign = %v, unknown = %v; want both ErrNotFound", foreign, unknown)
|
||||
}
|
||||
if apperr.KindOf(foreign) != apperr.KindNotFound {
|
||||
t.Fatalf("ErrNotFound must carry KindNotFound, got %v", apperr.KindOf(foreign))
|
||||
}
|
||||
}
|
||||
|
||||
func TestListPagesOnlyOwnChats(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
var mine []uuid.UUID
|
||||
for range 3 {
|
||||
c, err := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, f.bob})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mine = append(mine, c.ID)
|
||||
if _, err := f.svc.Create(f.ctx, f.eve, "t", []uuid.UUID{f.eve}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
page, err := f.svc.List(f.ctx, f.alice, nil, 2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(page.Items) != 2 || page.Next == nil || *page.Next != mine[1] {
|
||||
t.Fatalf("first page = %+v", page)
|
||||
}
|
||||
if len(page.Items[0].MemberIDs) != 2 {
|
||||
t.Fatalf("listed chats must carry their members: %+v", page.Items[0])
|
||||
}
|
||||
rest, err := f.svc.List(f.ctx, f.alice, page.Next, 2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(rest.Items) != 1 || rest.Items[0].ID != mine[2] || rest.Next != nil {
|
||||
t.Fatalf("second page = %+v", rest)
|
||||
}
|
||||
all, err := f.svc.List(f.ctx, f.bob, nil, 100)
|
||||
if err != nil || len(all.Items) != 3 || all.Next != nil {
|
||||
t.Fatalf("bob page = %+v, %v", all, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendChecksAccessBeforeValidation(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
c, err := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, f.bob})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = f.svc.Send(f.ctx, f.eve, chat.MessagePayload{ChatID: c.ID, IdempotencyKey: "k1", Text: " "})
|
||||
if !errors.Is(err, chat.ErrNotFound) {
|
||||
t.Fatalf("outsider with blank text: err = %v, want ErrNotFound", err)
|
||||
}
|
||||
_, err = f.svc.Send(f.ctx, f.bob, chat.MessagePayload{ChatID: c.ID, IdempotencyKey: "k2", Text: " "})
|
||||
if apperr.KindOf(err) != apperr.KindInvalid {
|
||||
t.Fatalf("member with blank text: kind = %v, want KindInvalid", apperr.KindOf(err))
|
||||
}
|
||||
msg, err := f.svc.Send(f.ctx, f.bob, chat.MessagePayload{ChatID: c.ID, IdempotencyKey: "k3", Text: "Привет 👋"})
|
||||
if err != nil || msg.SenderID != f.bob || msg.ChatID != c.ID || msg.Text != "Привет 👋" {
|
||||
t.Fatalf("Send = %+v, %v", msg, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHistoryPagesAndHidesForeignChat(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
c, err := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, f.bob})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
first, _ := f.svc.Send(f.ctx, f.alice, chat.MessagePayload{ChatID: c.ID, IdempotencyKey: "k1", Text: "1"})
|
||||
second, _ := f.svc.Send(f.ctx, f.bob, chat.MessagePayload{ChatID: c.ID, IdempotencyKey: "k2", Text: "2"})
|
||||
|
||||
page, err := f.svc.History(f.ctx, f.bob, c.ID, nil, 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(page.Items) != 1 || !reflect.DeepEqual(page.Items[0], first) || page.Next == nil || *page.Next != first.ID {
|
||||
t.Fatalf("first page = %+v", page)
|
||||
}
|
||||
rest, err := f.svc.History(f.ctx, f.alice, c.ID, page.Next, 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(rest.Items) != 1 || !reflect.DeepEqual(rest.Items[0], second) || rest.Next != nil {
|
||||
t.Fatalf("second page = %+v", rest)
|
||||
}
|
||||
if _, err := f.svc.History(f.ctx, f.eve, c.ID, nil, 10); !errors.Is(err, chat.ErrNotFound) {
|
||||
t.Fatalf("outsider history: err = %v, want ErrNotFound", err)
|
||||
}
|
||||
empty, err := f.svc.History(f.ctx, f.alice, c.ID, &second.ID, 10)
|
||||
if err != nil || empty.Items == nil || len(empty.Items) != 0 || empty.Next != nil {
|
||||
t.Fatalf("page after last = %+v, %v; want empty non-nil items", empty, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (f fixture) newChat(t *testing.T, members ...uuid.UUID) uuid.UUID {
|
||||
t.Helper()
|
||||
c, err := f.svc.Create(f.ctx, members[0], "t", members)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return c.ID
|
||||
}
|
||||
|
||||
func (f fixture) send(t *testing.T, actor, c uuid.UUID, key, text string) chat.Message {
|
||||
t.Helper()
|
||||
m, err := f.svc.Send(f.ctx, actor, chat.MessagePayload{ChatID: c, IdempotencyKey: key, Text: text})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func TestSendReplaysSameBody(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
c := f.newChat(t, f.alice, f.bob)
|
||||
first := f.send(t, f.alice, c, "k", "привет")
|
||||
again := f.send(t, f.alice, c, "k", "привет")
|
||||
if again.ID != first.ID || !again.CreatedAt.Equal(first.CreatedAt) {
|
||||
t.Fatalf("replay = %+v, want %+v", again, first)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendConflictsOnDifferentBody(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
c := f.newChat(t, f.alice, f.bob)
|
||||
f.send(t, f.alice, c, "k", "первое")
|
||||
_, err := f.svc.Send(f.ctx, f.alice, chat.MessagePayload{ChatID: c, IdempotencyKey: "k", Text: "другое"})
|
||||
if !errors.Is(err, chat.ErrKeyReused) || apperr.KindOf(err) != apperr.KindConflict {
|
||||
t.Fatalf("different body under a used key: err = %v, want ErrKeyReused (conflict)", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKeysAreScopedByUserAndChat(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
c1 := f.newChat(t, f.alice, f.bob)
|
||||
c2 := f.newChat(t, f.alice, f.bob)
|
||||
a1 := f.send(t, f.alice, c1, "k", "текст")
|
||||
b1 := f.send(t, f.bob, c1, "k", "текст")
|
||||
a2 := f.send(t, f.alice, c2, "k", "текст")
|
||||
if a1.ID == b1.ID || a1.ID == a2.ID || b1.ID == a2.ID {
|
||||
t.Fatalf("one key shared across users or chats: %v %v %v", a1.ID, b1.ID, a2.ID)
|
||||
}
|
||||
}
|
||||
|
||||
// A replay checks access again: an outsider presenting a member's key learns
|
||||
// nothing and the key stays bound to the member's message.
|
||||
func TestSendChecksAccessBeforeKey(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
c := f.newChat(t, f.alice, f.bob)
|
||||
first := f.send(t, f.alice, c, "k", "секрет")
|
||||
if _, err := f.svc.Send(f.ctx, f.eve, chat.MessagePayload{ChatID: c, IdempotencyKey: "k", Text: "секрет"}); !errors.Is(err, chat.ErrNotFound) {
|
||||
t.Fatalf("outsider with a used key: err = %v, want ErrNotFound", err)
|
||||
}
|
||||
if again := f.send(t, f.alice, c, "k", "секрет"); again.ID != first.ID {
|
||||
t.Fatalf("key rebound after the outsider: %v, want %v", again.ID, first.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvalidTextWithUsedKeyIsInvalid(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
c := f.newChat(t, f.alice, f.bob)
|
||||
f.send(t, f.alice, c, "k", "текст")
|
||||
if _, err := f.svc.Send(f.ctx, f.alice, chat.MessagePayload{ChatID: c, IdempotencyKey: "k", Text: " "}); apperr.KindOf(err) != apperr.KindInvalid {
|
||||
t.Fatalf("blank text under a used key: kind = %v, want KindInvalid", apperr.KindOf(err))
|
||||
}
|
||||
}
|
||||
|
||||
func TestPurgeExpiredKeysDeletesInBatches(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
c := f.newChat(t, f.alice, f.bob)
|
||||
const expired = 2500
|
||||
_, err := db.Pool.Exec(f.ctx, `
|
||||
WITH m AS (
|
||||
INSERT INTO messages (chat_id, id, sender_id, text, created_at)
|
||||
SELECT $1, gen_random_uuid(), $2, 'old', now() FROM generate_series(1, $3)
|
||||
RETURNING id)
|
||||
INSERT INTO message_idempotency_keys (user_id, chat_id, key, message_id, created_at)
|
||||
SELECT $2, $1, id::text, id, now() - interval '2 hours' FROM m`, c, f.alice, expired)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.send(t, f.alice, c, "fresh", "новое")
|
||||
deleted, err := f.svc.PurgeExpiredKeys(f.ctx)
|
||||
if err != nil || deleted != expired {
|
||||
t.Fatalf("PurgeExpiredKeys = %d, %v; want %d", deleted, err, expired)
|
||||
}
|
||||
var left int
|
||||
if err := db.Pool.QueryRow(f.ctx, `SELECT count(*) FROM message_idempotency_keys`).Scan(&left); err != nil || left != 1 {
|
||||
t.Fatalf("%d keys left, %v; want the fresh one", left, err)
|
||||
}
|
||||
}
|
||||
|
||||
// A key the claim has just seen valid must survive until the replay lookup,
|
||||
// so the cleanup leaves keys alone for a while after they expire.
|
||||
func TestPurgeExpiredKeysWaitsAGraceAfterTTL(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
c := f.newChat(t, f.alice)
|
||||
f.send(t, f.alice, c, "just-expired", "1")
|
||||
f.send(t, f.alice, c, "long-expired", "2")
|
||||
if _, err := db.Pool.Exec(f.ctx, `
|
||||
UPDATE message_idempotency_keys
|
||||
SET created_at = now() - interval '1 hour' - CASE key WHEN 'just-expired' THEN interval '10 seconds' ELSE interval '10 minutes' END`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if deleted, err := f.svc.PurgeExpiredKeys(f.ctx); err != nil || deleted != 1 {
|
||||
t.Fatalf("PurgeExpiredKeys = %d, %v; want only the long expired key", deleted, err)
|
||||
}
|
||||
var left string
|
||||
if err := db.Pool.QueryRow(f.ctx, `SELECT key FROM message_idempotency_keys`).Scan(&left); err != nil || left != "just-expired" {
|
||||
t.Fatalf("left %q, %v; want just-expired", left, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunKeyCleanupPurgesAndStops(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
c := f.newChat(t, f.alice)
|
||||
f.send(t, f.alice, c, "old", "старое")
|
||||
if _, err := db.Pool.Exec(f.ctx, `UPDATE message_idempotency_keys SET created_at = now() - interval '2 hours'`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(f.ctx)
|
||||
stopped := make(chan struct{})
|
||||
go func() {
|
||||
chat.RunKeyCleanup(ctx, slog.New(slog.NewTextHandler(io.Discard, nil)), f.svc, 50*time.Millisecond)
|
||||
close(stopped)
|
||||
}()
|
||||
deadline := time.Now().Add(2 * time.Second)
|
||||
for {
|
||||
var left int
|
||||
if err := db.Pool.QueryRow(f.ctx, `SELECT count(*) FROM message_idempotency_keys`).Scan(&left); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if left == 0 {
|
||||
break
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatal("expired key was not purged within 2s")
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
cancel()
|
||||
select {
|
||||
case <-stopped:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("RunKeyCleanup did not return after cancel")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,312 @@
|
||||
package store_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"messenger/internal/chat"
|
||||
"messenger/internal/chat/store"
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/ident"
|
||||
)
|
||||
|
||||
func (f fixture) attachment(t *testing.T, chatID, owner uuid.UUID, status string) uuid.UUID {
|
||||
t.Helper()
|
||||
id := ident.New()
|
||||
hash := sha256.Sum256([]byte(id.String()))
|
||||
_, err := db.Pool.Exec(f.ctx, `
|
||||
WITH file AS (
|
||||
INSERT INTO files (hash, size_bytes, mime_type, status, created_at)
|
||||
VALUES ($1, 7, 'image/png', $2, now()) RETURNING hash)
|
||||
INSERT INTO attachments (id, chat_id, owner_id, filename, file_hash, created_at)
|
||||
SELECT $3, $4, $5, 'image.png', hash, now() FROM file`,
|
||||
hash[:], status, id, chatID, owner)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
func attachedMessage(t *testing.T, c, sender uuid.UUID, ids ...uuid.UUID) chat.Message {
|
||||
t.Helper()
|
||||
m, err := chat.NewMessage(ident.New(), c, sender, "", ids...)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func assertCounts(t *testing.T, messages, keys, joins int) {
|
||||
t.Helper()
|
||||
var gotMessages, gotKeys, gotJoins int
|
||||
err := db.Pool.QueryRow(context.Background(), `
|
||||
SELECT (SELECT count(*) FROM messages),
|
||||
(SELECT count(*) FROM message_idempotency_keys),
|
||||
(SELECT count(*) FROM message_attachments)`).Scan(&gotMessages, &gotKeys, &gotJoins)
|
||||
if err != nil || gotMessages != messages || gotKeys != keys || gotJoins != joins {
|
||||
t.Fatalf("counts = %d/%d/%d, %v; want %d/%d/%d", gotMessages, gotKeys, gotJoins, err, messages, keys, joins)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppendOnceRejectsUnknownAttachmentWithoutClaim(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
alice := f.mustUser(t)
|
||||
c := f.mustChat(t, alice)
|
||||
m := attachedMessage(t, c.ID, alice, ident.New())
|
||||
_, err := f.messages.AppendOnce(f.ctx, m, "unknown", keyTTL)
|
||||
if err == nil || apperr.KindOf(err) != apperr.KindNotFound {
|
||||
t.Fatalf("unknown attachment: %v; want not found", err)
|
||||
}
|
||||
var messages, keys int
|
||||
err = db.Pool.QueryRow(f.ctx, `SELECT (SELECT count(*) FROM messages), (SELECT count(*) FROM message_idempotency_keys)`).Scan(&messages, &keys)
|
||||
if err != nil || messages != 0 || keys != 0 {
|
||||
t.Fatalf("failed validation persisted %d messages/%d keys: %v", messages, keys, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppendOnceAttachmentValidationIsAtomic(t *testing.T) {
|
||||
for _, status := range []string{"foreign", "failed", "pending", "mixed"} {
|
||||
t.Run(status, func(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
alice, bob := f.mustUser(t), f.mustUser(t)
|
||||
c := f.mustChat(t, alice, bob)
|
||||
other := f.mustChat(t, alice)
|
||||
ready := f.attachment(t, c.ID, bob, "ready")
|
||||
var invalid uuid.UUID
|
||||
want := apperr.KindConflict
|
||||
switch status {
|
||||
case "foreign":
|
||||
invalid = f.attachment(t, other.ID, alice, "ready")
|
||||
want = apperr.KindNotFound
|
||||
case "mixed":
|
||||
ready = f.attachment(t, c.ID, bob, "failed")
|
||||
invalid = ident.New()
|
||||
want = apperr.KindNotFound
|
||||
default:
|
||||
invalid = f.attachment(t, c.ID, alice, status)
|
||||
}
|
||||
_, err := f.messages.AppendOnce(f.ctx, attachedMessage(t, c.ID, alice, ready, invalid), "bad", keyTTL)
|
||||
if err == nil || apperr.KindOf(err) != want {
|
||||
t.Fatalf("validation: %v; want %v", err, want)
|
||||
}
|
||||
assertCounts(t, 0, 0, 0)
|
||||
good := f.attachment(t, c.ID, bob, "ready")
|
||||
f.appendOnce(t, attachedMessage(t, c.ID, alice, good), "bad")
|
||||
assertCounts(t, 1, 1, 1)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppendOnceAttachmentOrderReplayAndExpiration(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
alice, bob := f.mustUser(t), f.mustUser(t)
|
||||
c := f.mustChat(t, alice, bob)
|
||||
a, b := f.attachment(t, c.ID, bob, "ready"), f.attachment(t, c.ID, bob, "ready")
|
||||
first := attachedMessage(t, c.ID, alice, b, a)
|
||||
got := f.appendOnce(t, first, "k")
|
||||
if !reflect.DeepEqual(got, first) {
|
||||
t.Fatalf("append = %+v, want %+v", got, first)
|
||||
}
|
||||
replayed := f.appendOnce(t, attachedMessage(t, c.ID, alice, a, b), "k")
|
||||
if !reflect.DeepEqual(replayed, first) {
|
||||
t.Fatalf("replay must return original ordered attachments: %+v", replayed)
|
||||
}
|
||||
assertCounts(t, 1, 1, 2)
|
||||
expire(t)
|
||||
second := attachedMessage(t, c.ID, bob, a)
|
||||
f.appendOnce(t, second, "k2")
|
||||
replacement := attachedMessage(t, c.ID, alice, a, b)
|
||||
f.appendOnce(t, replacement, "k")
|
||||
assertCounts(t, 3, 2, 5)
|
||||
page, err := f.messages.ListByChat(f.ctx, c.ID, nil, 1)
|
||||
if err != nil || len(page) != 1 || !reflect.DeepEqual(page[0], first) {
|
||||
t.Fatalf("first page = %+v, %v", page, err)
|
||||
}
|
||||
page, err = f.messages.ListByChat(f.ctx, c.ID, &first.ID, 10)
|
||||
if err != nil || !reflect.DeepEqual(page, []chat.Message{second, replacement}) {
|
||||
t.Fatalf("remaining page = %+v, %v", page, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppendOnceRevalidatesAttachmentOnReplay(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
alice := f.mustUser(t)
|
||||
c := f.mustChat(t, alice)
|
||||
a := f.attachment(t, c.ID, alice, "ready")
|
||||
first := attachedMessage(t, c.ID, alice, a)
|
||||
f.appendOnce(t, first, "k")
|
||||
if _, err := db.Pool.Exec(f.ctx, `UPDATE files SET status = 'failed'`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err := f.messages.AppendOnce(f.ctx, attachedMessage(t, c.ID, alice, a), "k", keyTTL)
|
||||
if err == nil || apperr.KindOf(err) != apperr.KindConflict {
|
||||
t.Fatalf("failed replay attachment = %v, want conflict", err)
|
||||
}
|
||||
page, err := f.messages.ListByChat(f.ctx, c.ID, nil, 10)
|
||||
if err != nil || len(page) != 1 || !reflect.DeepEqual(page[0], first) {
|
||||
t.Fatalf("failed blob must preserve history attachment ids: %+v, %v", page, err)
|
||||
}
|
||||
assertCounts(t, 1, 1, 1)
|
||||
}
|
||||
|
||||
func TestAppendOnceRollsBackClaimAndMessageOnJoinFailure(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
alice := f.mustUser(t)
|
||||
c := f.mustChat(t, alice)
|
||||
a := f.attachment(t, c.ID, alice, "ready")
|
||||
// A database fault at the join boundary must not leave a key or message.
|
||||
_, err := db.Pool.Exec(f.ctx, `ALTER TABLE message_attachments ADD CONSTRAINT test_reject_join CHECK (ordinal < 0)`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() {
|
||||
if _, err := db.Pool.Exec(f.ctx, `ALTER TABLE message_attachments DROP CONSTRAINT test_reject_join`); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}()
|
||||
if _, err := f.messages.AppendOnce(f.ctx, attachedMessage(t, c.ID, alice, a), "k", keyTTL); err == nil {
|
||||
t.Fatal("join fault must fail append")
|
||||
}
|
||||
assertCounts(t, 0, 0, 0)
|
||||
}
|
||||
|
||||
func TestConcurrentAttachmentAppendsSameKeyStoreOneOrderedSet(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
alice := f.mustUser(t)
|
||||
c := f.mustChat(t, alice)
|
||||
a, b := f.attachment(t, c.ID, alice, "ready"), f.attachment(t, c.ID, alice, "ready")
|
||||
const requests = 20
|
||||
results := make(chan chat.Message, requests)
|
||||
start := make(chan struct{})
|
||||
var wg sync.WaitGroup
|
||||
for i := range requests {
|
||||
ids := []uuid.UUID{a, b}
|
||||
if i%2 != 0 {
|
||||
ids = []uuid.UUID{b, a}
|
||||
}
|
||||
m := attachedMessage(t, c.ID, alice, ids...)
|
||||
wg.Go(func() {
|
||||
<-start
|
||||
got, err := f.messages.AppendOnce(f.ctx, m, "same", keyTTL)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
results <- got
|
||||
})
|
||||
}
|
||||
close(start)
|
||||
wg.Wait()
|
||||
close(results)
|
||||
var winner *chat.Message
|
||||
n := 0
|
||||
for got := range results {
|
||||
n++
|
||||
if winner == nil {
|
||||
winner = &got
|
||||
} else if !reflect.DeepEqual(got, *winner) {
|
||||
t.Fatalf("concurrent replay differs: %+v, want %+v", got, *winner)
|
||||
}
|
||||
}
|
||||
if n != requests {
|
||||
t.Fatalf("got %d successful replies, want %d", n, requests)
|
||||
}
|
||||
assertCounts(t, 1, 1, 2)
|
||||
}
|
||||
|
||||
type queryCounter struct{ count atomic.Int32 }
|
||||
|
||||
func (c *queryCounter) TraceQueryStart(ctx context.Context, _ *pgx.Conn, _ pgx.TraceQueryStartData) context.Context {
|
||||
c.count.Add(1)
|
||||
return ctx
|
||||
}
|
||||
func (*queryCounter) TraceQueryEnd(context.Context, *pgx.Conn, pgx.TraceQueryEndData) {}
|
||||
|
||||
func TestAttachmentHistorySingleQueryAndBatchedValidation(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
alice := f.mustUser(t)
|
||||
c := f.mustChat(t, alice)
|
||||
var ids []uuid.UUID
|
||||
for range 10 {
|
||||
ids = append(ids, f.attachment(t, c.ID, alice, "ready"))
|
||||
}
|
||||
cfg := db.Pool.Config()
|
||||
counter := &queryCounter{}
|
||||
cfg.ConnConfig.Tracer = counter
|
||||
pool, err := pgxpool.NewWithConfig(f.ctx, cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer pool.Close()
|
||||
if err := pool.Ping(f.ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
repo := store.NewMessages(pool)
|
||||
counter.count.Store(0)
|
||||
if _, err := repo.AppendOnce(f.ctx, attachedMessage(t, c.ID, alice, ids[0]), "one", keyTTL); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
one := counter.count.Swap(0)
|
||||
if _, err := repo.AppendOnce(f.ctx, attachedMessage(t, c.ID, alice, ids...), "ten", keyTTL); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ten := counter.count.Swap(0)
|
||||
if one != ten {
|
||||
t.Fatalf("queries grew from %d to %d for 1 vs 10 attachments", one, ten)
|
||||
}
|
||||
page, err := repo.ListByChat(f.ctx, c.ID, nil, 10)
|
||||
if err != nil || len(page) != 2 || !reflect.DeepEqual(page[1].AttachmentIDs, ids) {
|
||||
t.Fatalf("history = %+v, %v", page, err)
|
||||
}
|
||||
if n := counter.count.Load(); n != 1 {
|
||||
t.Fatalf("history used %d queries, want one", n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppendOnceRetriesWhenBoundKeyDisappears(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
alice := f.mustUser(t)
|
||||
c := f.mustChat(t, alice)
|
||||
first := f.newMessage(t, c.ID, alice)
|
||||
f.appendOnce(t, first, "vanish")
|
||||
next := f.newMessage(t, c.ID, alice)
|
||||
// Simulate the missing-key boundary after an unsuccessful claim using a
|
||||
// statement trigger. Its second invocation sees no key and is a no-op.
|
||||
_, err := db.Pool.Exec(f.ctx, fmt.Sprintf(`
|
||||
CREATE FUNCTION test_vanish_key() RETURNS trigger LANGUAGE plpgsql AS $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (SELECT 1 FROM messages WHERE id = '%s'::uuid) THEN
|
||||
DELETE FROM message_idempotency_keys WHERE key = 'vanish';
|
||||
END IF;
|
||||
RETURN NULL;
|
||||
END $$;
|
||||
CREATE TRIGGER test_vanish_key AFTER INSERT ON messages
|
||||
FOR EACH STATEMENT EXECUTE FUNCTION test_vanish_key()`, next.ID.String()))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() {
|
||||
if _, err := db.Pool.Exec(f.ctx, `DROP TRIGGER test_vanish_key ON messages; DROP FUNCTION test_vanish_key()`); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}()
|
||||
ctx, cancel := context.WithTimeout(f.ctx, 3*time.Second)
|
||||
defer cancel()
|
||||
got, err := f.messages.AppendOnce(ctx, next, "vanish", keyTTL)
|
||||
if err != nil || got.ID != next.ID {
|
||||
t.Fatalf("missing bound key = %+v, %v; want new message", got, err)
|
||||
}
|
||||
if n := f.count(t, c.ID); n != 2 {
|
||||
t.Fatalf("history has %d messages, want 2", n)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
package store_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/chat"
|
||||
)
|
||||
|
||||
const keyTTL = time.Hour
|
||||
|
||||
// expire makes every stored key older than keyTTL.
|
||||
func expire(t *testing.T) {
|
||||
t.Helper()
|
||||
if _, err := db.Pool.Exec(context.Background(),
|
||||
`UPDATE message_idempotency_keys SET created_at = now() - interval '2 hours'`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (f fixture) appendOnce(t *testing.T, m chat.Message, key string) chat.Message {
|
||||
t.Helper()
|
||||
got, err := f.messages.AppendOnce(f.ctx, m, key, keyTTL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return got
|
||||
}
|
||||
|
||||
func (f fixture) count(t *testing.T, chatID uuid.UUID) int {
|
||||
t.Helper()
|
||||
got, err := f.messages.ListByChat(f.ctx, chatID, nil, 1000)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return len(got)
|
||||
}
|
||||
|
||||
func keyTarget(t *testing.T, key string) uuid.UUID {
|
||||
t.Helper()
|
||||
var id uuid.UUID
|
||||
if err := db.Pool.QueryRow(context.Background(),
|
||||
`SELECT message_id FROM message_idempotency_keys WHERE key = $1`, key).Scan(&id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
func TestAppendOnceInsertsThenReplays(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
alice := f.mustUser(t)
|
||||
c := f.mustChat(t, alice)
|
||||
first := f.newMessage(t, c.ID, alice)
|
||||
if got := f.appendOnce(t, first, "k1"); got.ID != first.ID {
|
||||
t.Fatalf("first append returned %v, want the new message %v", got.ID, first.ID)
|
||||
}
|
||||
retry := f.newMessage(t, c.ID, alice)
|
||||
got := f.appendOnce(t, retry, "k1")
|
||||
if got.ID != first.ID || !got.CreatedAt.Equal(first.CreatedAt) || got.Text != first.Text || got.SenderID != alice {
|
||||
t.Fatalf("replay returned %+v, want the stored %+v", got, first)
|
||||
}
|
||||
if n := f.count(t, c.ID); n != 1 {
|
||||
t.Fatalf("history has %d messages, want 1", n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppendOnceTakesOverExpiredKey(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
alice := f.mustUser(t)
|
||||
c := f.mustChat(t, alice)
|
||||
f.appendOnce(t, f.newMessage(t, c.ID, alice), "k1")
|
||||
expire(t)
|
||||
next := f.newMessage(t, c.ID, alice)
|
||||
if got := f.appendOnce(t, next, "k1"); got.ID != next.ID {
|
||||
t.Fatalf("expired key returned %v, want the new message %v", got.ID, next.ID)
|
||||
}
|
||||
if n := f.count(t, c.ID); n != 2 {
|
||||
t.Fatalf("history has %d messages, want 2", n)
|
||||
}
|
||||
if target := keyTarget(t, "k1"); target != next.ID {
|
||||
t.Fatalf("key points to %v, want %v", target, next.ID)
|
||||
}
|
||||
}
|
||||
|
||||
// A cleaner batch holds the expired key row; the request waits for it and,
|
||||
// once the row is gone, inserts its message under the now free key.
|
||||
func TestAppendOnceWaitsForCleanerThenInserts(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
alice := f.mustUser(t)
|
||||
c := f.mustChat(t, alice)
|
||||
f.appendOnce(t, f.newMessage(t, c.ID, alice), "k1")
|
||||
expire(t)
|
||||
|
||||
tx, err := db.Pool.Begin(f.ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer tx.Rollback(f.ctx)
|
||||
if _, err := tx.Exec(f.ctx, `SELECT 1 FROM message_idempotency_keys WHERE key = 'k1' FOR UPDATE`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
next := f.newMessage(t, c.ID, alice)
|
||||
type result struct {
|
||||
m chat.Message
|
||||
err error
|
||||
}
|
||||
done := make(chan result, 1)
|
||||
go func() {
|
||||
m, err := f.messages.AppendOnce(f.ctx, next, "k1", keyTTL)
|
||||
done <- result{m, err}
|
||||
}()
|
||||
select {
|
||||
case r := <-done:
|
||||
t.Fatalf("AppendOnce did not wait for the locked key: %+v", r)
|
||||
case <-time.After(300 * time.Millisecond):
|
||||
}
|
||||
if _, err := tx.Exec(f.ctx, `DELETE FROM message_idempotency_keys WHERE key = 'k1'`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := tx.Commit(f.ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
select {
|
||||
case r := <-done:
|
||||
if r.err != nil || r.m.ID != next.ID {
|
||||
t.Fatalf("after the cleaner: %+v, %v; want the new message %v", r.m, r.err, next.ID)
|
||||
}
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("AppendOnce did not finish after the cleaner committed")
|
||||
}
|
||||
}
|
||||
|
||||
// A request holding a key row must not make the cleaner wait: it skips the
|
||||
// row and deletes it on a later run.
|
||||
func TestCleanerSkipsRowLockedByRequest(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
alice := f.mustUser(t)
|
||||
c := f.mustChat(t, alice)
|
||||
f.appendOnce(t, f.newMessage(t, c.ID, alice), "k1")
|
||||
expire(t)
|
||||
|
||||
tx, err := db.Pool.Begin(f.ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := tx.Exec(f.ctx, `SELECT 1 FROM message_idempotency_keys WHERE key = 'k1' FOR UPDATE`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
start := time.Now()
|
||||
n, err := f.messages.DeleteExpiredKeys(f.ctx, keyTTL, 1000)
|
||||
if err != nil || n != 0 {
|
||||
t.Fatalf("cleaner over a locked row: deleted %d, %v; want 0", n, err)
|
||||
}
|
||||
if waited := time.Since(start); waited > time.Second {
|
||||
t.Fatalf("cleaner waited %v for the locked row", waited)
|
||||
}
|
||||
if err := tx.Rollback(f.ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n, err := f.messages.DeleteExpiredKeys(f.ctx, keyTTL, 1000); err != nil || n != 1 {
|
||||
t.Fatalf("cleaner after release: deleted %d, %v; want 1", n, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteExpiredKeysOnlyExpiredAndUpToLimit(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
alice := f.mustUser(t)
|
||||
c := f.mustChat(t, alice)
|
||||
for _, k := range []string{"a", "b", "c"} {
|
||||
f.appendOnce(t, f.newMessage(t, c.ID, alice), k)
|
||||
}
|
||||
expire(t)
|
||||
f.appendOnce(t, f.newMessage(t, c.ID, alice), "fresh")
|
||||
|
||||
if n, err := f.messages.DeleteExpiredKeys(f.ctx, keyTTL, 2); err != nil || n != 2 {
|
||||
t.Fatalf("first batch deleted %d, %v; want 2", n, err)
|
||||
}
|
||||
if n, err := f.messages.DeleteExpiredKeys(f.ctx, keyTTL, 2); err != nil || n != 1 {
|
||||
t.Fatalf("second batch deleted %d, %v; want 1", n, err)
|
||||
}
|
||||
var left int
|
||||
if err := db.Pool.QueryRow(f.ctx, `SELECT count(*) FROM message_idempotency_keys`).Scan(&left); err != nil || left != 1 {
|
||||
t.Fatalf("%d keys left, %v; want only the fresh one", left, err)
|
||||
}
|
||||
if n := f.count(t, c.ID); n != 4 {
|
||||
t.Fatalf("cleanup removed messages: %d left, want 4", n)
|
||||
}
|
||||
}
|
||||
|
||||
// Every request brings its own new message under one key; exactly one is
|
||||
// stored and all of them get it back. This also proves the key row and the
|
||||
// message it references are written by one statement.
|
||||
func TestConcurrentAppendOnceSameKeyStoresOneMessage(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
alice := f.mustUser(t)
|
||||
c := f.mustChat(t, alice)
|
||||
const requests = 20
|
||||
ids := make(chan uuid.UUID, requests)
|
||||
var wg sync.WaitGroup
|
||||
for range requests {
|
||||
m := f.newMessage(t, c.ID, alice)
|
||||
wg.Go(func() {
|
||||
got, err := f.messages.AppendOnce(f.ctx, m, "same", keyTTL)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
ids <- got.ID
|
||||
})
|
||||
}
|
||||
wg.Wait()
|
||||
close(ids)
|
||||
seen := map[uuid.UUID]int{}
|
||||
for id := range ids {
|
||||
seen[id]++
|
||||
}
|
||||
if len(seen) != 1 {
|
||||
t.Fatalf("requests got %d different messages, want 1: %v", len(seen), seen)
|
||||
}
|
||||
if n := f.count(t, c.ID); n != 1 {
|
||||
t.Fatalf("history has %d messages, want 1", n)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,276 @@
|
||||
// Package store holds the repository implementations of the chats module.
|
||||
//
|
||||
// Keyset pages use `id > COALESCE($after, nil uuid)`: every UUIDv7 is
|
||||
// greater than the nil UUID, so one query serves the first page and the
|
||||
// following ones, and the condition stays a range the index can scan. Ids
|
||||
// are UUIDv7 and created_at derives from them, so ordering by id is the
|
||||
// contract order (created_at, id). Errors pass through postgres.Classify, so
|
||||
// an unreachable database surfaces as unavailability.
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"messenger/internal/attachment"
|
||||
"messenger/internal/chat"
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/postgres"
|
||||
)
|
||||
|
||||
type Chats struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
var _ chat.ChatRepository = (*Chats)(nil)
|
||||
|
||||
func NewChats(pool *pgxpool.Pool) *Chats {
|
||||
return &Chats{pool: pool}
|
||||
}
|
||||
|
||||
func (r *Chats) Create(ctx context.Context, c chat.Chat, memberIDs []uuid.UUID) error {
|
||||
return postgres.Classify(pgx.BeginFunc(ctx, r.pool, func(tx pgx.Tx) error {
|
||||
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 fmt.Errorf("insert chat: %w", err)
|
||||
}
|
||||
if _, err := tx.Exec(ctx,
|
||||
`INSERT INTO chat_members (chat_id, user_id) SELECT $1, unnest($2::uuid[])`,
|
||||
c.ID, memberIDs); err != nil {
|
||||
return fmt.Errorf("insert chat members: %w", err)
|
||||
}
|
||||
return nil
|
||||
}))
|
||||
}
|
||||
|
||||
func (r *Chats) GetForMember(ctx context.Context, chatID, userID uuid.UUID) (chat.ChatWithMembers, error) {
|
||||
row := r.pool.QueryRow(ctx, `
|
||||
SELECT c.id, c.title, c.created_at,
|
||||
ARRAY(SELECT m.user_id FROM chat_members m WHERE m.chat_id = c.id ORDER BY m.user_id)
|
||||
FROM chats c
|
||||
WHERE c.id = $1
|
||||
AND EXISTS (SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2)`,
|
||||
chatID, userID)
|
||||
c, err := scanChat(row)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return chat.ChatWithMembers{}, chat.ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return chat.ChatWithMembers{}, fmt.Errorf("select chat: %w", postgres.Classify(err))
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (r *Chats) IsMember(ctx context.Context, chatID, userID uuid.UUID) (bool, error) {
|
||||
var ok bool
|
||||
err := r.pool.QueryRow(ctx,
|
||||
`SELECT EXISTS (SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2)`,
|
||||
chatID, userID).Scan(&ok)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("check membership: %w", postgres.Classify(err))
|
||||
}
|
||||
return ok, nil
|
||||
}
|
||||
|
||||
func (r *Chats) ListByMember(ctx context.Context, userID uuid.UUID, after *uuid.UUID, limit int) ([]chat.ChatWithMembers, error) {
|
||||
rows, err := r.pool.Query(ctx, `
|
||||
SELECT c.id, c.title, c.created_at,
|
||||
ARRAY(SELECT m.user_id FROM chat_members m WHERE m.chat_id = c.id ORDER BY m.user_id)
|
||||
FROM chat_members own
|
||||
JOIN chats c ON c.id = own.chat_id
|
||||
WHERE own.user_id = $1
|
||||
AND own.chat_id > COALESCE($2, '00000000-0000-0000-0000-000000000000'::uuid)
|
||||
ORDER BY own.chat_id
|
||||
LIMIT $3`,
|
||||
userID, after, limit)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("select chats: %w", postgres.Classify(err))
|
||||
}
|
||||
chats, err := pgx.CollectRows(rows, func(row pgx.CollectableRow) (chat.ChatWithMembers, error) {
|
||||
return scanChat(row)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read chats: %w", postgres.Classify(err))
|
||||
}
|
||||
return chats, nil
|
||||
}
|
||||
|
||||
func scanChat(row pgx.Row) (chat.ChatWithMembers, error) {
|
||||
var c chat.ChatWithMembers
|
||||
if err := row.Scan(&c.ID, &c.Title, &c.CreatedAt, &c.MemberIDs); err != nil {
|
||||
return chat.ChatWithMembers{}, err
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
type Messages struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
var _ chat.MessageRepository = (*Messages)(nil)
|
||||
|
||||
func NewMessages(pool *pgxpool.Pool) *Messages {
|
||||
return &Messages{pool: pool}
|
||||
}
|
||||
|
||||
// claimSQL binds the key and inserts the message in one statement. An expired
|
||||
// key is taken over; a valid one leaves claimed and the message insert empty.
|
||||
const claimSQL = `
|
||||
WITH claimed AS (
|
||||
INSERT INTO message_idempotency_keys AS k (user_id, chat_id, key, message_id)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (user_id, chat_id, key) DO UPDATE
|
||||
SET message_id = EXCLUDED.message_id, created_at = now()
|
||||
WHERE k.created_at < now() - $5::interval
|
||||
RETURNING message_id)
|
||||
INSERT INTO messages (chat_id, id, sender_id, text, created_at)
|
||||
SELECT $2, message_id, $1, $6, $7 FROM claimed`
|
||||
|
||||
// The transaction keeps attachment readiness, message joins and the key atomic.
|
||||
// The replay uses a new statement snapshot to see a concurrent winner.
|
||||
func (r *Messages) AppendOnce(ctx context.Context, m chat.Message, key string, ttl time.Duration) (chat.Message, error) {
|
||||
var stored chat.Message
|
||||
err := pgx.BeginFunc(ctx, r.pool, func(tx pgx.Tx) error {
|
||||
if err := validateAttachments(ctx, tx, m.ChatID, m.AttachmentIDs); err != nil {
|
||||
return err
|
||||
}
|
||||
for {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
tag, err := tx.Exec(ctx, claimSQL,
|
||||
m.SenderID, m.ChatID, key, m.ID, interval(ttl), m.Text, m.CreatedAt)
|
||||
if err != nil {
|
||||
return fmt.Errorf("insert message: %w", err)
|
||||
}
|
||||
if tag.RowsAffected() == 1 {
|
||||
if len(m.AttachmentIDs) > 0 {
|
||||
_, err := tx.Exec(ctx, `
|
||||
INSERT INTO message_attachments (chat_id, message_id, attachment_id, ordinal)
|
||||
SELECT $1, $2, id, (ord - 1)::smallint
|
||||
FROM unnest($3::uuid[]) WITH ORDINALITY AS a(id, ord)`,
|
||||
m.ChatID, m.ID, m.AttachmentIDs)
|
||||
if err != nil {
|
||||
return fmt.Errorf("insert message attachments: %w", err)
|
||||
}
|
||||
}
|
||||
stored = m
|
||||
return nil
|
||||
}
|
||||
stored, err = scanMessage(tx.QueryRow(ctx, `
|
||||
SELECT m.id, m.chat_id, m.sender_id, m.text, m.created_at,
|
||||
ARRAY(SELECT a.attachment_id FROM message_attachments a
|
||||
WHERE a.chat_id = m.chat_id AND a.message_id = m.id ORDER BY a.ordinal)
|
||||
FROM message_idempotency_keys k
|
||||
JOIN messages m ON m.chat_id = k.chat_id AND m.id = k.message_id
|
||||
WHERE k.user_id = $1 AND k.chat_id = $2 AND k.key = $3`,
|
||||
m.SenderID, m.ChatID, key))
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
// A vanished key is available again; retry its atomic claim.
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("select message by key: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
return chat.Message{}, postgres.Classify(err)
|
||||
}
|
||||
return stored, nil
|
||||
}
|
||||
|
||||
func validateAttachments(ctx context.Context, tx pgx.Tx, chatID uuid.UUID, ids []uuid.UUID) error {
|
||||
if len(ids) == 0 {
|
||||
return nil
|
||||
}
|
||||
// Shared row locks prevent deletion or a readiness change until joins commit.
|
||||
rows, err := tx.Query(ctx, `
|
||||
SELECT f.status
|
||||
FROM attachments a
|
||||
JOIN files f ON f.hash = a.file_hash
|
||||
WHERE a.chat_id = $1 AND a.id = ANY($2::uuid[])
|
||||
ORDER BY a.id
|
||||
FOR SHARE OF a, f`, chatID, ids)
|
||||
if err != nil {
|
||||
return fmt.Errorf("check attachments: %w", err)
|
||||
}
|
||||
statuses, err := pgx.CollectRows(rows, pgx.RowTo[attachment.FileStatus])
|
||||
if err != nil {
|
||||
return fmt.Errorf("read attachment status: %w", err)
|
||||
}
|
||||
if len(statuses) != len(ids) {
|
||||
return apperr.NotFound("attachment not found")
|
||||
}
|
||||
for _, status := range statuses {
|
||||
if status != attachment.FileReady {
|
||||
return apperr.Conflict("attachment is not ready")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func scanMessage(row pgx.Row) (chat.Message, error) {
|
||||
var m chat.Message
|
||||
err := row.Scan(&m.ID, &m.ChatID, &m.SenderID, &m.Text, &m.CreatedAt, &m.AttachmentIDs)
|
||||
if len(m.AttachmentIDs) == 0 {
|
||||
m.AttachmentIDs = nil
|
||||
}
|
||||
return m, err
|
||||
}
|
||||
|
||||
// DeleteExpiredKeys skips rows locked by another cleaner or a request rather
|
||||
// than waiting for them.
|
||||
func (r *Messages) DeleteExpiredKeys(ctx context.Context, olderThan time.Duration, limit int) (int64, error) {
|
||||
tag, err := r.pool.Exec(ctx, `
|
||||
DELETE FROM message_idempotency_keys
|
||||
WHERE (user_id, chat_id, key) IN (
|
||||
SELECT user_id, chat_id, key FROM message_idempotency_keys
|
||||
WHERE created_at < now() - $1::interval
|
||||
ORDER BY created_at
|
||||
LIMIT $2
|
||||
FOR UPDATE SKIP LOCKED)`,
|
||||
interval(olderThan), limit)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("delete expired keys: %w", postgres.Classify(err))
|
||||
}
|
||||
return tag.RowsAffected(), nil
|
||||
}
|
||||
|
||||
// interval renders d for a ::interval parameter.
|
||||
func interval(d time.Duration) string {
|
||||
return fmt.Sprintf("%d milliseconds", d.Milliseconds())
|
||||
}
|
||||
|
||||
// ListByChat keeps keyset pagination on messages and collects each ordered
|
||||
// attachment array in the same statement.
|
||||
func (r *Messages) ListByChat(ctx context.Context, chatID uuid.UUID, after *uuid.UUID, limit int) ([]chat.Message, error) {
|
||||
rows, err := r.pool.Query(ctx, `
|
||||
SELECT m.id, m.chat_id, m.sender_id, m.text, m.created_at,
|
||||
ARRAY(SELECT a.attachment_id FROM message_attachments a
|
||||
WHERE a.chat_id = m.chat_id AND a.message_id = m.id ORDER BY a.ordinal)
|
||||
FROM messages m
|
||||
WHERE m.chat_id = $1
|
||||
AND m.id > COALESCE($2, '00000000-0000-0000-0000-000000000000'::uuid)
|
||||
ORDER BY id
|
||||
LIMIT $3`,
|
||||
chatID, after, limit)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("select messages: %w", postgres.Classify(err))
|
||||
}
|
||||
messages, err := pgx.CollectRows(rows, func(row pgx.CollectableRow) (chat.Message, error) {
|
||||
return scanMessage(row)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read messages: %w", postgres.Classify(err))
|
||||
}
|
||||
return messages, nil
|
||||
}
|
||||
@@ -0,0 +1,321 @@
|
||||
package store_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/chat"
|
||||
"messenger/internal/chat/store"
|
||||
"messenger/internal/platform/ident"
|
||||
"messenger/internal/testenv"
|
||||
"messenger/internal/user"
|
||||
userstore "messenger/internal/user/store"
|
||||
)
|
||||
|
||||
var db *testenv.DB
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
var err error
|
||||
db, err = testenv.StartPostgres(context.Background())
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
code := m.Run()
|
||||
db.Close()
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
type fixture struct {
|
||||
ctx context.Context
|
||||
chats *store.Chats
|
||||
messages *store.Messages
|
||||
}
|
||||
|
||||
func newFixture(t *testing.T) fixture {
|
||||
t.Helper()
|
||||
db.Reset(t)
|
||||
return fixture{ctx: context.Background(), chats: store.NewChats(db.Pool), messages: store.NewMessages(db.Pool)}
|
||||
}
|
||||
|
||||
// mustUser stores a user row: chats and messages reference users by FK.
|
||||
func (f fixture) mustUser(t *testing.T) uuid.UUID {
|
||||
t.Helper()
|
||||
id := ident.New()
|
||||
u, err := user.New(id, "u"+id.String()[24:], "User")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
u.PasswordHash = "x"
|
||||
if err := userstore.NewUsers(db.Pool).Create(f.ctx, u); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
func (f fixture) mustChat(t *testing.T, members ...uuid.UUID) chat.Chat {
|
||||
t.Helper()
|
||||
c, err := chat.NewChat(ident.New(), "title")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := f.chats.Create(f.ctx, c, members); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func (f fixture) newMessage(t *testing.T, chatID, sender uuid.UUID) chat.Message {
|
||||
t.Helper()
|
||||
m, err := chat.NewMessage(ident.New(), chatID, sender, "text")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// appendErr stores m under a key of its own, as every send now carries one.
|
||||
func (f fixture) appendErr(m chat.Message) error {
|
||||
_, err := f.messages.AppendOnce(f.ctx, m, m.ID.String(), time.Hour)
|
||||
return err
|
||||
}
|
||||
|
||||
func TestChatCreateThenGetForMember(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
alice, bob := f.mustUser(t), f.mustUser(t)
|
||||
c := f.mustChat(t, alice, bob)
|
||||
|
||||
got, err := f.chats.GetForMember(f.ctx, c.ID, bob)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.Chat != c || len(got.MemberIDs) != 2 || got.MemberIDs[0] != alice || got.MemberIDs[1] != bob {
|
||||
t.Fatalf("GetForMember = %+v, want %+v with members [alice bob]", got, c)
|
||||
}
|
||||
if _, err := f.chats.GetForMember(f.ctx, c.ID, f.mustUser(t)); !errors.Is(err, chat.ErrNotFound) {
|
||||
t.Fatalf("outsider must get ErrNotFound, got %v", err)
|
||||
}
|
||||
for who, want := range map[uuid.UUID]bool{alice: true, bob: true, ident.New(): false} {
|
||||
ok, err := f.chats.IsMember(f.ctx, c.ID, who)
|
||||
if err != nil || ok != want {
|
||||
t.Fatalf("IsMember(%s) = %v, %v; want %v", who, ok, err, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The chat row and its members are written in one transaction: a failing
|
||||
// member insert must leave no chat behind.
|
||||
func TestCreateRollsBackWhenMemberInsertFails(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
alice := f.mustUser(t)
|
||||
c, err := chat.NewChat(ident.New(), "half-made")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := f.chats.Create(f.ctx, c, []uuid.UUID{alice, ident.New()}); err == nil {
|
||||
t.Fatal("Create with an unknown member must fail")
|
||||
}
|
||||
var chats, members int
|
||||
if err := db.Pool.QueryRow(f.ctx, `SELECT (SELECT count(*) FROM chats), (SELECT count(*) FROM chat_members)`).Scan(&chats, &members); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if chats != 0 || members != 0 {
|
||||
t.Fatalf("chats = %d, members = %d after a failed create; want 0 and 0", chats, members)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatUnknownID(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
if _, err := f.chats.GetForMember(f.ctx, ident.New(), ident.New()); !errors.Is(err, chat.ErrNotFound) {
|
||||
t.Fatalf("GetForMember err = %v, want ErrNotFound", err)
|
||||
}
|
||||
if ok, err := f.chats.IsMember(f.ctx, ident.New(), ident.New()); err != nil || ok {
|
||||
t.Fatalf("IsMember on unknown chat = %v, %v; want false, nil", ok, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListByMemberFiltersOrdersAndPages(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
alice, bob := f.mustUser(t), f.mustUser(t)
|
||||
var mine []chat.Chat
|
||||
for range 5 {
|
||||
c, err := chat.NewChat(ident.New(), "title")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mine = append(mine, c)
|
||||
// A chat without alice must never show up in her listing.
|
||||
f.mustChat(t, bob)
|
||||
}
|
||||
// Insert the oldest chat last to prove the store sorts, not appends.
|
||||
for _, c := range append(mine[1:], mine[0]) {
|
||||
if err := f.chats.Create(f.ctx, c, []uuid.UUID{alice, bob}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
page, err := f.chats.ListByMember(f.ctx, alice, nil, 3)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(page) != 3 || page[0].ID != mine[0].ID || page[2].ID != mine[2].ID {
|
||||
t.Fatalf("first page mismatch: %v", page)
|
||||
}
|
||||
if len(page[0].MemberIDs) != 2 || page[0].MemberIDs[0] != alice {
|
||||
t.Fatalf("listing must carry members in id order: %+v", page[0])
|
||||
}
|
||||
last := page[2].ID
|
||||
rest, err := f.chats.ListByMember(f.ctx, alice, &last, 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(rest) != 2 || rest[0].ID != mine[3].ID || rest[1].ID != mine[4].ID {
|
||||
t.Fatalf("second page mismatch: %v", rest)
|
||||
}
|
||||
none, err := f.chats.ListByMember(f.ctx, ident.New(), nil, 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if none == nil || len(none) != 0 {
|
||||
t.Fatalf("unknown member must get an empty non-nil slice, got %#v", none)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListByChatUnknownChatIsEmpty(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
got, err := f.messages.ListByChat(f.ctx, ident.New(), nil, 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got == nil || len(got) != 0 {
|
||||
t.Fatalf("want empty non-nil slice, got %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppendOutOfOrderIsListedByID(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
alice := f.mustUser(t)
|
||||
c := f.mustChat(t, alice)
|
||||
first, second, third := f.newMessage(t, c.ID, alice), f.newMessage(t, c.ID, alice), f.newMessage(t, c.ID, alice)
|
||||
for _, m := range []chat.Message{third, first, second} {
|
||||
if err := f.appendErr(m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
got, err := f.messages.ListByChat(f.ctx, c.ID, nil, 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got) != 3 || !reflect.DeepEqual(got[0], first) || !reflect.DeepEqual(got[1], second) || !reflect.DeepEqual(got[2], third) {
|
||||
t.Fatalf("history must be ordered by id and round-trip every field, got %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagingWalksWholeHistoryWithoutGapsOrDuplicates(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
alice := f.mustUser(t)
|
||||
c := f.mustChat(t, alice)
|
||||
const total = 250
|
||||
var expected []uuid.UUID
|
||||
for range total {
|
||||
m := f.newMessage(t, c.ID, alice)
|
||||
expected = append(expected, m.ID)
|
||||
if err := f.appendErr(m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
// Another chat's history must not leak into this one.
|
||||
other := f.mustChat(t, alice)
|
||||
if err := f.appendErr(f.newMessage(t, other.ID, alice)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, limit := range []int{1, 7, 100} {
|
||||
var seen []uuid.UUID
|
||||
var after *uuid.UUID
|
||||
for {
|
||||
page, err := f.messages.ListByChat(f.ctx, c.ID, after, limit)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(page) == 0 {
|
||||
break
|
||||
}
|
||||
if len(page) > limit {
|
||||
t.Fatalf("limit %d: page has %d items", limit, len(page))
|
||||
}
|
||||
for _, m := range page {
|
||||
seen = append(seen, m.ID)
|
||||
}
|
||||
last := page[len(page)-1].ID
|
||||
after = &last
|
||||
}
|
||||
if len(seen) != total {
|
||||
t.Fatalf("limit %d: walked %d messages, want %d", limit, len(seen), total)
|
||||
}
|
||||
for i := range seen {
|
||||
if seen[i] != expected[i] {
|
||||
t.Fatalf("limit %d: position %d has %s, want %s", limit, i, seen[i], expected[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAfterUnknownIDStartsAtNextGreater(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
alice := f.mustUser(t)
|
||||
c := f.mustChat(t, alice)
|
||||
a := f.newMessage(t, c.ID, alice)
|
||||
gap := ident.New() // never stored, but sorts between a and b
|
||||
b := f.newMessage(t, c.ID, alice)
|
||||
for _, m := range []chat.Message{a, b} {
|
||||
if err := f.appendErr(m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
got, err := f.messages.ListByChat(f.ctx, c.ID, &gap, 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got) != 1 || got[0].ID != b.ID {
|
||||
t.Fatalf("want only b after the gap id, got %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConcurrentAppendsKeepEveryMessage(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
alice := f.mustUser(t)
|
||||
c := f.mustChat(t, alice)
|
||||
const clients, perClient = 10, 10
|
||||
var wg sync.WaitGroup
|
||||
for range clients {
|
||||
wg.Go(func() {
|
||||
for range perClient {
|
||||
if err := f.appendErr(f.newMessage(t, c.ID, alice)); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
wg.Wait()
|
||||
got, err := f.messages.ListByChat(f.ctx, c.ID, nil, clients*perClient+1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got) != clients*perClient {
|
||||
t.Fatalf("stored %d messages, want %d", len(got), clients*perClient)
|
||||
}
|
||||
for i := 1; i < len(got); i++ {
|
||||
if bytes.Compare(got[i-1].ID[:], got[i].ID[:]) >= 0 {
|
||||
t.Fatalf("history out of order at %d", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
// Package health serves the probes. Liveness only proves the process
|
||||
// answers. Readiness means the process can serve the synchronous API: it is
|
||||
// switched off during shutdown, so an orchestrator stops routing new traffic
|
||||
// before the listener closes, and it fails while a critical dependency is
|
||||
// unreachable.
|
||||
package health
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/httpx"
|
||||
)
|
||||
|
||||
// checkTimeout is shared by all checks of one probe, so the probe answers
|
||||
// within it even when every dependency hangs; the Docker healthcheck allows
|
||||
// two seconds.
|
||||
const checkTimeout = time.Second
|
||||
|
||||
// Check is a dependency the API cannot serve requests without.
|
||||
type Check struct {
|
||||
Name string
|
||||
Probe func(ctx context.Context) error
|
||||
}
|
||||
|
||||
type Handler struct {
|
||||
ready atomic.Bool
|
||||
checks []Check
|
||||
}
|
||||
|
||||
type status struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
func New(checks ...Check) *Handler {
|
||||
h := &Handler{checks: checks}
|
||||
h.ready.Store(true)
|
||||
return h
|
||||
}
|
||||
|
||||
func (h *Handler) SetReady(ready bool) {
|
||||
h.ready.Store(ready)
|
||||
}
|
||||
|
||||
func (h *Handler) IsReady() bool {
|
||||
return h.ready.Load()
|
||||
}
|
||||
|
||||
func (h *Handler) Register(r gin.IRouter) {
|
||||
r.GET("/health/live", h.live)
|
||||
r.GET("/health/ready", h.readyz)
|
||||
}
|
||||
|
||||
func (h *Handler) live(c *gin.Context) {
|
||||
httpx.JSON(c, http.StatusOK, status{Status: "ok"})
|
||||
}
|
||||
|
||||
func (h *Handler) readyz(c *gin.Context) {
|
||||
if !h.ready.Load() {
|
||||
httpx.Error(c, apperr.Unavailable("service is shutting down"))
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), checkTimeout)
|
||||
defer cancel()
|
||||
for _, check := range h.checks {
|
||||
if err := check.Probe(ctx); err != nil {
|
||||
httpx.Error(c, apperr.Wrap(apperr.KindUnavailable, check.Name+" is unavailable", err))
|
||||
return
|
||||
}
|
||||
}
|
||||
httpx.JSON(c, http.StatusOK, status{Status: "ok"})
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
// Package apperr is the application's error vocabulary: every module reports
|
||||
// failures as a Kind plus a message that is safe to show to clients, and the
|
||||
// transport layer owns the only mapping from Kind to HTTP status. Causes are
|
||||
// kept for logs but never rendered.
|
||||
package apperr
|
||||
|
||||
import "errors"
|
||||
|
||||
type Kind int
|
||||
|
||||
const (
|
||||
KindInternal Kind = iota
|
||||
KindInvalid
|
||||
KindUnauthorized
|
||||
KindNotFound
|
||||
KindConflict
|
||||
KindPayloadTooLarge
|
||||
KindUnsupportedMedia
|
||||
KindUnavailable
|
||||
)
|
||||
|
||||
type Error struct {
|
||||
Kind Kind
|
||||
Message string
|
||||
Cause error
|
||||
}
|
||||
|
||||
func (e *Error) Error() string { return e.Message }
|
||||
|
||||
func (e *Error) Unwrap() error { return e.Cause }
|
||||
|
||||
func New(kind Kind, message string) *Error {
|
||||
return &Error{Kind: kind, Message: message}
|
||||
}
|
||||
|
||||
func Wrap(kind Kind, message string, cause error) *Error {
|
||||
return &Error{Kind: kind, Message: message, Cause: cause}
|
||||
}
|
||||
|
||||
// KindOf classifies any error; foreign errors are internal.
|
||||
func KindOf(err error) Kind {
|
||||
if e, ok := errors.AsType[*Error](err); ok {
|
||||
return e.Kind
|
||||
}
|
||||
return KindInternal
|
||||
}
|
||||
|
||||
func Invalid(message string) *Error { return New(KindInvalid, message) }
|
||||
func Unauthorized(message string) *Error { return New(KindUnauthorized, message) }
|
||||
func NotFound(message string) *Error { return New(KindNotFound, message) }
|
||||
func Conflict(message string) *Error { return New(KindConflict, message) }
|
||||
func PayloadTooLarge(message string) *Error { return New(KindPayloadTooLarge, message) }
|
||||
func UnsupportedMedia(message string) *Error { return New(KindUnsupportedMedia, message) }
|
||||
func Unavailable(message string) *Error { return New(KindUnavailable, message) }
|
||||
@@ -0,0 +1,49 @@
|
||||
package apperr_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"messenger/internal/platform/apperr"
|
||||
)
|
||||
|
||||
func TestKindOfReturnsKindForApplicationError(t *testing.T) {
|
||||
err := apperr.NotFound("chat not found")
|
||||
if got := apperr.KindOf(err); got != apperr.KindNotFound {
|
||||
t.Fatalf("KindOf = %v, want KindNotFound", got)
|
||||
}
|
||||
if err.Error() != "chat not found" {
|
||||
t.Fatalf("Error() = %q", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestKindOfSeesThroughWrapping(t *testing.T) {
|
||||
err := fmt.Errorf("service: %w", apperr.Conflict("username taken"))
|
||||
if got := apperr.KindOf(err); got != apperr.KindConflict {
|
||||
t.Fatalf("KindOf = %v, want KindConflict", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKindOfDefaultsToInternal(t *testing.T) {
|
||||
if got := apperr.KindOf(errors.New("boom")); got != apperr.KindInternal {
|
||||
t.Fatalf("KindOf = %v, want KindInternal", got)
|
||||
}
|
||||
if got := apperr.KindOf(nil); got != apperr.KindInternal {
|
||||
t.Fatalf("KindOf(nil) = %v, want KindInternal", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrapKeepsCause(t *testing.T) {
|
||||
cause := errors.New("cause")
|
||||
err := apperr.Wrap(apperr.KindInvalid, "bad input", cause)
|
||||
if !errors.Is(err, cause) {
|
||||
t.Fatal("wrapped error must unwrap to cause")
|
||||
}
|
||||
if apperr.KindOf(err) != apperr.KindInvalid {
|
||||
t.Fatal("wrapped error must keep its kind")
|
||||
}
|
||||
if err.Error() != "bad input" {
|
||||
t.Fatalf("Error() = %q, want public message only", err.Error())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
// Package config reads the process configuration from the environment.
|
||||
// Settings have defaults suited to the lab stand, except credentials, which
|
||||
// are required and read from files named by *_FILE variables so that their
|
||||
// values never appear in the environment. An invalid value stops the process
|
||||
// at start-up with a message naming the variable, never the secret.
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Allow a 30s binary request, its independent 3s cleanup, and 5s margin.
|
||||
const defaultShutdownTimeout = 38 * time.Second
|
||||
|
||||
type Logging struct {
|
||||
Env string
|
||||
Level slog.Level
|
||||
}
|
||||
|
||||
func (l Logging) NewLogger() *slog.Logger {
|
||||
opts := &slog.HandlerOptions{Level: l.Level}
|
||||
if l.Env == "dev" {
|
||||
return slog.New(slog.NewTextHandler(os.Stdout, opts))
|
||||
}
|
||||
return slog.New(slog.NewJSONHandler(os.Stdout, opts))
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Logging
|
||||
Host string
|
||||
Port int
|
||||
ShutdownTimeout time.Duration
|
||||
MaxBodyBytes int64
|
||||
DatabaseURL string
|
||||
SessionStoreURL string
|
||||
SessionTTL time.Duration
|
||||
// DrainDelay keeps a stopping process serving with readiness off; it must
|
||||
// exceed the balancer's detection time.
|
||||
DrainDelay time.Duration
|
||||
IdempotencyTTL time.Duration
|
||||
DBMaxConns int32
|
||||
DebugAddr string
|
||||
S3 S3
|
||||
}
|
||||
|
||||
type Migrate struct {
|
||||
Logging
|
||||
DatabaseURL string
|
||||
}
|
||||
|
||||
// Lookup reads one variable, reporting whether it was set; os.LookupEnv
|
||||
// in production, a map in tests.
|
||||
type Lookup func(key string) (string, bool)
|
||||
|
||||
func Load(lookup Lookup) (Config, error) {
|
||||
cfg := Config{
|
||||
Host: "0.0.0.0",
|
||||
Port: 8080,
|
||||
ShutdownTimeout: defaultShutdownTimeout,
|
||||
MaxBodyBytes: 64 * 1024,
|
||||
SessionTTL: time.Hour,
|
||||
DrainDelay: 2 * time.Second,
|
||||
IdempotencyTTL: 24 * time.Hour,
|
||||
DBMaxConns: 10,
|
||||
}
|
||||
if v, ok := lookup("APP_HOST"); ok {
|
||||
if strings.TrimSpace(v) == "" {
|
||||
return Config{}, fmt.Errorf("APP_HOST must not be empty")
|
||||
}
|
||||
cfg.Host = v
|
||||
}
|
||||
if v, ok := lookup("APP_PORT"); ok {
|
||||
port, err := strconv.Atoi(v)
|
||||
if err != nil || port < 1 || port > 65535 {
|
||||
return Config{}, fmt.Errorf("APP_PORT must be an integer between 1 and 65535, got %q", v)
|
||||
}
|
||||
cfg.Port = port
|
||||
}
|
||||
var err error
|
||||
if cfg.Logging, err = loadLogging(lookup); err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
if v, ok := lookup("APP_SHUTDOWN_TIMEOUT"); ok {
|
||||
d, err := time.ParseDuration(v)
|
||||
if err != nil || d <= 0 {
|
||||
return Config{}, fmt.Errorf("APP_SHUTDOWN_TIMEOUT must be a positive duration such as 8s, got %q", v)
|
||||
}
|
||||
cfg.ShutdownTimeout = d
|
||||
}
|
||||
if v, ok := lookup("APP_MAX_BODY_BYTES"); ok {
|
||||
n, err := strconv.ParseInt(v, 10, 64)
|
||||
if err != nil || n <= 0 {
|
||||
return Config{}, fmt.Errorf("APP_MAX_BODY_BYTES must be a positive integer, got %q", v)
|
||||
}
|
||||
cfg.MaxBodyBytes = n
|
||||
}
|
||||
if v, ok := lookup("SESSION_TTL_SECONDS"); ok {
|
||||
n, err := strconv.ParseUint(v, 10, 32)
|
||||
if err != nil || n == 0 {
|
||||
return Config{}, fmt.Errorf("SESSION_TTL_SECONDS must be a positive whole number of seconds, got %q", v)
|
||||
}
|
||||
cfg.SessionTTL = time.Duration(n) * time.Second
|
||||
}
|
||||
if v, ok := lookup("APP_DRAIN_DELAY"); ok {
|
||||
d, err := time.ParseDuration(v)
|
||||
if err != nil || d < 0 {
|
||||
return Config{}, fmt.Errorf("APP_DRAIN_DELAY must be a non-negative duration such as 2s, got %q", v)
|
||||
}
|
||||
cfg.DrainDelay = d
|
||||
}
|
||||
if v, ok := lookup("IDEMPOTENCY_TTL_SECONDS"); ok {
|
||||
n, err := strconv.ParseUint(v, 10, 32)
|
||||
if err != nil || n == 0 {
|
||||
return Config{}, fmt.Errorf("IDEMPOTENCY_TTL_SECONDS must be a positive whole number of seconds, got %q", v)
|
||||
}
|
||||
cfg.IdempotencyTTL = time.Duration(n) * time.Second
|
||||
}
|
||||
if v, ok := lookup("DB_MAX_CONNS"); ok {
|
||||
n, err := strconv.ParseInt(v, 10, 32)
|
||||
if err != nil || n < 1 || n > 1000 {
|
||||
return Config{}, fmt.Errorf("DB_MAX_CONNS must be an integer between 1 and 1000, got %q", v)
|
||||
}
|
||||
cfg.DBMaxConns = int32(n)
|
||||
}
|
||||
if v, ok := lookup("APP_DEBUG_ADDR"); ok && v != "" {
|
||||
if _, _, err := net.SplitHostPort(v); err != nil {
|
||||
return Config{}, fmt.Errorf("APP_DEBUG_ADDR must be host:port such as :9090, got %q", v)
|
||||
}
|
||||
cfg.DebugAddr = v
|
||||
}
|
||||
if cfg.DatabaseURL, err = LoadDatabaseURL(lookup); err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
if cfg.SessionStoreURL, err = secret(lookup, "SESSION_STORE_URL_FILE"); err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
if _, ok := lookup("S3_ENDPOINT"); ok {
|
||||
if cfg.S3, err = LoadS3(lookup); err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func LoadMigrate(lookup Lookup) (Migrate, error) {
|
||||
var cfg Migrate
|
||||
var err error
|
||||
if cfg.Logging, err = loadLogging(lookup); err != nil {
|
||||
return Migrate{}, err
|
||||
}
|
||||
if cfg.DatabaseURL, err = LoadDatabaseURL(lookup); err != nil {
|
||||
return Migrate{}, err
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func LoadDatabaseURL(lookup Lookup) (string, error) {
|
||||
return secret(lookup, "DATABASE_URL_FILE")
|
||||
}
|
||||
|
||||
func loadLogging(lookup Lookup) (Logging, error) {
|
||||
l := Logging{Env: "lab", Level: slog.LevelInfo}
|
||||
if v, ok := lookup("APP_ENV"); ok && strings.TrimSpace(v) != "" {
|
||||
l.Env = v
|
||||
}
|
||||
if v, ok := lookup("APP_LOG_LEVEL"); ok {
|
||||
if err := l.Level.UnmarshalText([]byte(v)); err != nil {
|
||||
return Logging{}, fmt.Errorf("APP_LOG_LEVEL must be debug, info, warn or error, got %q", v)
|
||||
}
|
||||
}
|
||||
return l, nil
|
||||
}
|
||||
|
||||
// secret reads the file named by the variable; surrounding whitespace,
|
||||
// including the trailing newline editors add, is dropped.
|
||||
func secret(lookup Lookup, key string) (string, error) {
|
||||
path, ok := lookup(key)
|
||||
if !ok || strings.TrimSpace(path) == "" {
|
||||
return "", fmt.Errorf("%s must name a file with the value", key)
|
||||
}
|
||||
raw, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%s: cannot read %s: %w", key, path, err)
|
||||
}
|
||||
value := strings.TrimSpace(string(raw))
|
||||
if value == "" {
|
||||
return "", fmt.Errorf("%s: file %s is empty", key, path)
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func (c Config) Addr() string {
|
||||
return net.JoinHostPort(c.Host, strconv.Itoa(c.Port))
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
package config_test
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/internal/platform/config"
|
||||
)
|
||||
|
||||
func env(values map[string]string) func(string) (string, bool) {
|
||||
return func(key string) (string, bool) {
|
||||
v, ok := values[key]
|
||||
return v, ok
|
||||
}
|
||||
}
|
||||
|
||||
func secretFile(t *testing.T, content string) string {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), "secret")
|
||||
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
const (
|
||||
databaseURL = "postgres://app:s3cret@db:5432/messenger"
|
||||
sessionStoreURL = "redis://:s3cret@valkey:6379/0"
|
||||
)
|
||||
|
||||
// required holds the variables without defaults, merged with extra.
|
||||
func required(t *testing.T, extra map[string]string) map[string]string {
|
||||
t.Helper()
|
||||
values := map[string]string{
|
||||
"DATABASE_URL_FILE": secretFile(t, databaseURL+"\n"),
|
||||
"SESSION_STORE_URL_FILE": secretFile(t, sessionStoreURL+"\r\n"),
|
||||
}
|
||||
for k, v := range extra {
|
||||
values[k] = v
|
||||
}
|
||||
return values
|
||||
}
|
||||
|
||||
func TestDefaults(t *testing.T) {
|
||||
cfg, err := config.Load(env(required(t, nil)))
|
||||
if err != nil {
|
||||
t.Fatalf("Load error: %v", err)
|
||||
}
|
||||
if cfg.Addr() != "0.0.0.0:8080" {
|
||||
t.Fatalf("Addr = %q", cfg.Addr())
|
||||
}
|
||||
if cfg.Env != "lab" || cfg.Level != slog.LevelInfo {
|
||||
t.Fatalf("cfg = %+v", cfg)
|
||||
}
|
||||
if cfg.ShutdownTimeout != 38*time.Second || cfg.MaxBodyBytes != 65536 {
|
||||
t.Fatalf("cfg = %+v", cfg)
|
||||
}
|
||||
if cfg.DatabaseURL != databaseURL || cfg.SessionStoreURL != sessionStoreURL {
|
||||
t.Fatalf("urls = %q, %q; want the file contents without line endings", cfg.DatabaseURL, cfg.SessionStoreURL)
|
||||
}
|
||||
if cfg.SessionTTL != time.Hour {
|
||||
t.Fatalf("SessionTTL = %v, want 1h", cfg.SessionTTL)
|
||||
}
|
||||
if cfg.DrainDelay != 2*time.Second || cfg.IdempotencyTTL != 24*time.Hour || cfg.DBMaxConns != 10 || cfg.DebugAddr != "" {
|
||||
t.Fatalf("lab3 defaults = drain %v, key ttl %v, pool %d, debug %q", cfg.DrainDelay, cfg.IdempotencyTTL, cfg.DBMaxConns, cfg.DebugAddr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultShutdownTimeoutCoversBinaryLifecycle(t *testing.T) {
|
||||
cfg, err := config.Load(env(required(t, nil)))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Binary requests can use 30s, followed by 3s independent cleanup. Keep
|
||||
// another 5s for shutdown scheduling and closing the remaining resources.
|
||||
budget := 30*time.Second + 3*time.Second + 5*time.Second
|
||||
if cfg.ShutdownTimeout < budget {
|
||||
t.Fatalf("default shutdown budget %v cannot cover binary request and cleanup budget %v", cfg.ShutdownTimeout, budget)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSecretFilesAreRequiredAndNeverEchoed(t *testing.T) {
|
||||
for _, key := range []string{"DATABASE_URL_FILE", "SESSION_STORE_URL_FILE"} {
|
||||
cases := map[string]func(map[string]string){
|
||||
"unset": func(v map[string]string) { delete(v, key) },
|
||||
"empty path": func(v map[string]string) { v[key] = "" },
|
||||
"no file": func(v map[string]string) { v[key] = filepath.Join(t.TempDir(), "missing") },
|
||||
"blank file": func(v map[string]string) { v[key] = secretFile(t, " \r\n") },
|
||||
}
|
||||
for name, breakIt := range cases {
|
||||
values := required(t, nil)
|
||||
breakIt(values)
|
||||
_, err := config.Load(env(values))
|
||||
if err == nil {
|
||||
t.Errorf("%s %s: want error", key, name)
|
||||
continue
|
||||
}
|
||||
if !strings.Contains(err.Error(), key) || strings.Contains(err.Error(), "s3cret") {
|
||||
t.Errorf("%s %s: error must name the variable and hide the secret: %v", key, name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestOverrides(t *testing.T) {
|
||||
cfg, err := config.Load(env(required(t, map[string]string{
|
||||
"APP_HOST": "127.0.0.1",
|
||||
"APP_PORT": "9090",
|
||||
"APP_ENV": "dev",
|
||||
"APP_LOG_LEVEL": "debug",
|
||||
"APP_SHUTDOWN_TIMEOUT": "3s",
|
||||
"APP_MAX_BODY_BYTES": "1024",
|
||||
"SESSION_TTL_SECONDS": "300",
|
||||
"APP_DRAIN_DELAY": "0s",
|
||||
"IDEMPOTENCY_TTL_SECONDS": "60",
|
||||
"DB_MAX_CONNS": "25",
|
||||
"APP_DEBUG_ADDR": ":9090",
|
||||
})))
|
||||
if err != nil {
|
||||
t.Fatalf("Load error: %v", err)
|
||||
}
|
||||
if cfg.Addr() != "127.0.0.1:9090" || cfg.Env != "dev" || cfg.Level != slog.LevelDebug {
|
||||
t.Fatalf("cfg = %+v", cfg)
|
||||
}
|
||||
if cfg.ShutdownTimeout != 3*time.Second || cfg.MaxBodyBytes != 1024 || cfg.SessionTTL != 5*time.Minute {
|
||||
t.Fatalf("cfg = %+v", cfg)
|
||||
}
|
||||
if cfg.DrainDelay != 0 || cfg.IdempotencyTTL != time.Minute || cfg.DBMaxConns != 25 || cfg.DebugAddr != ":9090" {
|
||||
t.Fatalf("lab3 overrides = drain %v, key ttl %v, pool %d, debug %q", cfg.DrainDelay, cfg.IdempotencyTTL, cfg.DBMaxConns, cfg.DebugAddr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvalidValuesAreReported(t *testing.T) {
|
||||
cases := map[string]map[string]string{
|
||||
"port text": {"APP_PORT": "abc"},
|
||||
"port zero": {"APP_PORT": "0"},
|
||||
"port too big": {"APP_PORT": "70000"},
|
||||
"level": {"APP_LOG_LEVEL": "loud"},
|
||||
"timeout": {"APP_SHUTDOWN_TIMEOUT": "soon"},
|
||||
"timeout negative": {"APP_SHUTDOWN_TIMEOUT": "-1s"},
|
||||
"body zero": {"APP_MAX_BODY_BYTES": "0"},
|
||||
"body text": {"APP_MAX_BODY_BYTES": "big"},
|
||||
"empty host": {"APP_HOST": ""},
|
||||
"ttl zero": {"SESSION_TTL_SECONDS": "0"},
|
||||
"ttl negative": {"SESSION_TTL_SECONDS": "-5"},
|
||||
"ttl text": {"SESSION_TTL_SECONDS": "hour"},
|
||||
"ttl fraction": {"SESSION_TTL_SECONDS": "1.5"},
|
||||
"drain negative": {"APP_DRAIN_DELAY": "-1s"},
|
||||
"drain text": {"APP_DRAIN_DELAY": "abc"},
|
||||
"key ttl zero": {"IDEMPOTENCY_TTL_SECONDS": "0"},
|
||||
"key ttl text": {"IDEMPOTENCY_TTL_SECONDS": "day"},
|
||||
"pool zero": {"DB_MAX_CONNS": "0"},
|
||||
"pool too big": {"DB_MAX_CONNS": "1001"},
|
||||
"pool text": {"DB_MAX_CONNS": "many"},
|
||||
"debug addr": {"APP_DEBUG_ADDR": "nonsense"},
|
||||
}
|
||||
for name, values := range cases {
|
||||
_, err := config.Load(env(required(t, values)))
|
||||
if err == nil {
|
||||
t.Errorf("%s: want error", name)
|
||||
continue
|
||||
}
|
||||
for key := range values {
|
||||
if !strings.Contains(err.Error(), key) {
|
||||
t.Errorf("%s: error %q does not name %s", name, err, key)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadMigrateNeedsOnlyTheDatabaseURL(t *testing.T) {
|
||||
cfg, err := config.LoadMigrate(env(map[string]string{
|
||||
"DATABASE_URL_FILE": secretFile(t, databaseURL+"\n"),
|
||||
"APP_ENV": "dev",
|
||||
"APP_LOG_LEVEL": "warn",
|
||||
}))
|
||||
if err != nil {
|
||||
t.Fatalf("LoadMigrate error: %v", err)
|
||||
}
|
||||
if cfg.DatabaseURL != databaseURL || cfg.Env != "dev" || cfg.Level != slog.LevelWarn {
|
||||
t.Fatalf("cfg = %+v", cfg)
|
||||
}
|
||||
if _, err := config.LoadMigrate(env(map[string]string{})); err == nil || !strings.Contains(err.Error(), "DATABASE_URL_FILE") {
|
||||
t.Fatalf("missing database url: err = %v", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type S3 struct {
|
||||
Endpoint, Region, Bucket string
|
||||
AccessKey, SecretKey string
|
||||
MaxBytes, MaxPixels int64
|
||||
}
|
||||
|
||||
func LoadS3(lookup Lookup) (S3, error) {
|
||||
cfg := S3{Endpoint: "http://s3:8333", Region: "us-east-1", Bucket: "messenger-attachments", MaxBytes: 10485760, MaxPixels: 40000000}
|
||||
for key, dest := range map[string]*string{"S3_ENDPOINT": &cfg.Endpoint, "S3_REGION": &cfg.Region, "S3_BUCKET": &cfg.Bucket} {
|
||||
if v, ok := lookup(key); ok {
|
||||
*dest = v
|
||||
}
|
||||
}
|
||||
var err error
|
||||
if cfg.AccessKey, err = secret(lookup, "S3_ACCESS_KEY_ID_FILE"); err != nil {
|
||||
return S3{}, err
|
||||
}
|
||||
if cfg.SecretKey, err = secret(lookup, "S3_SECRET_ACCESS_KEY_FILE"); err != nil {
|
||||
return S3{}, err
|
||||
}
|
||||
for _, p := range []struct {
|
||||
key string
|
||||
dest *int64
|
||||
max int64
|
||||
}{{"MAX_UPLOAD_BYTES", &cfg.MaxBytes, 10485760}, {"MAX_IMAGE_PIXELS", &cfg.MaxPixels, 40000000}} {
|
||||
if v, ok := lookup(p.key); ok {
|
||||
n, e := strconv.ParseInt(v, 10, 64)
|
||||
if e != nil || n < 1 || n > p.max {
|
||||
return S3{}, fmt.Errorf("%s must be between 1 and %d", p.key, p.max)
|
||||
}
|
||||
*p.dest = n
|
||||
}
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
// ReconcileS3 selects the separate listing identity used by background scans.
|
||||
func ReconcileS3(lookup Lookup) Lookup {
|
||||
return func(key string) (string, bool) {
|
||||
if key == "S3_ACCESS_KEY_ID_FILE" {
|
||||
return lookup("S3_RECONCILE_ACCESS_KEY_ID_FILE")
|
||||
}
|
||||
if key == "S3_SECRET_ACCESS_KEY_FILE" {
|
||||
return lookup("S3_RECONCILE_SECRET_ACCESS_KEY_FILE")
|
||||
}
|
||||
return lookup(key)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package config_test
|
||||
|
||||
import (
|
||||
"messenger/internal/platform/config"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestS3LimitsAndSecretFiles(t *testing.T) {
|
||||
values := map[string]string{"S3_ACCESS_KEY_ID_FILE": secretFile(t, "access\n"), "S3_SECRET_ACCESS_KEY_FILE": secretFile(t, "private\n")}
|
||||
cfg, err := config.LoadS3(env(values))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg.AccessKey != "access" || cfg.SecretKey != "private" || cfg.MaxBytes != 10485760 || cfg.MaxPixels != 40000000 {
|
||||
t.Fatal("S3 defaults or secret trimming broken")
|
||||
}
|
||||
for _, tc := range []struct{ k, v string }{{"MAX_UPLOAD_BYTES", "0"}, {"MAX_UPLOAD_BYTES", "10485761"}, {"MAX_IMAGE_PIXELS", "40000001"}, {"MAX_IMAGE_PIXELS", "x"}} {
|
||||
v := map[string]string{}
|
||||
for k, x := range values {
|
||||
v[k] = x
|
||||
}
|
||||
v[tc.k] = tc.v
|
||||
if _, err := config.LoadS3(env(v)); err == nil {
|
||||
t.Fatalf("accepted %s=%s", tc.k, tc.v)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime"
|
||||
"net/http"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"messenger/internal/platform/apperr"
|
||||
)
|
||||
|
||||
// DecodeJSON reads exactly one JSON object into dst and enforces what gin's
|
||||
// binding does not: the media type must be application/json (415), invalid
|
||||
// UTF-8, unknown fields, wrong types, malformed syntax and trailing data are
|
||||
// 400, an oversized body is 413, and messages never expose Go type names.
|
||||
func DecodeJSON(c *gin.Context, dst any) error {
|
||||
mediaType, _, err := mime.ParseMediaType(c.GetHeader("Content-Type"))
|
||||
if err != nil || mediaType != "application/json" {
|
||||
return apperr.UnsupportedMedia("Content-Type must be application/json")
|
||||
}
|
||||
// The body is already capped by the bodyLimit middleware, so reading it
|
||||
// whole is bounded.
|
||||
body, err := io.ReadAll(c.Request.Body)
|
||||
if err != nil {
|
||||
return decodeError(err)
|
||||
}
|
||||
// encoding/json silently replaces invalid UTF-8 with U+FFFD; the contract
|
||||
// and RFC 8259 require UTF-8, so such a body is rejected, not repaired.
|
||||
if !utf8.Valid(body) {
|
||||
return apperr.Invalid("request body must be valid UTF-8")
|
||||
}
|
||||
dec := json.NewDecoder(bytes.NewReader(body))
|
||||
dec.DisallowUnknownFields()
|
||||
if err := dec.Decode(dst); err != nil {
|
||||
return decodeError(err)
|
||||
}
|
||||
if err := dec.Decode(&struct{}{}); !errors.Is(err, io.EOF) {
|
||||
return apperr.Invalid("unexpected data after JSON body")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func decodeError(err error) error {
|
||||
if tooLarge, ok := errors.AsType[*http.MaxBytesError](err); ok {
|
||||
return apperr.Wrap(apperr.KindPayloadTooLarge, fmt.Sprintf("request body must be at most %d bytes", tooLarge.Limit), err)
|
||||
}
|
||||
if errors.Is(err, io.EOF) {
|
||||
return apperr.Wrap(apperr.KindInvalid, "request body is required", err)
|
||||
}
|
||||
if wrongType, ok := errors.AsType[*json.UnmarshalTypeError](err); ok {
|
||||
if wrongType.Field == "" {
|
||||
return apperr.Wrap(apperr.KindInvalid, "request body must be a JSON object", err)
|
||||
}
|
||||
return apperr.Wrap(apperr.KindInvalid, fmt.Sprintf("field %q has wrong type", wrongType.Field), err)
|
||||
}
|
||||
// encoding/json reports unknown fields as a plain error without a type.
|
||||
const unknownPrefix = "json: unknown field "
|
||||
if msg := err.Error(); strings.HasPrefix(msg, unknownPrefix) {
|
||||
field := strings.Trim(strings.TrimPrefix(msg, unknownPrefix), `"`)
|
||||
return apperr.Wrap(apperr.KindInvalid, fmt.Sprintf("unknown field %q", field), err)
|
||||
}
|
||||
return apperr.Wrap(apperr.KindInvalid, "malformed JSON body", err)
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
// Package httpx is the transport layer over gin: request ids, access
|
||||
// logging, panic recovery, body limits, strict JSON decoding and the single
|
||||
// error format of the API contract. gin is used for routing and the
|
||||
// middleware chain; everything here exists because the contract demands a
|
||||
// specific behaviour that gin's own helpers do not provide.
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"messenger/internal/platform/apperr"
|
||||
)
|
||||
|
||||
const (
|
||||
keyRequestID = "messenger.request_id"
|
||||
keyLogger = "messenger.logger"
|
||||
)
|
||||
|
||||
// NewEngine builds an engine whose every response, including routing
|
||||
// failures and panics, carries X-Instance-Id, X-Request-Id and the contract
|
||||
// error body.
|
||||
func NewEngine(log *slog.Logger, maxBodyBytes int64, instanceID string) *gin.Engine {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
e := gin.New()
|
||||
// Redirects would answer with text/plain and without a request id.
|
||||
e.RedirectTrailingSlash = false
|
||||
e.RedirectFixedPath = false
|
||||
e.HandleMethodNotAllowed = true
|
||||
|
||||
e.Use(instance(instanceID), requestID(log), accessLog(), recovery(), bodyLimit(maxBodyBytes))
|
||||
e.NoRoute(func(c *gin.Context) { Error(c, apperr.NotFound("route not found")) })
|
||||
e.NoMethod(func(c *gin.Context) { Fail(c, http.StatusMethodNotAllowed, "invalid_request", "method not allowed") })
|
||||
return e
|
||||
}
|
||||
|
||||
func RequestID(c *gin.Context) string {
|
||||
return c.GetString(keyRequestID)
|
||||
}
|
||||
|
||||
func logger(c *gin.Context) *slog.Logger {
|
||||
if l, ok := c.Get(keyLogger); ok {
|
||||
if log, ok := l.(*slog.Logger); ok {
|
||||
return log
|
||||
}
|
||||
}
|
||||
return slog.Default()
|
||||
}
|
||||
@@ -0,0 +1,409 @@
|
||||
package httpx_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/httpx"
|
||||
)
|
||||
|
||||
type errorBody struct {
|
||||
Error struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
RequestID string `json:"request_id"`
|
||||
} `json:"error"`
|
||||
}
|
||||
|
||||
type payload struct {
|
||||
Text string `json:"text"`
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
const testInstance = "0123456789abcdef"
|
||||
|
||||
func newEngine(t *testing.T, logs *bytes.Buffer) *gin.Engine {
|
||||
t.Helper()
|
||||
return newEngineAt(t, logs, slog.LevelDebug)
|
||||
}
|
||||
|
||||
func newEngineAt(t *testing.T, logs *bytes.Buffer, level slog.Level) *gin.Engine {
|
||||
t.Helper()
|
||||
if logs == nil {
|
||||
logs = &bytes.Buffer{}
|
||||
}
|
||||
log := slog.New(slog.NewJSONHandler(logs, &slog.HandlerOptions{Level: level}))
|
||||
e := httpx.NewEngine(log, 64, testInstance)
|
||||
e.GET("/ok", func(c *gin.Context) { httpx.JSON(c, http.StatusOK, gin.H{"text": "<a&b>"}) })
|
||||
e.GET("/panic", func(c *gin.Context) { panic("secret detail") })
|
||||
e.GET("/fail", func(c *gin.Context) { httpx.Error(c, errors.New("db password leaked")) })
|
||||
e.GET("/notfound", func(c *gin.Context) { httpx.Error(c, apperr.NotFound("chat not found")) })
|
||||
e.GET("/who", func(c *gin.Context) {
|
||||
httpx.SetUserID(c, uuid.MustParse("11111111-1111-4111-8111-111111111111"))
|
||||
id, ok := httpx.UserID(c)
|
||||
httpx.JSON(c, http.StatusOK, gin.H{"id": id.String(), "ok": ok})
|
||||
})
|
||||
e.POST("/decode", func(c *gin.Context) {
|
||||
var p payload
|
||||
if err := httpx.DecodeJSON(c, &p); err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
httpx.JSON(c, http.StatusOK, p)
|
||||
})
|
||||
return e
|
||||
}
|
||||
|
||||
func do(t *testing.T, e *gin.Engine, method, path string, body string, headers map[string]string) (*httptest.ResponseRecorder, errorBody) {
|
||||
t.Helper()
|
||||
var reader io.Reader
|
||||
if body != "" {
|
||||
reader = strings.NewReader(body)
|
||||
}
|
||||
req := httptest.NewRequest(method, path, reader)
|
||||
for k, v := range headers {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
rec := httptest.NewRecorder()
|
||||
e.ServeHTTP(rec, req)
|
||||
var eb errorBody
|
||||
if rec.Code >= 400 {
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &eb); err != nil {
|
||||
t.Fatalf("%s %s: error body is not JSON: %v: %s", method, path, err, rec.Body.String())
|
||||
}
|
||||
}
|
||||
return rec, eb
|
||||
}
|
||||
|
||||
func assertError(t *testing.T, rec *httptest.ResponseRecorder, eb errorBody, status int, code string) {
|
||||
t.Helper()
|
||||
if rec.Code != status {
|
||||
t.Fatalf("status = %d, want %d (body %s)", rec.Code, status, rec.Body.String())
|
||||
}
|
||||
if ct := rec.Header().Get("Content-Type"); !strings.HasPrefix(ct, "application/json") {
|
||||
t.Fatalf("Content-Type = %q", ct)
|
||||
}
|
||||
rid := rec.Header().Get("X-Request-Id")
|
||||
if rid == "" {
|
||||
t.Fatal("missing X-Request-Id header")
|
||||
}
|
||||
if eb.Error.RequestID != rid {
|
||||
t.Fatalf("body request_id %q != header %q", eb.Error.RequestID, rid)
|
||||
}
|
||||
if eb.Error.Code != code || eb.Error.Message == "" {
|
||||
t.Fatalf("error = %+v, want code %q with a message", eb.Error, code)
|
||||
}
|
||||
}
|
||||
|
||||
// The balancer spreads one client's requests over the replicas; the header
|
||||
// is how a client tells which process answered, so no response may lack it.
|
||||
func TestEveryResponseCarriesInstanceID(t *testing.T) {
|
||||
e := newEngine(t, nil)
|
||||
for _, r := range []struct{ method, path string }{
|
||||
{http.MethodGet, "/ok"},
|
||||
{http.MethodGet, "/missing"},
|
||||
{http.MethodDelete, "/ok"},
|
||||
{http.MethodGet, "/panic"},
|
||||
{http.MethodGet, "/fail"},
|
||||
{http.MethodPost, "/decode"},
|
||||
} {
|
||||
rec, _ := do(t, e, r.method, r.path, "", nil)
|
||||
if got := rec.Header().Get("X-Instance-Id"); got != testInstance {
|
||||
t.Errorf("%s %s (status %d): X-Instance-Id = %q, want %q", r.method, r.path, rec.Code, got, testInstance)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnknownRouteIsJSON404(t *testing.T) {
|
||||
rec, eb := do(t, newEngine(t, nil), http.MethodGet, "/nope", "", nil)
|
||||
assertError(t, rec, eb, http.StatusNotFound, "not_found")
|
||||
}
|
||||
|
||||
func TestTrailingSlashIsNotRedirected(t *testing.T) {
|
||||
rec, eb := do(t, newEngine(t, nil), http.MethodGet, "/ok/", "", nil)
|
||||
assertError(t, rec, eb, http.StatusNotFound, "not_found")
|
||||
}
|
||||
|
||||
func TestWrongMethodIsJSON405WithAllow(t *testing.T) {
|
||||
rec, eb := do(t, newEngine(t, nil), http.MethodDelete, "/ok", "", nil)
|
||||
assertError(t, rec, eb, http.StatusMethodNotAllowed, "invalid_request")
|
||||
if !strings.Contains(rec.Header().Get("Allow"), "GET") {
|
||||
t.Fatalf("Allow = %q, want GET", rec.Header().Get("Allow"))
|
||||
}
|
||||
}
|
||||
|
||||
func TestPanicBecomesJSON500WithoutDetails(t *testing.T) {
|
||||
logs := &bytes.Buffer{}
|
||||
rec, eb := do(t, newEngine(t, logs), http.MethodGet, "/panic", "", nil)
|
||||
assertError(t, rec, eb, http.StatusInternalServerError, "internal_error")
|
||||
if strings.Contains(rec.Body.String(), "secret detail") {
|
||||
t.Fatal("panic value leaked to the client")
|
||||
}
|
||||
if !strings.Contains(logs.String(), "secret detail") {
|
||||
t.Fatal("panic must be logged for operators")
|
||||
}
|
||||
}
|
||||
|
||||
func TestForeignErrorBecomesJSON500WithoutDetails(t *testing.T) {
|
||||
logs := &bytes.Buffer{}
|
||||
rec, eb := do(t, newEngine(t, logs), http.MethodGet, "/fail", "", nil)
|
||||
assertError(t, rec, eb, http.StatusInternalServerError, "internal_error")
|
||||
if strings.Contains(rec.Body.String(), "password") {
|
||||
t.Fatal("internal error text leaked to the client")
|
||||
}
|
||||
if !strings.Contains(logs.String(), "db password leaked") {
|
||||
t.Fatal("cause must be logged for operators")
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplicationErrorMapping(t *testing.T) {
|
||||
rec, eb := do(t, newEngine(t, nil), http.MethodGet, "/notfound", "", nil)
|
||||
assertError(t, rec, eb, http.StatusNotFound, "not_found")
|
||||
if eb.Error.Message != "chat not found" {
|
||||
t.Fatalf("message = %q", eb.Error.Message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSuccessResponseHeadersAndEscaping(t *testing.T) {
|
||||
rec, _ := do(t, newEngine(t, nil), http.MethodGet, "/ok", "", nil)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d", rec.Code)
|
||||
}
|
||||
if ct := rec.Header().Get("Content-Type"); ct != "application/json; charset=utf-8" {
|
||||
t.Fatalf("Content-Type = %q", ct)
|
||||
}
|
||||
if rec.Header().Get("X-Request-Id") == "" {
|
||||
t.Fatal("missing X-Request-Id on success")
|
||||
}
|
||||
if !strings.Contains(rec.Body.String(), `"<a&b>"`) {
|
||||
t.Fatalf("HTML escaping must be off, body %s", rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// The balancer sets X-Request-Id on every request, overwriting what the
|
||||
// client sent; the API keeps it so that one id spans both logs.
|
||||
func TestRequestIDFromTheBalancerIsKept(t *testing.T) {
|
||||
const fromLB = "0123456789abcdef0123456789abcdef"
|
||||
rec, _ := do(t, newEngine(t, nil), http.MethodGet, "/ok", "", map[string]string{"X-Request-Id": fromLB})
|
||||
if got := rec.Header().Get("X-Request-Id"); got != fromLB {
|
||||
t.Fatalf("X-Request-Id = %q, want %q", got, fromLB)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMalformedRequestIDIsReplaced(t *testing.T) {
|
||||
for _, bad := range []string{"has space", "semi;colon", "tab\there", strings.Repeat("a", 65)} {
|
||||
rec, _ := do(t, newEngine(t, nil), http.MethodGet, "/ok", "", map[string]string{"X-Request-Id": bad})
|
||||
if got := rec.Header().Get("X-Request-Id"); got == "" || got == bad {
|
||||
t.Errorf("X-Request-Id for %q = %q, want a generated value", bad, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeJSONHappyPath(t *testing.T) {
|
||||
rec, _ := do(t, newEngine(t, nil), http.MethodPost, "/decode", `{"text":"Привет","count":2}`, map[string]string{"Content-Type": "application/json; charset=utf-8"})
|
||||
if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), `"Привет"`) {
|
||||
t.Fatalf("status %d body %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeJSONFailures(t *testing.T) {
|
||||
jsonCT := map[string]string{"Content-Type": "application/json"}
|
||||
cases := []struct {
|
||||
name string
|
||||
body string
|
||||
headers map[string]string
|
||||
status int
|
||||
code string
|
||||
contain string
|
||||
}{
|
||||
{"no content type", `{"text":"x"}`, nil, http.StatusUnsupportedMediaType, "unsupported_media_type", ""},
|
||||
{"wrong content type", `{"text":"x"}`, map[string]string{"Content-Type": "text/plain"}, http.StatusUnsupportedMediaType, "unsupported_media_type", ""},
|
||||
{"empty body", "", jsonCT, http.StatusBadRequest, "invalid_request", "body"},
|
||||
{"malformed", `{"text":`, jsonCT, http.StatusBadRequest, "invalid_request", "malformed"},
|
||||
{"invalid utf-8", "{\"text\":\"bad \xff byte\"}", jsonCT, http.StatusBadRequest, "invalid_request", "UTF-8"},
|
||||
{"unknown field", `{"text":"x","extra":1}`, jsonCT, http.StatusBadRequest, "invalid_request", "extra"},
|
||||
{"wrong type", `{"text":123}`, jsonCT, http.StatusBadRequest, "invalid_request", "text"},
|
||||
{"trailing data", `{"text":"x"} {"text":"y"}`, jsonCT, http.StatusBadRequest, "invalid_request", "after"},
|
||||
{"array instead of object", `[1,2]`, jsonCT, http.StatusBadRequest, "invalid_request", ""},
|
||||
{"too large", `{"text":"` + strings.Repeat("x", 100) + `"}`, jsonCT, http.StatusRequestEntityTooLarge, "payload_too_large", ""},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
rec, eb := do(t, newEngine(t, nil), http.MethodPost, "/decode", tc.body, tc.headers)
|
||||
assertError(t, rec, eb, tc.status, tc.code)
|
||||
if tc.contain != "" && !strings.Contains(eb.Error.Message, tc.contain) {
|
||||
t.Fatalf("message %q must mention %q", eb.Error.Message, tc.contain)
|
||||
}
|
||||
if strings.Contains(eb.Error.Message, "httpx_test") || strings.Contains(eb.Error.Message, "payload") {
|
||||
t.Fatalf("message must not leak Go type names: %q", eb.Error.Message)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccessLogHasRequestFieldsButNoBody(t *testing.T) {
|
||||
logs := &bytes.Buffer{}
|
||||
e := newEngine(t, logs)
|
||||
rec, _ := do(t, e, http.MethodPost, "/decode", `{"text":"confidential"}`, map[string]string{"Content-Type": "application/json"})
|
||||
var entry map[string]any
|
||||
line := lastLine(logs.String())
|
||||
if err := json.Unmarshal([]byte(line), &entry); err != nil {
|
||||
t.Fatalf("log line is not JSON: %v: %s", err, line)
|
||||
}
|
||||
for _, key := range []string{"request_id", "method", "path", "status", "duration_ms"} {
|
||||
if _, ok := entry[key]; !ok {
|
||||
t.Errorf("log entry missing %q: %s", key, line)
|
||||
}
|
||||
}
|
||||
if entry["request_id"] != rec.Header().Get("X-Request-Id") {
|
||||
t.Errorf("log request_id %v != header %q", entry["request_id"], rec.Header().Get("X-Request-Id"))
|
||||
}
|
||||
if strings.Contains(logs.String(), "confidential") {
|
||||
t.Fatal("request body must never be logged")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccessLogLevelFollowsStatus(t *testing.T) {
|
||||
logs := &bytes.Buffer{}
|
||||
e := newEngineAt(t, logs, slog.LevelInfo)
|
||||
|
||||
do(t, e, http.MethodGet, "/ok", "", nil)
|
||||
if strings.Contains(logs.String(), `"msg":"request"`) {
|
||||
t.Fatalf("successful requests must be logged at debug level only, got %s", logs.String())
|
||||
}
|
||||
do(t, e, http.MethodGet, "/notfound", "", nil)
|
||||
if entry := logEntry(t, logs); entry["level"] != "INFO" || entry["status"] != float64(404) {
|
||||
t.Fatalf("client error must be logged at info level with its status, got %v", entry)
|
||||
}
|
||||
do(t, e, http.MethodGet, "/fail", "", nil)
|
||||
if entry := logEntry(t, logs); entry["level"] != "ERROR" || entry["status"] != float64(500) {
|
||||
t.Fatalf("server error must be logged at error level with its status, got %v", entry)
|
||||
}
|
||||
|
||||
verbose := &bytes.Buffer{}
|
||||
do(t, newEngineAt(t, verbose, slog.LevelDebug), http.MethodGet, "/ok", "", nil)
|
||||
if entry := logEntry(t, verbose); entry["level"] != "DEBUG" || entry["status"] != float64(200) {
|
||||
t.Fatalf("at debug level successful requests must be logged, got %v", entry)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserIDRoundTripAndAccessLogAttribution(t *testing.T) {
|
||||
logs := &bytes.Buffer{}
|
||||
rec, _ := do(t, newEngine(t, logs), http.MethodGet, "/who", "", nil)
|
||||
if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), `"ok":true`) {
|
||||
t.Fatalf("UserID must return what SetUserID stored: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if entry := logEntry(t, logs); entry["user_id"] != "11111111-1111-4111-8111-111111111111" {
|
||||
t.Fatalf("access log must attribute the request to the user, got %v", entry)
|
||||
}
|
||||
anonymous := &bytes.Buffer{}
|
||||
do(t, newEngine(t, anonymous), http.MethodGet, "/ok", "", nil)
|
||||
if _, present := logEntry(t, anonymous)["user_id"]; present {
|
||||
t.Fatal("anonymous requests must not carry user_id")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithUserPassesTheIdentifiedCaller(t *testing.T) {
|
||||
actor := uuid.MustParse("22222222-2222-4222-8222-222222222222")
|
||||
e := newEngine(t, nil)
|
||||
e.GET("/actor",
|
||||
func(c *gin.Context) { httpx.SetUserID(c, actor) },
|
||||
httpx.WithUser(func(c *gin.Context, got uuid.UUID) {
|
||||
httpx.JSON(c, http.StatusOK, gin.H{"id": got.String()})
|
||||
}))
|
||||
rec, _ := do(t, e, http.MethodGet, "/actor", "", nil)
|
||||
if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), actor.String()) {
|
||||
t.Fatalf("handler did not receive the caller: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// Reaching WithUser without an authentication middleware in front is a
|
||||
// wiring mistake, so it must not be blamed on the client with a 401.
|
||||
func TestWithUserWithoutIdentityIsAWiringError(t *testing.T) {
|
||||
e := newEngine(t, nil)
|
||||
e.GET("/actor", httpx.WithUser(func(c *gin.Context, _ uuid.UUID) {
|
||||
httpx.JSON(c, http.StatusOK, gin.H{})
|
||||
}))
|
||||
rec, eb := do(t, e, http.MethodGet, "/actor", "", nil)
|
||||
assertError(t, rec, eb, http.StatusInternalServerError, "internal_error")
|
||||
}
|
||||
|
||||
// A 5xx tells the client only the safe message; the operator must still see
|
||||
// why the dependency failed, so the cause goes to the log.
|
||||
func TestUnavailableCauseIsLoggedButNotReturned(t *testing.T) {
|
||||
logs := &bytes.Buffer{}
|
||||
e := newEngine(t, logs)
|
||||
e.GET("/down", func(c *gin.Context) {
|
||||
cause := errors.New("dial tcp 10.0.0.7:5432: connect: connection refused")
|
||||
httpx.Error(c, fmt.Errorf("select user: %w", apperr.Wrap(apperr.KindUnavailable, "database unavailable", cause)))
|
||||
})
|
||||
rec, eb := do(t, e, http.MethodGet, "/down", "", nil)
|
||||
assertError(t, rec, eb, http.StatusServiceUnavailable, "unavailable")
|
||||
// Neither the cause nor the wrapping context of internal operations
|
||||
// ("select user: ...") may reach the client.
|
||||
if eb.Error.Message != "database unavailable" {
|
||||
t.Fatalf("client message = %q, want only the application message", eb.Error.Message)
|
||||
}
|
||||
if !strings.Contains(logs.String(), "connection refused") || !strings.Contains(logs.String(), eb.Error.RequestID) {
|
||||
t.Fatalf("log must carry the cause and the request id, got:\n%s", logs.String())
|
||||
}
|
||||
}
|
||||
|
||||
// Everything below Timeout sees one deadline, so waiting on a dependency
|
||||
// ends with the request instead of holding the client.
|
||||
func TestTimeoutBoundsTheRequestContext(t *testing.T) {
|
||||
e := newEngine(t, nil)
|
||||
e.GET("/slow", httpx.Timeout(50*time.Millisecond), func(c *gin.Context) {
|
||||
select {
|
||||
case <-c.Request.Context().Done():
|
||||
httpx.Error(c, apperr.Wrap(apperr.KindUnavailable, "gave up", c.Request.Context().Err()))
|
||||
case <-time.After(5 * time.Second):
|
||||
httpx.JSON(c, http.StatusOK, gin.H{})
|
||||
}
|
||||
})
|
||||
start := time.Now()
|
||||
rec, eb := do(t, e, http.MethodGet, "/slow", "", nil)
|
||||
assertError(t, rec, eb, http.StatusServiceUnavailable, "unavailable")
|
||||
if elapsed := time.Since(start); elapsed > time.Second {
|
||||
t.Fatalf("request took %v, want it cut at the 50ms deadline", elapsed)
|
||||
}
|
||||
}
|
||||
|
||||
func logEntry(t *testing.T, logs *bytes.Buffer) map[string]any {
|
||||
t.Helper()
|
||||
var entry map[string]any
|
||||
line := lastLine(logs.String())
|
||||
if err := json.Unmarshal([]byte(line), &entry); err != nil {
|
||||
t.Fatalf("log line is not JSON: %v: %s", err, line)
|
||||
}
|
||||
return entry
|
||||
}
|
||||
|
||||
func TestTimestampFormat(t *testing.T) {
|
||||
ts := time.Date(2026, 9, 1, 12, 0, 0, 0, time.FixedZone("MSK", 3*3600))
|
||||
if got := httpx.Timestamp(ts); got != "2026-09-01T09:00:00.000Z" {
|
||||
t.Fatalf("Timestamp = %q", got)
|
||||
}
|
||||
ts = time.Date(2026, 9, 1, 12, 0, 0, 7_000_000, time.UTC)
|
||||
if got := httpx.Timestamp(ts); got != "2026-09-01T12:00:00.007Z" {
|
||||
t.Fatalf("Timestamp = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func lastLine(s string) string {
|
||||
lines := strings.Split(strings.TrimSpace(s), "\n")
|
||||
return lines[len(lines)-1]
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const keyUserID = "messenger.user_id"
|
||||
|
||||
// SetUserID records the authenticated caller for the handlers behind the
|
||||
// authentication middleware and for the access log.
|
||||
func SetUserID(c *gin.Context, id uuid.UUID) {
|
||||
c.Set(keyUserID, id)
|
||||
}
|
||||
|
||||
func UserID(c *gin.Context) (uuid.UUID, bool) {
|
||||
v, ok := c.Get(keyUserID)
|
||||
if !ok {
|
||||
return uuid.Nil, false
|
||||
}
|
||||
id, ok := v.(uuid.UUID)
|
||||
return id, ok
|
||||
}
|
||||
|
||||
// UserHandler is a handler that needs the authenticated caller.
|
||||
type UserHandler func(c *gin.Context, actor uuid.UUID)
|
||||
|
||||
// WithUser adapts a UserHandler to gin. Reaching it without the
|
||||
// authentication middleware in front is a wiring mistake, so it is reported
|
||||
// as an internal error rather than blamed on the client with a 401.
|
||||
func WithUser(h UserHandler) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
actor, ok := UserID(c)
|
||||
if !ok {
|
||||
Error(c, errors.New("route is missing the authentication middleware"))
|
||||
return
|
||||
}
|
||||
h(c, actor)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"runtime/debug"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// instance names the process that answered, so a client behind the load
|
||||
// balancer can tell the replicas apart.
|
||||
func instance(id string) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
c.Writer.Header().Set("X-Instance-Id", id)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
var requestIDPattern = regexp.MustCompile(`^[A-Za-z0-9-]{1,64}$`)
|
||||
|
||||
// requestID keeps the X-Request-Id set by the load balancer, which overwrites
|
||||
// any value a client sends; a malformed value is replaced, so a direct caller
|
||||
// cannot inject into the logs.
|
||||
func requestID(log *slog.Logger) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
id := c.GetHeader("X-Request-Id")
|
||||
if !requestIDPattern.MatchString(id) {
|
||||
id = newRequestID()
|
||||
}
|
||||
c.Set(keyRequestID, id)
|
||||
c.Set(keyLogger, log)
|
||||
c.Writer.Header().Set("X-Request-Id", id)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func newRequestID() string {
|
||||
var raw [16]byte
|
||||
if _, err := rand.Read(raw[:]); err != nil {
|
||||
panic("httpx: random source unavailable: " + err.Error())
|
||||
}
|
||||
return hex.EncodeToString(raw[:])
|
||||
}
|
||||
|
||||
// accessLog writes one line per request at a level that follows the
|
||||
// outcome (2xx debug, 4xx info, 5xx error), so the ordinary log level decides
|
||||
// how much traffic is recorded. Headers, query strings and bodies are never
|
||||
// recorded. time.Since uses the monotonic clock, so wall clock adjustments
|
||||
// cannot distort the duration.
|
||||
func accessLog() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
start := time.Now()
|
||||
c.Next()
|
||||
|
||||
status := c.Writer.Status()
|
||||
level := slog.LevelDebug
|
||||
switch {
|
||||
case status >= http.StatusInternalServerError:
|
||||
level = slog.LevelError
|
||||
case status >= http.StatusBadRequest:
|
||||
level = slog.LevelInfo
|
||||
}
|
||||
log := logger(c)
|
||||
ctx := c.Request.Context()
|
||||
if !log.Enabled(ctx, level) {
|
||||
return
|
||||
}
|
||||
attrs := []any{
|
||||
"request_id", RequestID(c),
|
||||
"method", c.Request.Method,
|
||||
"path", c.Request.URL.Path,
|
||||
"status", status,
|
||||
"duration_ms", float64(time.Since(start).Microseconds()) / 1000,
|
||||
"bytes", c.Writer.Size(),
|
||||
}
|
||||
if id, ok := UserID(c); ok {
|
||||
attrs = append(attrs, "user_id", id.String())
|
||||
}
|
||||
log.Log(ctx, level, "request", attrs...)
|
||||
}
|
||||
}
|
||||
|
||||
// recovery turns a panic into a contract 500; the stack goes to the log only.
|
||||
func recovery() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
logger(c).Error("panic recovered",
|
||||
"request_id", RequestID(c),
|
||||
"panic", fmt.Sprint(r),
|
||||
"stack", string(debug.Stack()),
|
||||
)
|
||||
if !c.Writer.Written() {
|
||||
Fail(c, http.StatusInternalServerError, "internal_error", "internal error")
|
||||
}
|
||||
c.Abort()
|
||||
}
|
||||
}()
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// Timeout bounds the whole request with one deadline: waiting for a pooled
|
||||
// connection, SQL statements and session lookups all observe it, so a hung
|
||||
// dependency ends in a 503 within d instead of holding the client.
|
||||
func Timeout(d time.Duration) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), d)
|
||||
defer cancel()
|
||||
c.Request = c.Request.WithContext(ctx)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// bodyLimit caps request bodies; exceeding it surfaces from the JSON
|
||||
// decoder as *http.MaxBytesError.
|
||||
func bodyLimit(maxBytes int64) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// The authenticated binary handler installs its own 10 MiB limit.
|
||||
if c.Request.Method == http.MethodPost && c.FullPath() == "/api/v1/chats/:chat_id/attachments" {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
if c.Request.Body != nil {
|
||||
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, maxBytes)
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"messenger/internal/platform/apperr"
|
||||
)
|
||||
|
||||
type errorEnvelope struct {
|
||||
Error errorDetail `json:"error"`
|
||||
}
|
||||
|
||||
type errorDetail struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
RequestID string `json:"request_id"`
|
||||
}
|
||||
|
||||
type statusCode struct {
|
||||
status int
|
||||
code string
|
||||
}
|
||||
|
||||
var kinds = map[apperr.Kind]statusCode{
|
||||
apperr.KindInvalid: {http.StatusBadRequest, "invalid_request"},
|
||||
apperr.KindUnauthorized: {http.StatusUnauthorized, "unauthorized"},
|
||||
apperr.KindNotFound: {http.StatusNotFound, "not_found"},
|
||||
apperr.KindConflict: {http.StatusConflict, "conflict"},
|
||||
apperr.KindPayloadTooLarge: {http.StatusRequestEntityTooLarge, "payload_too_large"},
|
||||
apperr.KindUnsupportedMedia: {http.StatusUnsupportedMediaType, "unsupported_media_type"},
|
||||
apperr.KindUnavailable: {http.StatusServiceUnavailable, "unavailable"},
|
||||
}
|
||||
|
||||
// JSON is the single place that fixes the response media type; PureJSON
|
||||
// keeps non-ASCII and HTML characters verbatim.
|
||||
func JSON(c *gin.Context, status int, body any) {
|
||||
c.PureJSON(status, body)
|
||||
}
|
||||
|
||||
// Fail writes the contract error body and stops the handler chain.
|
||||
func Fail(c *gin.Context, status int, code, message string) {
|
||||
JSON(c, status, errorEnvelope{Error: errorDetail{
|
||||
Code: code,
|
||||
Message: message,
|
||||
RequestID: RequestID(c),
|
||||
}})
|
||||
c.Abort()
|
||||
}
|
||||
|
||||
// Error maps an application error to its status. Anything that is not an
|
||||
// application error is logged with its text and reported as an opaque 500,
|
||||
// so internal details never reach the client. An application error shows the
|
||||
// client only its own message, never the context it was wrapped in on the
|
||||
// way up; when it is answered with a 5xx (a dependency outage) the full
|
||||
// chain and the cause go to the log for the operator.
|
||||
func Error(c *gin.Context, err error) {
|
||||
appErr, isApp := errors.AsType[*apperr.Error](err)
|
||||
var mapped statusCode
|
||||
known := false
|
||||
if isApp {
|
||||
mapped, known = kinds[appErr.Kind]
|
||||
}
|
||||
if !known {
|
||||
logger(c).Error("request failed", "request_id", RequestID(c), "error", err.Error())
|
||||
Fail(c, http.StatusInternalServerError, "internal_error", "internal error")
|
||||
return
|
||||
}
|
||||
if mapped.status >= http.StatusInternalServerError {
|
||||
attrs := []any{"request_id", RequestID(c), "error", err.Error()}
|
||||
if appErr.Cause != nil {
|
||||
attrs = append(attrs, "cause", appErr.Cause.Error())
|
||||
}
|
||||
logger(c).Error("request failed", attrs...)
|
||||
}
|
||||
Fail(c, mapped.status, mapped.code, appErr.Message)
|
||||
}
|
||||
|
||||
// Timestamp renders RFC 3339 UTC with a fixed millisecond width: Python 3.9
|
||||
// (the oldest version the public tests support) only parses 3 or 6 fractional
|
||||
// digits, and RFC3339Nano would trim trailing zeros to a variable width.
|
||||
func Timestamp(t time.Time) string {
|
||||
return t.UTC().Format("2006-01-02T15:04:05.000Z")
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
// Package ident generates and parses entity identifiers.
|
||||
//
|
||||
// Identifiers are UUIDv7 (RFC 9562): the first 48 bits carry a Unix
|
||||
// millisecond timestamp and google/uuid keeps them strictly increasing within
|
||||
// one process even when the system clock steps backwards, so lexicographic
|
||||
// order of ids equals creation order.
|
||||
package ident
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
var ErrInvalid = errors.New("invalid uuid")
|
||||
|
||||
// New panics only if the platform random source is unavailable, which is a
|
||||
// fatal environment fault rather than a request error.
|
||||
func New() uuid.UUID {
|
||||
id, err := uuid.NewV7()
|
||||
if err != nil {
|
||||
panic("ident: random source unavailable: " + err.Error())
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
// NewAlias names one server process for X-Instance-Id; random rather than the
|
||||
// hostname, so the header reveals nothing about the infrastructure.
|
||||
func NewAlias() string {
|
||||
var raw [8]byte
|
||||
_, _ = rand.Read(raw[:]) // crypto/rand.Read never returns an error since Go 1.24.
|
||||
return hex.EncodeToString(raw[:])
|
||||
}
|
||||
|
||||
// TimeOf extracts the creation time carried by a UUIDv7 as UTC with
|
||||
// millisecond precision.
|
||||
func TimeOf(id uuid.UUID) time.Time {
|
||||
sec, nsec := id.Time().UnixTime()
|
||||
return time.Unix(sec, nsec).UTC()
|
||||
}
|
||||
|
||||
// Parse accepts only the canonical 36-character form; uuid.Parse alone would
|
||||
// also take braces, urn prefixes and bare hex.
|
||||
func Parse(raw string) (uuid.UUID, error) {
|
||||
if len(raw) != 36 {
|
||||
return uuid.Nil, ErrInvalid
|
||||
}
|
||||
id, err := uuid.Parse(raw)
|
||||
if err != nil {
|
||||
return uuid.Nil, ErrInvalid
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package ident_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"regexp"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/internal/platform/ident"
|
||||
)
|
||||
|
||||
func TestNewProducesVersion7(t *testing.T) {
|
||||
id := ident.New()
|
||||
if got := id.Version(); got != 7 {
|
||||
t.Fatalf("version = %d, want 7", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewIsStrictlyIncreasingWithinProcess(t *testing.T) {
|
||||
prev := ident.New()
|
||||
for i := 0; i < 20000; i++ {
|
||||
next := ident.New()
|
||||
if bytes.Compare(next[:], prev[:]) <= 0 {
|
||||
t.Fatalf("id %s is not greater than previous %s at iteration %d", next, prev, i)
|
||||
}
|
||||
prev = next
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimeOfMatchesWallClockAtMillisecondPrecision(t *testing.T) {
|
||||
before := time.Now().Truncate(time.Millisecond)
|
||||
id := ident.New()
|
||||
after := time.Now().Add(time.Millisecond)
|
||||
|
||||
got := ident.TimeOf(id)
|
||||
if got.Location() != time.UTC {
|
||||
t.Fatalf("TimeOf must return UTC, got %v", got.Location())
|
||||
}
|
||||
if got.Before(before) || got.After(after) {
|
||||
t.Fatalf("TimeOf = %v, want within [%v, %v]", got, before, after)
|
||||
}
|
||||
if got.Nanosecond()%int(time.Millisecond) != 0 {
|
||||
t.Fatalf("TimeOf must be truncated to milliseconds, got %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseAcceptsCanonicalForm(t *testing.T) {
|
||||
id := ident.New()
|
||||
parsed, err := ident.Parse(id.String())
|
||||
if err != nil {
|
||||
t.Fatalf("Parse(%q) error: %v", id, err)
|
||||
}
|
||||
if parsed != id {
|
||||
t.Fatalf("Parse round trip mismatch: %s != %s", parsed, id)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseRejectsNonCanonicalForms(t *testing.T) {
|
||||
id := ident.New().String()
|
||||
for _, raw := range []string{
|
||||
"",
|
||||
"not-a-uuid",
|
||||
"{" + id + "}",
|
||||
"urn:uuid:" + id,
|
||||
id[:32] + id[33:], // hex without one dash
|
||||
id + "0",
|
||||
" " + id,
|
||||
} {
|
||||
if _, err := ident.Parse(raw); err == nil {
|
||||
t.Errorf("Parse(%q) must fail", raw)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewAliasIs16HexAndRandom(t *testing.T) {
|
||||
a, b := ident.NewAlias(), ident.NewAlias()
|
||||
hex16 := regexp.MustCompile(`^[0-9a-f]{16}$`)
|
||||
if !hex16.MatchString(a) || !hex16.MatchString(b) {
|
||||
t.Fatalf("aliases %q, %q are not 16 lowercase hex characters", a, b)
|
||||
}
|
||||
if a == b {
|
||||
t.Fatalf("two aliases are equal: %q", a)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
// Package pagination implements keyset paging. A cursor is opaque to the
|
||||
// client and bound to the resource it pages (the chat for history, the user
|
||||
// for the chat list), so a cursor from one resource is rejected on another.
|
||||
// Because ids are UUIDv7 and creation time derives from the id, the keyset
|
||||
// (created_at, id) collapses to the id of the last returned item.
|
||||
package pagination
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultLimit = 50
|
||||
MaxLimit = 100
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalidCursor = errors.New("invalid cursor")
|
||||
ErrInvalidLimit = errors.New("limit must be an integer between 1 and 100")
|
||||
)
|
||||
|
||||
const cursorVersion = 1
|
||||
|
||||
func EncodeCursor(scope, last uuid.UUID) string {
|
||||
raw := make([]byte, 0, 1+16+16)
|
||||
raw = append(raw, cursorVersion)
|
||||
raw = append(raw, scope[:]...)
|
||||
raw = append(raw, last[:]...)
|
||||
return base64.RawURLEncoding.EncodeToString(raw)
|
||||
}
|
||||
|
||||
func DecodeCursor(token string, scope uuid.UUID) (uuid.UUID, error) {
|
||||
raw, err := base64.RawURLEncoding.DecodeString(token)
|
||||
if err != nil || len(raw) != 1+16+16 || raw[0] != cursorVersion {
|
||||
return uuid.Nil, ErrInvalidCursor
|
||||
}
|
||||
if got, err := uuid.FromBytes(raw[1:17]); err != nil || got != scope {
|
||||
return uuid.Nil, ErrInvalidCursor
|
||||
}
|
||||
last, err := uuid.FromBytes(raw[17:])
|
||||
if err != nil {
|
||||
return uuid.Nil, ErrInvalidCursor
|
||||
}
|
||||
return last, nil
|
||||
}
|
||||
|
||||
// ParseLimit accepts an unsigned decimal between 1 and MaxLimit; an empty
|
||||
// value means DefaultLimit. ParseUint rejects signs, whitespace and
|
||||
// fractions and reports overflow, so no length guard is needed.
|
||||
func ParseLimit(raw string) (int, error) {
|
||||
if raw == "" {
|
||||
return DefaultLimit, nil
|
||||
}
|
||||
n, err := strconv.ParseUint(raw, 10, 32)
|
||||
if err != nil || n < 1 || n > MaxLimit {
|
||||
return 0, ErrInvalidLimit
|
||||
}
|
||||
return int(n), nil
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package pagination_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"messenger/internal/platform/ident"
|
||||
"messenger/internal/platform/pagination"
|
||||
)
|
||||
|
||||
func TestCursorRoundTrip(t *testing.T) {
|
||||
scope, last := ident.New(), ident.New()
|
||||
encoded := pagination.EncodeCursor(scope, last)
|
||||
if encoded == "" || strings.ContainsAny(encoded, "+/=") {
|
||||
t.Fatalf("cursor must be non-empty URL-safe base64 without padding, got %q", encoded)
|
||||
}
|
||||
got, err := pagination.DecodeCursor(encoded, scope)
|
||||
if err != nil {
|
||||
t.Fatalf("DecodeCursor error: %v", err)
|
||||
}
|
||||
if got != last {
|
||||
t.Fatalf("decoded %s, want %s", got, last)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeCursorRejectsForeignScope(t *testing.T) {
|
||||
encoded := pagination.EncodeCursor(ident.New(), ident.New())
|
||||
_, err := pagination.DecodeCursor(encoded, ident.New())
|
||||
if !errors.Is(err, pagination.ErrInvalidCursor) {
|
||||
t.Fatalf("err = %v, want ErrInvalidCursor", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeCursorRejectsGarbage(t *testing.T) {
|
||||
scope := ident.New()
|
||||
valid := pagination.EncodeCursor(scope, ident.New())
|
||||
for _, raw := range []string{"", "***", "abc", valid[:len(valid)-2], valid + "AA", strings.ToUpper(valid)} {
|
||||
if _, err := pagination.DecodeCursor(raw, scope); !errors.Is(err, pagination.ErrInvalidCursor) {
|
||||
t.Errorf("DecodeCursor(%q) err = %v, want ErrInvalidCursor", raw, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseLimit(t *testing.T) {
|
||||
cases := []struct {
|
||||
raw string
|
||||
want int
|
||||
ok bool
|
||||
}{
|
||||
{"", 50, true},
|
||||
{"1", 1, true},
|
||||
{"100", 100, true},
|
||||
{"0", 0, false},
|
||||
{"101", 0, false},
|
||||
{"-1", 0, false},
|
||||
{"+5", 0, false},
|
||||
{"5.0", 0, false},
|
||||
{"abc", 0, false},
|
||||
{" 5", 0, false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
got, err := pagination.ParseLimit(tc.raw)
|
||||
if tc.ok && (err != nil || got != tc.want) {
|
||||
t.Errorf("ParseLimit(%q) = %d, %v; want %d", tc.raw, got, err, tc.want)
|
||||
}
|
||||
if !tc.ok && !errors.Is(err, pagination.ErrInvalidLimit) {
|
||||
t.Errorf("ParseLimit(%q) err = %v, want ErrInvalidLimit", tc.raw, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user