forked from mtusi2026/mtusi2026lab5
Реализована лабораторная работа 5 на оценку 4
This commit is contained in:
+6
-2
@@ -7,7 +7,7 @@ SESSION_TTL_SECONDS=3600
|
||||
# Choose credentials locally. File paths contain no secret values.
|
||||
DATABASE_URL_FILE=/run/secrets/database_url
|
||||
# Optional on 3, required use on 4 in lab2; Redis OR Valkey:
|
||||
SESSION_STORE=postgres
|
||||
SESSION_STORE=redis
|
||||
CACHE_URL_FILE=/run/secrets/cache_url
|
||||
API_REPLICAS=2
|
||||
IDEMPOTENCY_TTL_SECONDS=86400
|
||||
@@ -19,7 +19,11 @@ S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret_access_key
|
||||
MAX_UPLOAD_BYTES=10485760
|
||||
MAX_IMAGE_PIXELS=40000000
|
||||
BROKER_URL_FILE=/run/secrets/broker_url
|
||||
WORKER_CONCURRENCY=2
|
||||
WORKER_CONCURRENCY=1
|
||||
JOB_MAX_ATTEMPTS=5
|
||||
JOB_TIMEOUT_SECONDS=60
|
||||
STAGING_RETENTION_SECONDS=86400
|
||||
|
||||
DB_TIMEOUT_MS=3000
|
||||
DB_MAX_CONNS=10
|
||||
CLEANUP_GRACE_SECONDS=3600
|
||||
|
||||
@@ -1,18 +1,42 @@
|
||||
## Лабораторная 5
|
||||
|
||||
Автор / группа: TODO
|
||||
Целевая оценка (3/4/5): TODO
|
||||
Трек 5, если заявлен: TODO
|
||||
Автор / группа: Меркулова М. А. / БВТ2303
|
||||
Целевая оценка: **4**
|
||||
Трек 5: не заявлен.
|
||||
|
||||
Что реализовано и что перенесено: TODO
|
||||
Команды запуска из чистого clone: TODO
|
||||
Результаты проверок и ссылка на REPORT.md: TODO
|
||||
Ограничения / известные ошибки: TODO
|
||||
Источники и использование ИИ: TODO
|
||||
### Что сделано
|
||||
|
||||
- [ ] Весь обязательный минимум текущей и предыдущих работ сохранён с объявленными переходами.
|
||||
- [ ] Контракт и публичные тесты не ослаблены.
|
||||
- [ ] make check и make test выполнены; приложен реальный вывод.
|
||||
- [ ] Сценарии отказов/безопасности своего уровня воспроизведены.
|
||||
- [ ] Все секреты и приватные ключи находятся вне Git; отчёт не содержит tokens.
|
||||
- [ ] README задания сохранён, REPORT.md содержит инструкцию именно моего решения.
|
||||
Перенесена собственная реализация lab4: Go API, сессии Redis, PostgreSQL, приватный MinIO, две API-реплики и Nginx. Добавлены отдельный Redis Streams broker с AOF/volume, dispatcher с transactional outbox и worker. Upload возвращает 202 queued после durable staging и COMMIT задания; worker публикует original, thumbnail JPEG, crop JPEG 128×128 и optimized WebP. Есть lease, ограниченные повторы с backoff/jitter, failed registry, безопасный audit replay и очистка staging.
|
||||
|
||||
### Запуск
|
||||
|
||||
```sh
|
||||
sh scripts/init-local.sh
|
||||
docker compose --env-file .env up -d --build --wait
|
||||
make check
|
||||
make test
|
||||
python3 student-tests/verify_variants.py
|
||||
python3 student-tests/verify_pipeline.py
|
||||
python3 student-tests/verify_resilience.py
|
||||
```
|
||||
|
||||
При занятом 8080 изменить APP_PORT в локальном .env и передать соответствующий BASE_URL тестам. Локальная проверка проводилась на http://localhost:18085.
|
||||
|
||||
### Результаты
|
||||
|
||||
- [Отчёт и архитектура](../REPORT.md)
|
||||
- [Команды и результаты](../evidence/lab5-results.md)
|
||||
- [Варианты и границы](../evidence/variants-grade4.json)
|
||||
- [Outbox, crash, retry и replay](../evidence/pipeline-grade4.json)
|
||||
- [Перезапуски и отказ API](../evidence/resilience.json)
|
||||
|
||||
### Ограничения и источники
|
||||
|
||||
Один Docker-host; нет HA PostgreSQL/MinIO/broker. Доставка at least once, эффекты идемпотентны; exactly-once не заявляется. Pipeline version=1. Источники — собственная lab4, официальная документация Redis/Pillow/MinIO; помощь ИИ Codex описана в REPORT.md.
|
||||
|
||||
- [x] Обязательный минимум сохранён с переходом upload 201 → 202.
|
||||
- [x] Контракт и публичные tests не изменены.
|
||||
- [x] make check и make test выполнены.
|
||||
- [x] Сценарии уровня 4 воспроизведены.
|
||||
- [x] Секреты исключены из Git.
|
||||
- [x] README задания сохранён, инструкция решения находится в REPORT.md.
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
FROM golang:1.26.8-alpine3.23@sha256:a8fa79c5bd40d880b52bd3b6d7669ecdcfd00e85facdd427d279efb5ddd79cb1 AS build
|
||||
WORKDIR /src
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
COPY cmd/ ./cmd/
|
||||
COPY internal/ ./internal/
|
||||
RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o /messenger ./cmd/messenger
|
||||
|
||||
FROM scratch
|
||||
COPY --from=build /messenger /messenger
|
||||
USER 65532:65532
|
||||
ENV APP_HOST=0.0.0.0 APP_PORT=8080
|
||||
EXPOSE 8080
|
||||
HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 CMD ["/messenger", "healthcheck"]
|
||||
ENTRYPOINT ["/messenger"]
|
||||
@@ -1,37 +1,218 @@
|
||||
# Отчёт — лабораторная 5
|
||||
|
||||
- Автор, группа: TODO
|
||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
||||
- Commit/PR: TODO после публикации
|
||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
||||
- Автор: Меркулова М. А., БВТ2303.
|
||||
- Целевая оценка: **4** (уровни 3 и 4). Треки оценки 5 не заявляются.
|
||||
- Ветка: `solution/BVT2303-Merkulova`, создана от `main` skeleton lab5.
|
||||
- Основа: собственная реализация lab4; Go API, PostgreSQL, Redis-сессии, две API-реплики, Nginx, приватный MinIO. Контракт, публичные tests, README, COURSE и Makefile текущего skeleton сохранены.
|
||||
- Commit/PR: заполняется после публикации ветки в fork.
|
||||
- Помощь ИИ: Codex использован для реализации, анализа отказов, тестов и документации. Код проверялся запуском локального стенда; результаты находятся в `evidence/`.
|
||||
|
||||
## Запуск из чистого clone
|
||||
|
||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
||||
Нужны Docker Desktop с Linux containers, Docker Compose **2.22+** и BuildKit, Python 3.9+, make, OpenSSL; интернет для Docker Hub, GitHub/codeload, Go modules и PyPI. Windows: команды выполняются в WSL2 с интеграцией Docker Desktop. Нативный PowerShell не является средой исполнения shell scripts. Локальный Go необязателен.
|
||||
|
||||
## Архитектура и решения
|
||||
```sh
|
||||
cd mtusi2026lab5
|
||||
sh scripts/init-local.sh
|
||||
# Если localhost:8080 занят другой лабой, изменить APP_PORT в локальном .env.
|
||||
docker compose --env-file .env config --quiet
|
||||
docker compose --env-file .env up -d --build --wait
|
||||
make check
|
||||
make test
|
||||
```
|
||||
|
||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab4, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
||||
Compose build context `application: service:migrate` гарантирует сборку Go binary перед worker даже без локального образа. Данные/secrets предыдущей лабораторной не нужны. `s3-init` настраивает MinIO, `migrate` применяет SQL; оба после успеха завершаются с кодом 0.
|
||||
|
||||
Локально при проверке использовался **APP_PORT=18085**, поскольку 8080 занят lab4. Для всех команд проверки ниже в этом случае добавляется `BASE_URL=http://localhost:18085`:
|
||||
|
||||
```sh
|
||||
make test BASE_URL=http://localhost:18085
|
||||
BASE_URL=http://localhost:18085 python3 student-tests/verify_variants.py
|
||||
BASE_URL=http://localhost:18085 python3 student-tests/verify_pipeline.py
|
||||
```
|
||||
|
||||
При стандартном порте:
|
||||
|
||||
```sh
|
||||
python3 student-tests/verify_variants.py
|
||||
python3 student-tests/verify_pipeline.py
|
||||
make test-persistence ACTION_SCRIPT=scripts/restart.sh
|
||||
make test-failover ACTION_SCRIPT=scripts/stop-one.sh
|
||||
sh scripts/start-api.sh
|
||||
# Автоматически: StartedAt всех сервисов и целевой отказ каждой API-реплики
|
||||
python3 student-tests/verify_resilience.py
|
||||
```
|
||||
|
||||
Failover оставляет выбранную реплику остановленной; после проверки её надо восстановить указанной командой. Pipeline-тест сам восстанавливает остановленные им сервисы. Не запускать одновременно несколько тестов, меняющих состояние контейнеров.
|
||||
|
||||
Остановка с сохранением данных:
|
||||
|
||||
```sh
|
||||
docker compose --env-file .env down
|
||||
```
|
||||
|
||||
Намеренное удаление **только учебных данных этой лабы**, если требуется полный сброс:
|
||||
|
||||
```sh
|
||||
docker compose --env-file .env down --volumes
|
||||
```
|
||||
|
||||
Секреты лежат в `secrets/`, создаются с закрытыми правами и исключены из Git. `.env` также не коммитится. Менять ключи при существующем volume без процедуры ротации не следует.
|
||||
|
||||
Проверка чистого запуска выполнена в отдельной копии без `.env`/secrets, с четырьмя новыми volumes и отдельным project name: [clean-start.json](evidence/clean-start.json). При сборке использовался BuildKit cache.
|
||||
|
||||
## Версии и ресурсы
|
||||
|
||||
| Компонент | Версия / лимит |
|
||||
|---|---|
|
||||
| API и dispatcher | Go 1.26.8; исходный образ закреплён digest |
|
||||
| PostgreSQL | 17.6-alpine3.22, 1 CPU / 512 MiB |
|
||||
| Redis сессий | 8.2.1-alpine3.22, 0.25 CPU / 128 MiB |
|
||||
| Broker | отдельный Redis 8.2.1-alpine3.22, 0.25 CPU / 128 MiB, AOF `appendfsync always`, `noeviction`, отдельный volume |
|
||||
| MinIO | RELEASE.2025-10-15T17-29-55Z, сборка официальных исходников; checksum архива в `infra/Dockerfile.minio`, 1 CPU / 512 MiB |
|
||||
| Worker | Python 3.13.7-slim-bookworm, Pillow 12.0.0 + Go binary, 1 CPU / 1536 MiB |
|
||||
| API | две реплики, каждая 0.5 CPU / 1536 MiB; один upload slot, tmpfs 20 MiB |
|
||||
| Dispatcher / cleanup | по 0.25 CPU / 128 MiB |
|
||||
| Nginx | 1.29.1-alpine3.22, 0.25 CPU / 64 MiB |
|
||||
|
||||
Worker subprocess ограничен адресным пространством 1 GiB через `RLIMIT_AS`; весь контейнер ограничен 1536 MiB. `exec.CommandContext` завершает subprocess при timeout/cancel. Worker tmpfs 128 MiB, `prefetch=1`, `WORKER_CONCURRENCY=1` по умолчанию (допускается 1–4; увеличение требует проверки ресурсов). API не делает resize/WebP, но полностью декодирует вход для проверки повреждений перед принятием.
|
||||
|
||||
### Соответствие настроек
|
||||
|
||||
Из `.env` Compose использует внешний `APP_PORT`, `APP_HOST`, `APP_ENV`, TTL сессии и идемпотентности, DB timeout/pool, настройки попыток/worker/retention и grace очистки. Внутренний порт API — 8080, число API-реплик — 2. Адрес MinIO, регион и bucket заданы в `compose.yaml`; 10 MiB и 40 млн пикселей закреплены в коде. Поля `SESSION_STORE`, `API_REPLICAS`, `MAX_UPLOAD_BYTES`, `MAX_IMAGE_PIXELS` в примере skeleton служат справочными значениями и не переключают реализацию.
|
||||
|
||||
## Архитектура
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
C[Клиент] --> LB[Nginx]
|
||||
LB --> A1[API 1]
|
||||
LB --> A2[API 2]
|
||||
A1 --> PG[(PostgreSQL)]
|
||||
A2 --> PG
|
||||
A1 --> R[(Redis: сессии)]
|
||||
A2 --> R
|
||||
A1 --> S[(MinIO: staging, originals, variants)]
|
||||
A2 --> S
|
||||
PG --> D[Dispatcher: outbox]
|
||||
D --> B[(Redis Streams: broker)]
|
||||
B --> W[Worker + Pillow subprocess]
|
||||
W --> S
|
||||
W --> PG
|
||||
```
|
||||
|
||||
### Что изменено относительно lab4
|
||||
|
||||
- `004_async_pipeline.sql`: jobs, outbox, lease, попытки, варианты, реестр failed/replay, состояния queued/processing/ready/failed.
|
||||
- `attachments.go`: durable staging вместо немедленного ready, `202 queued`; метаданные вариантов и скачивание выбранного варианта; content до ready возвращает 409.
|
||||
- `server.go`: queued/processing разрешены в сообщениях, failed запрещён; проверка сессии/участия в чате сохранена.
|
||||
- `internal/pipeline/pipeline.go`: dispatcher, consumer group, XAUTOCLAIM, retry, lease, фиксация результата, retention, replay, точки crash injection.
|
||||
- `worker/process_image.py`: EXIF orientation, белый фон alpha, JPEG thumbnail/crop, WebP optimized.
|
||||
- Compose: отдельные broker/dispatcher/worker, volume брокера, параметры и лимиты.
|
||||
- `bootstrap.go`: права ограничены bucket и prefixes `staging/`, `originals/`, `variants/`; root credentials отсутствуют у API/worker/dispatcher/cleanup.
|
||||
- `cleanup.go`: старые неподтверждённые staging uploads удаляются только после grace, принятые jobs защищены.
|
||||
- `main.go`: режимы worker/dispatcher/replay/failed-jobs/fault, чтение конфигурации.
|
||||
- `restart.sh`: перезапускает также broker, dispatcher и worker с сохранением volumes.
|
||||
|
||||
### Граница 202 и transactional outbox
|
||||
|
||||
1. Проверяются доступ, имя, bytes (до 10 MiB), PNG/JPEG, пиксели (до 40 млн) и полная декодируемость.
|
||||
2. Запись о начатой загрузке фиксируется в PostgreSQL. Под общей с cleanup блокировкой оригинал записывается **в приватный MinIO prefix `staging/`**. Это сетевой S3 PUT, а не ephemeral disk и не завершение фоновой работы.
|
||||
3. **Одной PostgreSQL-транзакцией** сохраняются attachment queued, job и outbox; удаляется upload intent.
|
||||
4. Только после COMMIT клиент получает `202` со снимком queued, `variants=[]`, `error_code=null`.
|
||||
5. Dispatcher читает outbox и выполняет XADD. После положительного ответа брокера отмечает публикацию. Пока работа queued/due или lease истёк, намерение может публиковаться снова (не чаще 5 секунд).
|
||||
|
||||
Если API умер после staging до COMMIT — ответа 202 нет, остаток удаляется после grace. Если COMMIT выполнен, а publish не состоялся — outbox остаётся и dispatcher публикует после восстановления. Если publish состоялся, но его отметка потерялась — допустим дубль. API не требует доступного worker или broker для приёма: устойчивый синхронный путь требует PG, Redis sessions и MinIO. Broker/worker отсутствуют в API readiness намеренно; backlog хранится в PG/staging.
|
||||
|
||||
### Worker, lease и идемпотентность
|
||||
|
||||
Redis Stream `attachment.jobs.v1`, consumer group `image-workers`; сообщения JSON соответствуют `contracts/jobs.schema.json`. В них только schema/job/attachment/pipeline/attempt/time, без bytes, storage URL, filename или credentials. Storage key берётся из доверенной БД. `job_id` стабилен, pipeline_version=1, управляемая попытка берётся из PostgreSQL; повтор доставки не создаёт новый логический job.
|
||||
|
||||
Worker получает/восстанавливает сообщение, захватывает PostgreSQL advisory lock по job и в короткой транзакции переводит job/attachment в processing. Lease = `JOB_TIMEOUT_SECONDS + 5 секунд`, reclaim pending через XAUTOCLAIM после timeout+10 секунд. Dispatcher также повторно публикует expired lease; поэтому зависшая работа восстанавливается даже при потере stream-сообщения. Session advisory lock исключает одновременную обработку одного job; lease token проверяется перед публикацией результата.
|
||||
|
||||
Объектные ключи детерминированы:
|
||||
|
||||
- `originals/<attachment_id>` — неизменный оригинал;
|
||||
- `variants/<attachment_id>/v1/thumbnail`;
|
||||
- `variants/<attachment_id>/v1/crop`;
|
||||
- `variants/<attachment_id>/v1/optimized`.
|
||||
|
||||
После записи всех четырёх объектов **одна транзакция** устанавливает ready, variants/checksums и завершает job. Ack только после COMMIT результата или устойчивой записи retry/failed. Ready-дубль подтверждается без повторного resize/смены состояния/увеличения attempts. Для единственной consumer group после XACK сообщение удаляется XDEL; durable intent и результат остаются в PostgreSQL. Это **at least once с идемпотентным эффектом**, а не exactly-once доставка.
|
||||
|
||||
### Правила изображений
|
||||
|
||||
Оригинал возвращается побайтно неизменным. SHA-256/size/type вложения всегда относятся к оригиналу.
|
||||
|
||||
- EXIF orientation применяется перед преобразованиями.
|
||||
- Alpha компонуется на белый фон.
|
||||
- Thumbnail: JPEG quality=85, fit в 256×256, без увеличения, размеры округляются вниз, минимум 1; 320×200 → 256×160.
|
||||
- Crop: JPEG quality=85, центрированный квадрат 128×128; маленькие изображения допускают увеличение.
|
||||
- Optimized: lossy WebP quality=80, method=4, fit в 1280×1280 без увеличения.
|
||||
- В новых изображениях не сохраняются EXIF/ICC/text metadata. Уменьшение размера каждого исходника не обещается.
|
||||
|
||||
### Retry, failed registry и replay
|
||||
|
||||
`JOB_MAX_ATTEMPTS=5` (1–20), `JOB_TIMEOUT_SECONDS=60` (1–120). Временные ошибки storage/processing/timeout: задержка `2^(attempt-1)` секунд (база ограничена 32 сек) + случайный jitter 0–50%. Изменение next_at и queued фиксируется до ack; dispatcher публикует только наступившую попытку. Повреждённый источник/checksum — постоянная `invalid_image`, без бесполезных повторов. Исчерпание попыток — failed / `retry_exhausted`.
|
||||
|
||||
Реестр failed — таблица attachment_jobs, видимая оператору через CLI; публичные метаданные содержат только безопасный error_code. Некорректные broker envelopes попадают в `attachment.invalid.dlq` без содержимого/секретов.
|
||||
|
||||
```sh
|
||||
docker compose --env-file .env exec -T dispatcher /messenger failed-jobs
|
||||
# После устранения причины, с job UUID из реестра:
|
||||
docker compose --env-file .env exec -T dispatcher /messenger replay <job-uuid>
|
||||
```
|
||||
|
||||
Replay разрешён только failed, при сохранённом и доступном staging. Он атомарно создаёт audit-запись, возвращает queued, обнуляет бюджет попыток и открывает outbox для той же логической работы. Ready replay отклоняется. После истечения retention и удаления staging replay отклоняется; исходник нужно загрузить заново как новую операцию. CLI не публикуется как HTTP endpoint.
|
||||
|
||||
### Очистка
|
||||
|
||||
Dispatcher удаляет staging после атомарного ready. Для failed staging сохраняется `STAGING_RETENTION_SECONDS=86400` после последнего перехода; затем удаляется под блокировкой строки job, совместимой с replay. Processing/queued staging не удаляется. Не принятые uploads без job/метаданных удаляет cleanup после `CLEANUP_GRACE_SECONDS=3600` (минимум 120). Свежие и занятые объекты сохраняются. Ошибка удаления не уничтожает запись намерения, следующий проход повторяет очистку.
|
||||
|
||||
### Совместимость
|
||||
|
||||
Сохранены Basic→Bearer, expiry/logout, ACL, keyset pagination, идемпотентность сообщений и две реплики. Семантика пагинации как в lab4: порядок `(created_at,id)`, snapshot между запросами не гарантируется; запись с поздним COMMIT и более ранним ключом может не попасть в уже пройденную страницу. Подтверждённые записи видны новым чтениям через обе реплики.
|
||||
|
||||
## Доказательства критериев
|
||||
|
||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
||||
| --- | --- | --- | --- |
|
||||
| Публичный smoke | make test | TODO | TODO |
|
||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
||||
См. актуальный протокол [evidence/lab5-results.md](evidence/lab5-results.md), JSON [variants-grade4](evidence/variants-grade4.json), [pipeline-grade4](evidence/pipeline-grade4.json) и [resilience](evidence/resilience.json).
|
||||
|
||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
||||
| Критерий | Команда / сценарий | Что проверяется |
|
||||
|---|---|---|
|
||||
| Публичный минимум lab5 | `make check`, `make test` | 202, ready, original, настоящий JPEG thumbnail, предыдущие API |
|
||||
| Crop/WebP, orientation, alpha | `python3 student-tests/verify_variants.py` | Полное Pillow-декодирование ответов, размеры, EXIF, checksum |
|
||||
| Byte/pixel limits и доступ | тот же тест | Точные 10 MiB через LB, 413/415, outsider 404, приватность S3 |
|
||||
| Worker отсутствует, задача durable | `python3 student-tests/verify_pipeline.py` | queued/409, сообщение с queued, реальные рестарты API/broker и ready после старта worker |
|
||||
| COMMIT/publish gap | тот же тест | Реальный exit 86 dispatcher до XADD, автоматическое восстановление из outbox |
|
||||
| Crash S3 до COMMIT/ack | тот же тест | Exit 86 worker после объектов, expired processing, attempts=2, один набор variants |
|
||||
| COMMIT до ack, redelivery ready | тот же тест | Exit 86 перед ack, явный дубль, неизменный результат и attempts |
|
||||
| Backoff/bounded retry, failed/replay | тот же тест + `go test ./...` | Временная недоступность S3, исчерпание тестового бюджета 2, реестр, audit replay, запрет replay ready |
|
||||
| Постоянная ошибка | pipeline test | Неверный checksum → invalid_image, одна попытка; replay ранее созданного сообщения сохранён |
|
||||
| Безопасная очистка staging | pipeline test | Staging удалён после ready; queued переживает рестарт |
|
||||
| Сохранность всех хранилищ | `make test-persistence ACTION_SCRIPT=scripts/restart.sh` | Те же данные/токен/оригинал после перезапуска, StartedAt в evidence |
|
||||
| Отказ API | `make test-failover ACTION_SCRIPT=scripts/stop-one.sh` | Работа через surviving API, оригинал/данные сохранены |
|
||||
|
||||
## Эксперименты и отказы
|
||||
После обработки 40 млн пикселей измеренный `memory.peak` worker составил **701 313 024 байта (668.82 MiB)** при hard limit **1 610 612 736 байт (1536 MiB)**. Это результат указанного набора тестов, а не максимальная оценка для всех PNG/JPEG.
|
||||
|
||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
||||
## Сценарии отказов
|
||||
|
||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
||||
| --- | --- | --- | --- |
|
||||
| TODO | TODO | TODO | TODO |
|
||||
В `pipeline_faults` существуют CLI-only одноразовые точки `dispatcher_before_publish`, `dispatcher_after_publish`, `worker_after_claim`, `worker_after_objects`, `worker_before_ack`. Тест ставит точку через `/messenger fault arm <point>`; процесс устойчиво записывает hit и реально завершается `os.Exit(86)`. Политика Compose восстанавливает процесс. Это не симуляция успешного результата. Публичного HTTP управления fault injection нет.
|
||||
|
||||
## Ограничения и следующие шаги
|
||||
Проверки выполняются на синтетических данных. Нагрузочный трек оценки 5 не заявлен; производительность и production SLA из этих сценариев не выводятся.
|
||||
|
||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|
||||
## Ограничения
|
||||
|
||||
- Один Docker-host и одиночные PostgreSQL/MinIO/broker/Nginx: потерю host/volume не покрываем; необходимы резервные копии/репликация для production.
|
||||
- Durable staging уже находится в S3, worker публикует оригинал в конечный prefix того же MinIO. Это не отдельная физическая система хранения.
|
||||
- Backlog принятой работы хранится без установленной квоты; нагрузочный backpressure и сравнение concurrency относятся к треку 5 и не заявлены.
|
||||
- Публикация может создавать дубли; количество обработок ограничено, эффекты защищены. PostgreSQL jobs/outbox/audit сохраняются для диагностики; их архивирование не реализовано.
|
||||
- Параметры concurrency выше 1 требуют повторной проверки памяти; проверяемая конфигурация — 1.
|
||||
- Простой HTTP допустим только для локального изолированного стенда. Windows/WSL2 инструкция приведена, отдельный запуск на Windows не выполнялся.
|
||||
|
||||
## Источники
|
||||
|
||||
- Собственная реализация лабораторной 4.
|
||||
- [Redis Streams: consumer groups, pending и ack](https://redis.io/docs/latest/develop/data-types/streams/).
|
||||
- [Redis XAUTOCLAIM](https://redis.io/docs/latest/commands/xautoclaim/).
|
||||
- [Pillow ImageOps: EXIF и crop](https://pillow.readthedocs.io/en/stable/reference/ImageOps.html).
|
||||
- [Pillow форматы JPEG и WebP](https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html).
|
||||
- [Официальные исходники MinIO](https://github.com/minio/minio).
|
||||
|
||||
@@ -0,0 +1,338 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"mtusi-messenger/internal/database"
|
||||
"mtusi-messenger/internal/messenger"
|
||||
"mtusi-messenger/internal/objectstore"
|
||||
"mtusi-messenger/internal/pipeline"
|
||||
)
|
||||
|
||||
func env(name, fallback string) string {
|
||||
if value := os.Getenv(name); value != "" {
|
||||
return value
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
func secret(envName, fileEnv string) (string, error) {
|
||||
if value := os.Getenv(envName); value != "" {
|
||||
return value, nil
|
||||
}
|
||||
path := os.Getenv(fileEnv)
|
||||
if path == "" {
|
||||
return "", fmt.Errorf("set %s or %s", envName, fileEnv)
|
||||
}
|
||||
b, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
value := strings.TrimSpace(string(b))
|
||||
if value == "" {
|
||||
return "", fmt.Errorf("%s is empty", path)
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
func positiveInt(name string, fallback int) (int, error) {
|
||||
raw := env(name, strconv.Itoa(fallback))
|
||||
n, err := strconv.Atoi(raw)
|
||||
if err != nil || n < 1 {
|
||||
return 0, fmt.Errorf("invalid %s", name)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) > 1 && os.Args[1] == "memory" {
|
||||
values := map[string]int64{}
|
||||
for _, name := range []string{"memory.current", "memory.peak", "memory.max"} {
|
||||
data, err := os.ReadFile("/sys/fs/cgroup/" + name)
|
||||
if err != nil {
|
||||
log.Fatal("cgroup memory metrics unavailable")
|
||||
}
|
||||
n, err := strconv.ParseInt(strings.TrimSpace(string(data)), 10, 64)
|
||||
if err != nil {
|
||||
log.Fatal("invalid cgroup metric")
|
||||
}
|
||||
values[name] = n
|
||||
}
|
||||
_ = json.NewEncoder(os.Stdout).Encode(values)
|
||||
return
|
||||
}
|
||||
port := env("APP_PORT", "8080")
|
||||
if len(os.Args) > 1 && os.Args[1] == "healthcheck" {
|
||||
client := &http.Client{Timeout: 2 * time.Second}
|
||||
resp, err := client.Get("http://127.0.0.1:" + port + "/health/ready")
|
||||
if err != nil || resp.StatusCode != http.StatusOK {
|
||||
os.Exit(1)
|
||||
}
|
||||
_ = resp.Body.Close()
|
||||
return
|
||||
}
|
||||
host := env("APP_HOST", "0.0.0.0")
|
||||
if _, err := strconv.Atoi(port); err != nil {
|
||||
log.Fatal("invalid APP_PORT")
|
||||
}
|
||||
ttlSeconds, err := positiveInt("SESSION_TTL_SECONDS", 3600)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
idempotencyTTLSeconds, err := positiveInt("IDEMPOTENCY_TTL_SECONDS", 86400)
|
||||
if err != nil || idempotencyTTLSeconds < 86400 {
|
||||
log.Fatal("IDEMPOTENCY_TTL_SECONDS must be at least 86400")
|
||||
}
|
||||
dbTimeoutMS, err := positiveInt("DB_TIMEOUT_MS", 3000)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
maxConns, err := positiveInt("DB_MAX_CONNS", 10)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
databaseURL, err := secret("DATABASE_URL", "DATABASE_URL_FILE")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
logger := log.New(os.Stdout, "", log.LstdFlags|log.LUTC)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
config, err := pgxpool.ParseConfig(databaseURL)
|
||||
if err != nil {
|
||||
log.Fatal("invalid database configuration")
|
||||
}
|
||||
config.MaxConns = int32(maxConns)
|
||||
config.ConnConfig.RuntimeParams["application_name"] = env("INSTANCE_ID", "maintenance")
|
||||
config.MinConns = 1
|
||||
config.MaxConnLifetime = 30 * time.Minute
|
||||
var pool *pgxpool.Pool
|
||||
for {
|
||||
pool, err = pgxpool.NewWithConfig(ctx, config)
|
||||
if err == nil {
|
||||
err = pool.Ping(ctx)
|
||||
}
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
if pool != nil {
|
||||
pool.Close()
|
||||
}
|
||||
if ctx.Err() != nil {
|
||||
log.Fatal("database unavailable during startup")
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
defer pool.Close()
|
||||
if len(os.Args) > 1 && os.Args[1] == "migrate" {
|
||||
if err = database.Migrate(ctx, pool); err != nil {
|
||||
log.Fatalf("migration failed: %v", err)
|
||||
}
|
||||
logger.Print("migrations complete")
|
||||
return
|
||||
}
|
||||
access, err := secret("S3_ACCESS_KEY_ID", "S3_ACCESS_KEY_ID_FILE")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
secretKey, err := secret("S3_SECRET_ACCESS_KEY", "S3_SECRET_ACCESS_KEY_FILE")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
objects, err := objectstore.New(env("S3_ENDPOINT", "http://s3:9000"), env("S3_REGION", "us-east-1"), env("S3_BUCKET", "messenger-attachments"), access, secretKey)
|
||||
if err != nil {
|
||||
log.Fatal("invalid S3 configuration")
|
||||
}
|
||||
if len(os.Args) > 1 && os.Args[1] == "s3-bootstrap" {
|
||||
for {
|
||||
err = objects.Client.MakeBucket(ctx, objects.Bucket, minio.MakeBucketOptions{Region: env("S3_REGION", "us-east-1")})
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
if exists, e := objects.Client.BucketExists(ctx, objects.Bucket); e == nil && exists {
|
||||
break
|
||||
}
|
||||
if ctx.Err() != nil {
|
||||
log.Fatal("S3 bootstrap unavailable")
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
if err = objects.Client.SetBucketPolicy(ctx, objects.Bucket, ""); err != nil {
|
||||
log.Fatal("cannot enforce private bucket")
|
||||
}
|
||||
apiAccess, err := secret("S3_API_ACCESS_KEY_ID", "S3_API_ACCESS_KEY_ID_FILE")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
apiSecret, err := secret("S3_API_SECRET_ACCESS_KEY", "S3_API_SECRET_ACCESS_KEY_FILE")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err = objectstore.Bootstrap(ctx, env("S3_ENDPOINT", "http://s3:9000"), objects.Bucket, access, secretKey, apiAccess, apiSecret); err != nil {
|
||||
log.Fatalf("S3 policy bootstrap: %v", err)
|
||||
}
|
||||
logger.Print("private MinIO bucket and limited API user ready")
|
||||
return
|
||||
}
|
||||
|
||||
if len(os.Args) > 1 && (os.Args[1] == "worker" || os.Args[1] == "dispatcher" || os.Args[1] == "replay" || os.Args[1] == "failed-jobs" || os.Args[1] == "fault") {
|
||||
|
||||
var broker *redis.Client
|
||||
if os.Args[1] == "worker" || os.Args[1] == "dispatcher" {
|
||||
brokerURL, e := secret("BROKER_URL", "BROKER_URL_FILE")
|
||||
if e != nil {
|
||||
log.Fatal(e)
|
||||
}
|
||||
options, e := redis.ParseURL(brokerURL)
|
||||
if e != nil {
|
||||
log.Fatal("invalid broker configuration")
|
||||
}
|
||||
options.DialTimeout = 2 * time.Second
|
||||
options.ReadTimeout = 3 * time.Second
|
||||
options.WriteTimeout = 3 * time.Second
|
||||
broker = redis.NewClient(options)
|
||||
defer broker.Close()
|
||||
}
|
||||
timeout, e := positiveInt("JOB_TIMEOUT_SECONDS", 60)
|
||||
if e != nil || timeout > 120 {
|
||||
log.Fatal("JOB_TIMEOUT_SECONDS must be 1..120")
|
||||
}
|
||||
retention, e := positiveInt("STAGING_RETENTION_SECONDS", 86400)
|
||||
if e != nil {
|
||||
log.Fatal(e)
|
||||
}
|
||||
processor := &pipeline.Pipeline{DB: pool, Broker: broker, Objects: objects, Timeout: time.Duration(timeout) * time.Second, Retention: time.Duration(retention) * time.Second, Logger: logger}
|
||||
runctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, os.Interrupt)
|
||||
defer stop()
|
||||
switch os.Args[1] {
|
||||
case "dispatcher":
|
||||
processor.Dispatch(runctx)
|
||||
case "worker":
|
||||
concurrency, e := positiveInt("WORKER_CONCURRENCY", 1)
|
||||
if e != nil {
|
||||
log.Fatal(e)
|
||||
}
|
||||
if e = processor.Worker(runctx, concurrency); e != nil {
|
||||
log.Fatal(e)
|
||||
}
|
||||
default:
|
||||
if e = processor.Command(ctx, os.Args[1:]); e != nil {
|
||||
log.Fatal(e)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
if len(os.Args) > 1 && (os.Args[1] == "cleanup" || os.Args[1] == "cleanup-loop") {
|
||||
grace, err := positiveInt("CLEANUP_GRACE_SECONDS", 3600)
|
||||
if err != nil || grace < 120 {
|
||||
log.Fatal("CLEANUP_GRACE_SECONDS must be at least 120")
|
||||
}
|
||||
loopctx, stopCleanup := signal.NotifyContext(context.Background(), syscall.SIGTERM, os.Interrupt)
|
||||
defer stopCleanup()
|
||||
for {
|
||||
cleanupctx, cancelCleanup := context.WithTimeout(loopctx, 60*time.Second)
|
||||
result, cleanupErr := messenger.Cleanup(cleanupctx, pool, objects, time.Duration(grace)*time.Second)
|
||||
cancelCleanup()
|
||||
if cleanupErr != nil {
|
||||
logger.Print("cleanup failed; retained objects for next sweep")
|
||||
} else {
|
||||
_ = json.NewEncoder(os.Stdout).Encode(result)
|
||||
}
|
||||
if os.Args[1] == "cleanup" {
|
||||
if cleanupErr != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
return
|
||||
}
|
||||
select {
|
||||
case <-loopctx.Done():
|
||||
return
|
||||
case <-time.After(60 * time.Second):
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(os.Args) > 2 && os.Args[1] == "s3-audit" {
|
||||
if err = objects.Audit(ctx, os.Args[2]); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println("PASS: anonymous S3 GET=403; API credentials cannot create buckets")
|
||||
return
|
||||
}
|
||||
if len(os.Args) > 2 && os.Args[1] == "s3-stat" {
|
||||
info, err := objects.Client.StatObject(ctx, objects.Bucket, os.Args[2], minio.StatObjectOptions{})
|
||||
if err != nil {
|
||||
if minio.ToErrorResponse(err).Code != "NoSuchKey" {
|
||||
log.Fatal("S3 stat failed")
|
||||
}
|
||||
fmt.Println(`{"exists":false}`)
|
||||
return
|
||||
}
|
||||
_ = json.NewEncoder(os.Stdout).Encode(map[string]any{"exists": true, "size_bytes": info.Size})
|
||||
return
|
||||
}
|
||||
instanceID := env("INSTANCE_ID", "")
|
||||
if instanceID == "" {
|
||||
log.Fatal("INSTANCE_ID is required")
|
||||
}
|
||||
cacheURL, err := secret("CACHE_URL", "CACHE_URL_FILE")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
redisOptions, err := redis.ParseURL(cacheURL)
|
||||
if err != nil {
|
||||
log.Fatal("invalid cache configuration")
|
||||
}
|
||||
sessions := redis.NewClient(redisOptions)
|
||||
defer sessions.Close()
|
||||
for {
|
||||
if err = sessions.Ping(ctx).Err(); err == nil {
|
||||
break
|
||||
}
|
||||
if ctx.Err() != nil {
|
||||
log.Fatal("session store unavailable during startup")
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
for {
|
||||
if err = objects.Ready(ctx); err == nil {
|
||||
break
|
||||
}
|
||||
if ctx.Err() != nil {
|
||||
log.Fatal("S3 unavailable during startup")
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
dbTimeout := time.Duration(dbTimeoutMS) * time.Millisecond
|
||||
srv := &http.Server{Addr: host + ":" + port, Handler: messenger.NewHandler(pool, sessions, objects, time.Duration(ttlSeconds)*time.Second, time.Duration(idempotencyTTLSeconds)*time.Second, dbTimeout, instanceID, logger), ReadHeaderTimeout: 5 * time.Second, ReadTimeout: 45 * time.Second, WriteTimeout: 60 * time.Second, IdleTimeout: 30 * time.Second, MaxHeaderBytes: 32 * 1024}
|
||||
stop := make(chan os.Signal, 1)
|
||||
signal.Notify(stop, syscall.SIGTERM, os.Interrupt)
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
<-stop
|
||||
logger.Print("shutdown started")
|
||||
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 8*time.Second)
|
||||
defer shutdownCancel()
|
||||
if shutdownErr := srv.Shutdown(shutdownCtx); shutdownErr != nil {
|
||||
logger.Printf("shutdown error: %v", shutdownErr)
|
||||
}
|
||||
logger.Print("shutdown complete")
|
||||
}()
|
||||
logger.Printf("listening on %s", srv.Addr)
|
||||
if err = srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
logger.Fatal(err)
|
||||
}
|
||||
<-done
|
||||
}
|
||||
+363
@@ -0,0 +1,363 @@
|
||||
name: mtusi-lab5
|
||||
|
||||
x-api-common: &api-common
|
||||
image: mtusi-lab5-api:local
|
||||
environment: &api-environment
|
||||
APP_HOST: ${APP_HOST:-0.0.0.0}
|
||||
APP_PORT: 8080
|
||||
APP_ENV: ${APP_ENV:-lab}
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
|
||||
DB_TIMEOUT_MS: ${DB_TIMEOUT_MS:-3000}
|
||||
DB_MAX_CONNS: ${DB_MAX_CONNS:-10}
|
||||
GOMEMLIMIT: 1200MiB
|
||||
S3_ENDPOINT: http://s3:9000
|
||||
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
|
||||
BROKER_URL_FILE: /run/secrets/broker_url
|
||||
JOB_MAX_ATTEMPTS: ${JOB_MAX_ATTEMPTS:-5}
|
||||
JOB_TIMEOUT_SECONDS: ${JOB_TIMEOUT_SECONDS:-60}
|
||||
WORKER_CONCURRENCY: ${WORKER_CONCURRENCY:-1}
|
||||
STAGING_RETENTION_SECONDS: ${STAGING_RETENTION_SECONDS:-86400}
|
||||
CLEANUP_GRACE_SECONDS: ${CLEANUP_GRACE_SECONDS:-3600}
|
||||
DATABASE_URL_FILE: ${DATABASE_URL_FILE:-/run/secrets/database_url}
|
||||
CACHE_URL_FILE: ${CACHE_URL_FILE:-/run/secrets/cache_url}
|
||||
secrets:
|
||||
- database_url
|
||||
- cache_url
|
||||
- s3_access_key_id
|
||||
- s3_secret_access_key
|
||||
tmpfs:
|
||||
- /tmp:size=20m,mode=1777
|
||||
networks:
|
||||
- frontend
|
||||
- backend
|
||||
healthcheck:
|
||||
test: ["CMD", "/messenger", "healthcheck"]
|
||||
interval: 3s
|
||||
timeout: 2s
|
||||
retries: 20
|
||||
start_period: 3s
|
||||
restart: unless-stopped
|
||||
init: true
|
||||
stop_grace_period: 10s
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.50"
|
||||
memory: 1536M
|
||||
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:17.6-alpine3.22
|
||||
environment:
|
||||
POSTGRES_DB: messenger
|
||||
POSTGRES_USER: messenger
|
||||
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
|
||||
secrets:
|
||||
- postgres_password
|
||||
volumes:
|
||||
- postgres_data:/var/lib/postgresql/data
|
||||
networks:
|
||||
- backend
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"]
|
||||
interval: 3s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
restart: unless-stopped
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "1.00"
|
||||
memory: 512M
|
||||
|
||||
redis:
|
||||
image: redis:8.2.1-alpine3.22
|
||||
command:
|
||||
- /bin/sh
|
||||
- -ec
|
||||
- exec redis-server --appendonly yes --appendfsync always --requirepass "$$(cat /run/secrets/redis_password)"
|
||||
secrets:
|
||||
- redis_password
|
||||
volumes:
|
||||
- redis_data:/data
|
||||
networks:
|
||||
- backend
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "redis-cli --no-auth-warning -a \"$$(cat /run/secrets/redis_password)\" ping | grep PONG"]
|
||||
interval: 3s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
restart: unless-stopped
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.25"
|
||||
memory: 128M
|
||||
|
||||
migrate:
|
||||
image: mtusi-lab5-api:local
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
healthcheck:
|
||||
disable: true
|
||||
command: ["migrate"]
|
||||
environment:
|
||||
<<: *api-environment
|
||||
secrets:
|
||||
- database_url
|
||||
networks:
|
||||
- backend
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
restart: "no"
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.25"
|
||||
memory: 128M
|
||||
|
||||
api1:
|
||||
<<: *api-common
|
||||
environment:
|
||||
<<: *api-environment
|
||||
INSTANCE_ID: api-1
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
redis:
|
||||
condition: service_healthy
|
||||
s3-init:
|
||||
condition: service_completed_successfully
|
||||
|
||||
api2:
|
||||
<<: *api-common
|
||||
environment:
|
||||
<<: *api-environment
|
||||
INSTANCE_ID: api-2
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
redis:
|
||||
condition: service_healthy
|
||||
s3-init:
|
||||
condition: service_completed_successfully
|
||||
|
||||
s3:
|
||||
image: mtusi-lab5-minio:RELEASE.2025-10-15T17-29-55Z
|
||||
build:
|
||||
context: .
|
||||
dockerfile: infra/Dockerfile.minio
|
||||
command: ["server", "/data", "--address", ":9000", "--console-address", "127.0.0.1:9001"]
|
||||
environment:
|
||||
MINIO_ROOT_USER_FILE: /run/secrets/s3_admin_access
|
||||
MINIO_ROOT_PASSWORD_FILE: /run/secrets/s3_admin_secret
|
||||
MINIO_BROWSER: "off"
|
||||
MINIO_UPDATE: "off"
|
||||
volumes:
|
||||
- minio_data:/data
|
||||
secrets:
|
||||
- s3_admin_access
|
||||
- s3_admin_secret
|
||||
networks:
|
||||
- backend
|
||||
healthcheck:
|
||||
test: ["CMD", "wget", "-q", "-T", "1", "-O", "/dev/null", "http://127.0.0.1:9000/minio/health/ready"]
|
||||
interval: 3s
|
||||
timeout: 2s
|
||||
retries: 20
|
||||
restart: unless-stopped
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "1.00"
|
||||
memory: 512M
|
||||
|
||||
s3-init:
|
||||
image: mtusi-lab5-api:local
|
||||
healthcheck:
|
||||
disable: true
|
||||
command: ["s3-bootstrap"]
|
||||
environment:
|
||||
<<: *api-environment
|
||||
S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_admin_access
|
||||
S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_admin_secret
|
||||
S3_API_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id
|
||||
S3_API_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key
|
||||
secrets:
|
||||
- database_url
|
||||
- s3_access_key_id
|
||||
- s3_secret_access_key
|
||||
- s3_admin_access
|
||||
- s3_admin_secret
|
||||
networks:
|
||||
- backend
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
s3:
|
||||
condition: service_healthy
|
||||
restart: "no"
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.25"
|
||||
memory: 128M
|
||||
|
||||
cleanup:
|
||||
image: mtusi-lab5-api:local
|
||||
healthcheck:
|
||||
disable: true
|
||||
command: ["cleanup-loop"]
|
||||
environment:
|
||||
<<: *api-environment
|
||||
secrets:
|
||||
- database_url
|
||||
- s3_access_key_id
|
||||
- s3_secret_access_key
|
||||
networks:
|
||||
- backend
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
s3-init:
|
||||
condition: service_completed_successfully
|
||||
restart: unless-stopped
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.25"
|
||||
memory: 128M
|
||||
|
||||
broker:
|
||||
image: redis:8.2.1-alpine3.22
|
||||
command: ["/bin/sh", "-ec", 'exec redis-server --appendonly yes --appendfsync always --maxmemory-policy noeviction --requirepass "$$(cat /run/secrets/broker_password)"']
|
||||
secrets: [broker_password]
|
||||
volumes: [broker_data:/data]
|
||||
networks: [backend]
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", 'redis-cli --no-auth-warning -a "$$(cat /run/secrets/broker_password)" ping | grep PONG']
|
||||
interval: 3s
|
||||
timeout: 2s
|
||||
retries: 20
|
||||
restart: unless-stopped
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.25"
|
||||
memory: 128M
|
||||
|
||||
dispatcher:
|
||||
image: mtusi-lab5-api:local
|
||||
command: ["dispatcher"]
|
||||
healthcheck:
|
||||
disable: true
|
||||
environment:
|
||||
<<: *api-environment
|
||||
secrets: [database_url, s3_access_key_id, s3_secret_access_key, broker_url]
|
||||
networks: [backend]
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
s3-init:
|
||||
condition: service_completed_successfully
|
||||
broker:
|
||||
condition: service_healthy
|
||||
restart: unless-stopped
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.25"
|
||||
memory: 128M
|
||||
|
||||
worker:
|
||||
image: mtusi-lab5-worker:local
|
||||
build:
|
||||
context: .
|
||||
dockerfile: infra/Dockerfile.worker
|
||||
additional_contexts:
|
||||
application: service:migrate
|
||||
command: ["worker"]
|
||||
environment:
|
||||
<<: *api-environment
|
||||
GOMEMLIMIT: 128MiB
|
||||
secrets: [database_url, s3_access_key_id, s3_secret_access_key, broker_url]
|
||||
networks: [backend]
|
||||
tmpfs: ["/tmp:size=128m,mode=1777"]
|
||||
depends_on:
|
||||
dispatcher:
|
||||
condition: service_started
|
||||
init: true
|
||||
restart: unless-stopped
|
||||
stop_grace_period: 5s
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "1.00"
|
||||
memory: 1536M
|
||||
|
||||
nginx:
|
||||
image: nginx:1.29.1-alpine3.22
|
||||
command: ["/bin/sh", "/health-aware.sh"]
|
||||
volumes:
|
||||
- ./infra/nginx.conf:/etc/nginx/nginx.conf:ro
|
||||
- ./infra/health-aware.sh:/health-aware.sh:ro
|
||||
networks:
|
||||
- frontend
|
||||
depends_on:
|
||||
api1:
|
||||
condition: service_healthy
|
||||
api2:
|
||||
condition: service_healthy
|
||||
ports:
|
||||
- "127.0.0.1:${APP_PORT:-8080}:8080"
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "wget -q -O /dev/null http://127.0.0.1:8080/health/ready"]
|
||||
interval: 3s
|
||||
timeout: 2s
|
||||
retries: 20
|
||||
restart: unless-stopped
|
||||
stop_grace_period: 5s
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.25"
|
||||
memory: 64M
|
||||
|
||||
secrets:
|
||||
broker_password:
|
||||
file: ./secrets/broker_password
|
||||
broker_url:
|
||||
file: ./secrets/broker_url
|
||||
postgres_password:
|
||||
file: ./secrets/postgres_password
|
||||
redis_password:
|
||||
file: ./secrets/redis_password
|
||||
database_url:
|
||||
file: ./secrets/database_url
|
||||
cache_url:
|
||||
file: ./secrets/cache_url
|
||||
s3_access_key_id:
|
||||
file: ./secrets/s3_access_key_id
|
||||
s3_secret_access_key:
|
||||
file: ./secrets/s3_secret_access_key
|
||||
s3_admin_access:
|
||||
file: ./secrets/s3_admin_access
|
||||
s3_admin_secret:
|
||||
file: ./secrets/s3_admin_secret
|
||||
|
||||
volumes:
|
||||
broker_data:
|
||||
postgres_data:
|
||||
redis_data:
|
||||
minio_data:
|
||||
|
||||
networks:
|
||||
frontend:
|
||||
backend:
|
||||
internal: true
|
||||
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"status": "PASS",
|
||||
"date": "2026-11-02",
|
||||
"project": "mtusi-lab5-clean-audit",
|
||||
"base_url": "http://localhost:18086",
|
||||
"source": "isolated source copy excluding .git, .env, secrets and virtual environments",
|
||||
"fresh_secret_files": true,
|
||||
"fresh_volumes": [
|
||||
"postgres_data",
|
||||
"redis_data",
|
||||
"minio_data",
|
||||
"broker_data"
|
||||
],
|
||||
"build": "docker compose -p mtusi-lab5-clean-audit --env-file .env up -d --build --wait",
|
||||
"build_cache_used": true,
|
||||
"public_smoke": "PASS: lab5 public API scenario; infrastructure and grading evidence remain separate."
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
# Результаты лабораторной 5 — оценка 4
|
||||
|
||||
Дата: **2 ноября 2026 года**. Проверка выполнена на macOS arm64, Docker Desktop Linux VM: Engine **29.2.1**, Compose **5.0.2**, 10 CPU и 8 217 059 328 байт RAM VM. Лимиты каждого сервиса заданы в compose.yaml и описаны в REPORT.md. Локальный порт — **18085**, прежняя lab4 использует 8080. Все пользователи, изображения и сообщения синтетические; токены и credentials в доказательства не записаны.
|
||||
|
||||
## Сборка и локальные проверки
|
||||
|
||||
```sh
|
||||
sh scripts/init-local.sh
|
||||
docker compose --env-file .env config --quiet
|
||||
docker compose --env-file .env build worker
|
||||
docker compose --env-file .env up -d --wait
|
||||
make check
|
||||
CGO_ENABLED=0 GOCACHE=/private/tmp/mtusi-lab5-go-cache go test ./...
|
||||
CGO_ENABLED=0 GOCACHE=/private/tmp/mtusi-lab5-go-cache go vet ./...
|
||||
make test BASE_URL=http://localhost:18085
|
||||
```
|
||||
|
||||
Результат:
|
||||
|
||||
```text
|
||||
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
|
||||
ok mtusi-messenger/internal/messenger
|
||||
ok mtusi-messenger/internal/pipeline
|
||||
# go vet: exit 0, без диагностик
|
||||
PASS: lab5 public API scenario; infrastructure and grading evidence remain separate.
|
||||
```
|
||||
|
||||
Также проверены Python AST/py_compile, `sh -n`, `git diff --check`, исключение `.env`/secrets из Git. Публичные README/COURSE/CONTRIBUTING/Makefile/lab.json/tests/contracts не изменены. Полный необязательный `make validate` здесь не запускался: зависимости requirements-dev.txt локально не установлены. Публичный smoke выполняет предусмотренную своим клиентом проверку ответов API.
|
||||
|
||||
## Чистый запуск
|
||||
|
||||
В отдельную директорию скопировано текущее решение **без `.git`, `.env`, secrets и виртуальных сред**. Созданы новые секреты. Стенд `mtusi-lab5-clean-audit` поднят с пустыми PostgreSQL/Redis/MinIO/broker volumes, портом 18086 и `up -d --build --wait`. Миграции, bootstrap и публичный smoke прошли. BuildKit cache использовался. Проверка выполнена на изолированной исходной копии.
|
||||
|
||||
[clean-start.json](clean-start.json). После проверки удалены только временные контейнеры и volumes проекта clean-audit; основной стенд сохранён.
|
||||
|
||||
## Изображения, ограничения и доступ
|
||||
|
||||
```sh
|
||||
BASE_URL=http://localhost:18085 python3 student-tests/verify_variants.py
|
||||
```
|
||||
|
||||
```text
|
||||
PASS: actual JPEG/WebP, EXIF orientation, white alpha, bounds, ACL, checksums, private S3
|
||||
```
|
||||
|
||||
Фактически проверено:
|
||||
|
||||
- JPEG с EXIF orientation=6: исходные 320×200 → thumbnail 160×256 и optimized 200×320 после поворота; original неизменен.
|
||||
- Прозрачный PNG 8×4: thumbnail 8×4 без увеличения, crop 128×128, белый фон; EXIF у вариантов отсутствует.
|
||||
- PNG 1601×801: thumbnail 256×128 и optimized 1280×640 с округлением вниз.
|
||||
- **Ровно 40 млн пикселей**, 8000×5000: ready, thumbnail 256×160, crop 128×128, WebP 1280×800.
|
||||
- Все варианты реально декодированы Pillow из HTTP-ответа; размеры, MIME, SHA-256 и размер bytes совпали.
|
||||
- Корректные PNG **6 294 458** и **10 485 760** байт прошли через Nginx и сохранились побайтно.
|
||||
- 10 485 761 байт → 413; заголовок 20000×10000 пикселей → 413; обрезанные PNG и чужой формат → 415.
|
||||
- Посторонний пользователь → 404; неизвестный вариант → 404.
|
||||
- Анонимное чтение MinIO → 403; создание bucket прикладными ключами → AccessDenied.
|
||||
|
||||
Пик памяти worker за этот запуск: **701 313 024 байта = 668.82 MiB**, hard limit **1536 MiB**, OOM в этом сценарии не было. Это измерение cgroup memory.peak, включает запуск Go процесса и дочерние декодеры. Оно не доказывает максимальную производительность/память для любого изображения.
|
||||
|
||||
Доказательства: [variants-grade4.json](variants-grade4.json).
|
||||
|
||||
## Устойчивый pipeline, crash, retry и replay
|
||||
|
||||
```sh
|
||||
BASE_URL=http://localhost:18085 python3 student-tests/verify_pipeline.py
|
||||
```
|
||||
|
||||
Результат: **PASS**, все проверки ниже выполнены.
|
||||
|
||||
| Сценарий | Наблюдение |
|
||||
|---|---|
|
||||
| Worker остановлен | Upload 202 queued; content 409; сообщение с queued-вложением создано |
|
||||
| Рестарт API и broker | StartedAt изменились, job сохранился; после старта worker тот же attachment стал ready |
|
||||
| COMMIT → publish | Dispatcher реально завершился exit 86 перед XADD; Compose восстановил процесс, outbox доставлен |
|
||||
| S3 objects → COMMIT результата/ack | Worker завершился exit 86, остался processing; lease восстановлен, **65.386 с**, attempts=2, один набор variants |
|
||||
| COMMIT ready → ack | Worker завершился exit 86; явный ready-дубль подтверждён, результат/attempts неизменны |
|
||||
| S3 недоступен | Две управляемые тестовые попытки, backoff, финальный failed/retry_exhausted |
|
||||
| Явный replay после восстановления S3 | Тот же job → ready; одна audit-запись; повтор replay ready отклонён |
|
||||
| Постоянная ошибка источника | Несовпадение checksum → invalid_image, одна попытка |
|
||||
| Replay уже созданного сообщения после failed вложения | Возвращает прежний Message; создание нового сообщения с failed-вложением → 409 |
|
||||
| Staging после ready | Удалён после безопасной фиксации результата |
|
||||
|
||||
Фрагменты реального лога (UTC):
|
||||
|
||||
```text
|
||||
2026/11/02 14:00:54 injected crash: dispatcher_before_publish
|
||||
2026/11/02 14:00:55 injected crash: worker_after_objects
|
||||
2026/11/02 14:02:02 injected crash: worker_before_ack
|
||||
```
|
||||
|
||||
Тест отказа запускает worker через `docker start <container-id>`, чтобы Compose не поднял зависимость S3 автоматически. Дополнительно проверяется, что S3 остаётся остановленным. Retry delay unit-тест проверяет нижнюю/верхнюю границу jitter для попыток 1–20.
|
||||
|
||||
Доказательства: [pipeline-grade4.json](pipeline-grade4.json).
|
||||
|
||||
## Сохранность и отказ API
|
||||
|
||||
```sh
|
||||
BASE_URL=http://localhost:18085 python3 student-tests/verify_resilience.py
|
||||
```
|
||||
|
||||
```text
|
||||
PASS: all ten services restarted, same token/data/original preserved; both API failures covered
|
||||
```
|
||||
|
||||
Публичный persistence hook действительно перезапустил **10 сервисов**: PostgreSQL, Redis sessions, MinIO, broker, api1, api2, dispatcher, worker, cleanup, Nginx. У каждого изменился StartedAt; прежний токен, данные и оригинал сохранились. Таблицы timestamps находятся в JSON.
|
||||
|
||||
Публичный failover: 2 реплики наблюдались до отказа, **132 успешные записи**, 0 неудачных попыток, первая успешная запись после переключения **0.835 с**.
|
||||
|
||||
Дополнительный целевой отказ каждой реплики:
|
||||
|
||||
| Остановлена | Подтверждённые сообщения | Переходные ошибки | Первый успех surviving API |
|
||||
|---|---:|---:|---:|
|
||||
| api1 | 68 | 0 | 0.838 с |
|
||||
| api2 | 67 | 1 | 0.845 с |
|
||||
|
||||
Остановленный контейнер действительно оставался остановленным до восстановления. Подтверждённые сообщения найдены в истории; original совпал побайтно. Одна переходная ошибка при остановке api2 учтена, запрос повторялся с тем же ключом. После проверок обе API-реплики восстановлены, публичный smoke повторно прошёл.
|
||||
|
||||
Доказательства: [resilience.json](resilience.json).
|
||||
|
||||
## Границы результата
|
||||
|
||||
Проверены требования уровней 3 и 4 README на локальном стенде с concurrency worker=1. Нагрузочный/версионный треки оценки 5 не заявлены. Один host и одиночные хранилища не защищают от потери host или volume. Staging и конечные prefixes находятся в одном приватном MinIO; Redis Streams обеспечивает повторяемую доставку, PostgreSQL — источник истины о работе и результате. Секреты и данные предыдущей лабы для запуска не требуются.
|
||||
@@ -0,0 +1,36 @@
|
||||
{
|
||||
"stopped_worker_and_restart": {
|
||||
"status": "PASS",
|
||||
"before": {
|
||||
"api1": "2026-11-02T14:00:34.080908093Z",
|
||||
"api2": "2026-11-02T14:00:34.08241651Z",
|
||||
"broker": "2026-11-02T13:57:41.170552555Z"
|
||||
},
|
||||
"after": {
|
||||
"api1": "2026-11-02T14:00:51.126274796Z",
|
||||
"api2": "2026-11-02T14:00:51.059763046Z",
|
||||
"broker": "2026-11-02T14:00:51.091309254Z"
|
||||
}
|
||||
},
|
||||
"commit_publish_gap": "PASS: actual dispatcher exit 86 and automatic restart",
|
||||
"crash_after_objects": {
|
||||
"status": "PASS",
|
||||
"recovery_seconds": 65.386,
|
||||
"attempts": 2
|
||||
},
|
||||
"commit_before_ack_and_duplicate": "PASS",
|
||||
"retry_failed_registry_replay": {
|
||||
"status": "PASS",
|
||||
"exhausted_attempts": 2,
|
||||
"error_code": "retry_exhausted",
|
||||
"replay_audit_rows": 1,
|
||||
"ready_replay_rejected": true
|
||||
},
|
||||
"permanent_error": "PASS",
|
||||
"message_replay_after_attachment_failure": "PASS",
|
||||
"crash_log": [
|
||||
"2026/11/02 14:00:54 injected crash: dispatcher_before_publish",
|
||||
"2026/11/02 14:00:55 injected crash: worker_after_objects",
|
||||
"2026/11/02 14:02:02 injected crash: worker_before_ack"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
{
|
||||
"persistence": {
|
||||
"status": "PASS",
|
||||
"before": {
|
||||
"postgres": {
|
||||
"id": "52dc31d0bffdf348ef3e53c9d2828152997e4c75416af7b19fe837c4102b683a",
|
||||
"started_at": "2026-11-02T13:57:41.143467097Z"
|
||||
},
|
||||
"redis": {
|
||||
"id": "ade5ef52cec5df789f052224c1b54a3cdb087fc4aa8193e496252052558076f1",
|
||||
"started_at": "2026-11-02T13:57:41.19192493Z"
|
||||
},
|
||||
"s3": {
|
||||
"id": "380e323fa3e1c06dca1f45faa9cc747b0a10779dd83865c764b64bd033f19af5",
|
||||
"started_at": "2026-11-02T14:02:09.541073263Z"
|
||||
},
|
||||
"broker": {
|
||||
"id": "19e7320d3b1318079de372520e4596155ecb1e0e0c3c8e186b17701eee32d9b0",
|
||||
"started_at": "2026-11-02T14:00:51.091309254Z"
|
||||
},
|
||||
"api1": {
|
||||
"id": "cfb68d8c5736a1804823e8ef1f5f70c3a9fda18d1e60ee8b6770187ca3eab92c",
|
||||
"started_at": "2026-11-02T14:00:51.126274796Z"
|
||||
},
|
||||
"api2": {
|
||||
"id": "615dd320898997f574ae62ab70aa552558af0d397ecb9119d049a89adc15c7cc",
|
||||
"started_at": "2026-11-02T14:00:51.059763046Z"
|
||||
},
|
||||
"dispatcher": {
|
||||
"id": "a9fdb7fdc7894bfb5d2d9e84541d0e7c0ef7f90daf3f813e8cc1bc14f8d3a996",
|
||||
"started_at": "2026-11-02T14:00:54.751197006Z"
|
||||
},
|
||||
"worker": {
|
||||
"id": "e15232a06d7ba3d9651f8e06a4970f019bcf17b320fcbd75808b0a8a5f552e50",
|
||||
"started_at": "2026-11-02T14:02:12.627305167Z"
|
||||
},
|
||||
"cleanup": {
|
||||
"id": "57020b4bfd31c96021c84fc1bd45f0d707a86a9893e407d3e54be347a8fa2585",
|
||||
"started_at": "2026-11-02T14:00:34.065026385Z"
|
||||
},
|
||||
"nginx": {
|
||||
"id": "c6d8af4081cad86a95d921fcfc0e2551f08ac6dd3121f82eb373d6e06243a744",
|
||||
"started_at": "2026-11-02T13:57:51.356519046Z"
|
||||
}
|
||||
},
|
||||
"after": {
|
||||
"postgres": {
|
||||
"id": "52dc31d0bffdf348ef3e53c9d2828152997e4c75416af7b19fe837c4102b683a",
|
||||
"started_at": "2026-11-02T14:02:16.780510585Z"
|
||||
},
|
||||
"redis": {
|
||||
"id": "ade5ef52cec5df789f052224c1b54a3cdb087fc4aa8193e496252052558076f1",
|
||||
"started_at": "2026-11-02T14:02:16.852362044Z"
|
||||
},
|
||||
"s3": {
|
||||
"id": "380e323fa3e1c06dca1f45faa9cc747b0a10779dd83865c764b64bd033f19af5",
|
||||
"started_at": "2026-11-02T14:02:16.759127877Z"
|
||||
},
|
||||
"broker": {
|
||||
"id": "19e7320d3b1318079de372520e4596155ecb1e0e0c3c8e186b17701eee32d9b0",
|
||||
"started_at": "2026-11-02T14:02:16.819876127Z"
|
||||
},
|
||||
"api1": {
|
||||
"id": "cfb68d8c5736a1804823e8ef1f5f70c3a9fda18d1e60ee8b6770187ca3eab92c",
|
||||
"started_at": "2026-11-02T14:02:20.787831545Z"
|
||||
},
|
||||
"api2": {
|
||||
"id": "615dd320898997f574ae62ab70aa552558af0d397ecb9119d049a89adc15c7cc",
|
||||
"started_at": "2026-11-02T14:02:20.762466754Z"
|
||||
},
|
||||
"dispatcher": {
|
||||
"id": "a9fdb7fdc7894bfb5d2d9e84541d0e7c0ef7f90daf3f813e8cc1bc14f8d3a996",
|
||||
"started_at": "2026-11-02T14:02:20.621504045Z"
|
||||
},
|
||||
"worker": {
|
||||
"id": "e15232a06d7ba3d9651f8e06a4970f019bcf17b320fcbd75808b0a8a5f552e50",
|
||||
"started_at": "2026-11-02T14:02:21.417775004Z"
|
||||
},
|
||||
"cleanup": {
|
||||
"id": "57020b4bfd31c96021c84fc1bd45f0d707a86a9893e407d3e54be347a8fa2585",
|
||||
"started_at": "2026-11-02T14:02:20.651516837Z"
|
||||
},
|
||||
"nginx": {
|
||||
"id": "c6d8af4081cad86a95d921fcfc0e2551f08ac6dd3121f82eb373d6e06243a744",
|
||||
"started_at": "2026-11-02T14:02:27.213374173Z"
|
||||
}
|
||||
}
|
||||
},
|
||||
"failover": [
|
||||
{
|
||||
"status": "PASS",
|
||||
"stopped_service": "api1",
|
||||
"confirmed_messages": 68,
|
||||
"transient_failures": 0,
|
||||
"first_survivor_seconds": 0.838,
|
||||
"original_preserved": true
|
||||
},
|
||||
{
|
||||
"status": "PASS",
|
||||
"stopped_service": "api2",
|
||||
"confirmed_messages": 67,
|
||||
"transient_failures": 1,
|
||||
"first_survivor_seconds": 0.845,
|
||||
"original_preserved": true
|
||||
}
|
||||
],
|
||||
"public_failover": {
|
||||
"status": "PASS",
|
||||
"output": "python3 tests/resilience.py --mode failover --action-script \"scripts/stop-one.sh\" --base-url \"http://localhost:18085\" \nExecuting explicit failover hook; hook output suppressed to avoid leaking secrets.\n{\"observed_instances_before\": 2, \"successful_writes\": 132, \"failed_attempts\": 0, \"first_recovery_seconds\": 0.835}\nPASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence."
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
{
|
||||
"orientation": {
|
||||
"status": "PASS",
|
||||
"variants": {
|
||||
"thumbnail": {
|
||||
"content_type": "image/jpeg",
|
||||
"height": 256,
|
||||
"name": "thumbnail",
|
||||
"sha256": "335b4953e981a975642374e12196a4701e353972f223c1a7e850ac5998588f1a",
|
||||
"size_bytes": 1270,
|
||||
"width": 160
|
||||
},
|
||||
"crop": {
|
||||
"content_type": "image/jpeg",
|
||||
"height": 128,
|
||||
"name": "crop",
|
||||
"sha256": "b7f4884df301163117ebb3189f6141a9e376cc6d94804e2f56ce38e0dc3dd047",
|
||||
"size_bytes": 886,
|
||||
"width": 128
|
||||
},
|
||||
"optimized": {
|
||||
"content_type": "image/webp",
|
||||
"height": 320,
|
||||
"name": "optimized",
|
||||
"sha256": "577722d04127a774de08f674717fa03e3058124395f9ee31a949e6f508e86b43",
|
||||
"size_bytes": 210,
|
||||
"width": 200
|
||||
}
|
||||
}
|
||||
},
|
||||
"alpha": {
|
||||
"status": "PASS",
|
||||
"variants": {
|
||||
"thumbnail": {
|
||||
"content_type": "image/jpeg",
|
||||
"height": 4,
|
||||
"name": "thumbnail",
|
||||
"sha256": "a89b0af8a3c520ca079ed01f7a6579b39fdf0a593d5ff94b7a5128c194f3d065",
|
||||
"size_bytes": 631,
|
||||
"width": 8
|
||||
},
|
||||
"crop": {
|
||||
"content_type": "image/jpeg",
|
||||
"height": 128,
|
||||
"name": "crop",
|
||||
"sha256": "257edb1205654cf43e411c9fc333ee5544c2dc8753470ab667cdafbfeae4ff7c",
|
||||
"size_bytes": 883,
|
||||
"width": 128
|
||||
},
|
||||
"optimized": {
|
||||
"content_type": "image/webp",
|
||||
"height": 4,
|
||||
"name": "optimized",
|
||||
"sha256": "f4e61263caab4e749c64fc56091797191901d5affdb15ee2d828d905c429e950",
|
||||
"size_bytes": 44,
|
||||
"width": 8
|
||||
}
|
||||
}
|
||||
},
|
||||
"large": {
|
||||
"status": "PASS",
|
||||
"variants": {
|
||||
"thumbnail": {
|
||||
"content_type": "image/jpeg",
|
||||
"height": 128,
|
||||
"name": "thumbnail",
|
||||
"sha256": "126971107bc487792a7b813c01e037c58140ab0a039d4c7fd0cdbd26a9d9d781",
|
||||
"size_bytes": 1142,
|
||||
"width": 256
|
||||
},
|
||||
"crop": {
|
||||
"content_type": "image/jpeg",
|
||||
"height": 128,
|
||||
"name": "crop",
|
||||
"sha256": "4e67b8bf92db7d696303a433822a79479ca6d8ac5ca6bf2c87e5f4514ed3fa4a",
|
||||
"size_bytes": 886,
|
||||
"width": 128
|
||||
},
|
||||
"optimized": {
|
||||
"content_type": "image/webp",
|
||||
"height": 640,
|
||||
"name": "optimized",
|
||||
"sha256": "8e5a8340d7a7b9c37e935e090bb34494e588b7534a6d091aba33aca5ba83a267",
|
||||
"size_bytes": 1540,
|
||||
"width": 1280
|
||||
}
|
||||
}
|
||||
},
|
||||
"boundary": {
|
||||
"status": "PASS",
|
||||
"variants": {
|
||||
"thumbnail": {
|
||||
"content_type": "image/jpeg",
|
||||
"height": 160,
|
||||
"name": "thumbnail",
|
||||
"sha256": "d8d8358f529fd170984ee7926c54e9f6ead7d6a397fd6b803a6c20af197a2ce2",
|
||||
"size_bytes": 1270,
|
||||
"width": 256
|
||||
},
|
||||
"crop": {
|
||||
"content_type": "image/jpeg",
|
||||
"height": 128,
|
||||
"name": "crop",
|
||||
"sha256": "4e67b8bf92db7d696303a433822a79479ca6d8ac5ca6bf2c87e5f4514ed3fa4a",
|
||||
"size_bytes": 886,
|
||||
"width": 128
|
||||
},
|
||||
"optimized": {
|
||||
"content_type": "image/webp",
|
||||
"height": 800,
|
||||
"name": "optimized",
|
||||
"sha256": "a9bc2a93f44b92bf58d3c1a2f53390b21f0330b073411886eeccac3b12eb6769",
|
||||
"size_bytes": 1904,
|
||||
"width": 1280
|
||||
}
|
||||
}
|
||||
},
|
||||
"byte_limits_corrupt_pixel_limits": "PASS",
|
||||
"private_bucket_limited_credentials": "PASS",
|
||||
"worker_memory_bytes": {
|
||||
"memory.current": 17575936,
|
||||
"memory.max": 1610612736,
|
||||
"memory.peak": 701313024
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
module mtusi-messenger
|
||||
|
||||
go 1.26.0
|
||||
|
||||
require (
|
||||
github.com/jackc/pgx/v5 v5.7.6
|
||||
github.com/minio/madmin-go/v3 v3.0.95
|
||||
github.com/minio/minio-go/v7 v7.0.95
|
||||
github.com/redis/go-redis/v9 v9.14.0
|
||||
golang.org/x/crypto v0.42.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/go-ini/ini v1.67.0 // indirect
|
||||
github.com/go-ole/go-ole v1.3.0 // indirect
|
||||
github.com/goccy/go-json v0.10.5 // indirect
|
||||
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
|
||||
github.com/golang/protobuf v1.5.4 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||
github.com/klauspost/compress v1.18.0 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.11 // indirect
|
||||
github.com/lufia/plan9stats v0.0.0-20240909124753-873cd0166683 // indirect
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
|
||||
github.com/minio/crc64nvme v1.0.2 // indirect
|
||||
github.com/minio/md5-simd v1.1.2 // indirect
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
||||
github.com/philhofer/fwd v1.2.0 // indirect
|
||||
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
|
||||
github.com/prometheus/client_model v0.6.1 // indirect
|
||||
github.com/prometheus/common v0.59.1 // indirect
|
||||
github.com/prometheus/procfs v0.15.1 // indirect
|
||||
github.com/prometheus/prom2json v1.4.0 // indirect
|
||||
github.com/prometheus/prometheus v0.54.1 // indirect
|
||||
github.com/rs/xid v1.6.0 // indirect
|
||||
github.com/safchain/ethtool v0.4.1 // indirect
|
||||
github.com/secure-io/sio-go v0.3.1 // indirect
|
||||
github.com/shirou/gopsutil/v3 v3.24.5 // indirect
|
||||
github.com/shoenig/go-m1cpu v0.1.6 // indirect
|
||||
github.com/tinylib/msgp v1.3.0 // indirect
|
||||
github.com/tklauser/go-sysconf v0.3.14 // indirect
|
||||
github.com/tklauser/numcpus v0.8.0 // indirect
|
||||
github.com/yusufpapurcu/wmi v1.2.4 // indirect
|
||||
golang.org/x/net v0.43.0 // indirect
|
||||
golang.org/x/sync v0.17.0 // indirect
|
||||
golang.org/x/sys v0.36.0 // indirect
|
||||
golang.org/x/text v0.29.0 // indirect
|
||||
google.golang.org/protobuf v1.34.2 // indirect
|
||||
)
|
||||
@@ -0,0 +1,128 @@
|
||||
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
||||
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
|
||||
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
|
||||
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
|
||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
|
||||
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
|
||||
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/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/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
|
||||
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
|
||||
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
||||
github.com/jackc/pgx/v5 v5.7.6 h1:rWQc5FwZSPX58r1OQmkuaNicxdmExaEz5A2DO2hUuTk=
|
||||
github.com/jackc/pgx/v5 v5.7.6/go.mod h1:aruU7o91Tc2q2cFp5h4uP3f6ztExVpyVv88Xl/8Vl8M=
|
||||
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
|
||||
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
|
||||
github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
|
||||
github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
github.com/klauspost/cpuid/v2 v2.2.11 h1:0OwqZRYI2rFrjS4kvkDnqJkKHdHaRnCm68/DY4OxRzU=
|
||||
github.com/klauspost/cpuid/v2 v2.2.11/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
|
||||
github.com/lufia/plan9stats v0.0.0-20240909124753-873cd0166683 h1:7UMa6KCCMjZEMDtTVdcGu0B1GmmC7QJKiCCjyTAWQy0=
|
||||
github.com/lufia/plan9stats v0.0.0-20240909124753-873cd0166683/go.mod h1:ilwx/Dta8jXAgpFYFvSWEMwxmbWXyiUHkd5FwyKhb5k=
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
|
||||
github.com/minio/crc64nvme v1.0.2 h1:6uO1UxGAD+kwqWWp7mBFsi5gAse66C4NXO8cmcVculg=
|
||||
github.com/minio/crc64nvme v1.0.2/go.mod h1:eVfm2fAzLlxMdUGc0EEBGSMmPwmXD5XiNRpnu9J3bvg=
|
||||
github.com/minio/madmin-go/v3 v3.0.95 h1:KRy4NIpTLKrHcuTQbKrwKs3SvGoe+ZAHT2Pbl1zJxDc=
|
||||
github.com/minio/madmin-go/v3 v3.0.95/go.mod h1:pMLdj9OtN0CANNs5tdm6opvOlDFfj0WhbztboZAjRWE=
|
||||
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.0.95 h1:ywOUPg+PebTMTzn9VDsoFJy32ZuARN9zhB+K3IYEvYU=
|
||||
github.com/minio/minio-go/v7 v7.0.95/go.mod h1:wOOX3uxS334vImCNRVyIDdXX9OsXDm89ToynKgqUKlo=
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
||||
github.com/philhofer/fwd v1.2.0 h1:e6DnBTl7vGY+Gz322/ASL4Gyp1FspeMvx1RNDoToZuM=
|
||||
github.com/philhofer/fwd v1.2.0/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 h1:o4JXh1EVt9k/+g42oCprj/FisM4qX9L3sZB3upGN2ZU=
|
||||
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
|
||||
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
|
||||
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
|
||||
github.com/prometheus/common v0.59.1 h1:LXb1quJHWm1P6wq/U824uxYi4Sg0oGvNeUm1z5dJoX0=
|
||||
github.com/prometheus/common v0.59.1/go.mod h1:GpWM7dewqmVYcd7SmRaiWVe9SSqjf0UrwnYnpEZNuT0=
|
||||
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
|
||||
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
|
||||
github.com/prometheus/prom2json v1.4.0 h1:2AEOsd1ebqql/p9u0IWgCpUAteAAf9Lnf/SVyieqer4=
|
||||
github.com/prometheus/prom2json v1.4.0/go.mod h1:DmcIMPspQD/fMyFCYti5qJJbuEnqDh3DGoooO0sgr4w=
|
||||
github.com/prometheus/prometheus v0.54.1 h1:vKuwQNjnYN2/mDoWfHXDhAsz/68q/dQDb+YbcEqU7MQ=
|
||||
github.com/prometheus/prometheus v0.54.1/go.mod h1:xlLByHhk2g3ycakQGrMaU8K7OySZx98BzeCR99991NY=
|
||||
github.com/redis/go-redis/v9 v9.14.0 h1:u4tNCjXOyzfgeLN+vAZaW1xUooqWDqVEsZN0U01jfAE=
|
||||
github.com/redis/go-redis/v9 v9.14.0/go.mod h1:huWgSWd8mW6+m0VPhJjSSQ+d6Nh1VICQ6Q5lHuCH/Iw=
|
||||
github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
|
||||
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
|
||||
github.com/safchain/ethtool v0.4.1 h1:S6mEleTADqgynileXoiapt/nKnatyR6bmIHoF+h2ADo=
|
||||
github.com/safchain/ethtool v0.4.1/go.mod h1:XLLnZmy4OCRTkksP/UiMjij96YmIsBfmBQcs7H6tA48=
|
||||
github.com/secure-io/sio-go v0.3.1 h1:dNvY9awjabXTYGsTF1PiCySl9Ltofk9GA3VdWlo7rRc=
|
||||
github.com/secure-io/sio-go v0.3.1/go.mod h1:+xbkjDzPjwh4Axd07pRKSNriS9SCiYksWnZqdnfpQxs=
|
||||
github.com/shirou/gopsutil/v3 v3.24.5 h1:i0t8kL+kQTvpAYToeuiVk3TgDeKOFioZO3Ztz/iZ9pI=
|
||||
github.com/shirou/gopsutil/v3 v3.24.5/go.mod h1:bsoOS1aStSs9ErQ1WWfxllSeS1K5D+U30r2NfcubMVk=
|
||||
github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM=
|
||||
github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ=
|
||||
github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU=
|
||||
github.com/shoenig/test v0.6.4/go.mod h1:byHiCGXqrVaflBLAMq/srcZIHynQPQgeyvkvXnjqq0k=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/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.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/tinylib/msgp v1.3.0 h1:ULuf7GPooDaIlbyvgAxBV/FI7ynli6LZ1/nVUNu+0ww=
|
||||
github.com/tinylib/msgp v1.3.0/go.mod h1:ykjzy2wzgrlvpDCRc4LA8UXy6D8bzMSuAF3WD57Gok0=
|
||||
github.com/tklauser/go-sysconf v0.3.14 h1:g5vzr9iPFFz24v2KZXs/pvpvh8/V9Fw6vQK5ZZb78yU=
|
||||
github.com/tklauser/go-sysconf v0.3.14/go.mod h1:1ym4lWMLUOhuBOPGtRcJm7tEGX4SCYNEEEtghGG/8uY=
|
||||
github.com/tklauser/numcpus v0.8.0 h1:Mx4Wwe/FjZLeQsK/6kt2EOepwwSl7SmJrK5bV/dXYgY=
|
||||
github.com/tklauser/numcpus v0.8.0/go.mod h1:ZJZlAY+dmR4eut8epnzf0u/VwodKmryxR8txiloSqBE=
|
||||
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
|
||||
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.42.0 h1:chiH31gIWm57EkTXpwnqf8qeuMUi0yekh6mT2AvFlqI=
|
||||
golang.org/x/crypto v0.42.0/go.mod h1:4+rDnOTJhQCx2q7/j6rAN5XDw8kPjeaXEUR2eL94ix8=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.43.0 h1:lat02VYK2j4aLzMzecihNvTlJNQUq316m2Mr9rnM6YE=
|
||||
golang.org/x/net v0.43.0/go.mod h1:vhO1fvI4dGsIjh73sWfUVjj3N7CA9WkKJNQm2svM6Jg=
|
||||
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
|
||||
golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k=
|
||||
golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.29.0 h1:1neNs90w9YzJ9BocxfsQNHKuAT4pkghyXc4nhZ6sJvk=
|
||||
golang.org/x/text v0.29.0/go.mod h1:7MhJOA9CD2qZyOKYazxdYMF85OwPdEr9jTtBpO7ydH4=
|
||||
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
|
||||
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
@@ -0,0 +1,16 @@
|
||||
# Build MinIO from a pinned commit of the official upstream source.
|
||||
FROM golang:1.26.8-alpine3.23@sha256:a8fa79c5bd40d880b52bd3b6d7669ecdcfd00e85facdd427d279efb5ddd79cb1 AS build
|
||||
ADD --checksum=sha256:be6d0bd3696c3a13a35f02d3a0280b64319c67918b4501c5c3d87f96d000085c https://codeload.github.com/minio/minio/tar.gz/refs/tags/RELEASE.2025-10-15T17-29-55Z /source.tar.gz
|
||||
WORKDIR /src
|
||||
RUN tar -xzf /source.tar.gz --strip-components=1 -C /src
|
||||
RUN --mount=type=cache,target=/go/pkg/mod --mount=type=cache,target=/root/.cache/go-build \
|
||||
CGO_ENABLED=0 GOMAXPROCS=2 go build -p 2 -trimpath -buildvcs=false \
|
||||
-ldflags='-s -w -X github.com/minio/minio/cmd.Version=2025-10-15T17:29:55Z -X github.com/minio/minio/cmd.ReleaseTag=RELEASE.2025-10-15T17-29-55Z -X github.com/minio/minio/cmd.CommitID=9e49d5e7a648f00e26f2246f4dc28e6b07f8c84a -X github.com/minio/minio/cmd.CopyrightYear=2025' \
|
||||
-o /minio .
|
||||
|
||||
FROM busybox:1.37.0-musl
|
||||
COPY --from=build /minio /minio
|
||||
COPY --from=build /src/LICENSE /licenses/MinIO-LICENSE
|
||||
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
|
||||
RUN mkdir -p /data
|
||||
ENTRYPOINT ["/minio"]
|
||||
@@ -0,0 +1,8 @@
|
||||
FROM application AS app_binary
|
||||
FROM python:3.13.7-slim-bookworm
|
||||
RUN pip install --no-cache-dir Pillow==12.0.0
|
||||
COPY --from=app_binary /messenger /messenger
|
||||
COPY worker/process_image.py /opt/process_image.py
|
||||
USER 65532:65532
|
||||
ENTRYPOINT ["/messenger"]
|
||||
CMD ["worker"]
|
||||
@@ -0,0 +1,43 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
upstream=/run/nginx-upstream.conf
|
||||
|
||||
write_servers() {
|
||||
if [ "$1" = 1 ]; then
|
||||
printf '%s\n' 'server api1:8080 resolve max_fails=0;'
|
||||
fi
|
||||
if [ "$2" = 1 ]; then
|
||||
printf '%s\n' 'server api2:8080 resolve max_fails=0;'
|
||||
fi
|
||||
}
|
||||
|
||||
# With no healthy replica, keep both reachable so API can return its normal
|
||||
# 401/503 JSON responses instead of Nginx reporting "no live upstreams".
|
||||
write_servers 1 1 > "$upstream"
|
||||
|
||||
monitor() {
|
||||
sleep 2
|
||||
while :; do
|
||||
ready1=0
|
||||
ready2=0
|
||||
wget -q -T 2 -O /dev/null http://api1:8080/health/ready && ready1=1 || true
|
||||
wget -q -T 2 -O /dev/null http://api2:8080/health/ready && ready2=1 || true
|
||||
if [ "$ready1" = 0 ] && [ "$ready2" = 0 ]; then
|
||||
ready1=1
|
||||
ready2=1
|
||||
fi
|
||||
next=$(mktemp /run/nginx-upstream.XXXXXX)
|
||||
write_servers "$ready1" "$ready2" > "$next"
|
||||
if cmp -s "$next" "$upstream"; then
|
||||
rm -f "$next"
|
||||
else
|
||||
mv "$next" "$upstream"
|
||||
nginx -t >/dev/null 2>&1 && nginx -s reload
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
}
|
||||
|
||||
monitor &
|
||||
exec nginx -g 'daemon off;'
|
||||
@@ -0,0 +1,35 @@
|
||||
events {}
|
||||
|
||||
http {
|
||||
access_log /dev/stdout;
|
||||
error_log /dev/stderr notice;
|
||||
|
||||
resolver 127.0.0.11 valid=2s ipv6=off;
|
||||
|
||||
upstream messenger_api {
|
||||
zone messenger_api 64k;
|
||||
include /run/nginx-upstream.conf;
|
||||
keepalive 16;
|
||||
}
|
||||
|
||||
server {
|
||||
listen 8080;
|
||||
client_max_body_size 0;
|
||||
client_body_timeout 20s;
|
||||
|
||||
location / {
|
||||
proxy_request_buffering off;
|
||||
proxy_buffering off;
|
||||
proxy_pass http://messenger_api;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Connection "";
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_connect_timeout 500ms;
|
||||
proxy_send_timeout 65s;
|
||||
proxy_read_timeout 65s;
|
||||
proxy_next_upstream error timeout http_502 http_503 http_504;
|
||||
proxy_next_upstream_tries 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"embed"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
//go:embed migrations/*.sql
|
||||
var migrations embed.FS
|
||||
|
||||
func Migrate(ctx context.Context, pool *pgxpool.Pool) error {
|
||||
tx, err := pool.BeginTx(ctx, pgx.TxOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
if _, err = tx.Exec(ctx, `SELECT pg_advisory_xact_lock(202602)`); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `CREATE TABLE IF NOT EXISTS schema_migrations (version integer PRIMARY KEY, applied_at timestamptz NOT NULL DEFAULT clock_timestamp())`); err != nil {
|
||||
return err
|
||||
}
|
||||
entries, err := fs.ReadDir(migrations, "migrations")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sort.Slice(entries, func(i, j int) bool { return entries[i].Name() < entries[j].Name() })
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".sql") {
|
||||
continue
|
||||
}
|
||||
part := strings.SplitN(entry.Name(), "_", 2)[0]
|
||||
version, parseErr := strconv.Atoi(part)
|
||||
if parseErr != nil {
|
||||
return fmt.Errorf("invalid migration name %s", entry.Name())
|
||||
}
|
||||
var applied bool
|
||||
if err = tx.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM schema_migrations WHERE version=$1)`, version).Scan(&applied); err != nil {
|
||||
return err
|
||||
}
|
||||
if applied {
|
||||
continue
|
||||
}
|
||||
sqlBytes, readErr := migrations.ReadFile("migrations/" + entry.Name())
|
||||
if readErr != nil {
|
||||
return readErr
|
||||
}
|
||||
if _, err = tx.Exec(ctx, string(sqlBytes)); err != nil {
|
||||
return fmt.Errorf("migration %s: %w", entry.Name(), err)
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `INSERT INTO schema_migrations(version) VALUES($1)`, version); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
CREATE TABLE users (
|
||||
id uuid PRIMARY KEY,
|
||||
username text NOT NULL UNIQUE CHECK (username ~ '^[a-z0-9_]{3,32}$'),
|
||||
display_name text NOT NULL CHECK (char_length(btrim(display_name)) > 0 AND char_length(display_name) <= 100),
|
||||
password_hash text NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
|
||||
CREATE TABLE chats (
|
||||
id uuid PRIMARY KEY,
|
||||
title text NOT NULL CHECK (char_length(btrim(title)) > 0 AND char_length(title) <= 100),
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
|
||||
CREATE TABLE chat_members (
|
||||
chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
user_id uuid NOT NULL REFERENCES users(id) ON DELETE RESTRICT,
|
||||
PRIMARY KEY (chat_id, user_id)
|
||||
);
|
||||
|
||||
CREATE INDEX chat_members_user_chat_idx ON chat_members (user_id, chat_id);
|
||||
|
||||
CREATE TABLE messages (
|
||||
id uuid PRIMARY KEY,
|
||||
chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
sender_id uuid NOT NULL REFERENCES users(id) ON DELETE RESTRICT,
|
||||
text text NOT NULL CHECK (char_length(btrim(text)) > 0 AND char_length(text) <= 4000),
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
|
||||
CREATE INDEX messages_chat_history_idx ON messages (chat_id, created_at, id);
|
||||
@@ -0,0 +1,13 @@
|
||||
CREATE TABLE message_idempotency (
|
||||
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
method text NOT NULL,
|
||||
path text NOT NULL,
|
||||
idempotency_key text NOT NULL,
|
||||
request_hash bytea NOT NULL,
|
||||
message_id uuid REFERENCES messages(id) ON DELETE CASCADE,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
PRIMARY KEY (user_id, method, path, idempotency_key)
|
||||
);
|
||||
|
||||
CREATE INDEX message_idempotency_created_at_idx
|
||||
ON message_idempotency (created_at);
|
||||
@@ -0,0 +1,30 @@
|
||||
CREATE TABLE attachments (
|
||||
id uuid PRIMARY KEY,
|
||||
chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
owner_id uuid NOT NULL REFERENCES users(id),
|
||||
filename text NOT NULL,
|
||||
object_key text NOT NULL UNIQUE,
|
||||
content_type text NOT NULL CHECK (content_type IN ('image/png','image/jpeg')),
|
||||
size_bytes bigint NOT NULL CHECK (size_bytes BETWEEN 1 AND 10485760),
|
||||
sha256 text NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'),
|
||||
status text NOT NULL DEFAULT 'ready' CHECK (status='ready'),
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
updated_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
CREATE INDEX attachments_chat_idx ON attachments(chat_id,created_at,id);
|
||||
|
||||
-- Durable intent is committed BEFORE any S3 PUT. A crashed upload remains
|
||||
-- visible to the cleanup process even if its ready metadata never committed.
|
||||
CREATE TABLE attachment_uploads (
|
||||
id uuid PRIMARY KEY,
|
||||
object_key text NOT NULL UNIQUE,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
CREATE INDEX attachment_uploads_created_idx ON attachment_uploads(created_at);
|
||||
|
||||
ALTER TABLE messages DROP CONSTRAINT messages_text_check;
|
||||
ALTER TABLE messages ADD CONSTRAINT messages_text_check CHECK (char_length(text)<=4000);
|
||||
ALTER TABLE messages ADD COLUMN attachment_ids uuid[] NOT NULL DEFAULT '{}';
|
||||
ALTER TABLE messages ADD CONSTRAINT messages_body_check
|
||||
CHECK (char_length(btrim(text))>0 OR cardinality(attachment_ids)>0);
|
||||
ALTER TABLE messages ADD CONSTRAINT messages_attachments_limit CHECK (cardinality(attachment_ids)<=10);
|
||||
@@ -0,0 +1,37 @@
|
||||
ALTER TABLE attachments DROP CONSTRAINT attachments_status_check;
|
||||
ALTER TABLE attachments ALTER COLUMN status SET DEFAULT 'queued';
|
||||
ALTER TABLE attachments ADD CONSTRAINT attachments_status_check CHECK (status IN ('queued','processing','ready','failed'));
|
||||
ALTER TABLE attachments ADD COLUMN error_code text;
|
||||
ALTER TABLE attachments ADD COLUMN variants jsonb NOT NULL DEFAULT '[]';
|
||||
|
||||
CREATE TABLE attachment_jobs (
|
||||
id uuid PRIMARY KEY,
|
||||
attachment_id uuid NOT NULL UNIQUE REFERENCES attachments(id) ON DELETE CASCADE,
|
||||
pipeline_version integer NOT NULL DEFAULT 1 CHECK (pipeline_version=1),
|
||||
staging_key text NOT NULL UNIQUE,
|
||||
state text NOT NULL DEFAULT 'queued' CHECK (state IN ('queued','processing','ready','failed')),
|
||||
attempts integer NOT NULL DEFAULT 0,
|
||||
max_attempts integer NOT NULL DEFAULT 5 CHECK (max_attempts BETWEEN 1 AND 20),
|
||||
next_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
lease_until timestamptz,
|
||||
lease_token uuid,
|
||||
error_code text,
|
||||
staging_deleted_at timestamptz,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
updated_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
CREATE INDEX attachment_jobs_due_idx ON attachment_jobs(state,next_at);
|
||||
-- Same transaction as attachment + job; repeated publication is intentional.
|
||||
CREATE TABLE attachment_outbox (
|
||||
job_id uuid PRIMARY KEY REFERENCES attachment_jobs(id) ON DELETE CASCADE,
|
||||
published_at timestamptz,
|
||||
publications integer NOT NULL DEFAULT 0
|
||||
);
|
||||
CREATE TABLE attachment_replays (
|
||||
id bigserial PRIMARY KEY,
|
||||
job_id uuid NOT NULL REFERENCES attachment_jobs(id),
|
||||
previous_error text NOT NULL,
|
||||
replayed_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
-- Internal CLI-only, one-shot crash injection. No HTTP endpoint exposes it.
|
||||
CREATE TABLE pipeline_faults (point text PRIMARY KEY, armed boolean NOT NULL DEFAULT true, hit_at timestamptz);
|
||||
@@ -0,0 +1,261 @@
|
||||
package messenger
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"image"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"io"
|
||||
"mime"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
const MaxUploadBytes int64 = 10 * 1024 * 1024
|
||||
const MaxImagePixels int64 = 40000000
|
||||
|
||||
type Attachment struct {
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
OwnerID string `json:"owner_id"`
|
||||
Filename string `json:"filename"`
|
||||
ContentType string `json:"content_type"`
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
SHA256 string `json:"sha256"`
|
||||
Status string `json:"status"`
|
||||
Variants []any `json:"variants"`
|
||||
ErrorCode *string `json:"error_code"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
|
||||
func tooLarge() *apiError {
|
||||
return &apiError{status: 413, code: "payload_too_large", message: "Превышен лимит изображения"}
|
||||
}
|
||||
func invalidImage() *apiError {
|
||||
return &apiError{status: 415, code: "unsupported_media_type", message: "Требуется корректный PNG или JPEG"}
|
||||
}
|
||||
|
||||
func rollbackUpload(tx pgx.Tx) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
_ = tx.Rollback(ctx)
|
||||
}
|
||||
|
||||
func validFilename(s string) bool {
|
||||
if !utf8.ValidString(s) || strings.TrimSpace(s) == "" || utf8.RuneCountInString(s) > 255 {
|
||||
return false
|
||||
}
|
||||
for _, c := range s {
|
||||
if c == '/' || c == '\\' || unicode.IsControl(c) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (a *API) isMember(r *http.Request, uid, chatID string) *apiError {
|
||||
ctx, cancel := a.dbContext(r.Context())
|
||||
defer cancel()
|
||||
var member bool
|
||||
if err := a.db.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM chat_members WHERE chat_id=$1 AND user_id=$2)`, chatID, uid).Scan(&member); err != nil {
|
||||
return unavailable()
|
||||
}
|
||||
if !member {
|
||||
return missing()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *API) uploadAttachment(r *http.Request, uid, chatID string) (response, *apiError) {
|
||||
if e := a.isMember(r, uid, chatID); e != nil {
|
||||
return response{}, e
|
||||
}
|
||||
query, err := url.ParseQuery(r.URL.RawQuery)
|
||||
if err != nil || len(query) != 1 || len(query["filename"]) != 1 || !validFilename(query.Get("filename")) {
|
||||
return response{}, bad("Неверное имя файла")
|
||||
}
|
||||
kind, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
||||
if err != nil || (kind != "image/png" && kind != "image/jpeg") {
|
||||
return response{}, invalidImage()
|
||||
}
|
||||
if r.ContentLength > MaxUploadBytes {
|
||||
return response{}, tooLarge()
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 45*time.Second)
|
||||
defer cancel()
|
||||
// One decoder/temporary original per replica bounds RAM and tmpfs usage.
|
||||
select {
|
||||
case a.uploadSlots <- struct{}{}:
|
||||
defer func() { <-a.uploadSlots }()
|
||||
case <-ctx.Done():
|
||||
return response{}, unavailable()
|
||||
}
|
||||
file, err := os.CreateTemp("", "upload-*")
|
||||
if err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
defer os.Remove(file.Name())
|
||||
defer file.Close()
|
||||
hash := sha256.New()
|
||||
size, err := io.CopyBuffer(io.MultiWriter(file, hash), io.LimitReader(r.Body, MaxUploadBytes+1), make([]byte, 32*1024))
|
||||
if size > MaxUploadBytes {
|
||||
return response{}, tooLarge()
|
||||
}
|
||||
if err != nil {
|
||||
return response{}, bad("Загрузка прервана")
|
||||
}
|
||||
if size == 0 {
|
||||
return response{}, invalidImage()
|
||||
}
|
||||
file.Seek(0, io.SeekStart)
|
||||
config, format, err := image.DecodeConfig(file)
|
||||
if err != nil || (format != "png" && format != "jpeg") || "image/"+format != kind {
|
||||
return response{}, invalidImage()
|
||||
}
|
||||
if config.Width < 1 || config.Height < 1 || int64(config.Width) > MaxImagePixels/int64(config.Height) {
|
||||
return response{}, tooLarge()
|
||||
}
|
||||
file.Seek(0, io.SeekStart)
|
||||
// Decode after the dimension guard to reject corrupt payloads, not just
|
||||
// apparently valid headers. The decoded bitmap is never persisted.
|
||||
if e := validateImage(file); e != nil {
|
||||
return response{}, e
|
||||
}
|
||||
file.Seek(0, io.SeekStart)
|
||||
id := newID()
|
||||
key := "staging/" + id
|
||||
dbctx, dbcancel := a.dbContext(ctx)
|
||||
_, err = a.db.Exec(dbctx, `INSERT INTO attachment_uploads(id,object_key) VALUES($1,$2)`, id, key)
|
||||
dbcancel()
|
||||
if err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
tx, err := a.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
defer rollbackUpload(tx)
|
||||
// Cleanup and upload use the same lock. Cleanup cannot delete an object
|
||||
// while its upload/metadata transaction is in progress.
|
||||
if _, err = tx.Exec(ctx, `SELECT pg_advisory_xact_lock(hashtextextended($1,4))`, key); err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
var pending bool
|
||||
if err = tx.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM attachment_uploads WHERE id=$1)`, id).Scan(&pending); err != nil || !pending {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
putctx, putcancel := context.WithTimeout(ctx, 20*time.Second)
|
||||
err = a.objects.Put(putctx, key, file, size, kind)
|
||||
putcancel()
|
||||
if err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
att := Attachment{ID: id, ChatID: chatID, OwnerID: uid, Filename: query.Get("filename"), ContentType: kind, SizeBytes: size, SHA256: hex.EncodeToString(hash.Sum(nil)), Status: "queued", Variants: []any{}}
|
||||
var created, updated time.Time
|
||||
if err = tx.QueryRow(ctx, `INSERT INTO attachments(id,chat_id,owner_id,filename,object_key,content_type,size_bytes,sha256,status) VALUES($1,$2,$3,$4,$5,$6,$7,$8,'queued') RETURNING created_at,updated_at`, id, chatID, uid, att.Filename, key, kind, size, att.SHA256).Scan(&created, &updated); err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `DELETE FROM attachment_uploads WHERE id=$1`, id); err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
|
||||
maxAttempts := 5
|
||||
if raw := os.Getenv("JOB_MAX_ATTEMPTS"); raw != "" {
|
||||
n, parseErr := strconv.Atoi(raw)
|
||||
if parseErr != nil || n < 1 || n > 20 {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
maxAttempts = n
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `INSERT INTO attachment_jobs(id,attachment_id,staging_key,max_attempts) VALUES($1,$2,$3,$4)`, id, id, key, maxAttempts); err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `INSERT INTO attachment_outbox(job_id) VALUES($1)`, id); err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
att.CreatedAt = formatTime(created)
|
||||
att.UpdatedAt = formatTime(updated)
|
||||
return response{status: 202, body: att}, nil
|
||||
}
|
||||
|
||||
func validateImage(r io.Reader) *apiError {
|
||||
_, _, err := image.Decode(r)
|
||||
if err != nil {
|
||||
return invalidImage()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *API) getAttachment(r *http.Request, uid, id string, content bool) (response, *apiError) {
|
||||
query, err := url.ParseQuery(r.URL.RawQuery)
|
||||
if err != nil {
|
||||
return response{}, bad("Неверные параметры вложения")
|
||||
}
|
||||
if (!content && len(query) > 0) || (content && (len(query) > 1 || (len(query) > 0 && len(query["variant"]) != 1))) {
|
||||
return response{}, bad("Неверные параметры вложения")
|
||||
}
|
||||
ctx, cancel := a.dbContext(r.Context())
|
||||
defer cancel()
|
||||
att := Attachment{Variants: []any{}}
|
||||
var key string
|
||||
var variantsJSON []byte
|
||||
var created, updated time.Time
|
||||
err = a.db.QueryRow(ctx, `SELECT a.id::text,a.chat_id::text,a.owner_id::text,a.filename,a.object_key,a.content_type,a.size_bytes,a.sha256,a.status,a.created_at,a.updated_at,a.variants,a.error_code FROM attachments a JOIN chat_members cm ON cm.chat_id=a.chat_id AND cm.user_id=$2 WHERE a.id=$1`, id, uid).Scan(&att.ID, &att.ChatID, &att.OwnerID, &att.Filename, &key, &att.ContentType, &att.SizeBytes, &att.SHA256, &att.Status, &created, &updated, &variantsJSON, &att.ErrorCode)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return response{}, missing()
|
||||
}
|
||||
if err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
_ = json.Unmarshal(variantsJSON, &att.Variants)
|
||||
att.CreatedAt = formatTime(created)
|
||||
att.UpdatedAt = formatTime(updated)
|
||||
if !content {
|
||||
return response{status: 200, body: att}, nil
|
||||
}
|
||||
|
||||
if att.Status != "ready" {
|
||||
return response{}, &apiError{status: 409, code: "conflict", message: "Вложение не готово"}
|
||||
}
|
||||
if variant, ok := query["variant"]; ok && variant[0] != "original" {
|
||||
found := false
|
||||
for _, item := range att.Variants {
|
||||
v, ok := item.(map[string]any)
|
||||
if ok && v["name"] == variant[0] {
|
||||
key = "variants/" + id + "/v1/" + variant[0]
|
||||
att.ContentType = v["content_type"].(string)
|
||||
att.SizeBytes = int64(v["size_bytes"].(float64))
|
||||
att.Filename = variant[0] + map[bool]string{true: ".webp", false: ".jpg"}[variant[0] == "optimized"]
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return response{}, missing()
|
||||
}
|
||||
}
|
||||
|
||||
downloadCtx, downloadCancel := context.WithTimeout(r.Context(), 45*time.Second)
|
||||
object, err := a.objects.Get(downloadCtx, key)
|
||||
if err != nil {
|
||||
downloadCancel()
|
||||
return response{}, unavailable()
|
||||
}
|
||||
return response{status: 200, content: object, contentType: att.ContentType, length: att.SizeBytes, filename: att.Filename, cancel: downloadCancel}, nil
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
package messenger
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"mtusi-messenger/internal/objectstore"
|
||||
)
|
||||
|
||||
type CleanupResult struct {
|
||||
RemovedObjects int `json:"removed_objects"`
|
||||
RemovedIntents int64 `json:"removed_intents"`
|
||||
KeptReady int `json:"kept_ready"`
|
||||
SkippedRecent int `json:"skipped_recent"`
|
||||
SkippedBusy int `json:"skipped_busy"`
|
||||
}
|
||||
|
||||
// Cleanup removes abandoned uploads. Accepted pipeline jobs protect staging.
|
||||
func Cleanup(ctx context.Context, db *pgxpool.Pool, store *objectstore.Store, grace time.Duration) (CleanupResult, error) {
|
||||
result := CleanupResult{}
|
||||
cutoff := time.Now().Add(-grace)
|
||||
clean := func(key string) error {
|
||||
if !strings.HasPrefix(key, "originals/") && !strings.HasPrefix(key, "staging/") {
|
||||
return nil
|
||||
}
|
||||
tx, err := db.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rollbackUpload(tx)
|
||||
var locked bool
|
||||
if err = tx.QueryRow(ctx, `SELECT pg_try_advisory_xact_lock(hashtextextended($1,4))`, key).Scan(&locked); err != nil {
|
||||
return err
|
||||
}
|
||||
if !locked {
|
||||
result.SkippedBusy++
|
||||
return nil
|
||||
}
|
||||
var ready, recent bool
|
||||
if err = tx.QueryRow(ctx, `SELECT (EXISTS(SELECT 1 FROM attachments WHERE object_key=$1) OR EXISTS(SELECT 1 FROM attachment_jobs WHERE staging_key=$1)),EXISTS(SELECT 1 FROM attachment_uploads WHERE object_key=$1 AND created_at>$2)`, key, cutoff).Scan(&ready, &recent); err != nil {
|
||||
return err
|
||||
}
|
||||
if ready {
|
||||
result.KeptReady++
|
||||
return nil
|
||||
}
|
||||
if recent {
|
||||
result.SkippedRecent++
|
||||
return nil
|
||||
}
|
||||
info, err := store.Client.StatObject(ctx, store.Bucket, key, minio.StatObjectOptions{})
|
||||
exists := err == nil
|
||||
if err != nil && minio.ToErrorResponse(err).Code != "NoSuchKey" {
|
||||
return err
|
||||
}
|
||||
if exists && info.LastModified.After(cutoff) {
|
||||
result.SkippedRecent++
|
||||
return nil
|
||||
}
|
||||
if exists {
|
||||
if err = store.Delete(ctx, key); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
command, err := tx.Exec(ctx, `DELETE FROM attachment_uploads WHERE object_key=$1`, key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
result.RemovedIntents += command.RowsAffected()
|
||||
if exists {
|
||||
result.RemovedObjects++
|
||||
}
|
||||
return nil
|
||||
}
|
||||
rows, err := db.Query(ctx, `SELECT object_key FROM attachment_uploads WHERE created_at<$1 ORDER BY created_at LIMIT 1000`, cutoff)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
keys := []string{}
|
||||
for rows.Next() {
|
||||
var key string
|
||||
if err = rows.Scan(&key); err != nil {
|
||||
rows.Close()
|
||||
return result, err
|
||||
}
|
||||
keys = append(keys, key)
|
||||
}
|
||||
err = rows.Err()
|
||||
rows.Close()
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
for _, key := range keys {
|
||||
if err = clean(key); err != nil {
|
||||
return result, err
|
||||
}
|
||||
}
|
||||
listctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
for object := range store.Client.ListObjects(listctx, store.Bucket, minio.ListObjectsOptions{Recursive: true}) {
|
||||
if object.Err != nil {
|
||||
return result, fmt.Errorf("S3 cleanup listing: %w", object.Err)
|
||||
}
|
||||
if object.LastModified.Before(cutoff) {
|
||||
if err = clean(object.Key); err != nil {
|
||||
return result, err
|
||||
}
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package messenger
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/subtle"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/crypto/scrypt"
|
||||
)
|
||||
|
||||
const scryptN, scryptR, scryptP = 32768, 8, 1
|
||||
|
||||
func hashPassword(password string) (string, error) {
|
||||
salt := make([]byte, 16)
|
||||
if _, err := rand.Read(salt); err != nil {
|
||||
return "", err
|
||||
}
|
||||
hash, err := scrypt.Key([]byte(password), salt, scryptN, scryptR, scryptP, 32)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
b64 := base64.RawStdEncoding
|
||||
return fmt.Sprintf("scrypt$%d$%d$%d$%s$%s", scryptN, scryptR, scryptP, b64.EncodeToString(salt), b64.EncodeToString(hash)), nil
|
||||
}
|
||||
|
||||
func verifyPassword(encoded, password string) bool {
|
||||
parts := strings.Split(encoded, "$")
|
||||
if len(parts) != 6 || parts[0] != "scrypt" {
|
||||
return false
|
||||
}
|
||||
n, e1 := strconv.Atoi(parts[1])
|
||||
r, e2 := strconv.Atoi(parts[2])
|
||||
p, e3 := strconv.Atoi(parts[3])
|
||||
salt, e4 := base64.RawStdEncoding.DecodeString(parts[4])
|
||||
expected, e5 := base64.RawStdEncoding.DecodeString(parts[5])
|
||||
if e1 != nil || e2 != nil || e3 != nil || e4 != nil || e5 != nil || len(expected) != 32 {
|
||||
return false
|
||||
}
|
||||
actual, err := scrypt.Key([]byte(password), salt, n, r, p, len(expected))
|
||||
return err == nil && subtle.ConstantTimeCompare(actual, expected) == 1
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package messenger
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPasswordHash(t *testing.T) {
|
||||
const password = "long-test-password"
|
||||
first, err := hashPassword(password)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second, err := hashPassword(password)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if first == password || strings.Contains(first, password) {
|
||||
t.Fatal("hash contains the original password")
|
||||
}
|
||||
if first == second {
|
||||
t.Fatal("independent hashes must use different salts")
|
||||
}
|
||||
if !verifyPassword(first, password) {
|
||||
t.Fatal("correct password was rejected")
|
||||
}
|
||||
if verifyPassword(first, "wrong-test-password") {
|
||||
t.Fatal("wrong password was accepted")
|
||||
}
|
||||
if verifyPassword("damaged-hash", password) {
|
||||
t.Fatal("malformed hash was accepted")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,754 @@
|
||||
package messenger
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"mime"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"mtusi-messenger/internal/objectstore"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID string `json:"id"`
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display_name"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
}
|
||||
type Chat struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
MemberIDs []string `json:"member_ids"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
}
|
||||
type Message struct {
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
SenderID string `json:"sender_id"`
|
||||
Text string `json:"text"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
AttachmentIDs []string `json:"attachment_ids"`
|
||||
}
|
||||
type Session struct {
|
||||
Token string `json:"token"`
|
||||
TokenType string `json:"token_type"`
|
||||
ExpiresAt string `json:"expires_at"`
|
||||
User User `json:"user"`
|
||||
}
|
||||
|
||||
type API struct {
|
||||
objects *objectstore.Store
|
||||
uploadSlots chan struct{}
|
||||
db *pgxpool.Pool
|
||||
sessions *redis.Client
|
||||
ttl time.Duration
|
||||
idempotencyTTL time.Duration
|
||||
dbTimeout time.Duration
|
||||
instanceID string
|
||||
logger *log.Logger
|
||||
}
|
||||
|
||||
func NewHandler(db *pgxpool.Pool, sessions *redis.Client, objects *objectstore.Store, ttl, idempotencyTTL, dbTimeout time.Duration, instanceID string, logger *log.Logger) http.Handler {
|
||||
return &API{db: db, sessions: sessions, objects: objects, uploadSlots: make(chan struct{}, 1), ttl: ttl, idempotencyTTL: idempotencyTTL, dbTimeout: dbTimeout, instanceID: instanceID, logger: logger}
|
||||
}
|
||||
|
||||
var uuidRE = regexp.MustCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`)
|
||||
var usernameRE = regexp.MustCompile(`^[a-z0-9_]{3,32}$`)
|
||||
var idempotencyKeyRE = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`)
|
||||
|
||||
type apiError struct {
|
||||
status int
|
||||
code, message, challenge string
|
||||
}
|
||||
type response struct {
|
||||
status int
|
||||
body any
|
||||
noBody bool
|
||||
content io.ReadCloser
|
||||
contentType string
|
||||
length int64
|
||||
filename string
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
func bad(msg string) *apiError { return &apiError{status: 400, code: "invalid_request", message: msg} }
|
||||
func missing() *apiError {
|
||||
return &apiError{status: 404, code: "not_found", message: "Объект не найден"}
|
||||
}
|
||||
func unavailable() *apiError {
|
||||
return &apiError{status: 503, code: "unavailable", message: "Сервис временно недоступен"}
|
||||
}
|
||||
func unauthorized(scheme string) *apiError {
|
||||
challenge := `Bearer realm="messenger"`
|
||||
if scheme == "Basic" {
|
||||
challenge = `Basic realm="messenger", charset="UTF-8"`
|
||||
}
|
||||
return &apiError{status: 401, code: "unauthorized", message: "Неверные учётные данные", challenge: challenge}
|
||||
}
|
||||
|
||||
func (a *API) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
id := newID()
|
||||
w.Header().Set("X-Request-Id", id)
|
||||
w.Header().Set("X-Instance-Id", a.instanceID)
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
start, status := time.Now(), 500
|
||||
defer func() {
|
||||
if x := recover(); x != nil {
|
||||
status = 500
|
||||
a.logger.Printf("request_id=%s panic=%T", id, x)
|
||||
writeError(w, id, &apiError{status: 500, code: "internal_error", message: "Внутренняя ошибка"})
|
||||
}
|
||||
a.logger.Printf("request_id=%s method=%s path=%s status=%d duration_ms=%d", id, r.Method, r.URL.Path, status, time.Since(start).Milliseconds())
|
||||
}()
|
||||
res, apiErr := a.route(r)
|
||||
if apiErr != nil {
|
||||
status = apiErr.status
|
||||
writeError(w, id, apiErr)
|
||||
return
|
||||
}
|
||||
status = res.status
|
||||
if res.content != nil {
|
||||
if res.cancel != nil {
|
||||
defer res.cancel()
|
||||
}
|
||||
defer res.content.Close()
|
||||
w.Header().Set("Content-Type", res.contentType)
|
||||
w.Header().Set("Content-Length", strconv.FormatInt(res.length, 10))
|
||||
w.Header().Set("Content-Disposition", mime.FormatMediaType("inline", map[string]string{"filename": res.filename}))
|
||||
w.Header().Set("Cache-Control", "private, no-store")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.WriteHeader(status)
|
||||
if _, err := io.Copy(w, res.content); err != nil {
|
||||
a.logger.Printf("request_id=%s download interrupted", id)
|
||||
}
|
||||
return
|
||||
}
|
||||
w.WriteHeader(status)
|
||||
if !res.noBody {
|
||||
_ = json.NewEncoder(w).Encode(res.body)
|
||||
}
|
||||
}
|
||||
|
||||
func writeError(w http.ResponseWriter, requestID string, e *apiError) {
|
||||
if e.challenge != "" {
|
||||
w.Header().Set("WWW-Authenticate", e.challenge)
|
||||
}
|
||||
w.WriteHeader(e.status)
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"error": map[string]string{"code": e.code, "message": e.message, "request_id": requestID}})
|
||||
}
|
||||
|
||||
func decode(r *http.Request, dst any) *apiError {
|
||||
if strings.ToLower(strings.TrimSpace(strings.Split(r.Header.Get("Content-Type"), ";")[0])) != "application/json" {
|
||||
return &apiError{status: 415, code: "unsupported_media_type", message: "Требуется application/json"}
|
||||
}
|
||||
if r.ContentLength > 1<<20 {
|
||||
return &apiError{status: 413, code: "payload_too_large", message: "Слишком большой запрос"}
|
||||
}
|
||||
data, err := io.ReadAll(io.LimitReader(r.Body, (1<<20)+1))
|
||||
if err != nil {
|
||||
return bad("Не удалось прочитать JSON")
|
||||
}
|
||||
if len(data) > 1<<20 {
|
||||
return &apiError{status: 413, code: "payload_too_large", message: "Слишком большой запрос"}
|
||||
}
|
||||
if !utf8.Valid(data) {
|
||||
return bad("JSON должен быть в UTF-8")
|
||||
}
|
||||
var fields map[string]json.RawMessage
|
||||
if json.Unmarshal(data, &fields) == nil {
|
||||
for _, value := range fields {
|
||||
if bytes.Equal(bytes.TrimSpace(value), []byte("null")) {
|
||||
return bad("Поля запроса не должны быть null")
|
||||
}
|
||||
}
|
||||
}
|
||||
d := json.NewDecoder(bytes.NewReader(data))
|
||||
d.DisallowUnknownFields()
|
||||
if err = d.Decode(dst); err != nil {
|
||||
return bad("Неверный JSON или поля")
|
||||
}
|
||||
var rest any
|
||||
if !errors.Is(d.Decode(&rest), io.EOF) {
|
||||
return bad("Ожидался один JSON-объект")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validText(s string, max int) bool {
|
||||
return utf8.ValidString(s) && strings.TrimSpace(s) != "" && utf8.RuneCountInString(s) <= max
|
||||
}
|
||||
func validPassword(s string) bool {
|
||||
n := utf8.RuneCountInString(s)
|
||||
return utf8.ValidString(s) && n >= 12 && n <= 128
|
||||
}
|
||||
func formatTime(t time.Time) string { return t.UTC().Format(time.RFC3339Nano) }
|
||||
func newID() string {
|
||||
var b [16]byte
|
||||
if _, err := rand.Read(b[:]); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
b[6] = (b[6] & 0x0f) | 0x40
|
||||
b[8] = (b[8] & 0x3f) | 0x80
|
||||
return fmt.Sprintf("%x-%x-%x-%x-%x", b[:4], b[4:6], b[6:8], b[8:10], b[10:])
|
||||
}
|
||||
func newToken() string {
|
||||
b := make([]byte, 32)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return base64.RawURLEncoding.EncodeToString(b)
|
||||
}
|
||||
func tokenKey(token string) string {
|
||||
sum := sha256.Sum256([]byte(token))
|
||||
return "session:" + hex.EncodeToString(sum[:])
|
||||
}
|
||||
|
||||
func cursorFor(resource string, created time.Time, id string) string {
|
||||
b, _ := json.Marshal([]string{resource, formatTime(created), id})
|
||||
return base64.RawURLEncoding.EncodeToString(b)
|
||||
}
|
||||
func parsePage(r *http.Request, resource string) (int, *time.Time, string, *apiError) {
|
||||
q, err := url.ParseQuery(r.URL.RawQuery)
|
||||
if err != nil {
|
||||
return 0, nil, "", bad("Неверные параметры страницы")
|
||||
}
|
||||
for k, v := range q {
|
||||
if (k != "limit" && k != "cursor") || len(v) != 1 {
|
||||
return 0, nil, "", bad("Неверные параметры страницы")
|
||||
}
|
||||
}
|
||||
limit := 50
|
||||
if raw := q.Get("limit"); raw != "" {
|
||||
n, e := strconv.Atoi(raw)
|
||||
if e != nil || n < 1 || n > 100 {
|
||||
return 0, nil, "", bad("limit должен быть от 1 до 100")
|
||||
}
|
||||
limit = n
|
||||
} else if _, ok := q["limit"]; ok {
|
||||
return 0, nil, "", bad("Пустой limit")
|
||||
}
|
||||
if _, ok := q["cursor"]; !ok {
|
||||
return limit, nil, "", nil
|
||||
}
|
||||
raw := q.Get("cursor")
|
||||
if raw == "" || len(raw) > 1024 {
|
||||
return 0, nil, "", bad("Неверный cursor")
|
||||
}
|
||||
b, e := base64.RawURLEncoding.DecodeString(raw)
|
||||
if e != nil {
|
||||
return 0, nil, "", bad("Неверный cursor")
|
||||
}
|
||||
var p []string
|
||||
if json.Unmarshal(b, &p) != nil || len(p) != 3 || p[0] != resource || !uuidRE.MatchString(p[2]) {
|
||||
return 0, nil, "", bad("Неверный cursor")
|
||||
}
|
||||
t, e := time.Parse(time.RFC3339Nano, p[1])
|
||||
if e != nil {
|
||||
return 0, nil, "", bad("Неверный cursor")
|
||||
}
|
||||
return limit, &t, p[2], nil
|
||||
}
|
||||
func (a *API) dbContext(parent context.Context) (context.Context, context.CancelFunc) {
|
||||
return context.WithTimeout(parent, a.dbTimeout)
|
||||
}
|
||||
|
||||
func (a *API) authenticate(r *http.Request) (string, string, *apiError) {
|
||||
h := r.Header.Get("Authorization")
|
||||
if !strings.HasPrefix(h, "Bearer ") || strings.TrimSpace(strings.TrimPrefix(h, "Bearer ")) == "" {
|
||||
return "", "", unauthorized("Bearer")
|
||||
}
|
||||
token := strings.TrimSpace(strings.TrimPrefix(h, "Bearer "))
|
||||
ctx, cancel := context.WithTimeout(r.Context(), a.dbTimeout)
|
||||
defer cancel()
|
||||
uid, err := a.sessions.Get(ctx, tokenKey(token)).Result()
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return "", "", unauthorized("Bearer")
|
||||
}
|
||||
if err != nil {
|
||||
a.logger.Printf("session store unavailable: %v", err)
|
||||
return "", "", unavailable()
|
||||
}
|
||||
if !uuidRE.MatchString(uid) {
|
||||
return "", "", unauthorized("Bearer")
|
||||
}
|
||||
return uid, token, nil
|
||||
}
|
||||
|
||||
func scanUser(row pgx.Row) (User, string, error) {
|
||||
var u User
|
||||
var created time.Time
|
||||
var password string
|
||||
err := row.Scan(&u.ID, &u.Username, &u.DisplayName, &created, &password)
|
||||
u.CreatedAt = formatTime(created)
|
||||
return u, password, err
|
||||
}
|
||||
|
||||
func (a *API) route(r *http.Request) (response, *apiError) {
|
||||
p := r.URL.Path
|
||||
if r.Method == "GET" && p == "/health/live" {
|
||||
return response{status: 200, body: map[string]string{"status": "ok"}}, nil
|
||||
}
|
||||
if r.Method == "GET" && p == "/health/ready" {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), time.Second)
|
||||
defer cancel()
|
||||
if a.db.Ping(ctx) != nil || a.sessions.Ping(ctx).Err() != nil || a.objects.Ready(ctx) != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
return response{status: 200, body: map[string]string{"status": "ok"}}, nil
|
||||
}
|
||||
if r.Method == "POST" && p == "/api/v1/users" {
|
||||
return a.createUser(r)
|
||||
}
|
||||
if r.Method == "POST" && p == "/api/v1/auth/sessions" {
|
||||
return a.createSession(r)
|
||||
}
|
||||
if !strings.HasPrefix(p, "/api/v1/") {
|
||||
return response{}, missing()
|
||||
}
|
||||
uid, token, authErr := a.authenticate(r)
|
||||
if authErr != nil {
|
||||
return response{}, authErr
|
||||
}
|
||||
if r.Method == "DELETE" && p == "/api/v1/auth/sessions/current" {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), a.dbTimeout)
|
||||
defer cancel()
|
||||
if err := a.sessions.Del(ctx, tokenKey(token)).Err(); err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
return response{status: 204, noBody: true}, nil
|
||||
}
|
||||
if r.Method == "GET" && p == "/api/v1/users/me" {
|
||||
return a.currentUser(r, uid)
|
||||
}
|
||||
if strings.HasPrefix(p, "/api/v1/attachments/") {
|
||||
parts := strings.Split(strings.TrimPrefix(p, "/api/v1/attachments/"), "/")
|
||||
if !uuidRE.MatchString(parts[0]) {
|
||||
return response{}, bad("Неверный attachment_id")
|
||||
}
|
||||
if r.Method == "GET" && (len(parts) == 1 || (len(parts) == 2 && parts[1] == "content")) {
|
||||
return a.getAttachment(r, uid, parts[0], len(parts) == 2)
|
||||
}
|
||||
return response{}, missing()
|
||||
}
|
||||
if p == "/api/v1/chats" {
|
||||
if r.Method == "POST" {
|
||||
return a.createChat(r, uid)
|
||||
}
|
||||
if r.Method == "GET" {
|
||||
return a.listChats(r, uid)
|
||||
}
|
||||
}
|
||||
if !strings.HasPrefix(p, "/api/v1/chats/") {
|
||||
return response{}, missing()
|
||||
}
|
||||
parts := strings.Split(strings.TrimPrefix(p, "/api/v1/chats/"), "/")
|
||||
if len(parts) < 1 || !uuidRE.MatchString(parts[0]) {
|
||||
return response{}, bad("Неверный chat_id")
|
||||
}
|
||||
chatID := parts[0]
|
||||
if len(parts) == 2 && parts[1] == "attachments" && r.Method == "POST" {
|
||||
return a.uploadAttachment(r, uid, chatID)
|
||||
}
|
||||
if len(parts) == 1 && r.Method == "GET" {
|
||||
return a.getChat(r, uid, chatID)
|
||||
}
|
||||
if len(parts) == 2 && parts[1] == "messages" {
|
||||
if r.Method == "POST" {
|
||||
return a.createMessage(r, uid, chatID)
|
||||
}
|
||||
if r.Method == "GET" {
|
||||
return a.listMessages(r, uid, chatID)
|
||||
}
|
||||
}
|
||||
return response{}, missing()
|
||||
}
|
||||
|
||||
func (a *API) createUser(r *http.Request) (response, *apiError) {
|
||||
var in struct {
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display_name"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
if e := decode(r, &in); e != nil {
|
||||
return response{}, e
|
||||
}
|
||||
if !usernameRE.MatchString(in.Username) || !validText(in.DisplayName, 100) || !validPassword(in.Password) {
|
||||
return response{}, bad("Неверные данные пользователя")
|
||||
}
|
||||
passwordHash, err := hashPassword(in.Password)
|
||||
if err != nil {
|
||||
return response{}, &apiError{status: 500, code: "internal_error", message: "Внутренняя ошибка"}
|
||||
}
|
||||
ctx, cancel := a.dbContext(r.Context())
|
||||
defer cancel()
|
||||
var u User
|
||||
var created time.Time
|
||||
err = a.db.QueryRow(ctx, `INSERT INTO users(id,username,display_name,password_hash) VALUES($1,$2,$3,$4) RETURNING id::text,username,display_name,created_at`, newID(), in.Username, in.DisplayName, passwordHash).Scan(&u.ID, &u.Username, &u.DisplayName, &created)
|
||||
if err != nil {
|
||||
var pgErr *pgconn.PgError
|
||||
if errors.As(err, &pgErr) && pgErr.Code == "23505" {
|
||||
return response{}, &apiError{status: 409, code: "conflict", message: "Имя пользователя занято"}
|
||||
}
|
||||
return response{}, unavailable()
|
||||
}
|
||||
u.CreatedAt = formatTime(created)
|
||||
return response{status: 201, body: u}, nil
|
||||
}
|
||||
|
||||
func (a *API) createSession(r *http.Request) (response, *apiError) {
|
||||
if r.ContentLength > 0 {
|
||||
return response{}, bad("Тело запроса не допускается")
|
||||
}
|
||||
h := r.Header.Get("Authorization")
|
||||
if !strings.HasPrefix(h, "Basic ") {
|
||||
return response{}, unauthorized("Basic")
|
||||
}
|
||||
raw, err := base64.StdEncoding.DecodeString(strings.TrimSpace(strings.TrimPrefix(h, "Basic ")))
|
||||
if err != nil {
|
||||
return response{}, unauthorized("Basic")
|
||||
}
|
||||
parts := strings.SplitN(string(raw), ":", 2)
|
||||
if len(parts) != 2 || !usernameRE.MatchString(parts[0]) {
|
||||
return response{}, unauthorized("Basic")
|
||||
}
|
||||
ctx, cancel := a.dbContext(r.Context())
|
||||
defer cancel()
|
||||
u, passwordHash, err := scanUser(a.db.QueryRow(ctx, `SELECT id::text,username,display_name,created_at,password_hash FROM users WHERE username=$1`, parts[0]))
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return response{}, unauthorized("Basic")
|
||||
}
|
||||
if err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
if !verifyPassword(passwordHash, parts[1]) {
|
||||
return response{}, unauthorized("Basic")
|
||||
}
|
||||
token := newToken()
|
||||
expires := time.Now().UTC().Add(a.ttl)
|
||||
if err = a.sessions.Set(ctx, tokenKey(token), u.ID, a.ttl).Err(); err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
return response{status: 201, body: Session{Token: token, TokenType: "Bearer", ExpiresAt: formatTime(expires), User: u}}, nil
|
||||
}
|
||||
|
||||
func (a *API) currentUser(r *http.Request, uid string) (response, *apiError) {
|
||||
ctx, cancel := a.dbContext(r.Context())
|
||||
defer cancel()
|
||||
u, _, err := scanUser(a.db.QueryRow(ctx, `SELECT id::text,username,display_name,created_at,password_hash FROM users WHERE id=$1`, uid))
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return response{}, unauthorized("Bearer")
|
||||
}
|
||||
if err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
return response{status: 200, body: u}, nil
|
||||
}
|
||||
|
||||
func (a *API) createChat(r *http.Request, uid string) (response, *apiError) {
|
||||
var in struct {
|
||||
Title string `json:"title"`
|
||||
MemberIDs []string `json:"member_ids"`
|
||||
}
|
||||
if e := decode(r, &in); e != nil {
|
||||
return response{}, e
|
||||
}
|
||||
if !validText(in.Title, 100) || len(in.MemberIDs) < 1 || len(in.MemberIDs) > 100 {
|
||||
return response{}, bad("Неверные данные чата")
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, id := range in.MemberIDs {
|
||||
if !uuidRE.MatchString(id) || seen[id] {
|
||||
return response{}, bad("Неверные участники чата")
|
||||
}
|
||||
seen[id] = true
|
||||
}
|
||||
if !seen[uid] {
|
||||
return response{}, bad("Создатель должен быть участником")
|
||||
}
|
||||
ctx, cancel := a.dbContext(r.Context())
|
||||
defer cancel()
|
||||
tx, err := a.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
c := Chat{ID: newID(), Title: in.Title, MemberIDs: append([]string{}, in.MemberIDs...)}
|
||||
var created time.Time
|
||||
if err = tx.QueryRow(ctx, `INSERT INTO chats(id,title) VALUES($1,$2) RETURNING created_at`, c.ID, c.Title).Scan(&created); err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
for _, id := range c.MemberIDs {
|
||||
if _, err = tx.Exec(ctx, `INSERT INTO chat_members(chat_id,user_id) VALUES($1,$2)`, c.ID, id); err != nil {
|
||||
var pgErr *pgconn.PgError
|
||||
if errors.As(err, &pgErr) && pgErr.Code == "23503" {
|
||||
return response{}, bad("Неверные участники чата")
|
||||
}
|
||||
return response{}, unavailable()
|
||||
}
|
||||
}
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
c.CreatedAt = formatTime(created)
|
||||
return response{status: 201, body: c}, nil
|
||||
}
|
||||
|
||||
func scanChat(row pgx.Row) (Chat, time.Time, error) {
|
||||
var c Chat
|
||||
var t time.Time
|
||||
err := row.Scan(&c.ID, &c.Title, &c.MemberIDs, &t)
|
||||
c.CreatedAt = formatTime(t)
|
||||
return c, t, err
|
||||
}
|
||||
func chatSelect() string {
|
||||
return `SELECT c.id::text,c.title,ARRAY(SELECT cm2.user_id::text FROM chat_members cm2 WHERE cm2.chat_id=c.id ORDER BY cm2.user_id),c.created_at FROM chats c`
|
||||
}
|
||||
|
||||
func (a *API) getChat(r *http.Request, uid, chatID string) (response, *apiError) {
|
||||
ctx, cancel := a.dbContext(r.Context())
|
||||
defer cancel()
|
||||
c, _, err := scanChat(a.db.QueryRow(ctx, chatSelect()+` WHERE c.id=$1 AND EXISTS(SELECT 1 FROM chat_members cm WHERE cm.chat_id=c.id AND cm.user_id=$2)`, chatID, uid))
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return response{}, missing()
|
||||
}
|
||||
if err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
return response{status: 200, body: c}, nil
|
||||
}
|
||||
|
||||
func (a *API) listChats(r *http.Request, uid string) (response, *apiError) {
|
||||
limit, afterTime, afterID, e := parsePage(r, "chats:"+uid)
|
||||
if e != nil {
|
||||
return response{}, e
|
||||
}
|
||||
ctx, cancel := a.dbContext(r.Context())
|
||||
defer cancel()
|
||||
if afterTime != nil {
|
||||
var ok bool
|
||||
err := a.db.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM chats c JOIN chat_members cm ON cm.chat_id=c.id WHERE cm.user_id=$1 AND c.id=$2 AND c.created_at=$3)`, uid, afterID, *afterTime).Scan(&ok)
|
||||
if err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
if !ok {
|
||||
return response{}, bad("Неверный cursor")
|
||||
}
|
||||
}
|
||||
var rows pgx.Rows
|
||||
var err error
|
||||
base := chatSelect() + ` JOIN chat_members mine ON mine.chat_id=c.id WHERE mine.user_id=$1`
|
||||
if afterTime == nil {
|
||||
rows, err = a.db.Query(ctx, base+` ORDER BY c.created_at,c.id LIMIT $2`, uid, limit+1)
|
||||
} else {
|
||||
rows, err = a.db.Query(ctx, base+` AND (c.created_at,c.id)>($2,$3) ORDER BY c.created_at,c.id LIMIT $4`, uid, *afterTime, afterID, limit+1)
|
||||
}
|
||||
if err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
defer rows.Close()
|
||||
items := make([]Chat, 0, limit+1)
|
||||
times := make([]time.Time, 0, limit+1)
|
||||
for rows.Next() {
|
||||
c, t, scanErr := scanChat(rows)
|
||||
if scanErr != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
items = append(items, c)
|
||||
times = append(times, t)
|
||||
}
|
||||
if rows.Err() != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
var next any
|
||||
if len(items) > limit {
|
||||
last := items[limit-1]
|
||||
next = cursorFor("chats:"+uid, times[limit-1], last.ID)
|
||||
items = items[:limit]
|
||||
}
|
||||
return response{status: 200, body: map[string]any{"items": items, "next_cursor": next}}, nil
|
||||
}
|
||||
|
||||
func (a *API) createMessage(r *http.Request, uid, chatID string) (response, *apiError) {
|
||||
key := r.Header.Get("Idempotency-Key")
|
||||
if !idempotencyKeyRE.MatchString(key) {
|
||||
return response{}, bad("Требуется корректный Idempotency-Key")
|
||||
}
|
||||
var in struct {
|
||||
Text *string `json:"text"`
|
||||
AttachmentIDs []string `json:"attachment_ids"`
|
||||
}
|
||||
if e := decode(r, &in); e != nil {
|
||||
return response{}, e
|
||||
}
|
||||
if in.Text == nil {
|
||||
return response{}, bad("Требуется поле text")
|
||||
}
|
||||
text := *in.Text
|
||||
if !utf8.ValidString(text) || utf8.RuneCountInString(text) > 4000 || len(in.AttachmentIDs) > 10 || (strings.TrimSpace(text) == "" && len(in.AttachmentIDs) == 0) {
|
||||
return response{}, bad("Текст сообщения должен содержать от 1 до 4000 символов")
|
||||
}
|
||||
if in.AttachmentIDs == nil {
|
||||
in.AttachmentIDs = []string{}
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for i, id := range in.AttachmentIDs {
|
||||
id = strings.ToLower(id)
|
||||
if !uuidRE.MatchString(id) || seen[id] {
|
||||
return response{}, bad("Неверные attachment_ids")
|
||||
}
|
||||
seen[id] = true
|
||||
in.AttachmentIDs[i] = id
|
||||
}
|
||||
ctx, cancel := a.dbContext(r.Context())
|
||||
defer cancel()
|
||||
tx, err := a.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
var member bool
|
||||
if err = tx.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM chat_members WHERE chat_id=$1 AND user_id=$2)`, chatID, uid).Scan(&member); err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
if !member {
|
||||
return response{}, missing()
|
||||
}
|
||||
requestHash := sha256.Sum256([]byte(text))
|
||||
if len(in.AttachmentIDs) > 0 {
|
||||
canonical, _ := json.Marshal(in)
|
||||
requestHash = sha256.Sum256(canonical)
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `DELETE FROM message_idempotency WHERE user_id=$1 AND method=$2 AND path=$3 AND idempotency_key=$4 AND created_at < clock_timestamp() - ($5 * interval '1 second')`, uid, r.Method, r.URL.Path, key, int64(a.idempotencyTTL/time.Second)); err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
result, err := tx.Exec(ctx, `INSERT INTO message_idempotency(user_id,method,path,idempotency_key,request_hash) VALUES($1,$2,$3,$4,$5) ON CONFLICT DO NOTHING`, uid, r.Method, r.URL.Path, key, requestHash[:])
|
||||
if err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
inserted := result.RowsAffected() == 1
|
||||
if !inserted {
|
||||
var storedHash []byte
|
||||
var messageID string
|
||||
if err = tx.QueryRow(ctx, `SELECT request_hash,message_id::text FROM message_idempotency WHERE user_id=$1 AND method=$2 AND path=$3 AND idempotency_key=$4 FOR UPDATE`, uid, r.Method, r.URL.Path, key).Scan(&storedHash, &messageID); err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
if !bytes.Equal(storedHash, requestHash[:]) {
|
||||
return response{}, &apiError{status: 409, code: "conflict", message: "Idempotency-Key уже использован с другим запросом"}
|
||||
}
|
||||
var m Message
|
||||
var created time.Time
|
||||
if err = tx.QueryRow(ctx, `SELECT id::text,chat_id::text,sender_id::text,text,created_at,attachment_ids::text[] FROM messages WHERE id=$1`, messageID).Scan(&m.ID, &m.ChatID, &m.SenderID, &m.Text, &created, &m.AttachmentIDs); err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
m.CreatedAt = formatTime(created)
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
return response{status: 201, body: m}, nil
|
||||
}
|
||||
for _, id := range in.AttachmentIDs {
|
||||
var state string
|
||||
err = tx.QueryRow(ctx, `SELECT status FROM attachments WHERE id=$1 AND chat_id=$2`, id, chatID).Scan(&state)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return response{}, missing()
|
||||
}
|
||||
if err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
if state == "failed" {
|
||||
return response{}, &apiError{status: 409, code: "conflict", message: "Вложение не готово"}
|
||||
}
|
||||
}
|
||||
m := Message{ID: newID(), ChatID: chatID, SenderID: uid, Text: text, AttachmentIDs: in.AttachmentIDs}
|
||||
var created time.Time
|
||||
if err = tx.QueryRow(ctx, `INSERT INTO messages(id,chat_id,sender_id,text,attachment_ids) VALUES($1,$2,$3,$4,$5) RETURNING created_at`, m.ID, m.ChatID, m.SenderID, m.Text, m.AttachmentIDs).Scan(&created); err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `UPDATE message_idempotency SET message_id=$1 WHERE user_id=$2 AND method=$3 AND path=$4 AND idempotency_key=$5`, m.ID, uid, r.Method, r.URL.Path, key); err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
m.CreatedAt = formatTime(created)
|
||||
return response{status: 201, body: m}, nil
|
||||
}
|
||||
|
||||
func (a *API) listMessages(r *http.Request, uid, chatID string) (response, *apiError) {
|
||||
limit, afterTime, afterID, e := parsePage(r, "messages:"+chatID)
|
||||
if e != nil {
|
||||
return response{}, e
|
||||
}
|
||||
ctx, cancel := a.dbContext(r.Context())
|
||||
defer cancel()
|
||||
var member bool
|
||||
if err := a.db.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM chat_members WHERE chat_id=$1 AND user_id=$2)`, chatID, uid).Scan(&member); err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
if !member {
|
||||
return response{}, missing()
|
||||
}
|
||||
if afterTime != nil {
|
||||
var ok bool
|
||||
err := a.db.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM messages WHERE chat_id=$1 AND id=$2 AND created_at=$3)`, chatID, afterID, *afterTime).Scan(&ok)
|
||||
if err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
if !ok {
|
||||
return response{}, bad("Неверный cursor")
|
||||
}
|
||||
}
|
||||
var rows pgx.Rows
|
||||
var err error
|
||||
base := `SELECT id::text,chat_id::text,sender_id::text,text,created_at,attachment_ids::text[] FROM messages WHERE chat_id=$1`
|
||||
if afterTime == nil {
|
||||
rows, err = a.db.Query(ctx, base+` ORDER BY created_at,id LIMIT $2`, chatID, limit+1)
|
||||
} else {
|
||||
rows, err = a.db.Query(ctx, base+` AND (created_at,id)>($2,$3) ORDER BY created_at,id LIMIT $4`, chatID, *afterTime, afterID, limit+1)
|
||||
}
|
||||
if err != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
defer rows.Close()
|
||||
items := make([]Message, 0, limit+1)
|
||||
times := make([]time.Time, 0, limit+1)
|
||||
for rows.Next() {
|
||||
var m Message
|
||||
var t time.Time
|
||||
if rows.Scan(&m.ID, &m.ChatID, &m.SenderID, &m.Text, &t, &m.AttachmentIDs) != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
m.CreatedAt = formatTime(t)
|
||||
items = append(items, m)
|
||||
times = append(times, t)
|
||||
}
|
||||
if rows.Err() != nil {
|
||||
return response{}, unavailable()
|
||||
}
|
||||
var next any
|
||||
if len(items) > limit {
|
||||
last := items[limit-1]
|
||||
next = cursorFor("messages:"+chatID, times[limit-1], last.ID)
|
||||
items = items[:limit]
|
||||
}
|
||||
return response{status: 200, body: map[string]any{"items": items, "next_cursor": next}}, nil
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package objectstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/url"
|
||||
|
||||
madmin "github.com/minio/madmin-go/v3"
|
||||
)
|
||||
|
||||
// Bootstrap receives root credentials only in the one-shot s3-init process.
|
||||
// The API and cleaner receive a separate user limited to this bucket/prefix.
|
||||
func Bootstrap(ctx context.Context, endpoint, bucket, rootAccess, rootSecret, apiAccess, apiSecret string) error {
|
||||
u, err := url.Parse(endpoint)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
admin, err := madmin.New(u.Host, rootAccess, rootSecret, u.Scheme == "https")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = admin.AddUser(ctx, apiAccess, apiSecret); err != nil {
|
||||
return err
|
||||
}
|
||||
policy := map[string]any{
|
||||
"Version": "2012-10-17",
|
||||
"Statement": []any{
|
||||
map[string]any{"Effect": "Allow", "Action": []string{"s3:GetBucketLocation", "s3:ListBucket"}, "Resource": []string{"arn:aws:s3:::" + bucket}},
|
||||
map[string]any{"Effect": "Allow", "Action": []string{"s3:GetObject", "s3:PutObject", "s3:DeleteObject"}, "Resource": []string{"arn:aws:s3:::" + bucket + "/originals/*", "arn:aws:s3:::" + bucket + "/staging/*", "arn:aws:s3:::" + bucket + "/variants/*"}},
|
||||
},
|
||||
}
|
||||
encoded, err := json.Marshal(policy)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = admin.AddCannedPolicy(ctx, "messenger-api", encoded); err != nil {
|
||||
return err
|
||||
}
|
||||
return admin.SetPolicy(ctx, "messenger-api", apiAccess, false)
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package objectstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
)
|
||||
|
||||
type Store struct {
|
||||
Client *minio.Client
|
||||
Bucket string
|
||||
endpoint string
|
||||
}
|
||||
|
||||
func New(endpoint, region, bucket, access, secret string) (*Store, error) {
|
||||
u, err := url.Parse(endpoint)
|
||||
if err != nil || u.Host == "" || (u.Scheme != "http" && u.Scheme != "https") || u.Path != "" {
|
||||
return nil, fmt.Errorf("invalid S3 endpoint")
|
||||
}
|
||||
transport := &http.Transport{
|
||||
DialContext: (&net.Dialer{Timeout: 2 * time.Second, KeepAlive: 30 * time.Second}).DialContext,
|
||||
MaxIdleConns: 16, MaxIdleConnsPerHost: 8, MaxConnsPerHost: 8,
|
||||
ResponseHeaderTimeout: 10 * time.Second, IdleConnTimeout: 30 * time.Second,
|
||||
TLSHandshakeTimeout: 3 * time.Second,
|
||||
}
|
||||
client, err := minio.New(u.Host, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(access, secret, ""), Secure: u.Scheme == "https",
|
||||
Region: region, Transport: transport, MaxRetries: 1,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Store{Client: client, Bucket: bucket, endpoint: endpoint}, nil
|
||||
}
|
||||
|
||||
func (s *Store) Ready(ctx context.Context) error {
|
||||
ok, err := s.Client.BucketExists(ctx, s.Bucket)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !ok {
|
||||
return fmt.Errorf("S3 bucket missing")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) Put(ctx context.Context, key string, r io.Reader, size int64, kind string) error {
|
||||
_, err := s.Client.PutObject(ctx, s.Bucket, key, r, size, minio.PutObjectOptions{
|
||||
ContentType: kind, DisableMultipart: true,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) Get(ctx context.Context, key string) (*minio.Object, error) {
|
||||
object, err := s.Client.GetObject(ctx, s.Bucket, key, minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err = object.Stat(); err != nil {
|
||||
object.Close()
|
||||
return nil, err
|
||||
}
|
||||
return object, nil
|
||||
}
|
||||
|
||||
func (s *Store) Delete(ctx context.Context, key string) error {
|
||||
return s.Client.RemoveObject(ctx, s.Bucket, key, minio.RemoveObjectOptions{})
|
||||
}
|
||||
|
||||
// Audit performs an actual unauthenticated GET and an out-of-scope operation
|
||||
// with API credentials. Neither check prints credentials or object URLs.
|
||||
func (s *Store) Audit(ctx context.Context, key string) error {
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", s.endpoint+"/"+s.Bucket+"/"+key, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
response, err := (&http.Client{Timeout: 5 * time.Second}).Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
response.Body.Close()
|
||||
if response.StatusCode != 403 {
|
||||
return fmt.Errorf("anonymous GET returned %d", response.StatusCode)
|
||||
}
|
||||
err = s.Client.MakeBucket(ctx, "forbidden-audit-bucket", minio.MakeBucketOptions{})
|
||||
if minio.ToErrorResponse(err).Code != "AccessDenied" {
|
||||
return fmt.Errorf("API credentials are not restricted: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,499 @@
|
||||
// Package pipeline implements durable intent, at-least-once delivery and fenced effects.
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"math/rand/v2"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"mtusi-messenger/internal/objectstore"
|
||||
)
|
||||
|
||||
const Stream = "attachment.jobs.v1"
|
||||
const Group = "image-workers"
|
||||
|
||||
type Pipeline struct {
|
||||
DB *pgxpool.Pool
|
||||
Broker *redis.Client
|
||||
Objects *objectstore.Store
|
||||
Timeout time.Duration
|
||||
Retention time.Duration
|
||||
Logger *log.Logger
|
||||
}
|
||||
type Job struct {
|
||||
SchemaVersion int `json:"schema_version"`
|
||||
ID string `json:"job_id"`
|
||||
Type string `json:"type"`
|
||||
AttachmentID string `json:"attachment_id"`
|
||||
Version int `json:"pipeline_version"`
|
||||
Attempt int `json:"attempt"`
|
||||
Created string `json:"created_at"`
|
||||
}
|
||||
type Variant struct {
|
||||
Name string `json:"name"`
|
||||
ContentType string `json:"content_type"`
|
||||
Size int64 `json:"size_bytes"`
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
SHA string `json:"sha256"`
|
||||
}
|
||||
|
||||
func (p *Pipeline) Checkpoint(ctx context.Context, point string) {
|
||||
var armed bool
|
||||
err := p.DB.QueryRow(ctx, `UPDATE pipeline_faults SET armed=false,hit_at=clock_timestamp() WHERE point=$1 AND armed RETURNING true`, point).Scan(&armed)
|
||||
if err == nil && armed {
|
||||
p.Logger.Printf("injected crash: %s", point)
|
||||
os.Exit(86)
|
||||
}
|
||||
}
|
||||
func pause(ctx context.Context, d time.Duration) {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
case <-time.After(d):
|
||||
}
|
||||
}
|
||||
|
||||
// Dispatch re-publishes unfinished intents if a broker confirmation/connection is lost.
|
||||
// Publication is deliberately outside the PostgreSQL transaction; duplicates are safe.
|
||||
func (p *Pipeline) Dispatch(ctx context.Context) {
|
||||
for ctx.Err() == nil {
|
||||
if err := p.dispatchOnce(ctx); err != nil {
|
||||
p.Logger.Print("dispatcher unavailable; intent retained")
|
||||
}
|
||||
sweepctx, cancelSweep := context.WithTimeout(ctx, 5*time.Second)
|
||||
if err := p.Sweep(sweepctx); err != nil {
|
||||
p.Logger.Print("staging sweep deferred")
|
||||
}
|
||||
cancelSweep()
|
||||
pause(ctx, time.Second)
|
||||
}
|
||||
}
|
||||
func (p *Pipeline) dispatchOnce(ctx context.Context) error {
|
||||
rows, err := p.DB.Query(ctx, `SELECT j.id::text,j.attachment_id::text,j.pipeline_version,j.attempts+1,j.created_at
|
||||
FROM attachment_jobs j JOIN attachment_outbox o ON o.job_id=j.id
|
||||
WHERE ((j.state='queued' AND j.next_at<=clock_timestamp()) OR (j.state='processing' AND j.lease_until<clock_timestamp()))
|
||||
AND (o.published_at IS NULL OR o.published_at<clock_timestamp()-interval '5 seconds') ORDER BY j.created_at LIMIT 50`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
jobs := []Job{}
|
||||
for rows.Next() {
|
||||
j := Job{SchemaVersion: 1, Type: "attachment.process.v1"}
|
||||
var created time.Time
|
||||
if err = rows.Scan(&j.ID, &j.AttachmentID, &j.Version, &j.Attempt, &created); err != nil {
|
||||
rows.Close()
|
||||
return err
|
||||
}
|
||||
j.Created = created.UTC().Format(time.RFC3339Nano)
|
||||
jobs = append(jobs, j)
|
||||
}
|
||||
err = rows.Err()
|
||||
rows.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, j := range jobs {
|
||||
p.Checkpoint(ctx, "dispatcher_before_publish")
|
||||
b, _ := json.Marshal(j)
|
||||
if err = p.Broker.XAdd(ctx, &redis.XAddArgs{Stream: Stream, Values: map[string]any{"job": string(b)}}).Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
p.Checkpoint(ctx, "dispatcher_after_publish")
|
||||
if _, err = p.DB.Exec(ctx, `UPDATE attachment_outbox SET published_at=clock_timestamp(),publications=publications+1 WHERE job_id=$1`, j.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (p *Pipeline) ack(ctx context.Context, id string) {
|
||||
// Stream belongs to one group. Delete only after acknowledged; PG remains recovery authority.
|
||||
if p.Broker.XAck(ctx, Stream, Group, id).Err() == nil {
|
||||
_ = p.Broker.XDel(ctx, Stream, id).Err()
|
||||
}
|
||||
}
|
||||
func (p *Pipeline) Worker(ctx context.Context, concurrency int) error {
|
||||
if concurrency < 1 || concurrency > 4 {
|
||||
return fmt.Errorf("WORKER_CONCURRENCY must be 1..4")
|
||||
}
|
||||
// One message at a time per consumer: prefetch=1.
|
||||
done := make(chan struct{}, concurrency)
|
||||
for i := 0; i < concurrency; i++ {
|
||||
go func() { defer func() { done <- struct{}{} }(); p.consume(ctx, uuid.NewString()) }()
|
||||
}
|
||||
for i := 0; i < concurrency; i++ {
|
||||
<-done
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (p *Pipeline) consume(ctx context.Context, consumer string) {
|
||||
reclaimIdle := p.Timeout + 10*time.Second
|
||||
for ctx.Err() == nil {
|
||||
_ = p.Broker.XGroupCreateMkStream(ctx, Stream, Group, "0").Err()
|
||||
pending, _, err := p.Broker.XAutoClaim(ctx, &redis.XAutoClaimArgs{Stream: Stream, Group: Group, Consumer: consumer, MinIdle: reclaimIdle, Start: "0-0", Count: 1}).Result()
|
||||
if err == nil && len(pending) > 0 {
|
||||
p.handle(ctx, pending[0])
|
||||
continue
|
||||
}
|
||||
messages, err := p.Broker.XReadGroup(ctx, &redis.XReadGroupArgs{Group: Group, Consumer: consumer, Streams: []string{Stream, ">"}, Count: 1, Block: time.Second}).Result()
|
||||
if err != nil {
|
||||
pause(ctx, 200*time.Millisecond)
|
||||
continue
|
||||
}
|
||||
for _, stream := range messages {
|
||||
for _, m := range stream.Messages {
|
||||
p.handle(ctx, m)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
func (p *Pipeline) handle(ctx context.Context, m redis.XMessage) {
|
||||
raw, ok := m.Values["job"].(string)
|
||||
var j Job
|
||||
if !ok || json.Unmarshal([]byte(raw), &j) != nil || j.SchemaVersion != 1 || j.Version != 1 || j.Type != "attachment.process.v1" || uuid.Validate(j.ID) != nil || uuid.Validate(j.AttachmentID) != nil {
|
||||
// Malformed envelopes go to a diagnostic dead-letter stream, never interpreted as storage paths.
|
||||
if p.Broker.XAdd(ctx, &redis.XAddArgs{Stream: "attachment.invalid.dlq", Values: map[string]any{"stream_id": m.ID, "error_code": "invalid_envelope"}}).Err() == nil {
|
||||
p.ack(ctx, m.ID)
|
||||
}
|
||||
return
|
||||
}
|
||||
jobctx, cancel := context.WithTimeout(ctx, p.Timeout)
|
||||
defer cancel()
|
||||
conn, err := p.DB.Acquire(jobctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer conn.Release()
|
||||
var locked bool
|
||||
if err = conn.QueryRow(jobctx, `SELECT pg_try_advisory_lock(hashtextextended($1,5))`, j.ID).Scan(&locked); err != nil || !locked {
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
unlockctx, done := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer done()
|
||||
_, e := conn.Exec(unlockctx, `SELECT pg_advisory_unlock(hashtextextended($1,5))`, j.ID)
|
||||
if e != nil {
|
||||
_ = conn.Conn().Close(unlockctx)
|
||||
}
|
||||
}()
|
||||
tx, err := conn.Begin(jobctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer tx.Rollback(context.Background())
|
||||
var state, staging, attachment string
|
||||
var attempt, maxAttempts int
|
||||
var due bool
|
||||
err = tx.QueryRow(jobctx, `SELECT state,staging_key,attachment_id::text,attempts,max_attempts,
|
||||
(state='queued' AND next_at<=clock_timestamp()) OR (state='processing' AND lease_until<clock_timestamp())
|
||||
FROM attachment_jobs WHERE id=$1 FOR UPDATE`, j.ID).Scan(&state, &staging, &attachment, &attempt, &maxAttempts, &due)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
p.ack(ctx, m.ID)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if attachment != j.AttachmentID {
|
||||
return
|
||||
}
|
||||
if state == "ready" || state == "failed" {
|
||||
_ = tx.Rollback(jobctx)
|
||||
p.ack(ctx, m.ID)
|
||||
return
|
||||
}
|
||||
if !due {
|
||||
_ = tx.Rollback(jobctx)
|
||||
p.ack(ctx, m.ID)
|
||||
return
|
||||
}
|
||||
if attempt >= maxAttempts {
|
||||
if p.failTx(jobctx, tx, j.ID, attachment, "retry_exhausted") == nil && tx.Commit(jobctx) == nil {
|
||||
p.ack(ctx, m.ID)
|
||||
}
|
||||
return
|
||||
}
|
||||
token := uuid.NewString()
|
||||
attempt++
|
||||
_, err = tx.Exec(jobctx, `UPDATE attachment_jobs SET state='processing',attempts=$2,lease_token=$3,lease_until=clock_timestamp()+($4*interval '1 second'),updated_at=clock_timestamp() WHERE id=$1`, j.ID, attempt, token, int(p.Timeout/time.Second)+5)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if _, err = tx.Exec(jobctx, `UPDATE attachments SET status='processing',updated_at=clock_timestamp() WHERE id=$1`, attachment); err != nil {
|
||||
return
|
||||
}
|
||||
if tx.Commit(jobctx) != nil {
|
||||
return
|
||||
}
|
||||
p.Checkpoint(jobctx, "worker_after_claim")
|
||||
variants, code := p.process(jobctx, attachment, staging)
|
||||
p.Checkpoint(jobctx, "worker_after_objects")
|
||||
// A separate connection completes after job timeout/cancel; token fences stale attempts.
|
||||
finishctx, finishcancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer finishcancel()
|
||||
finish, err := conn.Begin(finishctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer finish.Rollback(context.Background())
|
||||
var current string
|
||||
if err = finish.QueryRow(finishctx, `SELECT lease_token::text FROM attachment_jobs WHERE id=$1 AND state='processing' FOR UPDATE`, j.ID).Scan(¤t); err != nil || current != token {
|
||||
return
|
||||
}
|
||||
if code == "" {
|
||||
encoded, _ := json.Marshal(variants)
|
||||
_, err = finish.Exec(finishctx, `UPDATE attachments SET status='ready',object_key=$2,variants=$3,error_code=NULL,updated_at=clock_timestamp() WHERE id=$1`, attachment, "originals/"+attachment, encoded)
|
||||
if err == nil {
|
||||
_, err = finish.Exec(finishctx, `UPDATE attachment_jobs SET state='ready',lease_until=NULL,lease_token=NULL,error_code=NULL,updated_at=clock_timestamp() WHERE id=$1`, j.ID)
|
||||
}
|
||||
} else if code == "invalid_image" || attempt >= maxAttempts {
|
||||
if code != "invalid_image" {
|
||||
code = "retry_exhausted"
|
||||
}
|
||||
err = p.failTx(finishctx, finish, j.ID, attachment, code)
|
||||
} else {
|
||||
delay := RetryDelay(attempt)
|
||||
_, err = finish.Exec(finishctx, `UPDATE attachment_jobs SET state='queued',error_code=$2,next_at=clock_timestamp()+($3*interval '1 millisecond'),lease_until=NULL,lease_token=NULL,updated_at=clock_timestamp() WHERE id=$1`, j.ID, code, delay.Milliseconds())
|
||||
if err == nil {
|
||||
_, err = finish.Exec(finishctx, `UPDATE attachments SET status='queued',updated_at=clock_timestamp() WHERE id=$1`, attachment)
|
||||
}
|
||||
}
|
||||
if err != nil || finish.Commit(finishctx) != nil {
|
||||
return
|
||||
}
|
||||
p.Checkpoint(finishctx, "worker_before_ack")
|
||||
p.ack(finishctx, m.ID)
|
||||
}
|
||||
func RetryDelay(attempt int) time.Duration {
|
||||
if attempt > 6 {
|
||||
attempt = 6
|
||||
}
|
||||
base := time.Second * time.Duration(1<<uint(attempt-1))
|
||||
return base + time.Duration(rand.Int64N(int64(base/2)+1))
|
||||
}
|
||||
func (p *Pipeline) failTx(ctx context.Context, tx pgx.Tx, id, attachment, code string) error {
|
||||
_, err := tx.Exec(ctx, `UPDATE attachment_jobs SET state='failed',error_code=$2,lease_until=NULL,lease_token=NULL,updated_at=clock_timestamp() WHERE id=$1`, id, code)
|
||||
if err == nil {
|
||||
_, err = tx.Exec(ctx, `UPDATE attachments SET status='failed',error_code=$2,updated_at=clock_timestamp() WHERE id=$1`, attachment, code)
|
||||
}
|
||||
return err
|
||||
}
|
||||
func (p *Pipeline) process(ctx context.Context, id, key string) ([]Variant, string) {
|
||||
dir, err := os.MkdirTemp("", "job-*")
|
||||
if err != nil {
|
||||
return nil, "storage_unavailable"
|
||||
}
|
||||
defer os.RemoveAll(dir)
|
||||
object, err := p.Objects.Get(ctx, key)
|
||||
if err != nil {
|
||||
return nil, "storage_unavailable"
|
||||
}
|
||||
file, err := os.Create(filepath.Join(dir, "source"))
|
||||
if err != nil {
|
||||
object.Close()
|
||||
return nil, "storage_unavailable"
|
||||
}
|
||||
hash := sha256.New()
|
||||
n, copyErr := io.Copy(io.MultiWriter(file, hash), io.LimitReader(object, 10485761))
|
||||
object.Close()
|
||||
file.Close()
|
||||
if copyErr != nil {
|
||||
return nil, "storage_unavailable"
|
||||
}
|
||||
if n > 10485760 {
|
||||
return nil, "invalid_image"
|
||||
}
|
||||
var expected string
|
||||
if p.DB.QueryRow(ctx, `SELECT sha256 FROM attachments WHERE id=$1`, id).Scan(&expected) != nil {
|
||||
return nil, "storage_unavailable"
|
||||
}
|
||||
if hex.EncodeToString(hash.Sum(nil)) != expected {
|
||||
return nil, "invalid_image"
|
||||
}
|
||||
cmd := exec.CommandContext(ctx, "python3", "/opt/process_image.py", filepath.Join(dir, "source"), dir)
|
||||
output, err := cmd.Output()
|
||||
if err != nil {
|
||||
if ctx.Err() != nil {
|
||||
return nil, "processing_timeout"
|
||||
}
|
||||
var exit *exec.ExitError
|
||||
if errors.As(err, &exit) && exit.ExitCode() == 2 {
|
||||
return nil, "invalid_image"
|
||||
}
|
||||
return nil, "processing_failed"
|
||||
}
|
||||
variants := []Variant{}
|
||||
if json.Unmarshal(output, &variants) != nil || len(variants) != 3 {
|
||||
return nil, "processing_failed"
|
||||
}
|
||||
var kind string
|
||||
if p.DB.QueryRow(ctx, `SELECT content_type FROM attachments WHERE id=$1`, id).Scan(&kind) != nil {
|
||||
return nil, "storage_unavailable"
|
||||
}
|
||||
source, err := os.Open(filepath.Join(dir, "source"))
|
||||
if err != nil {
|
||||
return nil, "storage_unavailable"
|
||||
}
|
||||
err = p.Objects.Put(ctx, "originals/"+id, source, n, kind)
|
||||
source.Close()
|
||||
if err != nil {
|
||||
return nil, "storage_unavailable"
|
||||
}
|
||||
for _, v := range variants {
|
||||
f, err := os.Open(filepath.Join(dir, v.Name))
|
||||
if err != nil {
|
||||
return nil, "processing_failed"
|
||||
}
|
||||
err = p.Objects.Put(ctx, "variants/"+id+"/v1/"+v.Name, f, v.Size, v.ContentType)
|
||||
f.Close()
|
||||
if err != nil {
|
||||
return nil, "storage_unavailable"
|
||||
}
|
||||
}
|
||||
return variants, ""
|
||||
}
|
||||
|
||||
// Failed registry is PostgreSQL; explicit replay is audited and refuses successful jobs.
|
||||
func (p *Pipeline) Replay(ctx context.Context, id string) error {
|
||||
if uuid.Validate(id) != nil {
|
||||
return fmt.Errorf("invalid job id")
|
||||
}
|
||||
conn, err := p.DB.Acquire(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer conn.Release()
|
||||
tx, err := conn.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
var attachment, state, code string
|
||||
var retained bool
|
||||
err = tx.QueryRow(ctx, `SELECT attachment_id::text,state,COALESCE(error_code,''),staging_deleted_at IS NULL FROM attachment_jobs WHERE id=$1 FOR UPDATE`, id).Scan(&attachment, &state, &code, &retained)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if state != "failed" || !retained {
|
||||
return fmt.Errorf("only failed jobs with retained staging can be replayed")
|
||||
}
|
||||
var key string
|
||||
if err = tx.QueryRow(ctx, `SELECT staging_key FROM attachment_jobs WHERE id=$1`, id).Scan(&key); err != nil {
|
||||
return err
|
||||
}
|
||||
object, err := p.Objects.Get(ctx, key)
|
||||
if err != nil {
|
||||
return fmt.Errorf("staging unavailable")
|
||||
}
|
||||
object.Close()
|
||||
if _, err = tx.Exec(ctx, `INSERT INTO attachment_replays(job_id,previous_error) VALUES($1,$2)`, id, code); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `UPDATE attachment_jobs SET state='queued',attempts=0,error_code=NULL,next_at=clock_timestamp(),lease_until=NULL,lease_token=NULL,updated_at=clock_timestamp() WHERE id=$1`, id); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `UPDATE attachments SET status='queued',error_code=NULL,updated_at=clock_timestamp() WHERE id=$1`, attachment); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `UPDATE attachment_outbox SET published_at=NULL WHERE job_id=$1`, id); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (p *Pipeline) Sweep(ctx context.Context) error {
|
||||
rows, err := p.DB.Query(ctx, `SELECT id::text,staging_key FROM attachment_jobs WHERE staging_deleted_at IS NULL AND (state='ready' OR (state='failed' AND updated_at<clock_timestamp()-($1*interval '1 second'))) LIMIT 20`, int64(p.Retention/time.Second))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
type item struct{ id, key string }
|
||||
items := []item{}
|
||||
for rows.Next() {
|
||||
var i item
|
||||
if err = rows.Scan(&i.id, &i.key); err != nil {
|
||||
rows.Close()
|
||||
return err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
err = rows.Err()
|
||||
rows.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, i := range items {
|
||||
tx, err := p.DB.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Replay and retention serialize on this row. Never delete a newly queued job.
|
||||
var allowed bool
|
||||
err = tx.QueryRow(ctx, `SELECT state='ready' OR (state='failed' AND updated_at<clock_timestamp()-($2*interval '1 second')) FROM attachment_jobs WHERE id=$1 FOR UPDATE`, i.id, int64(p.Retention/time.Second)).Scan(&allowed)
|
||||
if err != nil || !allowed {
|
||||
_ = tx.Rollback(ctx)
|
||||
continue
|
||||
}
|
||||
err = p.Objects.Delete(ctx, i.key)
|
||||
if err == nil {
|
||||
_, err = tx.Exec(ctx, `UPDATE attachment_jobs SET staging_deleted_at=clock_timestamp() WHERE id=$1`, i.id)
|
||||
}
|
||||
if err != nil {
|
||||
_ = tx.Rollback(ctx)
|
||||
return err
|
||||
}
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Operator commands are available only through docker exec, not through HTTP.
|
||||
func (p *Pipeline) Command(ctx context.Context, args []string) error {
|
||||
if len(args) == 2 && args[0] == "replay" {
|
||||
return p.Replay(ctx, args[1])
|
||||
}
|
||||
if len(args) == 1 && args[0] == "failed-jobs" {
|
||||
rows, err := p.DB.Query(ctx, `SELECT id::text,error_code,attempts FROM attachment_jobs WHERE state='failed' ORDER BY created_at`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var id, code string
|
||||
var attempts int
|
||||
if err = rows.Scan(&id, &code, &attempts); err != nil {
|
||||
return err
|
||||
}
|
||||
_ = json.NewEncoder(os.Stdout).Encode(map[string]any{"job_id": id, "error_code": code, "attempts": attempts})
|
||||
}
|
||||
return rows.Err()
|
||||
}
|
||||
if len(args) == 3 && args[0] == "fault" && (args[1] == "arm" || args[1] == "clear") {
|
||||
allowed := map[string]bool{"dispatcher_before_publish": true, "dispatcher_after_publish": true, "worker_after_claim": true, "worker_after_objects": true, "worker_before_ack": true}
|
||||
if !allowed[args[2]] {
|
||||
return fmt.Errorf("unknown fault point")
|
||||
}
|
||||
if args[1] == "clear" {
|
||||
_, err := p.DB.Exec(ctx, `DELETE FROM pipeline_faults WHERE point=$1`, args[2])
|
||||
return err
|
||||
}
|
||||
_, err := p.DB.Exec(ctx, `INSERT INTO pipeline_faults(point) VALUES($1) ON CONFLICT(point) DO UPDATE SET armed=true,hit_at=NULL`, args[2])
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("unknown pipeline command: %s", strconv.Itoa(len(args)))
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestRetryDelayBounds(t *testing.T) {
|
||||
for attempt := 1; attempt <= 20; attempt++ {
|
||||
bounded := attempt
|
||||
if bounded > 6 {
|
||||
bounded = 6
|
||||
}
|
||||
base := time.Second * time.Duration(1<<uint(bounded-1))
|
||||
for i := 0; i < 100; i++ {
|
||||
got := RetryDelay(attempt)
|
||||
if got < base || got > base+base/2 {
|
||||
t.Fatalf("attempt %d: delay %s outside [%s,%s]", attempt, got, base, base+base/2)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Executable
+26
@@ -0,0 +1,26 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
mkdir -p secrets
|
||||
if [ ! -f secrets/postgres_password ]; then
|
||||
umask 077
|
||||
openssl rand -hex 24 > secrets/postgres_password
|
||||
fi
|
||||
if [ ! -f secrets/redis_password ]; then
|
||||
umask 077
|
||||
openssl rand -hex 24 > secrets/redis_password
|
||||
fi
|
||||
postgres_password=$(cat secrets/postgres_password)
|
||||
redis_password=$(cat secrets/redis_password)
|
||||
umask 077
|
||||
printf 'postgres://messenger:%s@postgres:5432/messenger?sslmode=disable\n' "$postgres_password" > secrets/database_url
|
||||
printf 'redis://:%s@redis:6379/0\n' "$redis_password" > secrets/cache_url
|
||||
if [ ! -f .env ]; then cp .env.example .env; fi
|
||||
for secret_name in s3_admin_access s3_admin_secret s3_access_key_id s3_secret_access_key; do
|
||||
if [ ! -f "secrets/$secret_name" ]; then openssl rand -hex 24 > "secrets/$secret_name"; fi
|
||||
done
|
||||
printf '%s\n' 'S3 credentials ready; bucket and policy are bootstrapped by s3-init.'
|
||||
|
||||
if [ ! -f secrets/broker_password ]; then openssl rand -hex 24 > secrets/broker_password; fi
|
||||
broker_password=$(cat secrets/broker_password)
|
||||
printf 'redis://:%s@broker:6379/0\n' "$broker_password" > secrets/broker_url
|
||||
Executable
+28
@@ -0,0 +1,28 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
wait_healthy() {
|
||||
service=$1
|
||||
container_id=$(docker compose --env-file .env ps -q "$service")
|
||||
attempts=0
|
||||
until [ "$(docker inspect --format '{{.State.Health.Status}}' "$container_id" 2>/dev/null || true)" = healthy ]; do
|
||||
attempts=$((attempts + 1))
|
||||
if [ "$attempts" -ge 60 ]; then
|
||||
printf '%s\n' "$service did not become healthy" >&2
|
||||
exit 1
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
}
|
||||
|
||||
docker compose --env-file .env restart postgres redis s3 broker >/dev/null
|
||||
wait_healthy postgres
|
||||
wait_healthy redis
|
||||
wait_healthy s3
|
||||
wait_healthy broker
|
||||
docker compose --env-file .env restart api1 api2 cleanup dispatcher worker >/dev/null
|
||||
wait_healthy api1
|
||||
wait_healthy api2
|
||||
docker compose --env-file .env restart nginx >/dev/null
|
||||
wait_healthy nginx
|
||||
Executable
+25
@@ -0,0 +1,25 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
wait_healthy() {
|
||||
service=$1
|
||||
container_id=$(docker compose --env-file .env ps -q "$service")
|
||||
attempts=0
|
||||
until [ "$(docker inspect --format '{{.State.Health.Status}}' "$container_id" 2>/dev/null || true)" = healthy ]; do
|
||||
attempts=$((attempts + 1))
|
||||
if [ "$attempts" -ge 60 ]; then
|
||||
printf '%s\n' "$service did not become healthy" >&2
|
||||
exit 1
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
}
|
||||
|
||||
docker compose --env-file .env build migrate >/dev/null
|
||||
docker compose --env-file .env run --rm --no-deps migrate >/dev/null
|
||||
for service in api1 api2; do
|
||||
docker compose --env-file .env up -d --no-deps --force-recreate "$service" >/dev/null
|
||||
wait_healthy "$service"
|
||||
docker compose --env-file .env exec -T nginx nginx -s reload >/dev/null
|
||||
done
|
||||
Executable
+17
@@ -0,0 +1,17 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
docker compose --env-file .env start api1 api2 >/dev/null
|
||||
for service in api1 api2; do
|
||||
container_id=$(docker compose --env-file .env ps -q "$service")
|
||||
attempts=0
|
||||
until [ "$(docker inspect --format '{{.State.Health.Status}}' "$container_id" 2>/dev/null || true)" = healthy ]; do
|
||||
attempts=$((attempts + 1))
|
||||
if [ "$attempts" -ge 60 ]; then
|
||||
printf '%s\n' "$service did not become healthy" >&2
|
||||
exit 1
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
done
|
||||
Executable
+12
@@ -0,0 +1,12 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
|
||||
|
||||
case "$TARGET_INSTANCE_ID" in
|
||||
api-1) service=api1 ;;
|
||||
api-2) service=api2 ;;
|
||||
*) printf '%s\n' "Unknown API instance alias" >&2; exit 2 ;;
|
||||
esac
|
||||
|
||||
docker compose --env-file .env stop --timeout 3 "$service" >/dev/null
|
||||
@@ -0,0 +1,82 @@
|
||||
"""Local synthetic tests; credentials stay in memory and never enter evidence."""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / 'tests'))
|
||||
from client import Client, Failure, check, run
|
||||
|
||||
|
||||
def compose(*args, data=None):
|
||||
result = subprocess.run(['docker', 'compose', '--env-file', '.env', *args], cwd=ROOT,
|
||||
input=data, capture_output=True, text=True)
|
||||
check(result.returncode == 0, 'compose command failed: ' + ' '.join(args[:3]))
|
||||
return result.stdout.strip()
|
||||
|
||||
|
||||
def psql(sql):
|
||||
return compose('exec', '-T', 'postgres', 'psql', '-XAtq', '-v', 'ON_ERROR_STOP=1',
|
||||
'-U', 'messenger', '-d', 'messenger', data=sql)
|
||||
|
||||
|
||||
def wait_for(test, message, timeout=100):
|
||||
deadline = time.monotonic() + timeout
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
value = test()
|
||||
if value:
|
||||
return value
|
||||
except Failure:
|
||||
pass
|
||||
time.sleep(.3)
|
||||
raise Failure(message)
|
||||
|
||||
|
||||
def ready(client):
|
||||
wait_for(lambda: client.call('GET', '/health/ready', schema='Health'), 'API not ready')
|
||||
|
||||
|
||||
def upload(client, actor, chat, raw=None):
|
||||
if raw is None:
|
||||
raw = (ROOT / 'tests/fixtures/sample.png').read_bytes()
|
||||
result = client.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=test.png',
|
||||
202, actor=actor, raw=raw, headers={'Content-Type': 'image/png'}, schema='Attachment')['json']
|
||||
check(result['status'] == 'queued', '202 must contain queued snapshot')
|
||||
return result
|
||||
|
||||
|
||||
def state(client, actor, attachment):
|
||||
return client.call('GET', '/api/v1/attachments/' + attachment['id'], actor=actor, schema='Attachment')['json']
|
||||
|
||||
|
||||
def wait_state(client, actor, attachment, wanted='ready', timeout=100):
|
||||
def poll():
|
||||
result = state(client, actor, attachment)
|
||||
if wanted == 'ready':
|
||||
check(result['status'] != 'failed', 'job unexpectedly failed')
|
||||
return result if result['status'] == wanted else None
|
||||
return wait_for(poll, 'attachment did not become ' + wanted, timeout)
|
||||
|
||||
|
||||
def fault(point):
|
||||
compose('exec', '-T', 'api1', '/messenger', 'fault', 'arm', point)
|
||||
|
||||
|
||||
def hit(point):
|
||||
wait_for(lambda: psql("SELECT hit_at IS NOT NULL FROM pipeline_faults WHERE point='" + point + "';") == 't',
|
||||
'fault not reached: ' + point)
|
||||
|
||||
|
||||
def dump_evidence(name, value):
|
||||
(ROOT / 'evidence').mkdir(exist_ok=True)
|
||||
(ROOT / 'evidence' / name).write_text(json.dumps(value, ensure_ascii=False, indent=2) + '\n')
|
||||
|
||||
|
||||
def start_only(service):
|
||||
# Compose start can also start dependencies; fault tests need exactly this container.
|
||||
container = compose('ps', '--all', '-q', service)
|
||||
result = subprocess.run(['docker', 'start', container], capture_output=True, text=True)
|
||||
check(result.returncode == 0, 'cannot start container ' + service)
|
||||
@@ -0,0 +1,138 @@
|
||||
"""Grade 4: durable acceptance, outbox, crashes, lease, retries, explicit replay."""
|
||||
import hashlib
|
||||
import json
|
||||
import time
|
||||
import uuid
|
||||
from pipeline_helpers import *
|
||||
|
||||
|
||||
def main():
|
||||
client = Client(timeout=30)
|
||||
ready(client)
|
||||
actor, _, _, chat = client.bootstrap()
|
||||
raw = (ROOT / 'tests/fixtures/sample.png').read_bytes()
|
||||
results = {}
|
||||
try:
|
||||
# Worker absent: no resize in API, accepted job survives real restarts.
|
||||
compose('stop', 'worker')
|
||||
attachment = upload(client, actor, chat)
|
||||
client.call('GET', '/api/v1/attachments/' + attachment['id'] + '/content', 409, actor=actor)
|
||||
client.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 201, actor=actor,
|
||||
body={'text': '', 'attachment_ids': [attachment['id']]},
|
||||
headers={'Idempotency-Key': uuid.uuid4().hex}, schema='Message')
|
||||
services = ('api1', 'api2', 'broker')
|
||||
def starts():
|
||||
values = {}
|
||||
for service in services:
|
||||
container = compose('ps', '-q', service)
|
||||
data = subprocess.run(['docker', 'inspect', container], capture_output=True, text=True, check=True)
|
||||
values[service] = json.loads(data.stdout)[0]['State']['StartedAt']
|
||||
return values
|
||||
before = starts()
|
||||
compose('restart', *services)
|
||||
ready(client)
|
||||
after = starts()
|
||||
check(all(before[s] != after[s] for s in services), 'actual restart not observed')
|
||||
check(state(client, actor, attachment)['status'] == 'queued', 'worker stopped but state changed')
|
||||
start_only('worker')
|
||||
result = wait_state(client, actor, attachment)
|
||||
check({v['name'] for v in result['variants']} == {'thumbnail', 'crop', 'optimized'}, 'missing grade4 variants')
|
||||
check(client.call('GET', '/api/v1/attachments/' + attachment['id'] + '/content', actor=actor)['bytes'] == raw, 'original changed')
|
||||
wait_for(lambda: psql("SELECT staging_deleted_at IS NOT NULL FROM attachment_jobs WHERE id='" + attachment['id'] + "';") == 't', 'staging not cleaned')
|
||||
results['stopped_worker_and_restart'] = dict(status='PASS', before=before, after=after)
|
||||
|
||||
# Committed outbox remains when dispatcher dies before XADD.
|
||||
compose('stop', 'dispatcher')
|
||||
fault('dispatcher_before_publish')
|
||||
item = upload(client, actor, chat)
|
||||
check(psql("SELECT count(*) FROM attachment_outbox WHERE job_id='" + item['id'] + "';") == '1', 'outbox absent')
|
||||
compose('start', 'dispatcher')
|
||||
hit('dispatcher_before_publish')
|
||||
wait_state(client, actor, item)
|
||||
results['commit_publish_gap'] = 'PASS: actual dispatcher exit 86 and automatic restart'
|
||||
|
||||
# Crash after S3 objects, before final PG COMMIT; reclaim expired processing.
|
||||
fault('worker_after_objects')
|
||||
item = upload(client, actor, chat)
|
||||
hit('worker_after_objects')
|
||||
check(psql("SELECT state FROM attachment_jobs WHERE id='" + item['id'] + "';") == 'processing', 'crash did not leave processing')
|
||||
started = time.monotonic()
|
||||
result = wait_state(client, actor, item, timeout=110)
|
||||
check(psql("SELECT attempts FROM attachment_jobs WHERE id='" + item['id'] + "';") == '2', 'expired lease not recovered')
|
||||
check(len(result['variants']) == 3, 'duplicate variant metadata')
|
||||
results['crash_after_objects'] = dict(status='PASS', recovery_seconds=round(time.monotonic()-started, 3), attempts=2)
|
||||
|
||||
# COMMIT ready then death before ack: ready must never regress/reprocess.
|
||||
fault('worker_before_ack')
|
||||
item = upload(client, actor, chat)
|
||||
hit('worker_before_ack')
|
||||
result = wait_state(client, actor, item)
|
||||
attempts = psql("SELECT attempts FROM attachment_jobs WHERE id='" + item['id'] + "';")
|
||||
duplicate = dict(schema_version=1, job_id=item['id'], type='attachment.process.v1', attachment_id=item['id'],
|
||||
pipeline_version=1, attempt=1, created_at=item['created_at'])
|
||||
# Use broker password only inside the container, never in evidence.
|
||||
duplicate_id = compose('exec', '-T', 'broker', 'sh', '-ec',
|
||||
'redis-cli --no-auth-warning -a "$(cat /run/secrets/broker_password)" XADD attachment.jobs.v1 "*" job "$1"',
|
||||
'sh', json.dumps(duplicate))
|
||||
wait_for(lambda: compose('exec', '-T', 'broker', 'sh', '-ec',
|
||||
'redis-cli --raw --no-auth-warning -a "$(cat /run/secrets/broker_password)" XRANGE attachment.jobs.v1 "$1" "$1"',
|
||||
'sh', duplicate_id) == '', 'ready duplicate was not acknowledged')
|
||||
check(state(client, actor, item) == result, 'ready duplicate changed result')
|
||||
check(psql("SELECT attempts FROM attachment_jobs WHERE id='" + item['id'] + "';") == attempts, 'ready duplicate processed again')
|
||||
results['commit_before_ack_and_duplicate'] = 'PASS'
|
||||
|
||||
# Temporary S3 failure: attempt budget/backoff, failed registry, audited replay.
|
||||
compose('stop', 'worker')
|
||||
item = upload(client, actor, chat)
|
||||
psql("UPDATE attachment_jobs SET max_attempts=2 WHERE id='" + item['id'] + "';")
|
||||
compose('stop', 's3')
|
||||
start_only('worker')
|
||||
check(compose('ps', '--status', 'running', '-q', 's3') == '', 'fault test unexpectedly restarted S3')
|
||||
wait_for(lambda: psql("SELECT state FROM attachment_jobs WHERE id='" + item['id'] + "';") == 'failed', 'temporary errors did not exhaust retries', 100)
|
||||
compose('start', 's3')
|
||||
ready(client)
|
||||
failed = state(client, actor, item)
|
||||
check(failed['error_code'] == 'retry_exhausted', 'unsafe or missing final error')
|
||||
client.call('GET', '/api/v1/attachments/' + item['id'] + '/content', 409, actor=actor)
|
||||
client.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 409, actor=actor,
|
||||
body={'text':'failed', 'attachment_ids':[item['id']]}, headers={'Idempotency-Key':uuid.uuid4().hex})
|
||||
registry = compose('exec', '-T', 'dispatcher', '/messenger', 'failed-jobs')
|
||||
check(item['id'] in registry, 'failed registry missing job')
|
||||
compose('exec', '-T', 'dispatcher', '/messenger', 'replay', item['id'])
|
||||
wait_state(client, actor, item)
|
||||
check(psql("SELECT count(*) FROM attachment_replays WHERE job_id='" + item['id'] + "';") == '1', 'replay audit missing')
|
||||
proc = subprocess.run(['docker','compose','--env-file','.env','exec','-T','dispatcher','/messenger','replay',item['id']], cwd=ROOT, capture_output=True)
|
||||
check(proc.returncode != 0, 'ready replay must be refused')
|
||||
results['retry_failed_registry_replay'] = dict(status='PASS', exhausted_attempts=2, error_code='retry_exhausted', replay_audit_rows=1, ready_replay_rejected=True)
|
||||
|
||||
# Permanent invalid staging: no endless retries, safe machine error.
|
||||
compose('stop', 'worker')
|
||||
item = upload(client, actor, chat)
|
||||
original_key = uuid.uuid4().hex
|
||||
original_body = {'text':'accepted while queued', 'attachment_ids':[item['id']]}
|
||||
original_message = client.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 201,
|
||||
actor=actor, body=original_body, headers={'Idempotency-Key':original_key}, schema='Message')['json']
|
||||
# Integrity metadata mismatch represents invalid durable source.
|
||||
psql("UPDATE attachments SET sha256=repeat('0',64) WHERE id='" + item['id'] + "';")
|
||||
start_only('worker')
|
||||
failed = wait_state(client, actor, item, 'failed')
|
||||
check(failed['error_code'] == 'invalid_image', 'permanent invalid source must fail safely')
|
||||
check(psql("SELECT attempts FROM attachment_jobs WHERE id='" + item['id'] + "';") == '1', 'permanent error retried')
|
||||
repeated = client.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 201,
|
||||
actor=actor, body=original_body, headers={'Idempotency-Key':original_key}, schema='Message')['json']
|
||||
check(repeated == original_message, 'committed message replay changed after attachment failed')
|
||||
client.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 409,
|
||||
actor=actor, body=original_body, headers={'Idempotency-Key':uuid.uuid4().hex})
|
||||
results['permanent_error'] = 'PASS'
|
||||
results['message_replay_after_attachment_failure'] = 'PASS'
|
||||
results['crash_points'] = json.loads(psql("SELECT json_agg(json_build_object('point',point,'armed',armed,'hit_at',hit_at) ORDER BY point) FROM pipeline_faults WHERE hit_at IS NOT NULL;"))
|
||||
dump_evidence('pipeline-grade4.json', results)
|
||||
print('PASS:', json.dumps(results))
|
||||
finally:
|
||||
compose('start', 's3', 'broker', 'dispatcher', 'worker', 'api1', 'api2')
|
||||
psql('DELETE FROM pipeline_faults;')
|
||||
ready(client)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(main)
|
||||
@@ -0,0 +1,79 @@
|
||||
"""Public persistence/failover hooks plus actual container restart evidence."""
|
||||
import json
|
||||
import subprocess
|
||||
import uuid
|
||||
from pipeline_helpers import *
|
||||
|
||||
|
||||
def snapshots():
|
||||
services=('postgres','redis','s3','broker','api1','api2','dispatcher','worker','cleanup','nginx')
|
||||
result={}
|
||||
for service in services:
|
||||
container=compose('ps','-q',service)
|
||||
info=subprocess.run(['docker','inspect',container],capture_output=True,text=True,check=True)
|
||||
data=json.loads(info.stdout)[0]
|
||||
result[service]=dict(id=data['Id'],started_at=data['State']['StartedAt'])
|
||||
return result
|
||||
|
||||
|
||||
def main():
|
||||
client=Client();ready(client)
|
||||
before=snapshots()
|
||||
base=client.base
|
||||
command=['make','test-persistence','ACTION_SCRIPT=scripts/restart.sh','BASE_URL='+base]
|
||||
result=subprocess.run(command,cwd=ROOT,capture_output=True,text=True)
|
||||
check(result.returncode==0,'public persistence failed: '+result.stdout[-500:])
|
||||
after=snapshots()
|
||||
check(all(before[s]['started_at']!=after[s]['started_at'] for s in before),'hook did not restart all required services')
|
||||
|
||||
output={'persistence':dict(status='PASS',before=before,after=after),'failover':[]}
|
||||
public=subprocess.run(['make','test-failover','ACTION_SCRIPT=scripts/stop-one.sh','BASE_URL='+base],cwd=ROOT,capture_output=True,text=True)
|
||||
check(public.returncode==0,'public failover failed: '+public.stdout[-500:])
|
||||
output['public_failover']=dict(status='PASS',output=public.stdout.strip())
|
||||
try:
|
||||
compose('start','api1','api2');ready(client)
|
||||
actor,_,_,chat=client.bootstrap()
|
||||
item=upload(client,actor,chat);wait_state(client,actor,item)
|
||||
raw=(ROOT/'tests/fixtures/sample.png').read_bytes()
|
||||
path='/api/v1/chats/'+chat['id']+'/messages'
|
||||
for target in ('api-1','api-2'):
|
||||
compose('start','api1','api2');ready(client)
|
||||
seen=set()
|
||||
wait_for(lambda: (seen.add(client.call('GET','/health/ready')['headers']['X-Instance-Id']) or len(seen)==2),'two replicas not visible')
|
||||
process=subprocess.Popen([str(ROOT/'scripts/stop-one.sh')],cwd=ROOT,
|
||||
env=dict(os.environ,TARGET_INSTANCE_ID=target),stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
|
||||
began=time.monotonic();deadline=began+8;first=None;confirmed=set();failures=0
|
||||
key=uuid.uuid4().hex
|
||||
while time.monotonic()<deadline:
|
||||
try:
|
||||
response=client.call('POST',path,201,actor=actor,body={'text':key},headers={'Idempotency-Key':key},schema='Message')
|
||||
confirmed.add(response['json']['id'])
|
||||
if process.poll() is not None and response['headers']['X-Instance-Id']!=target and first is None:
|
||||
first=time.monotonic()-began
|
||||
key=uuid.uuid4().hex
|
||||
except Failure:
|
||||
failures+=1
|
||||
time.sleep(.1)
|
||||
check(process.wait(timeout=10)==0,'stop hook failed')
|
||||
check(first is not None and first<=10,'surviving API did not recover within 10 seconds')
|
||||
service={'api-1':'api1','api-2':'api2'}[target]
|
||||
check(not compose('ps','--status','running','-q',service),'target was not actually stopped')
|
||||
page=client.call('GET',path+'?limit=100',actor=actor,schema='MessagePage')['json']
|
||||
visible={m['id'] for m in page['items']}
|
||||
# At most 80 requests per case: walk pages to include both cases.
|
||||
while page['next_cursor']:
|
||||
from urllib.parse import quote
|
||||
page=client.call('GET',path+'?limit=100&cursor='+quote(page['next_cursor']),actor=actor,schema='MessagePage')['json']
|
||||
visible.update(m['id'] for m in page['items'])
|
||||
check(confirmed.issubset(visible),'confirmed messages lost')
|
||||
check(client.call('GET','/api/v1/attachments/'+item['id']+'/content',actor=actor)['bytes']==raw,'original lost on API failover')
|
||||
output['failover'].append(dict(status='PASS',stopped_service=service,confirmed_messages=len(confirmed),
|
||||
transient_failures=failures,first_survivor_seconds=round(first,3),original_preserved=True))
|
||||
finally:
|
||||
compose('start','api1','api2');ready(client)
|
||||
dump_evidence('resilience.json',output)
|
||||
print('PASS: all ten services restarted, same token/data/original preserved; both API failures covered')
|
||||
|
||||
|
||||
if __name__=='__main__':
|
||||
run(main)
|
||||
@@ -0,0 +1,100 @@
|
||||
"""Grade 4 image contract: actual decoding, EXIF, alpha, limits and private storage."""
|
||||
import base64
|
||||
import hashlib
|
||||
import json
|
||||
import struct
|
||||
import uuid
|
||||
import zlib
|
||||
from pipeline_helpers import *
|
||||
|
||||
|
||||
def make_fixture(kind):
|
||||
code = '''
|
||||
import base64,io,sys
|
||||
from PIL import Image
|
||||
kind=sys.argv[1]
|
||||
if kind=='orientation':
|
||||
im=Image.new('RGB',(320,200),'red');exif=Image.Exif();exif[274]=6;exif[315]='synthetic test';kw=dict(format='JPEG',exif=exif)
|
||||
elif kind=='alpha':
|
||||
im=Image.new('RGBA',(8,4),(255,0,0,0));kw=dict(format='PNG')
|
||||
elif kind=='boundary':
|
||||
im=Image.new('RGB',(8000,5000),'blue');kw=dict(format='PNG')
|
||||
else:
|
||||
im=Image.new('RGB',(1601,801),'blue');kw=dict(format='PNG')
|
||||
b=io.BytesIO();im.save(b,**kw);print(base64.b64encode(b.getvalue()).decode())
|
||||
'''
|
||||
return base64.b64decode(compose('exec', '-T', 'worker', 'python3', '-c', code, kind))
|
||||
|
||||
|
||||
def inspect_image(raw):
|
||||
code = '''
|
||||
import base64,io,json,sys
|
||||
from PIL import Image
|
||||
im=Image.open(io.BytesIO(base64.b64decode(sys.stdin.read())));im.load()
|
||||
print(json.dumps(dict(format=im.format,width=im.width,height=im.height,exif=len(im.getexif()),pixel=list(im.convert('RGB').getpixel((0,0))))))
|
||||
'''
|
||||
return json.loads(compose('exec', '-T', 'worker', 'python3', '-c', code, data=base64.b64encode(raw).decode()))
|
||||
|
||||
|
||||
def chunk(kind, payload):
|
||||
return struct.pack('>I', len(payload)) + kind + payload + struct.pack('>I', zlib.crc32(kind+payload) & 0xffffffff)
|
||||
|
||||
|
||||
def main():
|
||||
client=Client(timeout=60)
|
||||
actor, _, outsider, chat=client.bootstrap()
|
||||
results={}
|
||||
for kind in ('orientation','alpha','large','boundary'):
|
||||
raw=make_fixture(kind)
|
||||
content_type='image/jpeg' if kind=='orientation' else 'image/png'
|
||||
item=client.call('POST','/api/v1/chats/'+chat['id']+'/attachments?filename=synthetic',202,actor=actor,raw=raw,
|
||||
headers={'Content-Type':content_type},schema='Attachment')['json']
|
||||
result=wait_state(client,actor,item)
|
||||
variants={v['name']:v for v in result['variants']}
|
||||
expected={'orientation':(160,256),'alpha':(8,4),'large':(256,128),'boundary':(256,160)}[kind]
|
||||
check((variants['thumbnail']['width'],variants['thumbnail']['height'])==expected,'thumbnail dimensions/orientation')
|
||||
check((variants['crop']['width'],variants['crop']['height'])==(128,128),'crop dimensions')
|
||||
optimized={'orientation':(200,320),'alpha':(8,4),'large':(1280,640),'boundary':(1280,800)}[kind]
|
||||
check((variants['optimized']['width'],variants['optimized']['height'])==optimized,'optimized fit dimensions')
|
||||
for name,meta in variants.items():
|
||||
blob=client.call('GET','/api/v1/attachments/'+item['id']+'/content?variant='+name,actor=actor)['bytes']
|
||||
check(hashlib.sha256(blob).hexdigest()==meta['sha256'] and len(blob)==meta['size_bytes'],'variant checksum/size')
|
||||
decoded=inspect_image(blob)
|
||||
check(decoded['format']==('WEBP' if name=='optimized' else 'JPEG'),'actual variant encoding')
|
||||
check(decoded['exif']==0,'EXIF leaked into variant')
|
||||
if kind=='alpha':
|
||||
check(all(v>=250 for v in decoded['pixel']),'alpha not composited on white')
|
||||
check(client.call('GET','/api/v1/attachments/'+item['id']+'/content',actor=actor)['bytes']==raw,'original changed')
|
||||
client.call('GET','/api/v1/attachments/'+item['id'],404,actor=outsider)
|
||||
client.call('GET','/api/v1/attachments/'+item['id']+'/content?variant=crop',404,actor=outsider)
|
||||
client.call('GET','/api/v1/attachments/'+item['id']+'/content?variant=missing',404,actor=actor)
|
||||
results[kind]={'status':'PASS','variants':variants}
|
||||
original=(ROOT/'tests/fixtures/sample.png').read_bytes()
|
||||
for size in (6294458,10485760):
|
||||
extra=size-len(original)-12
|
||||
raw=original[:-12]+chunk(b'paDd',bytes(extra))+original[-12:]
|
||||
check(len(raw)==size,'fixture exact size')
|
||||
item=upload(client,actor,chat,raw)
|
||||
wait_state(client,actor,item)
|
||||
check(client.call('GET','/api/v1/attachments/'+item['id']+'/content',actor=actor)['bytes']==raw,'large original changed')
|
||||
path='/api/v1/chats/'+chat['id']+'/attachments?filename=bad.png'
|
||||
client.call('POST',path,413,actor=actor,raw=bytes(10485761),headers={'Content-Type':'image/png'})
|
||||
for raw in (original[:40],original[:-12],b'not an image'):
|
||||
client.call('POST',path,415,actor=actor,raw=raw,headers={'Content-Type':'image/png'})
|
||||
huge=b'\x89PNG\r\n\x1a\n'+chunk(b'IHDR',struct.pack('>IIBBBBB',20000,10000,8,2,0,0,0))+chunk(b'IEND',b'')
|
||||
client.call('POST',path,413,actor=actor,raw=huge,headers={'Content-Type':'image/png'})
|
||||
results['byte_limits_corrupt_pixel_limits']='PASS'
|
||||
item=upload(client,actor,chat)
|
||||
wait_state(client,actor,item)
|
||||
audit=compose('exec','-T','api1','/messenger','s3-audit','originals/'+item['id'])
|
||||
check('PASS' in audit,'S3 credential audit failed')
|
||||
results['private_bucket_limited_credentials']='PASS'
|
||||
metrics=json.loads(compose('exec','-T','worker','/messenger','memory'))
|
||||
check(metrics['memory.peak'] < metrics['memory.max'],'worker reached memory limit')
|
||||
results['worker_memory_bytes']=metrics
|
||||
dump_evidence('variants-grade4.json',results)
|
||||
print('PASS: actual JPEG/WebP, EXIF orientation, white alpha, bounds, ACL, checksums, private S3')
|
||||
|
||||
|
||||
if __name__=='__main__':
|
||||
run(main)
|
||||
@@ -0,0 +1,60 @@
|
||||
"""Bounded subprocess: trusted local paths, no URLs, no credentials."""
|
||||
import hashlib
|
||||
import json
|
||||
import resource
|
||||
import sys
|
||||
import warnings
|
||||
from pathlib import Path
|
||||
from PIL import Image, ImageOps, UnidentifiedImageError
|
||||
|
||||
# Bound decoder subprocess independently of container and Go memory.
|
||||
resource.setrlimit(resource.RLIMIT_AS, (1024 * 1024 * 1024, 1024 * 1024 * 1024))
|
||||
Image.MAX_IMAGE_PIXELS = 40_000_000
|
||||
warnings.simplefilter('error', Image.DecompressionBombWarning)
|
||||
|
||||
|
||||
def contain(image, bound):
|
||||
w, h = image.size
|
||||
if max(w, h) <= bound:
|
||||
return image.copy()
|
||||
if w >= h:
|
||||
size = (bound, max(1, h * bound // w))
|
||||
else:
|
||||
size = (max(1, w * bound // h), bound)
|
||||
return image.resize(size, Image.Resampling.LANCZOS)
|
||||
|
||||
|
||||
def main():
|
||||
source, target = map(Path, sys.argv[1:3])
|
||||
if source.stat().st_size > 10_485_760:
|
||||
raise ValueError('size')
|
||||
with Image.open(source) as original:
|
||||
if original.format not in ('PNG', 'JPEG') or original.width * original.height > 40_000_000:
|
||||
raise ValueError('format or pixels')
|
||||
original.load()
|
||||
oriented = ImageOps.exif_transpose(original)
|
||||
rgba = oriented.convert('RGBA')
|
||||
image = Image.new('RGB', rgba.size, 'white')
|
||||
image.paste(rgba, mask=rgba.getchannel('A'))
|
||||
# New pixel images + empty info ensure EXIF/ICC/text are never propagated.
|
||||
variants = []
|
||||
for name, result, kind, quality in (
|
||||
('thumbnail', contain(image, 256), 'JPEG', 85),
|
||||
('crop', ImageOps.fit(image, (128, 128), method=Image.Resampling.LANCZOS, centering=(.5, .5)), 'JPEG', 85),
|
||||
('optimized', contain(image, 1280), 'WEBP', 80),
|
||||
):
|
||||
result.info.clear()
|
||||
path = target / name
|
||||
result.save(path, format=kind, quality=quality, **({'method': 4} if kind == 'WEBP' else {}))
|
||||
data = path.read_bytes()
|
||||
variants.append(dict(name=name, content_type='image/webp' if kind == 'WEBP' else 'image/jpeg',
|
||||
size_bytes=len(data), width=result.width, height=result.height,
|
||||
sha256=hashlib.sha256(data).hexdigest()))
|
||||
print(json.dumps(variants))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
main()
|
||||
except (UnidentifiedImageError, OSError, ValueError, Image.DecompressionBombError, Image.DecompressionBombWarning):
|
||||
sys.exit(2) # permanent invalid_image; details never reach public API
|
||||
Reference in New Issue
Block a user