160 changed files with 16598 additions and 25 deletions
+6 -1
View File
@@ -2,12 +2,15 @@
# These are logical interface suggestions; map actual framework names in REPORT.md.
APP_HOST=0.0.0.0
APP_PORT=8080
LAB4_HTTP_PORT=8081
LAB4_POSTGRES_PORT=5433
APP_ENV=lab
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
REDIS_ADDR=redis:6379
CACHE_URL_FILE=/run/secrets/cache_url
API_REPLICAS=2
IDEMPOTENCY_TTL_SECONDS=86400
@@ -18,3 +21,5 @@ S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_access_key_id
S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret_access_key
MAX_UPLOAD_BYTES=10485760
MAX_IMAGE_PIXELS=40000000
# Set true to inspect candidates without deleting them.
LAB4_CLEANUP_DRY_RUN=false
+21
View File
@@ -0,0 +1,21 @@
name: Lab4 application
on: [push, pull_request]
permissions:
contents: read
jobs:
application:
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Start disposable stack
run: |
sh scripts/init-local.sh
docker compose --env-file .env up -d --build --wait --wait-timeout 120
- run: make test BASE_URL=http://127.0.0.1:8081
- name: Remove disposable stack
if: always()
run: docker compose --env-file .env down --volumes
+3
View File
@@ -30,3 +30,6 @@ coverage/
.vscode/
# Keep small, sanitized evidence summaries in evidence/ under version control.
# Inspect git diff --cached: ignore rules are not a secret scanner.
# Transient local test runs; publish selected evidence under evidence/.
student-tests/results/
+37
View File
@@ -0,0 +1,37 @@
FROM golang:1.27.1-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY cmd/api/ ./cmd/api/
COPY cmd/attachment-cleanup/ ./cmd/attachment-cleanup/
COPY internal/ ./internal/
RUN CGO_ENABLED=0 GOOS=linux go build -o /app/server ./cmd/api
RUN CGO_ENABLED=0 GOOS=linux go build -o /app/attachment-cleanup ./cmd/attachment-cleanup
FROM alpine:3.22 AS runtime
RUN addgroup -S app && adduser -S app -G app
WORKDIR /app
USER app
FROM runtime AS cleanup
COPY --from=builder /app/attachment-cleanup ./attachment-cleanup
CMD ["./attachment-cleanup"]
FROM runtime AS api
COPY --from=builder /app/server ./server
EXPOSE 8080
HEALTHCHECK --interval=10s --timeout=3s --start-period=3s --retries=3 \
CMD wget -q -O /dev/null http://127.0.0.1:8080/health/ready || exit 1
CMD ["./server"]
+19
View File
@@ -0,0 +1,19 @@
FROM golang:1.27.1-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY cmd/migrate/ ./cmd/migrate/
COPY db/migrations/ ./db/migrations/
RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -o /app/migrate ./cmd/migrate
FROM alpine:3.22
RUN addgroup -S app && adduser -S app -G app
WORKDIR /app
COPY --from=builder /app/migrate ./migrate
USER app
ENTRYPOINT ["/app/migrate"]
+72 -24
View File
@@ -1,37 +1,85 @@
# Отчёт — лабораторная 4
# Лабораторная 4. Вложения в S3 и сверка хранилищ
- Автор, группа: TODO
- Целевая оценка; выбранный трек 5, если нужен: TODO
- Commit/PR: TODO после публикации
- Использованные источники, библиотеки и помощь ИИ: TODO
**Ремез Олег Романович · БПИ2304 · цель «5» · трек 2, согласованность S3 и PostgreSQL**
## Запуск из чистого clone
Мессенджер принимает PNG и JPEG, связывает оригиналы с сообщениями и отдаёт их участникам чата через любую из двух API-реплик. Приватный RustFS хранит байты, PostgreSQL — метаданные и связи, Redis — сессии. Команда `attachment-cleanup` обнаруживает расхождения в обе стороны. Старые объекты без строки удаляются после сверки, а пропавший оригинал восстанавливается из копии с сохранением ID и связей.
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
## Запуск
## Архитектура и решения
Требуются Docker с Compose и OpenSSL. Скрипт подготовки создаёт локальные `.env` и `secrets/` при их отсутствии.
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab3, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
```sh
sh scripts/init-local.sh
docker compose --env-file .env up -d --build --wait
make check
make test BASE_URL=http://127.0.0.1:8081
```
## Доказательства критериев
API доступен через HAProxy на `127.0.0.1:8081`. Перезапуск стенда с сохранением volumes выполняется через `sh scripts/restart.sh`. Остановка выполняется через `docker compose --env-file .env down`. Для экспериментов используйте отдельный Compose-проект с собственными портами и синтетическими данными.
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
| --- | --- | --- | --- |
| Публичный smoke | make test | TODO | TODO |
| Остальные пункты уровня 3 | TODO | TODO | TODO |
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
## Как работает загрузка и периодическая уборка
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
Обработчик принимает один PNG/JPEG до 10 МиБ и 40 млн пикселей. Тело копируется частями во временный файл на `tmpfs` с лимитом 32 МиБ на контейнер. Код закрывает дескриптор при завершении запроса, SHA-256 вычисляется по оригиналу, а S3 получает файл потоком. Каждая API-реплика обрабатывает одну загрузку одновременно. Запрос, который ждёт слот дольше секунды, получает `429` с `Retry-After: 1`. Проверка изображения декодирует его пиксели, поэтому ограничение по пикселям и слоту существенно для памяти.
## Эксперименты и отказы
API возвращает `201 ready` после записи оригинала в S3 и COMMIT в PostgreSQL. При обрыве между ними объект может остаться без метаданных. Уборка просматривает `attachments/` каждые 5 минут, проверяет только объекты старше 10 минут по S3 `LastModified` и удаляет их после успешного подтверждения отсутствия строки. Загрузка, уборка и восстановление используют одну транзакционную блокировку PostgreSQL по ключу. Занятый ключ переносится на следующий проход. Строка в `attachments` защищает оригинал с момента загрузки, в том числе до его привязки к сообщению. Ошибка хранилища прерывает проход.
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
## Трек 2. Сверка в обе стороны
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
| --- | --- | --- | --- |
| TODO | TODO | TODO | TODO |
**Гипотеза.** По старым ключам можно находить два вида расхождений без загрузки байтов при обходе. Удаление неподтверждённого объекта и восстановление проверенного оригинала должны сохранять исправные вложения. Повторная сверка после исправления должна показать ноль расхождений. При росте числа исправных пар растут число проверок и время обхода.
## Ограничения и следующие шаги
| Направление | Проверка | Действие |
| --- | --- | --- |
| S3 → PostgreSQL | Постраничный LIST, фильтр по возрасту, EXISTS под блокировкой | В dry-run только посчитать кандидата. В apply удалить старый объект при успешном EXISTS, вернувшем `false` |
| PostgreSQL → S3 | Страницы по уникальному `object_key`, HEAD для подходящих по возрасту строк | Показать число пропаж и до 32 ID. Для выбранного ID принять копию, проверить размер и SHA-256, перечитать метаданные под блокировкой и выполнить условный PUT |
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
Для автоматической уборки раз в 5 минут используется направление S3 → PostgreSQL. Полный двусторонний проход запускается явно и печатает JSON со счётчиками, длительностью и признаком завершения. Dry-run включён по умолчанию у разовой команды. Скан пропаж различает `404` от HEAD и сбой доступа. При сетевой ошибке или `403` проход завершается с ошибкой. Проверка существования бакета выполняется до HEAD объектов. [S3 HeadObject](https://docs.aws.amazon.com/AmazonS3/latest/API/API_HeadObject.html)
```sh
# Двусторонний dry-run
docker compose --env-file .env run --rm --no-deps -T attachment-cleanup \
/app/attachment-cleanup -reconcile
# Двусторонняя сверка с удалением старых объектов без строки
docker compose --env-file .env run --rm --no-deps -T attachment-cleanup \
/app/attachment-cleanup -reconcile -dry-run=false
# Dry-run восстановления одного пропавшего оригинала из проверяемой копии
docker compose --env-file .env run --rm --no-deps -T \
-v "$PWD/original.png:/backup:ro" attachment-cleanup \
/app/attachment-cleanup -restore-id UUID -backup-file /backup
```
Для восстановления добавьте `-dry-run=false` к последней команде. Запись отправляется с `If-None-Match: *`: если ключ уже существует, приложение его сохраняет, а S3 отклоняет конфликтующую запись, появившуюся после HEAD. При повторе с тем же оригиналом команда возвращает `already_present`. Источник резервных копий выбирает оператор. [Условная запись S3](https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html)
### Эксперимент и результаты
На отдельном стенде через API загружены четыре PNG. Затем у одного файла удалена строка PostgreSQL, у другого — объект S3. Пропавший оригинал был привязан к сообщению. После истечения тестового grace в 4 секунды dry-run нашёл по одному расхождению и оставил оба состояния без изменений. Копия с неверным SHA-256 отклонена. Верная копия успешно восстановлена, повтор вернул `already_present`. Прямой условный PUT по уже занятому ключу получил `412`. Проход apply удалил только старый объект без строки. Свежий объект и оба исправных файла, со связью и без неё, сохранились.
После исправлений повторная сверка нашла ноль расхождений. У восстановленного файла совпали ID, метаданные, SHA-256 и ссылка в истории сообщений. Посторонний получил `404`. При имитации отказа S3 проход завершился с ошибкой, счётчик пропавших объектов остался нулевым.
Затем добавлялись синтетические пары ключ/строка с однобайтовыми объектами. Для каждой выборки выполнены три dry-run без сторонней пользовательской нагрузки. В таблице указана длительность внутри процесса, отдельно от старта контейнера. Число HEAD и число проверок EXISTS в каждом проходе равны числу пар.
| Пар | Страниц S3 / БД | Три прохода, мс | Медиана, мс |
| ---: | :--- | :--- | ---: |
| 33 | 1 / 1 | 82, 100, 32 | 82 |
| 233 | 1 / 2 | 227, 132, 147 | 147 |
| 1053 | 2 / 6 | 629, 587, 3070 | 629 |
Объём запросов растёт вместе с числом ключей. Наибольший профиль пересёк границу страницы в обоих хранилищах. Разброс 587–3070 мс показывает, что три прогона на одном Docker-хосте дают ориентир, а предельную производительность надо измерять отдельно. [Исходные измерения](evidence/lab4-grade5/reconcile.json) · [Сценарий](student-tests/lab4/reconcile_experiment.py) · [Методика запуска](evidence/lab4-grade5/README.md)
## Проверки уровня 4
| Проверка | Наблюдение |
| --- | --- |
| Сборка, миграции и совместимость API | Чистый Compose запускает миграции 1–6, RustFS и обе API. Повторная генерация sqlc совпала с кодом в ветке. Публичный smoke и 54 группы Go-тестов прошли на выделенном PostgreSQL и Redis без пропусков, включая штатные перезапуски. [Запуск и результаты](evidence/lab4-grade5/README.md) |
| Отказ API и перезапуск | Другая реплика отдала оригинал с тем же SHA-256. Перезапуск сохранил данные и сессию. Мигратор и bootstrap при штатном перезапуске не запустились повторно. [Переключение файла](evidence/lab4-final/file-failover.json) · [До](evidence/lab4-final/restart-before.txt) · [После](evidence/lab4-final/restart-after.txt) |
| Ошибки и приватность | Размер и число пикселей сверх лимита — `413`, повреждённое изображение и подмена MIME — `415`, чужой файл и привязка из другого чата — `404`. Анонимные GET/LIST S3 — `403`, ключ API ограничен бакетом, консоль закрыта. После повторного bootstrap объект сохранился. [HTTP](evidence/lab4-final/attachment-http.json) · [Пиксели](evidence/lab4-final/pixel-limit.json) · [Политика](evidence/lab4-final/s3-security.jsonl) |
| Отказ S3 и обрыв загрузки | У API `live=200`, `ready=503`, загрузка — `503`, новые метаданные не появились. После восстановления файл совпал по SHA-256. При обрыве HTTP, SIGKILL после PUT и ошибке INSERT после PUT уборка прошла; подтверждённая строка после потери ответа сохранилась. [Отказ S3](evidence/lab4-final/s3-failure.json) · [Сценарии уборки](evidence/lab4-grade5/compensation.json) |
В двух прогонах по 12 одновременно запущенных загрузок лимит RAM составлял 512 МиБ на API. Обычный PNG 320 × 200 дал 12 ответов `201` с пиками 78,44 и 76,95 МиБ. При PNG/JPEG 6000 × 6000 четыре запроса получили `201`, восемь — `429`. Пики 280,59 и 410,29 МиБ включают процесс и `tmpfs`. OOM и перезапусков в обоих прогонах не было. [Обычный профиль](evidence/lab4-final/memory-normal.json) · [Тяжёлый профиль](evidence/lab4-final/memory-heavy.json) · [Методика](evidence/lab4-final/README.md)
## Границы и источники
Восстановление требует оригинальных байтов из резервной копии. HEAD подтверждает наличие объекта на момент запроса. Проверка целостности всех существующих объектов, восстановление потерянной строки БД и резервное копирование выходят за рамки трека. При ошибке или превышении двухминутного лимита проход завершится как неполный; уже выполненные удаления сохраняются. Сверка работает по текущим страницам, поэтому новые данные могут попасть в следующий проход. Стенд проверен на одном Docker-хосте, PostgreSQL, Redis и RustFS представлены по одному экземпляру.
Gitea Actions для форка и базового репозитория отключены. Проверки выполнены локально. Публичные контракты и тесты преподавателя сохранены. Использован ИИ для проектирования, реализации части Go и Compose, тестовой оснастки и оформления отчёта. [Блокировки PostgreSQL](https://www.postgresql.org/docs/18/explicit-locking.html#ADVISORY-LOCKS) · [RustFS credentials](https://docs.rustfs.com/en/operations/credentials)
+155
View File
@@ -0,0 +1,155 @@
package main
import (
"context"
"errors"
"highload/internal/config"
"highload/internal/database"
"highload/internal/database/sqlc"
"highload/internal/repository/postgres"
"highload/internal/repository/redis"
"highload/internal/service"
"highload/internal/storage/s3"
httpdeliver "highload/internal/transport/http"
"highload/internal/transport/http/handler"
"highload/internal/transport/http/middleware"
"log/slog"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
cfg, err := config.Load()
if err != nil {
slog.Error("invalid configuration", "error", err)
os.Exit(1)
}
sessionStore := cfg.Session.Store
if sessionStore != "redis" {
slog.Error("session store is "+sessionStore+"must redis", "error", nil)
os.Exit(1)
}
databaseURL := cfg.Database.PostgresURL
pool, err := database.NewPostgres(context.Background(), databaseURL)
if err != nil {
slog.Error("postgres error", "error", err)
os.Exit(1)
}
defer pool.Close()
if os.Getenv("PG_POOL_DIAGNOSTICS") == "1" {
diagnosticsCtx, stopDiagnostics := context.WithCancel(context.Background())
defer stopDiagnostics()
go logPoolStats(diagnosticsCtx, pool)
}
queries := sqlc.New(pool)
redisAddr := cfg.Database.RedisAddr
redisClient, err := database.NewRedis(context.Background(), redisAddr)
if err != nil {
slog.Error("redis error", "error", err)
os.Exit(1)
}
defer redisClient.Close()
instanceID, err := os.Hostname()
if err != nil || instanceID == "" {
slog.Error("cannot get instance ID", "error", err)
os.Exit(1)
}
s3Storage, err := s3.NewStorage(context.Background(), &cfg.S3)
if err != nil {
slog.Error("cannot create new storage", "error", err)
os.Exit(1)
}
userRepo := postgres.NewUserRepository(queries)
chatRepo := postgres.NewChatRepository(queries, pool)
messageRepo := postgres.NewMessageRepository(queries, pool)
sessionRepo := redis.NewSessionRepository(redisClient)
attachmentRepo := postgres.NewAttachmentRepository(queries, pool)
authService := service.NewAuthService(userRepo, sessionRepo, cfg.Session.TTL)
userService := service.NewUserService(userRepo)
chatService := service.NewChatService(chatRepo, userRepo, userService)
messageService := service.NewMessageService(messageRepo, chatService, userService)
attachmentService := service.NewAttachmentService(attachmentRepo, chatService, s3Storage)
healthHandler := handler.NewHealthHandler(pool, redisClient, s3Storage)
authHandler := handler.NewAuthHandler(authService)
userHandler := handler.NewUserHandler(userService)
chatHandler := handler.NewChatHandler(chatService)
messageHandler := handler.NewMessageHandler(messageService)
attachmentHandler := handler.NewAttachmentHandler(attachmentService)
mid := middleware.NewMiddleware(authService, instanceID)
router := httpdeliver.NewRouter(mid, authHandler, healthHandler, chatHandler, userHandler, messageHandler, attachmentHandler)
server := &http.Server{
Addr: net.JoinHostPort(cfg.HTTP.Host, cfg.HTTP.Port),
Handler: router.Handler(),
ReadHeaderTimeout: 5 * time.Second,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 30 * time.Second,
}
listener, err := net.Listen("tcp", server.Addr)
if err != nil {
slog.Error("server failed to start", "error", err)
return
}
slog.Info("server started", "addr", server.Addr)
serverErr := make(chan error, 1)
go func() {
err := server.Serve(listener)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
serverErr <- err
slog.Error("server failed", "error", err)
}
}()
ctx, stop := signal.NotifyContext(
context.Background(),
os.Interrupt,
syscall.SIGTERM,
)
defer stop()
select {
case <-ctx.Done():
slog.Info("shutdown signal received")
case err := <-serverErr:
slog.Error("server failed", "error", err)
return
}
shutdownCtx, cancel := context.WithTimeout(
context.Background(),
15*time.Second,
)
defer cancel()
if err := server.Shutdown(shutdownCtx); err != nil {
slog.Error("shutdown failed", "error", err)
}
}
+29
View File
@@ -0,0 +1,29 @@
package main
import (
"context"
"log/slog"
"time"
"github.com/jackc/pgx/v5/pgxpool"
)
// logPoolStats is opt-in diagnostics for the isolated scaling experiment.
// It contains only aggregate counters, never connection strings or SQL values.
func logPoolStats(ctx context.Context, pool *pgxpool.Pool) {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
s := pool.Stat()
slog.Info("pg_pool_stats", "max_conns", s.MaxConns(),
"acquired", s.AcquiredConns(), "idle", s.IdleConns(),
"acquire_count", s.AcquireCount(), "empty_count", s.EmptyAcquireCount(),
"empty_wait_ns", s.EmptyAcquireWaitTime().Nanoseconds(),
"canceled_count", s.CanceledAcquireCount())
}
}
}
+164
View File
@@ -0,0 +1,164 @@
package main
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"highload/internal/config"
"highload/internal/database"
"highload/internal/database/sqlc"
"highload/internal/repository/postgres"
"highload/internal/service"
"highload/internal/storage/s3"
"log/slog"
"os"
"os/signal"
"syscall"
"time"
"github.com/jackc/pgx/v5/pgconn"
)
type cleanupConfig struct {
once bool
dryRun bool
gracePeriod time.Duration
interval time.Duration
passTimeout time.Duration
reconcile bool
restoreID string
backupFile string
}
func main() {
var cfg cleanupConfig
flag.BoolVar(&cfg.once, "once", false, "Run once and exit")
flag.BoolVar(&cfg.dryRun, "dry-run", true, "Do not actually delete files")
flag.DurationVar(&cfg.gracePeriod, "grace-period", 10*time.Minute, "Grace period for old files")
flag.DurationVar(&cfg.interval, "interval", 5*time.Minute, "Interval between runs")
flag.DurationVar(&cfg.passTimeout, "pass-timeout", 2*time.Minute, "Timeout for a single pass")
flag.BoolVar(&cfg.reconcile, "reconcile", false, "Scan both stores once and print JSON observations")
flag.StringVar(&cfg.restoreID, "restore-id", "", "Restore the missing original for this attachment ID")
flag.StringVar(&cfg.backupFile, "backup-file", "", "Path to the original backup used for restore")
flag.Parse()
if cfg.gracePeriod <= 0 || cfg.interval <= 0 || cfg.passTimeout <= 0 {
slog.Error("all duration flags must be positive")
os.Exit(1)
}
if (cfg.restoreID == "") != (cfg.backupFile == "") || (cfg.reconcile && cfg.restoreID != "") {
slog.Error("use -reconcile alone, or supply both -restore-id and -backup-file")
os.Exit(1)
}
if err := run(cfg); err != nil {
slog.Error("attachment cleanup failed", "error", err)
os.Exit(1)
}
}
func run(cfg cleanupConfig) error {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
databaseURL, s3Config, err := config.LoadAttachmentCleanup()
if err != nil {
return fmt.Errorf("load cleanup configuration: %w", err)
}
startupCtx, cancelStartup := context.WithTimeout(ctx, 10*time.Second)
defer cancelStartup()
pool, err := database.NewPostgres(startupCtx, databaseURL)
if err != nil {
var configErr *pgconn.ParseConfigError
if errors.As(err, &configErr) {
return errors.New("invalid PostgreSQL connection string")
}
return fmt.Errorf("connect to PostgreSQL: %w", err)
}
defer pool.Close()
s3Storage, err := s3.NewStorage(startupCtx, &s3Config)
if err != nil {
return fmt.Errorf("create S3 client: %w", err)
}
cancelStartup()
attachmentRepo := postgres.NewAttachmentRepository(sqlc.New(pool), pool)
options := &service.AttachmentCleanupOptions{
GracePeriod: cfg.gracePeriod,
DryRun: cfg.dryRun,
ListTimeout: 5 * time.Second,
}
attachmentCleanupService := service.NewAttachmentCleanupService(attachmentRepo, s3Storage, options)
if cfg.reconcile || cfg.restoreID != "" {
passCtx, cancel := context.WithTimeout(ctx, cfg.passTimeout)
defer cancel()
reconciler := service.NewAttachmentReconciler(attachmentRepo, s3Storage, *options)
start := time.Now()
report := struct {
DryRun bool `json:"dry_run"`
DurationMS int64 `json:"duration_ms"`
Complete bool `json:"complete"`
Orphans *service.OrphanReport `json:"orphan_scan,omitempty"`
Missing *service.MissingReport `json:"missing_scan,omitempty"`
Restore string `json:"restore,omitempty"`
}{DryRun: cfg.dryRun}
var actionErr error
if cfg.reconcile {
missing, err := reconciler.ScanMissing(passCtx)
report.Missing = &missing
actionErr = err
if err == nil {
orphans, err := attachmentCleanupService.ScanOrphans(passCtx)
report.Orphans = &orphans
actionErr = err
}
} else {
file, err := os.Open(cfg.backupFile)
if err != nil {
return fmt.Errorf("open backup original: %w", err)
}
defer file.Close()
report.Restore, actionErr = reconciler.Restore(passCtx, cfg.restoreID, file)
}
report.DurationMS = time.Since(start).Milliseconds()
report.Complete = actionErr == nil
return errors.Join(actionErr, json.NewEncoder(os.Stdout).Encode(report))
}
runPass := func() error {
passCtx, cancelPass := context.WithTimeout(ctx, cfg.passTimeout)
defer cancelPass()
return attachmentCleanupService.RunOnce(passCtx)
}
if cfg.once {
return runPass()
}
if err := runPass(); err != nil && ctx.Err() == nil {
slog.Error("attachment cleanup pass failed", "error", err)
}
ticker := time.NewTicker(cfg.interval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return nil
case <-ticker.C:
if ctx.Err() != nil {
return nil
}
if err := runPass(); err != nil && ctx.Err() == nil {
slog.Error("attachment cleanup pass failed", "error", err)
}
}
}
}
+152
View File
@@ -0,0 +1,152 @@
package main
import (
"context"
"database/sql"
"errors"
"flag"
"log/slog"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"highload/db/migrations"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/stdlib"
"github.com/pressly/goose/v3"
"github.com/pressly/goose/v3/lock"
)
// Migration errors retain their cause for classification, but deliberately do
// not print connection strings, PostgreSQL DETAIL fields or raw SQL.
type migrationError struct {
stage string
cause error
}
func (e *migrationError) Error() string { return e.stage }
func (e *migrationError) Unwrap() error { return e.cause }
type boundedSessionLocker struct {
lock.SessionLocker
}
func (l boundedSessionLocker) SessionUnlock(ctx context.Context, conn *sql.Conn) error {
// Goose unlocks with a detached context; bound this cleanup separately.
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
return l.SessionLocker.SessionUnlock(ctx, conn)
}
func main() {
command := flag.String("command", "up", "migration action: up or status")
timeout := flag.Duration("timeout", 2*time.Minute, "total migration/connection/lock budget")
flag.Parse()
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
if flag.NArg() != 0 || (*command != "up" && *command != "status") || *timeout < time.Second {
logger.Error("invalid migration arguments", "allowed_commands", "up,status",
"minimum_timeout", "1s")
os.Exit(1)
}
signalCtx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
ctx, cancel := context.WithTimeout(signalCtx, *timeout)
defer cancel()
if err := run(ctx, *command, *timeout, logger); err != nil {
fields := []any{"stage", err.Error(), "timeout", errors.Is(err, context.DeadlineExceeded),
"cancelled", errors.Is(err, context.Canceled)}
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
fields = append(fields, "sqlstate", pgErr.Code)
}
var partial *goose.PartialError
if errors.As(err, &partial) && partial.Failed != nil && partial.Failed.Source != nil {
fields = append(fields, "migration", partial.Failed.Source.Path,
"version", partial.Failed.Source.Version)
}
logger.Error("migration failed", fields...)
os.Exit(1)
}
}
func run(ctx context.Context, command string, timeout time.Duration, logger *slog.Logger) error {
fail := func(stage string, err error) error {
return &migrationError{stage: stage, cause: err}
}
path := os.Getenv("DATABASE_URL_FILE")
if path == "" {
path = "/run/secrets/database_url"
}
data, err := os.ReadFile(path)
if err != nil {
return fail("read database secret file", err)
}
url := strings.TrimSpace(string(data))
if url == "" {
return fail("database secret file is empty", errors.New("empty database URL"))
}
cfg, err := pgx.ParseConfig(url)
if err != nil {
return fail("parse database secret", err)
}
cfg.ConnectTimeout = 5 * time.Second
cfg.RuntimeParams["application_name"] = "highload-migrate"
cfg.RuntimeParams["lock_timeout"] = "10000"
cfg.RuntimeParams["statement_timeout"] = strconv.FormatInt(min(timeout.Milliseconds(), 2147483647), 10)
db := stdlib.OpenDB(*cfg)
db.SetMaxOpenConns(2)
db.SetMaxIdleConns(2)
db.SetConnMaxLifetime(5 * time.Minute)
defer db.Close()
if err := db.PingContext(ctx); err != nil {
return fail("connect to PostgreSQL", err)
}
locker, err := lock.NewPostgresSessionLocker(
lock.WithLockTimeout(1, 120),
lock.WithUnlockTimeout(1, 5),
)
if err != nil {
return fail("configure migration lock", err)
}
provider, err := goose.NewProvider(goose.DialectPostgres, db, migrations.Files,
goose.WithSessionLocker(boundedSessionLocker{locker}),
goose.WithDisableGlobalRegistry(true),
goose.WithSlog(logger),
// Goose verbose logging includes SQL statements. Log only selected
// migration metadata ourselves, even when a migration fails.
goose.WithVerbose(false),
)
if err != nil {
return fail("load embedded migrations", err)
}
if command == "status" {
statuses, err := provider.Status(ctx)
if err != nil {
return fail("read migration status", err)
}
for _, status := range statuses {
logger.Info("migration status", "file", status.Source.Path,
"version", status.Source.Version, "state", status.State)
}
return nil
}
started := time.Now()
results, err := provider.Up(ctx)
if err != nil {
return fail("apply pending migrations", err)
}
for _, result := range results {
logger.Info("migration applied", "file", result.Source.Path,
"version", result.Source.Version, "duration_seconds", result.Duration.Seconds())
}
logger.Info("migrations completed", "applied", len(results),
"duration_seconds", time.Since(started).Seconds())
return nil
}
+111
View File
@@ -0,0 +1,111 @@
package main
import (
"context"
"database/sql"
"errors"
"io"
"log/slog"
"os"
"path/filepath"
"strings"
"testing"
"time"
"highload/db/migrations"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/stdlib"
"github.com/pressly/goose/v3"
)
func TestConnectionSecretErrorsAreSafe(t *testing.T) {
for _, test := range []struct {
name string
content string
missing bool
}{
{name: "missing", missing: true},
{name: "empty", content: " \n\t"},
{name: "malformed", content: "postgres://synthetic:fixture-password@localhost/%zz"},
} {
t.Run(test.name, func(t *testing.T) {
path := filepath.Join(t.TempDir(), "database_url")
if !test.missing {
if err := os.WriteFile(path, []byte(test.content), 0600); err != nil {
t.Fatal("cannot create test secret")
}
}
t.Setenv("DATABASE_URL_FILE", path)
logger := slog.New(slog.NewJSONHandler(io.Discard, nil))
err := run(context.Background(), "up", time.Second, logger)
if err == nil {
t.Fatal("invalid connection secret was accepted")
}
if strings.Contains(err.Error(), "fixture-password") ||
strings.Contains(err.Error(), "postgres://") {
t.Fatal("migration error exposed a connection secret")
}
var classified *migrationError
if !errors.As(err, &classified) || classified.Unwrap() == nil {
t.Fatal("migration error lost its classification/cause")
}
})
}
}
func TestEmbeddedMigrationsAreVersionedAndCollectable(t *testing.T) {
cfg, err := pgx.ParseConfig("postgres://synthetic@localhost/synthetic?sslmode=disable")
if err != nil {
t.Fatal("cannot configure test driver")
}
db := stdlib.OpenDB(*cfg)
defer db.Close()
// NewProvider collects sources without connecting to PostgreSQL.
provider, err := goose.NewProvider(goose.DialectPostgres, db, migrations.Files)
if err != nil {
t.Fatal("embedded migration names/versions cannot be collected by Goose")
}
sources := provider.ListSources()
if len(sources) == 0 {
t.Fatal("migrator build contains no SQL")
}
var previous int64
for _, source := range sources {
if source.Version <= previous {
t.Fatal("embedded versions are not unique and increasing")
}
previous = source.Version
data, err := migrations.Files.ReadFile(source.Path)
if err != nil || !strings.Contains(string(data), "-- +goose Up") {
t.Fatal("embedded migration is unavailable or has no Up section")
}
}
}
type deadlineProbe struct {
deadline time.Time
cancel error
}
func (*deadlineProbe) SessionLock(context.Context, *sql.Conn) error { return nil }
func (p *deadlineProbe) SessionUnlock(ctx context.Context, _ *sql.Conn) error {
p.deadline, _ = ctx.Deadline()
p.cancel = ctx.Err()
return nil
}
func TestUnlockHasBudgetEvenWithDetachedContext(t *testing.T) {
original, cancel := context.WithCancel(context.Background())
cancel()
probe := &deadlineProbe{}
locker := boundedSessionLocker{probe}
started := time.Now()
if err := locker.SessionUnlock(context.WithoutCancel(original), nil); err != nil {
t.Fatal("unlock delegation failed")
}
if probe.cancel != nil || probe.deadline.IsZero() ||
probe.deadline.Before(started) || probe.deadline.After(started.Add(6*time.Second)) {
t.Fatal("detached cleanup did not receive a bounded, live context")
}
}
+212
View File
@@ -0,0 +1,212 @@
package main
import (
"bytes"
"context"
"database/sql"
"encoding/json"
"errors"
"log/slog"
"net/url"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/stdlib"
)
// These tests need an explicitly supplied disposable PostgreSQL database.
// Each test creates and drops only its own randomly named schema.
func migrationPostgresFixture(t *testing.T) (*sql.DB, context.Context) {
t.Helper()
connectionURL := os.Getenv("MIGRATE_TEST_DATABASE_URL")
if connectionURL == "" {
t.Skip("set MIGRATE_TEST_DATABASE_URL to run PostgreSQL integration tests")
}
cfg, err := pgx.ParseConfig(connectionURL)
if err != nil {
t.Fatal("invalid test database configuration")
}
cfg.ConnectTimeout = 5 * time.Second
admin := stdlib.OpenDB(*cfg)
t.Cleanup(func() { admin.Close() })
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
t.Cleanup(cancel)
schema := "migration_test_" + strings.ReplaceAll(uuid.NewString(), "-", "")
quoted := pgx.Identifier{schema}.Sanitize()
if _, err := admin.ExecContext(ctx, "CREATE SCHEMA "+quoted); err != nil {
t.Fatal("cannot create isolated migration test schema")
}
t.Cleanup(func() {
cleanupCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if _, err := admin.ExecContext(cleanupCtx, "DROP SCHEMA "+quoted+" CASCADE"); err != nil {
t.Error("cannot remove isolated migration test schema")
}
})
cfg.RuntimeParams["search_path"] = schema
// ConnString retains the original parsed URL; changing RuntimeParams alone
// would not pass search_path to the separate migrator connection.
scopedURL, err := url.Parse(connectionURL)
if err != nil || (scopedURL.Scheme != "postgres" && scopedURL.Scheme != "postgresql") {
t.Fatal("MIGRATE_TEST_DATABASE_URL must be a PostgreSQL URL")
}
query := scopedURL.Query()
query.Set("search_path", schema)
scopedURL.RawQuery = query.Encode()
path := filepath.Join(t.TempDir(), "database_url")
if err := os.WriteFile(path, []byte(scopedURL.String()), 0600); err != nil {
t.Fatal("cannot write test connection secret")
}
t.Setenv("DATABASE_URL_FILE", path)
db := stdlib.OpenDB(*cfg)
t.Cleanup(func() { db.Close() })
return db, ctx
}
func migrationLogRecords(t *testing.T, output *bytes.Buffer) []map[string]any {
t.Helper()
var records []map[string]any
for _, line := range bytes.Split(bytes.TrimSpace(output.Bytes()), []byte("\n")) {
if len(line) == 0 {
continue
}
var record map[string]any
if err := json.Unmarshal(line, &record); err != nil {
t.Fatal("migrator emitted a non-JSON log record")
}
for key := range record {
switch key {
case "time", "level", "msg", "file", "version", "state", "applied", "duration_seconds":
default:
t.Fatalf("unexpected log field %q; SQL or connection details must not be logged", key)
}
}
if strings.Contains(string(line), "CREATE TABLE") || strings.Contains(string(line), "postgres://") {
t.Fatal("migration log contains SQL or a connection URL")
}
records = append(records, record)
}
return records
}
func completedMigrationCount(t *testing.T, output *bytes.Buffer) int {
t.Helper()
for _, record := range migrationLogRecords(t, output) {
if record["msg"] == "migrations completed" {
count, ok := record["applied"].(float64)
if !ok {
t.Fatal("migration completion log has no numeric applied count")
}
return int(count)
}
}
t.Fatal("successful migration run has no completion log")
return 0
}
const lab4MigrationCount = 6
func TestPostgresMigrationsRepeatWithoutChangingDataOrLoggingSQL(t *testing.T) {
db, ctx := migrationPostgresFixture(t)
var output bytes.Buffer
logger := slog.New(slog.NewJSONHandler(&output, nil))
if err := run(ctx, "up", time.Minute, logger); err != nil {
t.Fatal("migration of empty schema failed")
}
if count := completedMigrationCount(t, &output); count != lab4MigrationCount {
t.Fatalf("empty schema: applied %d migrations, want %d", count, lab4MigrationCount)
}
if _, err := db.ExecContext(ctx, "INSERT INTO users (username, display_name, password_hash) VALUES ($1, $2, $3)",
"migration_fixture", "Migration fixture", "synthetic-test-only-hash"); err != nil {
t.Fatal("cannot insert persistent migration fixture")
}
output.Reset()
if err := run(ctx, "up", time.Minute, logger); err != nil {
t.Fatal("repeat migration failed")
}
if count := completedMigrationCount(t, &output); count != 0 {
t.Fatalf("repeat run: applied %d migrations, want 0", count)
}
var users, versions int
if err := db.QueryRowContext(ctx, "SELECT count(*) FROM users WHERE username = $1", "migration_fixture").Scan(&users); err != nil || users != 1 {
t.Fatal("repeat migration lost or duplicated existing data")
}
if err := db.QueryRowContext(ctx, "SELECT count(*) FROM goose_db_version WHERE version_id > 0 AND is_applied").Scan(&versions); err != nil || versions != lab4MigrationCount {
t.Fatal("repeat migration changed version history")
}
output.Reset()
if err := run(ctx, "status", time.Minute, logger); err != nil {
t.Fatal("migration status failed")
}
records := migrationLogRecords(t, &output)
if len(records) != lab4MigrationCount {
t.Fatal("status did not report all six migrations")
}
for _, record := range records {
if record["state"] != "applied" {
t.Fatal("status did not report applied migrations")
}
}
}
func TestPostgresConcurrentMigratorsApplyEachVersionOnce(t *testing.T) {
db, ctx := migrationPostgresFixture(t)
var outputs [2]bytes.Buffer
var runErrors [2]error
var workers sync.WaitGroup
start := make(chan struct{})
for i := range outputs {
workers.Add(1)
go func(i int) {
defer workers.Done()
<-start
runErrors[i] = run(ctx, "up", time.Minute, slog.New(slog.NewJSONHandler(&outputs[i], nil)))
}(i)
}
close(start)
workers.Wait()
var applied int
for i, err := range runErrors {
if err != nil {
t.Fatal("concurrent migration run failed")
}
applied += completedMigrationCount(t, &outputs[i])
}
if applied != lab4MigrationCount {
t.Fatalf("concurrent runners applied %d migrations in total, want %d", applied, lab4MigrationCount)
}
var versions, distinctVersions int
if err := db.QueryRowContext(ctx, "SELECT count(*), count(DISTINCT version_id) FROM goose_db_version WHERE version_id > 0 AND is_applied").Scan(&versions, &distinctVersions); err != nil || versions != lab4MigrationCount || distinctVersions != lab4MigrationCount {
t.Fatal("concurrent runners duplicated or lost version history")
}
}
func TestPostgresMigrationFailureRollsBackAndPreservesCause(t *testing.T) {
db, ctx := migrationPostgresFixture(t)
// The first migration creates users, then conflicts with this chats table.
if _, err := db.ExecContext(ctx, "CREATE TABLE chats (marker text)"); err != nil {
t.Fatal("cannot create conflicting test table")
}
var output bytes.Buffer
err := run(ctx, "up", time.Minute, slog.New(slog.NewJSONHandler(&output, nil)))
var pgErr *pgconn.PgError
if err == nil || err.Error() != "apply pending migrations" || !errors.As(err, &pgErr) || pgErr.Code != "42P07" {
t.Fatal("migration failure did not retain a safely reportable duplicate-table error")
}
migrationLogRecords(t, &output)
var usersAbsent bool
if err := db.QueryRowContext(ctx, "SELECT to_regclass('users') IS NULL").Scan(&usersAbsent); err != nil || !usersAbsent {
t.Fatal("failed migration left a partially applied users table")
}
var versions int
if err := db.QueryRowContext(ctx, "SELECT count(*) FROM goose_db_version WHERE version_id > 0 AND is_applied").Scan(&versions); err != nil || versions != 0 {
t.Fatal("failed migration was recorded as applied")
}
}
+167
View File
@@ -0,0 +1,167 @@
package main
import (
"context"
"database/sql"
"io"
"log/slog"
"testing"
"github.com/pressly/goose/v3"
"highload/db/migrations"
)
func sessionSchema(t *testing.T, db *sql.DB, ctx context.Context) []string {
t.Helper()
rows, err := db.QueryContext(ctx, `
SELECT 'column:'||attname||':'||format_type(atttypid,atttypmod)||':'||attnotnull||':'||
COALESCE(pg_get_expr(d.adbin,d.adrelid),'')
FROM pg_attribute a LEFT JOIN pg_attrdef d ON d.adrelid=a.attrelid AND d.adnum=a.attnum
WHERE a.attrelid='sessions'::regclass AND a.attnum>0 AND NOT a.attisdropped
UNION ALL SELECT 'constraint:'||conname||':'||pg_get_constraintdef(oid)
FROM pg_constraint WHERE conrelid='sessions'::regclass
UNION ALL SELECT 'index:'||indexname||':'||indexdef
FROM pg_indexes WHERE schemaname=current_schema() AND tablename='sessions'
ORDER BY 1`)
if err != nil {
t.Fatal("cannot inspect restored session columns/defaults/constraints/indexes")
}
defer rows.Close()
var result []string
for rows.Next() {
var s string
if rows.Scan(&s) != nil {
t.Fatal("schema scan failed")
}
result = append(result, s)
}
if rows.Err() != nil || len(result) < 10 {
t.Fatal("incomplete session structure snapshot")
}
return result
}
func businessData(t *testing.T, db *sql.DB, ctx context.Context) []string {
t.Helper()
result := []string{}
for _, table := range []string{"users", "chats", "chat_members", "messages"} {
rows, err := db.QueryContext(ctx, "SELECT row_to_json(t)::text FROM "+table+" t ORDER BY row_to_json(t)::text")
if err != nil {
t.Fatal("business table disappeared during migration")
}
count := 0
for rows.Next() {
var value string
if rows.Scan(&value) != nil {
t.Fatal("data scan failed")
}
result = append(result, table+value)
count++
}
err = rows.Err()
rows.Close()
if err != nil || count == 0 {
t.Fatal("business fixture missing during migration")
}
}
return result
}
func equalStrings(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
func TestPostgresMigrationThreeDownTwoDownOneAndReapply(t *testing.T) {
db, ctx := migrationPostgresFixture(t)
provider, err := goose.NewProvider(goose.DialectPostgres, db, migrations.Files,
goose.WithDisableGlobalRegistry(true), goose.WithSlog(slog.New(slog.NewTextHandler(io.Discard, nil))))
if err != nil {
t.Fatal("cannot load actual embedded migrations")
}
if _, err = provider.UpTo(ctx, 1); err != nil {
t.Fatal("cannot apply version 1")
}
v1 := sessionSchema(t, db, ctx)
if _, err = db.ExecContext(ctx, `
INSERT INTO users(id,username,display_name,password_hash,created_at) VALUES
('00000000-0000-4000-8000-000000000001','roundtrip_user','Roundtrip','synthetic-hash','2026-01-01T00:00:00Z');
INSERT INTO chats(id,title,created_at) VALUES
('00000000-0000-4000-8000-000000000002','Roundtrip','2026-01-01T00:00:00Z');
INSERT INTO chat_members VALUES('00000000-0000-4000-8000-000000000002','00000000-0000-4000-8000-000000000001');
INSERT INTO messages(id,chat_id,sender_id,text,created_at) VALUES
('00000000-0000-4000-8000-000000000003','00000000-0000-4000-8000-000000000002','00000000-0000-4000-8000-000000000001','Keep this message','2026-01-01T00:00:00Z');
INSERT INTO sessions VALUES(decode(repeat('ab',32),'hex'),'00000000-0000-4000-8000-000000000001','2026-01-01T00:00:00Z','2026-01-02T00:00:00Z',NULL);
`); err != nil {
t.Fatal("cannot create migration preservation fixtures")
}
original := businessData(t, db, ctx)
if _, err = provider.UpTo(ctx, 2); err != nil {
t.Fatal("cannot apply version 2")
}
v2 := sessionSchema(t, db, ctx)
assertVersion := func(want int64) {
t.Helper()
got, e := provider.GetDBVersion(ctx)
if e != nil || got != want {
t.Fatalf("want migration version %d, got %d", want, got)
}
if !equalStrings(original, businessData(t, db, ctx)) {
t.Fatal("migration changed unrelated users/chats/members/messages")
}
}
assertAbsent := func() {
t.Helper()
var absent bool
if db.QueryRowContext(ctx, "SELECT to_regclass('sessions') IS NULL").Scan(&absent) != nil || !absent {
t.Fatal("version 3 must remove SQL sessions")
}
}
if _, err = provider.Up(ctx); err != nil {
t.Fatal("cannot migrate to 6")
}
assertVersion(6)
assertAbsent()
if _, err = provider.DownTo(ctx, 2); err != nil {
t.Fatal("6 -> 2 failed")
}
assertVersion(2)
if !equalStrings(v2, sessionSchema(t, db, ctx)) {
t.Fatal("3 -> 2 did not restore exact version-2 session columns, defaults, keys, FK, CHECK and indexes")
}
var count int
if db.QueryRowContext(ctx, "SELECT count(*) FROM sessions").Scan(&count) != nil || count != 0 {
t.Fatal("rollback must restore an empty sessions table; deleted credentials cannot be resurrected")
}
if _, err = db.ExecContext(ctx, `INSERT INTO sessions(token_hash,user_id,expires_at) VALUES(decode(repeat('cd',32),'hex'),'00000000-0000-4000-8000-000000000001',now()+interval '1 hour')`); err != nil {
t.Fatal("restored table/default/FK cannot accept a valid session")
}
for _, statement := range []string{
`INSERT INTO sessions(token_hash,user_id,expires_at) VALUES(decode(repeat('ef',32),'hex'),'00000000-0000-4000-8000-000000000099',now()+interval '1 hour')`,
`INSERT INTO sessions(token_hash,user_id,expires_at) VALUES(decode(repeat('ef',32),'hex'),'00000000-0000-4000-8000-000000000001',now()-interval '1 hour')`,
`INSERT INTO sessions(token_hash,user_id,expires_at) VALUES(decode(repeat('cd',32),'hex'),'00000000-0000-4000-8000-000000000001',now()+interval '1 hour')`,
} {
if _, err = db.ExecContext(ctx, statement); err == nil {
t.Fatal("restored table lost FK, expiry CHECK or primary key")
}
}
if _, err = provider.DownTo(ctx, 1); err != nil {
t.Fatal("2 -> 1 failed")
}
assertVersion(1)
if !equalStrings(v1, sessionSchema(t, db, ctx)) {
t.Fatal("2 -> 1 did not restore original sessions structure without created_at default")
}
if _, err = provider.Up(ctx); err != nil {
t.Fatal("1 -> 6 reapplication failed")
}
assertVersion(6)
assertAbsent()
}
+218
View File
@@ -0,0 +1,218 @@
name: highload-lab4
services:
api:
build:
context: .
dockerfile: Dockerfile
scale: 2
cpus: "2.0"
mem_limit: 512m
expose:
- "8080"
env_file:
- .env
secrets:
- database_url
- s3_access_key_id
- s3_secret_access_key
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_healthy
migrate:
condition: service_completed_successfully
s3-init:
condition: service_completed_successfully
restart: unless-stopped
stop_grace_period: 20s
read_only: true
tmpfs:
- /tmp:size=32m,mode=1777
cap_drop:
- ALL
security_opt:
- no-new-privileges:true
attachment-cleanup:
build:
context: .
dockerfile: Dockerfile
target: cleanup
command: ["/app/attachment-cleanup", "-dry-run=${LAB4_CLEANUP_DRY_RUN:-false}"]
cpus: "0.5"
mem_limit: 128m
env_file:
- .env
secrets:
- database_url
- s3_access_key_id
- s3_secret_access_key
depends_on:
postgres:
condition: service_healthy
migrate:
condition: service_completed_successfully
s3-init:
condition: service_completed_successfully
restart: unless-stopped
stop_grace_period: 20s
read_only: true
cap_drop:
- ALL
security_opt:
- no-new-privileges:true
lb:
image: haproxy:3.2.24-alpine
cpus: "1.0"
mem_limit: 256m
ports:
- "127.0.0.1:${LAB4_HTTP_PORT:-8081}:8080"
volumes:
- ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
depends_on:
api:
condition: service_healthy
restart: unless-stopped
read_only: true
cap_drop:
- ALL
security_opt:
- no-new-privileges:true
migrate:
build:
context: .
dockerfile: Dockerfile.migrate
cpus: "1.0"
mem_limit: 256m
environment:
DATABASE_URL_FILE: /run/secrets/database_url
secrets:
- database_url
depends_on:
postgres:
condition: service_healthy
restart: "no"
stop_grace_period: 20s
read_only: true
cap_drop:
- ALL
security_opt:
- no-new-privileges:true
postgres:
image: postgres:18
cpus: "2.0"
mem_limit: 512m
environment:
POSTGRES_DB: messenger
POSTGRES_USER: messenger
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
secrets:
- postgres_password
ports:
- "127.0.0.1:${LAB4_POSTGRES_PORT:-5433}:5432"
volumes:
- postgres_data:/var/lib/postgresql
healthcheck:
test: ["CMD-SHELL", "pg_isready -h 127.0.0.1 -U messenger -d messenger"]
interval: 5s
timeout: 5s
retries: 5
restart: unless-stopped
stop_grace_period: 60s
redis:
image: redis:7.4-alpine
cpus: "0.5"
mem_limit: 128m
restart: unless-stopped
command:
- redis-server
- --appendonly
- "yes"
- --appendfsync
- everysec
volumes:
- redis_data:/data
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 5s
timeout: 3s
retries: 5
s3:
image: rustfs/rustfs:1.0.4
cpus: "1.0"
mem_limit: 512m
environment:
RUSTFS_VOLUMES: /data
RUSTFS_ACCESS_KEY_FILE: /run/secrets/rustfs_root_access_key
RUSTFS_SECRET_KEY_FILE: /run/secrets/rustfs_root_secret_key
RUSTFS_CONSOLE_ENABLE: "false"
secrets:
- rustfs_root_access_key
- rustfs_root_secret_key
volumes:
- rustfs_data:/data
healthcheck:
test: ["CMD-SHELL", "curl -fsS http://127.0.0.1:9000/health/ready >/dev/null"]
interval: 5s
timeout: 3s
retries: 12
start_period: 30s
restart: unless-stopped
cap_drop:
- ALL
security_opt:
- no-new-privileges:true
s3-init:
image: rustfs/rc:v0.1.30
cpus: "0.5"
mem_limit: 128m
entrypoint: ["/bin/sh", "/bootstrap.sh"]
environment:
HOME: /tmp
S3_BUCKET: messenger-attachments
S3_REGION: us-east-1
secrets:
- rustfs_root_access_key
- rustfs_root_secret_key
- s3_access_key_id
- s3_secret_access_key
volumes:
- ./infra/s3/bootstrap.sh:/bootstrap.sh:ro
depends_on:
s3:
condition: service_healthy
restart: "no"
read_only: true
tmpfs:
- /tmp:uid=100,gid=101,mode=0700
cap_drop:
- ALL
security_opt:
- no-new-privileges:true
secrets:
database_url:
file: ./secrets/database_url
postgres_password:
file: ./secrets/postgres_password
rustfs_root_access_key:
file: ./secrets/rustfs_root_access_key
rustfs_root_secret_key:
file: ./secrets/rustfs_root_secret_key
s3_access_key_id:
file: ./secrets/s3_access_key_id
s3_secret_access_key:
file: ./secrets/s3_secret_access_key
volumes:
postgres_data:
redis_data:
rustfs_data:
+79
View File
@@ -0,0 +1,79 @@
-- +goose Up
CREATE TABLE users
(
id uuid PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(),
username VARCHAR(32) UNIQUE NOT NULL,
display_name VARCHAR(100) NOT NULL,
password_hash TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
CONSTRAINT users_username_format_check CHECK (username COLLATE "C" ~ '^[a-z0-9_]{3,32}$'),
CONSTRAINT users_display_name_length_check CHECK (char_length(display_name) BETWEEN 1 AND 100),
CONSTRAINT users_display_name_not_blank_check CHECK (char_length(btrim(display_name)) > 0),
CONSTRAINT users_password_hash_not_empty_check CHECK (char_length(password_hash) > 0)
);
CREATE TABLE chats
(
id UUID PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(),
title VARCHAR(100) NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
CONSTRAINT chats_title_length_check CHECK (char_length(title) BETWEEN 1 AND 100 ),
CONSTRAINT chats_title_not_blank_check CHECK (char_length(btrim(title)) > 0)
);
CREATE TABLE chat_members
(
chat_id UUID NOT NULL REFERENCES chats (id),
user_id UUID NOT NULL REFERENCES users (id),
PRIMARY KEY (chat_id, user_id)
);
CREATE INDEX idx_user_chat_id ON chat_members (user_id, chat_id);
CREATE TABLE messages
(
id UUID PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(),
chat_id UUID NOT NULL,
sender_id UUID NOT NULL,
text TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
FOREIGN KEY (chat_id, sender_id)
REFERENCES chat_members (chat_id, user_id),
CONSTRAINT messages_text_length_check CHECK (char_length(text) BETWEEN 1 AND 4000),
CONSTRAINT messages_text_not_blank_check CHECK (char_length(btrim(text)) > 0)
);
CREATE INDEX idx_messages_chat_created_id ON messages (chat_id, created_at, id);
CREATE TABLE sessions
(
token_hash BYTEA PRIMARY KEY NOT NULL,
user_id UUID NOT NULL REFERENCES users (id),
created_at TIMESTAMPTZ NOT NULL,
expires_at TIMESTAMPTZ NOT NULL,
revoked_at TIMESTAMPTZ NULL,
CONSTRAINT sessions_expiry_after_creation_check CHECK (expires_at > created_at)
);
CREATE INDEX idx_session_user_id ON sessions (user_id);
-- +goose Down
DROP TABLE IF EXISTS sessions;
DROP TABLE IF EXISTS messages;
DROP TABLE IF EXISTS chat_members;
DROP TABLE IF EXISTS chats;
DROP TABLE IF EXISTS users;
@@ -0,0 +1,20 @@
-- +goose Up
ALTER TABLE users
ALTER COLUMN created_at SET DEFAULT now();
ALTER TABLE chats
ALTER COLUMN created_at SET DEFAULT now();
ALTER TABLE messages
ALTER COLUMN created_at SET DEFAULT now();
ALTER TABLE sessions
ALTER COLUMN created_at SET DEFAULT now();
-- +goose Down
ALTER TABLE sessions
ALTER COLUMN created_at DROP DEFAULT;
ALTER TABLE messages
ALTER COLUMN created_at DROP DEFAULT;
ALTER TABLE chats
ALTER COLUMN created_at DROP DEFAULT;
ALTER TABLE users
ALTER COLUMN created_at DROP DEFAULT;
@@ -0,0 +1,17 @@
-- +goose Up
DROP TABLE IF EXISTS sessions;
-- +goose Down
-- Restore the schema at version 2; deleted session data cannot be recovered.
CREATE TABLE sessions
(
token_hash BYTEA PRIMARY KEY NOT NULL,
user_id UUID NOT NULL REFERENCES users (id),
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
expires_at TIMESTAMPTZ NOT NULL,
revoked_at TIMESTAMPTZ NULL,
CONSTRAINT sessions_expiry_after_creation_check CHECK (expires_at > created_at)
);
CREATE INDEX idx_session_user_id ON sessions (user_id);
@@ -0,0 +1,23 @@
-- +goose Up
CREATE TABLE message_idempotency
(
user_id UUID NOT NULL REFERENCES users(id),
chat_id UUID NOT NULL REFERENCES chats(id),
idempotency_key VARCHAR(128) NOT NULL,
message_id UUID NOT NULL REFERENCES messages(id),
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
expires_at TIMESTAMPTZ NOT NULL DEFAULT (now() + interval '24 hours'),
PRIMARY KEY (user_id, chat_id, idempotency_key),
UNIQUE (message_id),
CONSTRAINT message_idempotency_key_format_check
CHECK (idempotency_key COLLATE "C" ~ '^[A-Za-z0-9._:-]{1,128}$'),
CONSTRAINT message_idempotency_expiry_check
CHECK (expires_at > created_at)
);
-- +goose Down
DROP TABLE IF EXISTS message_idempotency;
+54
View File
@@ -0,0 +1,54 @@
-- +goose Up
CREATE TABLE attachments
(
id UUID PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(),
chat_id UUID NOT NULL,
owner_id UUID NOT NULL,
filename text NOT NULL,
object_key text UNIQUE NOT NULL,
content_type VARCHAR(10) NOT NULL,
size_bytes bigint NOT NULL,
sha256 text NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
CONSTRAINT fk_attachments_chat_member FOREIGN KEY (chat_id, owner_id)
REFERENCES chat_members(chat_id, user_id),
CONSTRAINT attachments_sha256_format_check CHECK (sha256 COLLATE "C" ~ '^[0-9a-f]{64}$'),
CONSTRAINT attachments_content_type_check CHECK (content_type IN ('image/png', 'image/jpeg')),
CONSTRAINT attachments_filename_check CHECK (
char_length(filename) BETWEEN 1 AND 255
AND filename !~ '[/\\]'
),
CONSTRAINT attachments_size_bytes_check CHECK (size_bytes BETWEEN 1 AND 10485760),
CONSTRAINT attachments_id_chat_unique UNIQUE (id, chat_id),
CONSTRAINT attachments_object_key_length_check CHECK (char_length(object_key) > 0)
);
ALTER TABLE messages
ADD CONSTRAINT messages_id_chat_unique UNIQUE (id, chat_id);
CREATE TABLE messages_attachments
(
message_id UUID NOT NULL ,
attachment_id UUID NOT NULL ,
chat_id UUID NOT NULL,
position SMALLINT NOT NULL,
CONSTRAINT message_attachments_pk PRIMARY KEY (message_id, attachment_id),
CONSTRAINT message_attachments_position_unique UNIQUE (message_id, position),
CONSTRAINT message_attachments_position_check CHECK (position BETWEEN 0 AND 9),
CONSTRAINT message_attachments_message_chat_fk
FOREIGN KEY (message_id, chat_id) REFERENCES messages(id, chat_id),
CONSTRAINT message_attachments_attachment_chat_fk
FOREIGN KEY (attachment_id, chat_id) REFERENCES attachments(id, chat_id)
);
-- +goose Down
DROP TABLE IF EXISTS messages_attachments;
DROP TABLE IF EXISTS attachments;
ALTER TABLE messages DROP CONSTRAINT IF EXISTS messages_id_chat_unique;
@@ -0,0 +1,18 @@
-- +goose Up
ALTER TABLE messages
DROP CONSTRAINT messages_text_length_check,
DROP CONSTRAINT messages_text_not_blank_check;
ALTER TABLE messages
ADD CONSTRAINT messages_text_length_check
CHECK (char_length(text) <= 4000);
-- +goose Down
ALTER TABLE messages
DROP CONSTRAINT messages_text_length_check;
ALTER TABLE messages
ADD CONSTRAINT messages_text_length_check
CHECK (char_length(text) BETWEEN 1 AND 4000),
ADD CONSTRAINT messages_text_not_blank_check
CHECK (char_length(btrim(text)) > 0);
+9
View File
@@ -0,0 +1,9 @@
package migrations
import "embed"
// Files contains the SQL shipped with this exact migrator build.
// Add schema changes as new numbered files; do not edit applied migrations.
//
//go:embed *.sql
var Files embed.FS
+41
View File
@@ -0,0 +1,41 @@
-- name: CreateAttachment :one
INSERT INTO attachments
(chat_id, owner_id, filename, object_key, content_type, size_bytes, sha256)
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING *;
-- name: GetAttachmentForUser :one
SELECT a.*
FROM attachments AS a
JOIN chat_members AS cm ON cm.chat_id = a.chat_id
WHERE a.id = sqlc.arg(attachment_id)
AND cm.user_id = sqlc.arg(user_id);
-- name: GetAttachmentsForMessages :many
SELECT message_id, attachment_id, position
FROM messages_attachments
WHERE message_id = ANY(sqlc.arg(message_ids)::uuid[])
ORDER BY message_id, position;
-- name: AttachmentExistsByObjectKey :one
SELECT EXISTS (
SELECT 1 FROM attachments
WHERE object_key = sqlc.arg(object_key)
);
-- name: TryLockAttachmentObjectKey :one
SELECT pg_try_advisory_xact_lock(
hashtextextended(sqlc.arg(object_key)::text, 0)
)::boolean AS locked;
-- name: ListAttachmentKeys :many
SELECT id, object_key, created_at
FROM attachments
WHERE object_key > sqlc.arg(after_key)
ORDER BY object_key
LIMIT sqlc.arg(page_size);
-- name: GetAttachmentByID :one
SELECT * FROM attachments WHERE id = sqlc.arg(attachment_id);
+3
View File
@@ -0,0 +1,3 @@
-- name: CreateChatMember :exec
INSERT INTO chat_members(chat_id, user_id)
VALUES ($1, $2);
+34
View File
@@ -0,0 +1,34 @@
-- name: CreateChat :one
INSERT INTO chats (title)
VALUES ($1)
RETURNING id, title, created_at;
-- name: GetChatByID :one
SELECT c.id, c.title, c.created_at, array_agg(cm.user_id)::uuid[] AS member_ids
FROM chats c
JOIN chat_members cm ON cm.chat_id = c.id
WHERE c.id = $1
GROUP BY c.id;
-- name: GetChatsByUserID :many
SELECT c.id, c.title, c.created_at, array_agg(cm.user_id)::uuid[] AS member_ids
FROM chats c
JOIN chat_members cm ON cm.chat_id = c.id
WHERE EXISTS (SELECT 1
FROM chat_members membership
WHERE membership.chat_id = c.id
AND membership.user_id = sqlc.arg(user_id))
AND (
sqlc.narg(after_created_at)::timestamptz IS NULL
OR (c.created_at, c.id) >
(
sqlc.narg(after_created_at)::timestamptz,
sqlc.narg(after_id)::uuid
)
)
GROUP BY c.id
ORDER BY c.created_at ASC, c.id ASC
LIMIT sqlc.arg(limit_count);
+13
View File
@@ -0,0 +1,13 @@
-- name: InsertMessageIdempotency :execrows
INSERT INTO message_idempotency(user_id, chat_id, idempotency_key, message_id)
VALUES ($1, $2, $3, $4)
ON CONFLICT (user_id, chat_id, idempotency_key) DO NOTHING;
-- name: GetIdempotentMessage :one
SELECT m.id, m.chat_id, m.sender_id, m.text, m.created_at
FROM message_idempotency AS i
JOIN messages AS m ON m.id = i.message_id
WHERE i.user_id = $1
AND i.chat_id = $2
AND i.idempotency_key = $3;
+19
View File
@@ -0,0 +1,19 @@
-- name: CreateMessage :one
INSERT INTO messages (chat_id, sender_id, text)
VALUES ($1, $2, $3)
RETURNING *;
-- name: GetMessagesByChatID :many
SELECT id, chat_id, sender_id, text, created_at
FROM messages
WHERE chat_id = sqlc.arg(chat_id)
AND (
sqlc.narg(after_created_at)::timestamptz IS NULL
OR (created_at, id) >
(sqlc.narg(after_created_at)::timestamptz,
sqlc.narg(after_id)::uuid
)
)
ORDER BY created_at ASC, id ASC
LIMIT sqlc.arg(limit_count);
+11
View File
@@ -0,0 +1,11 @@
-- name: CreateMessagesAttachments :execrows
INSERT INTO messages_attachments (message_id, attachment_id, chat_id, position)
SELECT $1, $2, $3, $4
FROM attachments
WHERE id = $2 AND chat_id = $3;
-- name: GetMessagesAttachments :many
SELECT attachment_id, position
FROM messages_attachments
WHERE message_id = sqlc.arg(message_id)
ORDER BY position ASC;
+21
View File
@@ -0,0 +1,21 @@
-- name: CreateUser :one
INSERT INTO users (username, display_name, password_hash)
VALUES ($1, $2, $3)
RETURNING id, username, display_name, password_hash, created_at;
-- name: GetUserByID :one
SELECT id, username, display_name, password_hash, created_at
FROM users
WHERE id = $1;
-- name: GetUserByUsername :one
SELECT id, username, display_name, password_hash, created_at
FROM users
WHERE username = $1;
+26
View File
@@ -0,0 +1,26 @@
# Проверки lab4 на отдельном Compose-стенде
В HTTP-проверках адрес стенда был `http://127.0.0.1:18094`. PostgreSQL и Redis для интеграционных тестов доступны только на loopback. Запускать сценарии отказов на синтетических данных в отдельном проекте.
Артефакты базовой реализации уровня 4, повторные проверки текущей реализации и команды запуска указаны в [отчёте](../../REPORT.md). [Манифест снимка](source.json) фиксирует исходники прогона. Полная проверка PostgreSQL, Redis и перезапусков повторена на [финальном снимке](../lab4-grade5/go.txt).
## Профиль памяти
Лёгкий сценарий использует публичный PNG 320 × 200. Для тяжёлого сценария можно воспроизвести PNG и JPEG размером 6000 × 6000 командой `generate` из [генератора](upload-fixture-generator.go.txt), сохранив его с расширением `.go` во временной папке и запустив `go run probe.go generate` из этой папки. Генерация выполняется отдельно от замера.
```sh
python3 student-tests/lab4/upload_memory.py \
--base-url http://127.0.0.1:18094 --project lab4-final-offline \
--fixtures /path/to/generated --output /path/to/results
```
Двенадцать клиентов выходят из одного барьера. Два профиля запускаются последовательно. `memory.peak` считан до и после каждого профиля, поэтому он относится к жизни контейнера. В обоих профилях счётчик вырос. Сырые выборки `memory.current` приложены в JSON. Сборщик не записывает пароли, токены и тела запросов.
## Отказы и границы
- `attachment_pixel_limit.py --base-url URL --output DIR` создаёт валидный PNG 6500 × 6200 и ожидает `413`.
- `attachment_runtime_faults.py --base-url URL --output DIR --probe PATH --probe-image IMAGE` останавливает загрузившую реплику, проверяет SHA-256 с другой, восстанавливает API и затем отключает RustFS. Для прямого HTTP-запроса собери [probe](direct-probe.go.txt) под Linux и архитектуру Docker-хоста. `COMPOSE_PROJECT_NAME` должен начинаться с `lab4-final-`, а `COMPOSE_FILE` при необходимости включает override.
- `attachment_compensation.py --spec SPEC --output DIR --as-hook` принимает JSON с `project`, `runtime` (каталог локальных secrets), `snapshot` (корень копии) и `base` (массив аргументов, начиная с `["docker","compose","-p",project]`). Используется выделенный Compose-проект. Тест включает SIGKILL API и короткий grace. [Результат](../lab4-grade5/compensation.json) · [Наблюдения](../lab4-grade5/compensation-events.jsonl)
- `s3_infrastructure.py --project PROJECT --base-url URL --output DIR` проверяет bootstrap, настройки прав, анонимный доступ и консоль.
В [первой попытке](faults-first-attempt.txt) утилита nc закрыла соединение до получения HTTP-ответа. Повтор из сетевого пространства API дал `live=200`, `ready=503`, upload `503`. [Результат](s3-failure.json)
+50
View File
@@ -0,0 +1,50 @@
{
"attachment_acl_bytes_and_idempotency": {
"detail": "",
"elapsed_seconds": 0.928,
"requirement": "lab4_grade3",
"status": "PASS"
},
"attachment_bucket_security": {
"detail": "configure hooks.attachment_bucket_security before starting the fault",
"elapsed_seconds": 0.0,
"requirement": "lab4_grade4",
"status": "PENDING_INFRA"
},
"attachment_compensation": {
"detail": "configure hooks.attachment_compensation before starting the fault",
"elapsed_seconds": 0.0,
"requirement": "lab4_grade4",
"status": "PENDING_INFRA"
},
"attachment_negative_uploads": {
"detail": "",
"elapsed_seconds": 0.776,
"requirement": "lab4_grade4",
"status": "PASS"
},
"attachment_parallel_memory": {
"detail": "configure hooks.attachment_parallel_memory before starting the fault",
"elapsed_seconds": 0.0,
"requirement": "lab4_grade4",
"status": "PENDING_INFRA"
},
"attachment_pixels_limit": {
"detail": "configure hooks.attachment_pixels_limit before starting the fault",
"elapsed_seconds": 0.0,
"requirement": "lab4_grade3",
"status": "PENDING_INFRA"
},
"attachment_s3_failure": {
"detail": "configure hooks.attachment_s3_failure before starting the fault",
"elapsed_seconds": 0.0,
"requirement": "lab4_grade3",
"status": "PENDING_INFRA"
},
"public_lb_endpoint": {
"detail": "",
"elapsed_seconds": 0.278,
"requirement": "inherited_contract_or_requested_check",
"status": "PASS"
}
}
+15
View File
@@ -0,0 +1,15 @@
package main
import ("bufio";"bytes";"encoding/json";"io";"net/http";"os";"strings";"time")
func main(){
in:=bufio.NewReader(os.Stdin)
line,err:=in.ReadString('\n'); if err!=nil {panic(err)}
token,err:=in.ReadString('\n'); if err!=nil {panic(err)}
body,err:=io.ReadAll(in); if err!=nil {panic(err)}
fields:=strings.Fields(line)
req,err:=http.NewRequest(fields[0],"http://127.0.0.1:8080"+fields[1],bytes.NewReader(body)); if err!=nil {panic(err)}
if t:=strings.TrimSpace(token); t!="" {req.Header.Set("Authorization","Bearer "+t); req.Header.Set("Content-Type","image/png")}
res,err:=(&http.Client{Timeout:12*time.Second}).Do(req); if err!=nil {panic(err)}
defer res.Body.Close()
b,err:=io.ReadAll(res.Body); if err!=nil {panic(err)}
if err:=json.NewEncoder(os.Stdout).Encode(map[string]any{"status":res.StatusCode,"body":string(b),"instance":res.Header.Get("X-Instance-Id")});err!=nil{panic(err)}
}
@@ -0,0 +1,7 @@
Traceback (most recent call last):
File "test-output/faults.py", line 77, in <module>
check((live,rd,upload)==(200,503,503) and b'unavailable' in body, "wrong API outage statuses")
~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "snapshot/tests/client.py", line 24, in check
raise Failure(message)
client.Failure: wrong API outage statuses
+7
View File
@@ -0,0 +1,7 @@
{
"source": "1c2fc2215977",
"replica": "38de6c779806",
"failures": 0,
"seconds": 0.023,
"sha256": "99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75"
}
+506
View File
@@ -0,0 +1,506 @@
{
"clients": 12,
"files": [
{
"name": "load.png",
"mime": "image/png",
"bytes": 642711
},
{
"name": "load.jpg",
"mime": "image/jpeg",
"bytes": 1269896
},
{
"name": "load.png",
"mime": "image/png",
"bytes": 642711
},
{
"name": "load.jpg",
"mime": "image/jpeg",
"bytes": 1269896
},
{
"name": "load.png",
"mime": "image/png",
"bytes": 642711
},
{
"name": "load.jpg",
"mime": "image/jpeg",
"bytes": 1269896
},
{
"name": "load.png",
"mime": "image/png",
"bytes": 642711
},
{
"name": "load.jpg",
"mime": "image/jpeg",
"bytes": 1269896
},
{
"name": "load.png",
"mime": "image/png",
"bytes": 642711
},
{
"name": "load.jpg",
"mime": "image/jpeg",
"bytes": 1269896
},
{
"name": "load.png",
"mime": "image/png",
"bytes": 642711
},
{
"name": "load.jpg",
"mime": "image/jpeg",
"bytes": 1269896
}
],
"statuses": {
"201": 4,
"429": 8
},
"results": [
{
"index": 0,
"status": 201,
"instance": "1c2fc2215977",
"seconds": 1.4950219999955152
},
{
"index": 1,
"status": 201,
"instance": "38de6c779806",
"seconds": 1.4105649160046596
},
{
"index": 2,
"status": 201,
"instance": "1c2fc2215977",
"seconds": 0.9557449580024695
},
{
"index": 3,
"status": 429,
"instance": "38de6c779806",
"seconds": 1.0306972920006956
},
{
"index": 4,
"status": 201,
"instance": "38de6c779806",
"seconds": 0.9493740839970997
},
{
"index": 5,
"status": 429,
"instance": "38de6c779806",
"seconds": 1.0500210840036743
},
{
"index": 6,
"status": 429,
"instance": "1c2fc2215977",
"seconds": 1.0534772089959006
},
{
"index": 7,
"status": 429,
"instance": "1c2fc2215977",
"seconds": 1.0391598749993136
},
{
"index": 8,
"status": 429,
"instance": "38de6c779806",
"seconds": 1.0649052080043475
},
{
"index": 9,
"status": 429,
"instance": "1c2fc2215977",
"seconds": 1.0194469999987632
},
{
"index": 10,
"status": 429,
"instance": "38de6c779806",
"seconds": 1.0226270000057411
},
{
"index": 11,
"status": 429,
"instance": "1c2fc2215977",
"seconds": 1.034560625004815
}
],
"replicas": {
"38de6c779806": {
"before_peak": 150097920,
"after_peak": 294215680,
"limit": 536870912,
"samples": [
[
0.075,
149831680
],
[
0.134,
148865024
],
[
0.184,
148680704
],
[
0.24,
148602880
],
[
0.293,
148631552
],
[
0.347,
148795392
],
[
0.401,
148758528
],
[
0.455,
149409792
],
[
0.505,
154324992
],
[
0.559,
162516992
],
[
0.606,
177471488
],
[
0.659,
188268544
],
[
0.714,
194568192
],
[
0.764,
202862592
],
[
0.816,
213696512
],
[
0.869,
219779072
],
[
0.921,
228106240
],
[
0.974,
238596096
],
[
1.026,
255332352
],
[
1.077,
268062720
],
[
1.128,
274460672
],
[
1.179,
282320896
],
[
1.231,
292990976
],
[
1.285,
292786176
],
[
1.337,
292786176
],
[
1.388,
293543936
],
[
1.442,
293543936
],
[
1.492,
293556224
],
[
1.56,
293953536
],
[
1.606,
293445632
],
[
1.67,
293560320
],
[
1.716,
293740544
],
[
1.767,
293765120
],
[
1.818,
292229120
],
[
1.871,
292200448
],
[
1.923,
292634624
],
[
1.976,
292233216
],
[
2.037,
291532800
],
[
2.094,
291721216
],
[
2.14,
291598336
],
[
2.191,
291680256
]
],
"events_delta": {
"low": 0,
"high": 0,
"max": 0,
"oom": 0,
"oom_kill": 0,
"oom_group_kill": 0
},
"restart_delta": 0,
"same_start": true,
"oom_killed": false
},
"1c2fc2215977": {
"before_peak": 147574784,
"after_peak": 430215168,
"limit": 536870912,
"samples": [
[
0.066,
146915328
],
[
0.117,
146403328
],
[
0.171,
146513920
],
[
0.223,
146685952
],
[
0.274,
146599936
],
[
0.328,
146632704
],
[
0.386,
146812928
],
[
0.437,
147116032
],
[
0.493,
161148928
],
[
0.543,
169295872
],
[
0.595,
183906304
],
[
0.649,
190574592
],
[
0.702,
196771840
],
[
0.758,
205017088
],
[
0.81,
215502848
],
[
0.861,
223907840
],
[
0.914,
230154240
],
[
0.966,
238407680
],
[
1.017,
253476864
],
[
1.07,
270123008
],
[
1.121,
274477056
],
[
1.173,
282595328
],
[
1.227,
293322752
],
[
1.283,
293048320
],
[
1.332,
293031936
],
[
1.387,
295157760
],
[
1.439,
304148480
],
[
1.49,
325140480
],
[
1.56,
337682432
],
[
1.644,
356155392
],
[
1.693,
366571520
],
[
1.744,
385736704
],
[
1.795,
400392192
],
[
1.846,
415043584
],
[
1.898,
429740032
],
[
1.951,
428744704
],
[
2.006,
428699648
],
[
2.06,
428515328
],
[
2.111,
428564480
],
[
2.162,
428535808
]
],
"events_delta": {
"low": 0,
"high": 0,
"max": 0,
"oom": 0,
"oom_kill": 0,
"oom_group_kill": 0
},
"restart_delta": 0,
"same_start": true,
"oom_killed": false
}
},
"sampling_seconds": 0.05
}
+309
View File
@@ -0,0 +1,309 @@
{
"clients": 12,
"files": [
{
"name": "sample.png",
"mime": "image/png",
"bytes": 111817
},
{
"name": "sample.png",
"mime": "image/png",
"bytes": 111817
},
{
"name": "sample.png",
"mime": "image/png",
"bytes": 111817
},
{
"name": "sample.png",
"mime": "image/png",
"bytes": 111817
},
{
"name": "sample.png",
"mime": "image/png",
"bytes": 111817
},
{
"name": "sample.png",
"mime": "image/png",
"bytes": 111817
},
{
"name": "sample.png",
"mime": "image/png",
"bytes": 111817
},
{
"name": "sample.png",
"mime": "image/png",
"bytes": 111817
},
{
"name": "sample.png",
"mime": "image/png",
"bytes": 111817
},
{
"name": "sample.png",
"mime": "image/png",
"bytes": 111817
},
{
"name": "sample.png",
"mime": "image/png",
"bytes": 111817
},
{
"name": "sample.png",
"mime": "image/png",
"bytes": 111817
}
],
"statuses": {
"201": 12
},
"results": [
{
"index": 0,
"status": 201,
"instance": "1c2fc2215977",
"seconds": 0.11950787499517901
},
{
"index": 1,
"status": 201,
"instance": "38de6c779806",
"seconds": 0.1198934999993071
},
{
"index": 2,
"status": 201,
"instance": "1c2fc2215977",
"seconds": 0.11025404199608602
},
{
"index": 3,
"status": 201,
"instance": "38de6c779806",
"seconds": 0.1579494590041577
},
{
"index": 4,
"status": 201,
"instance": "38de6c779806",
"seconds": 0.08275312499608845
},
{
"index": 5,
"status": 201,
"instance": "1c2fc2215977",
"seconds": 0.0824889580035233
},
{
"index": 6,
"status": 201,
"instance": "38de6c779806",
"seconds": 0.09312024999962887
},
{
"index": 7,
"status": 201,
"instance": "1c2fc2215977",
"seconds": 0.15798666600312572
},
{
"index": 8,
"status": 201,
"instance": "38de6c779806",
"seconds": 0.16619729199737776
},
{
"index": 9,
"status": 201,
"instance": "1c2fc2215977",
"seconds": 0.09303424999961862
},
{
"index": 10,
"status": 201,
"instance": "1c2fc2215977",
"seconds": 0.134490292002738
},
{
"index": 11,
"status": 201,
"instance": "38de6c779806",
"seconds": 0.1474745419982355
}
],
"replicas": {
"38de6c779806": {
"before_peak": 78581760,
"after_peak": 82251776,
"limit": 536870912,
"samples": [
[
0.074,
77082624
],
[
0.126,
76701696
],
[
0.177,
76554240
],
[
0.228,
76816384
],
[
0.281,
76812288
],
[
0.338,
77144064
],
[
0.396,
76996608
],
[
0.45,
77750272
],
[
0.499,
78159872
],
[
0.552,
78475264
],
[
0.601,
80609280
],
[
0.652,
80601088
],
[
0.706,
80605184
],
[
0.759,
80637952
],
[
0.812,
80785408
],
[
0.864,
80646144
]
],
"events_delta": {
"low": 0,
"high": 0,
"max": 0,
"oom": 0,
"oom_kill": 0,
"oom_group_kill": 0
},
"restart_delta": 0,
"same_start": true,
"oom_killed": false
},
"1c2fc2215977": {
"before_peak": 78098432,
"after_peak": 80687104,
"limit": 536870912,
"samples": [
[
0.074,
76730368
],
[
0.126,
76226560
],
[
0.178,
76251136
],
[
0.229,
76247040
],
[
0.281,
76398592
],
[
0.338,
76300288
],
[
0.396,
76271616
],
[
0.45,
76718080
],
[
0.499,
77037568
],
[
0.552,
78348288
],
[
0.602,
78499840
],
[
0.654,
78487552
],
[
0.707,
78499840
],
[
0.759,
78700544
],
[
0.812,
78569472
],
[
0.864,
78299136
]
],
"events_delta": {
"low": 0,
"high": 0,
"max": 0,
"oom": 0,
"oom_kill": 0,
"oom_group_kill": 0
},
"restart_delta": 0,
"same_start": true,
"oom_killed": false
}
},
"sampling_seconds": 0.05
}
+6
View File
@@ -0,0 +1,6 @@
{
"width": 6500,
"height": 6200,
"encoded_bytes": 39249,
"upload_status": 413
}
+9
View File
@@ -0,0 +1,9 @@
api 1c2fc22159776084669ac7ffdfc7ab18778485feb6810b4c534fb90baec30d62 2026-11-14T20:59:38.277558213Z
api 38de6c779806333887d344e72f5de2b4a4336b6ef02cc8bfe400809ad3b711d3 2026-11-14T20:59:38.462321713Z
attachment-cleanup e573c9b2f3fac6dbd03b8a941e8cff44199cbe5f8dcf0662d847ca60dfef3f33 2026-11-14T20:59:44.4007593Z
lb 85e0635f043975812d11820b400edbf1e14428928e526e5a644e41fea9e210ce 2026-11-14T20:59:44.279379091Z
migrate 8b1303443156719ba3c74bd9771f889a47689cdc447a254eb88ac44cb6ca6e3c 2026-11-14T20:58:31.100201127Z
postgres aabd87a6deea77a10e45a268726a2aa5bdd3111d1b62e8a5238c4efe96fa9a9d 2026-11-14T20:59:31.482838877Z lab4-final-offline_postgres_data
redis a6b9d8046bc219a2ee1f9624aab5a898f129f9081efcd241c8962941d65cb1b4 2026-11-14T20:59:31.973372085Z lab4-final-offline_redis_data
s3 1dd57160fa6a64fa3ea2e95e097c623d2beaef1926fc8d5f75e2aedff601eaa0 2026-11-14T20:59:32.286071627Z lab4-final-offline_rustfs_data
s3-init 7a365a86ababc0976e6b9cb4192bfa223a38e5baec615cc0a03bf369ec7a9042 2026-11-14T20:58:31.102788085Z
+9
View File
@@ -0,0 +1,9 @@
api 1c2fc22159776084669ac7ffdfc7ab18778485feb6810b4c534fb90baec30d62 2026-11-14T20:58:31.887295044Z
api 38de6c779806333887d344e72f5de2b4a4336b6ef02cc8bfe400809ad3b711d3 2026-11-14T20:57:52.42195897Z
attachment-cleanup e573c9b2f3fac6dbd03b8a941e8cff44199cbe5f8dcf0662d847ca60dfef3f33 2026-11-14T20:58:00.616614543Z
lb 85e0635f043975812d11820b400edbf1e14428928e526e5a644e41fea9e210ce 2026-11-14T20:57:58.431995375Z
migrate 8b1303443156719ba3c74bd9771f889a47689cdc447a254eb88ac44cb6ca6e3c 2026-11-14T20:58:31.100201127Z
postgres aabd87a6deea77a10e45a268726a2aa5bdd3111d1b62e8a5238c4efe96fa9a9d 2026-11-14T20:57:46.098278675Z lab4-final-offline_postgres_data
redis a6b9d8046bc219a2ee1f9624aab5a898f129f9081efcd241c8962941d65cb1b4 2026-11-14T20:57:45.953826092Z lab4-final-offline_redis_data
s3 1dd57160fa6a64fa3ea2e95e097c623d2beaef1926fc8d5f75e2aedff601eaa0 2026-11-14T20:58:49.826562677Z lab4-final-offline_rustfs_data
s3-init 7a365a86ababc0976e6b9cb4192bfa223a38e5baec615cc0a03bf369ec7a9042 2026-11-14T20:58:31.102788085Z
+8
View File
@@ -0,0 +1,8 @@
{
"fault": "s3_unreachable",
"live_status": 200,
"ready_status": 503,
"upload_status": 503,
"false_ready_attachments": 0,
"restored_original_checksum_equal": true
}
+68
View File
@@ -0,0 +1,68 @@
{
"s3_api_bucket_scope_and_iam_denials": {
"detail": "",
"elapsed_seconds": 5.314,
"requirement": "requested_operational_check",
"status": "PASS"
},
"s3_api_delete_effect": {
"detail": "",
"elapsed_seconds": 0.928,
"requirement": "requested_operational_check",
"status": "PASS"
},
"s3_api_effective_policy": {
"detail": "",
"elapsed_seconds": 0.945,
"requirement": "requested_operational_check",
"status": "PASS"
},
"s3_bootstrap_preserves_object_and_volume": {
"detail": "",
"elapsed_seconds": 2.742,
"requirement": "requested_operational_check",
"status": "PASS"
},
"s3_console_closed_on_shared_network": {
"detail": "",
"elapsed_seconds": 0.749,
"requirement": "requested_operational_check",
"status": "PASS"
},
"s3_fixture_cleanup": {
"detail": "",
"elapsed_seconds": 4.046,
"requirement": "requested_operational_check",
"status": "PASS"
},
"s3_restart_preserves_original_bytes": {
"detail": "",
"elapsed_seconds": 6.263,
"requirement": "requested_operational_check",
"status": "PASS"
},
"s3_setup_and_scenarios": {
"detail": "",
"elapsed_seconds": 26.37,
"requirement": "requested_operational_check",
"status": "PASS"
},
"s3_two_replicas_after_restart": {
"detail": "",
"elapsed_seconds": 0.474,
"requirement": "requested_operational_check",
"status": "PASS"
},
"s3_two_replicas_before": {
"detail": "",
"elapsed_seconds": 0.667,
"requirement": "requested_operational_check",
"status": "PASS"
},
"s3_unsigned_known_object_403": {
"detail": "",
"elapsed_seconds": 0.301,
"requirement": "requested_operational_check",
"status": "PASS"
}
}
+44
View File
@@ -0,0 +1,44 @@
{"bindings": [{"HostIp": "127.0.0.1", "HostPort": "18094"}], "event": "public_lb_endpoint", "port": 18094, "time_utc": "2026-11-14T20:58:41Z", "time_utc_ms": "2026-11-14T20:58:41.908+00:00"}
{"api_ids": ["1c2fc22159776084669ac7ffdfc7ab18778485feb6810b4c534fb90baec30d62", "38de6c779806333887d344e72f5de2b4a4336b6ef02cc8bfe400809ad3b711d3"], "event": "s3_api_topology", "lb_id": "85e0635f043975812d11820b400edbf1e14428928e526e5a644e41fea9e210ce", "lb_image": "haproxy:3.2.24-alpine", "observed_instances": ["1c2fc2215977", "38de6c779806"], "same_token": true, "time_utc": "2026-11-14T20:58:42Z", "time_utc_ms": "2026-11-14T20:58:42.146+00:00"}
{"detail": "", "elapsed_seconds": 0.667, "event": "case", "name": "s3_two_replicas_before", "requirement": "requested_operational_check", "status": "PASS", "time_utc": "2026-11-14T20:58:42Z", "time_utc_ms": "2026-11-14T20:58:42.147+00:00"}
{"control_bucket": "student-control-37a866cfecd04359ac1e1988712566b0", "create_probe_bucket": "student-forbidden-37a866cfecd04359ac1e1988712566b0", "event": "s3_initial", "fixture_prefix": "student-tests/s3-37a866cfecd04359ac1e1988712566b0/", "id": "1dd57160fa6a64fa3ea2e95e097c623d2beaef1926fc8d5f75e2aedff601eaa0", "image": "rustfs/rustfs:1.0.4", "payload_sha256": "15beb9311f947cccd73f69f39c44114ac8f7144c3b73b158610eba32f19fbcc8", "payload_size_bytes": 65824, "project": "lab4-final-offline", "running": true, "started_at": "2026-11-14T20:58:40.118706673Z", "time_utc": "2026-11-14T20:58:45Z", "time_utc_ms": "2026-11-14T20:58:45.585+00:00", "volumes": ["lab4-final-offline_rustfs_data"]}
{"authorization_denied": false, "event": "s3_operation", "exit_code": 0, "operation": "api_put_allowed_object", "s3_error_code": null, "time_utc": "2026-11-14T20:58:46Z", "time_utc_ms": "2026-11-14T20:58:46.220+00:00"}
{"byte_equal": true, "event": "s3_roundtrip", "phase": "before_bootstrap", "sha256": "15beb9311f947cccd73f69f39c44114ac8f7144c3b73b158610eba32f19fbcc8", "size_bytes": 65824, "time_utc": "2026-11-14T20:58:46Z", "time_utc_ms": "2026-11-14T20:58:46.751+00:00"}
{"attempt": 1, "event": "s3_bootstrap", "exit_code": 0, "time_utc": "2026-11-14T20:58:47Z", "time_utc_ms": "2026-11-14T20:58:47.491+00:00"}
{"byte_equal": true, "event": "s3_roundtrip", "phase": "bootstrap_1", "sha256": "15beb9311f947cccd73f69f39c44114ac8f7144c3b73b158610eba32f19fbcc8", "size_bytes": 65824, "time_utc": "2026-11-14T20:58:48Z", "time_utc_ms": "2026-11-14T20:58:48.065+00:00"}
{"attempt": 2, "event": "s3_bootstrap", "exit_code": 0, "time_utc": "2026-11-14T20:58:48Z", "time_utc_ms": "2026-11-14T20:58:48.830+00:00"}
{"byte_equal": true, "event": "s3_roundtrip", "phase": "bootstrap_2", "sha256": "15beb9311f947cccd73f69f39c44114ac8f7144c3b73b158610eba32f19fbcc8", "size_bytes": 65824, "time_utc": "2026-11-14T20:58:49Z", "time_utc_ms": "2026-11-14T20:58:49.494+00:00"}
{"detail": "", "elapsed_seconds": 2.742, "event": "case", "name": "s3_bootstrap_preserves_object_and_volume", "requirement": "requested_operational_check", "status": "PASS", "time_utc": "2026-11-14T20:58:49Z", "time_utc_ms": "2026-11-14T20:58:49.494+00:00"}
{"after": {"id": "1dd57160fa6a64fa3ea2e95e097c623d2beaef1926fc8d5f75e2aedff601eaa0", "image": "rustfs/rustfs:1.0.4", "running": true, "started_at": "2026-11-14T20:58:49.826562677Z", "volumes": ["lab4-final-offline_rustfs_data"]}, "before": {"id": "1dd57160fa6a64fa3ea2e95e097c623d2beaef1926fc8d5f75e2aedff601eaa0", "image": "rustfs/rustfs:1.0.4", "running": true, "started_at": "2026-11-14T20:58:40.118706673Z", "volumes": ["lab4-final-offline_rustfs_data"]}, "event": "s3_restart", "ready_seconds": 5.481, "time_utc": "2026-11-14T20:58:55Z", "time_utc_ms": "2026-11-14T20:58:55.101+00:00"}
{"byte_equal": true, "event": "s3_roundtrip", "phase": "after_restart", "sha256": "15beb9311f947cccd73f69f39c44114ac8f7144c3b73b158610eba32f19fbcc8", "size_bytes": 65824, "time_utc": "2026-11-14T20:58:55Z", "time_utc_ms": "2026-11-14T20:58:55.755+00:00"}
{"detail": "", "elapsed_seconds": 6.263, "event": "case", "name": "s3_restart_preserves_original_bytes", "requirement": "requested_operational_check", "status": "PASS", "time_utc": "2026-11-14T20:58:55Z", "time_utc_ms": "2026-11-14T20:58:55.757+00:00"}
{"bucket_list_status": 403, "event": "s3_unsigned", "object_get_status": 403, "time_utc": "2026-11-14T20:58:56Z", "time_utc_ms": "2026-11-14T20:58:56.059+00:00"}
{"detail": "", "elapsed_seconds": 0.301, "event": "case", "name": "s3_unsigned_known_object_403", "requirement": "requested_operational_check", "status": "PASS", "time_utc": "2026-11-14T20:58:56Z", "time_utc_ms": "2026-11-14T20:58:56.059+00:00"}
{"authorization_denied": false, "event": "s3_operation", "exit_code": 0, "operation": "api_list_allowed_bucket", "s3_error_code": null, "time_utc": "2026-11-14T20:58:56Z", "time_utc_ms": "2026-11-14T20:58:56.543+00:00"}
{"authorization_denied": false, "event": "s3_operation", "exit_code": 0, "operation": "prepare_control_bucket", "s3_error_code": null, "time_utc": "2026-11-14T20:58:57Z", "time_utc_ms": "2026-11-14T20:58:57.099+00:00"}
{"authorization_denied": false, "event": "s3_operation", "exit_code": 0, "operation": "prepare_control_object", "s3_error_code": null, "time_utc": "2026-11-14T20:58:57Z", "time_utc_ms": "2026-11-14T20:58:57.721+00:00"}
{"authorization_denied": true, "event": "s3_operation", "exit_code": 4, "operation": "api_other_bucket_put", "s3_error_code": "AccessDenied", "time_utc": "2026-11-14T20:58:58Z", "time_utc_ms": "2026-11-14T20:58:58.255+00:00"}
{"authorization_denied": true, "event": "s3_operation", "exit_code": 3, "operation": "api_other_bucket_get", "s3_error_code": "AccessDenied", "time_utc": "2026-11-14T20:58:58Z", "time_utc_ms": "2026-11-14T20:58:58.825+00:00"}
{"authorization_denied": true, "event": "s3_operation", "exit_code": 4, "operation": "api_other_bucket_delete", "s3_error_code": "AccessDenied", "time_utc": "2026-11-14T20:58:59Z", "time_utc_ms": "2026-11-14T20:58:59.375+00:00"}
{"authorization_denied": true, "event": "s3_operation", "exit_code": 3, "operation": "api_other_bucket_list", "s3_error_code": "AccessDenied", "time_utc": "2026-11-14T20:58:59Z", "time_utc_ms": "2026-11-14T20:58:59.847+00:00"}
{"authorization_denied": true, "event": "s3_operation", "exit_code": 4, "operation": "api_create_bucket", "s3_error_code": "AccessDenied", "time_utc": "2026-11-14T20:59:00Z", "time_utc_ms": "2026-11-14T20:59:00.330+00:00"}
{"authorization_denied": true, "event": "s3_operation", "exit_code": 1, "operation": "api_iam_user_list", "s3_error_code": "AccessDenied", "time_utc": "2026-11-14T20:59:00Z", "time_utc_ms": "2026-11-14T20:59:00.867+00:00"}
{"authorization_denied": true, "event": "s3_operation", "exit_code": 1, "operation": "api_iam_policy_list", "s3_error_code": "AccessDenied", "time_utc": "2026-11-14T20:59:01Z", "time_utc_ms": "2026-11-14T20:59:01.373+00:00"}
{"detail": "", "elapsed_seconds": 5.314, "event": "case", "name": "s3_api_bucket_scope_and_iam_denials", "requirement": "requested_operational_check", "status": "PASS", "time_utc": "2026-11-14T20:59:01Z", "time_utc_ms": "2026-11-14T20:59:01.373+00:00"}
{"event": "s3_effective_policy", "grants": [{"action": "s3:DeleteObject", "resource": "arn:aws:s3:::messenger-attachments/*"}, {"action": "s3:GetBucketLocation", "resource": "arn:aws:s3:::messenger-attachments"}, {"action": "s3:GetObject", "resource": "arn:aws:s3:::messenger-attachments/*"}, {"action": "s3:ListBucket", "resource": "arn:aws:s3:::messenger-attachments"}, {"action": "s3:PutObject", "resource": "arn:aws:s3:::messenger-attachments/*"}], "iam_admin_grants": 0, "inherited_groups": 0, "policy_name": "messenger-attachments-api-v1", "root_credentials_distinct": true, "time_utc": "2026-11-14T20:59:02Z", "time_utc_ms": "2026-11-14T20:59:02.317+00:00"}
{"detail": "", "elapsed_seconds": 0.945, "event": "case", "name": "s3_api_effective_policy", "requirement": "requested_operational_check", "status": "PASS", "time_utc": "2026-11-14T20:59:02Z", "time_utc_ms": "2026-11-14T20:59:02.318+00:00"}
{"console_curl_exit": 7, "console_http_status": "000", "control_status": 200, "event": "s3_console", "time_utc": "2026-11-14T20:59:03Z", "time_utc_ms": "2026-11-14T20:59:03.066+00:00"}
{"detail": "", "elapsed_seconds": 0.749, "event": "case", "name": "s3_console_closed_on_shared_network", "requirement": "requested_operational_check", "status": "PASS", "time_utc": "2026-11-14T20:59:03Z", "time_utc_ms": "2026-11-14T20:59:03.067+00:00"}
{"authorization_denied": false, "event": "s3_operation", "exit_code": 0, "operation": "api_delete_allowed_object", "s3_error_code": null, "time_utc": "2026-11-14T20:59:03Z", "time_utc_ms": "2026-11-14T20:59:03.561+00:00"}
{"authorization_denied": false, "event": "s3_operation", "exit_code": 0, "operation": "api_list_after_delete", "s3_error_code": null, "time_utc": "2026-11-14T20:59:03Z", "time_utc_ms": "2026-11-14T20:59:03.995+00:00"}
{"detail": "", "elapsed_seconds": 0.928, "event": "case", "name": "s3_api_delete_effect", "requirement": "requested_operational_check", "status": "PASS", "time_utc": "2026-11-14T20:59:03Z", "time_utc_ms": "2026-11-14T20:59:03.996+00:00"}
{"bindings": [{"HostIp": "127.0.0.1", "HostPort": "18094"}], "event": "public_lb_endpoint", "port": 18094, "time_utc": "2026-11-14T20:59:04Z", "time_utc_ms": "2026-11-14T20:59:04.404+00:00"}
{"api_ids": ["1c2fc22159776084669ac7ffdfc7ab18778485feb6810b4c534fb90baec30d62", "38de6c779806333887d344e72f5de2b4a4336b6ef02cc8bfe400809ad3b711d3"], "event": "s3_api_topology", "lb_id": "85e0635f043975812d11820b400edbf1e14428928e526e5a644e41fea9e210ce", "lb_image": "haproxy:3.2.24-alpine", "observed_instances": ["1c2fc2215977", "38de6c779806"], "same_token": true, "time_utc": "2026-11-14T20:59:04Z", "time_utc_ms": "2026-11-14T20:59:04.470+00:00"}
{"detail": "", "elapsed_seconds": 0.474, "event": "case", "name": "s3_two_replicas_after_restart", "requirement": "requested_operational_check", "status": "PASS", "time_utc": "2026-11-14T20:59:04Z", "time_utc_ms": "2026-11-14T20:59:04.471+00:00"}
{"already_absent": false, "event": "s3_cleanup", "exit_code": 0, "operation": "object remove", "target": "root/messenger-attachments/student-tests/s3-37a866cfecd04359ac1e1988712566b0/roundtrip-37a866cfecd04359ac1e1988712566b0.bin", "time_utc": "2026-11-14T20:59:05Z", "time_utc_ms": "2026-11-14T20:59:05.546+00:00"}
{"already_absent": false, "event": "s3_cleanup", "exit_code": 0, "operation": "object remove", "target": "root/student-control-37a866cfecd04359ac1e1988712566b0/sentinel.bin", "time_utc": "2026-11-14T20:59:06Z", "time_utc_ms": "2026-11-14T20:59:06.143+00:00"}
{"already_absent": false, "event": "s3_cleanup", "exit_code": 0, "operation": "object remove", "target": "root/student-control-37a866cfecd04359ac1e1988712566b0/api-write.bin", "time_utc": "2026-11-14T20:59:06Z", "time_utc_ms": "2026-11-14T20:59:06.827+00:00"}
{"already_absent": false, "event": "s3_cleanup", "exit_code": 0, "operation": "bucket remove", "target": "root/student-control-37a866cfecd04359ac1e1988712566b0", "time_utc": "2026-11-14T20:59:07Z", "time_utc_ms": "2026-11-14T20:59:07.803+00:00"}
{"already_absent": true, "event": "s3_cleanup", "exit_code": 5, "operation": "bucket remove", "target": "root/student-forbidden-37a866cfecd04359ac1e1988712566b0", "time_utc": "2026-11-14T20:59:08Z", "time_utc_ms": "2026-11-14T20:59:08.516+00:00"}
{"detail": "", "elapsed_seconds": 4.046, "event": "case", "name": "s3_fixture_cleanup", "requirement": "requested_operational_check", "status": "PASS", "time_utc": "2026-11-14T20:59:08Z", "time_utc_ms": "2026-11-14T20:59:08.517+00:00"}
{"detail": "", "elapsed_seconds": 26.37, "event": "case", "name": "s3_setup_and_scenarios", "requirement": "requested_operational_check", "status": "PASS", "time_utc": "2026-11-14T20:59:08Z", "time_utc_ms": "2026-11-14T20:59:08.517+00:00"}
+106
View File
@@ -0,0 +1,106 @@
{
"files": {
"Dockerfile": "17e5c9fa261dbe5395791213f605787824957da6567586ce990e44e3fd5a1236",
"Dockerfile.migrate": "0ab04cba4c2bb033c4d7c7d3a5984d8a27e8f355ff90e6023c3b7912d2211dec",
"cmd/api/main.go": "e9568a3a1098b01fded10cdc502dac652bf6a3a5761914c6860c9f45b56ed269",
"cmd/api/poolstats.go": "75c0ca950b85eaee3c0802e3001c61ba6436874b3be12cd38a5feb907b91528e",
"cmd/attachment-cleanup/main.go": "347895c38d76e0c2d866dc4e1eb5b7555b447b796a77c5da8c2d6e7198f614d3",
"cmd/migrate/main.go": "6e9d413a86f53cf5f2e83d5ed0308af9b2c33e69fcb3d061b19bf480e352f257",
"cmd/migrate/main_test.go": "a88f2252d70caf644904331182dc3afd4df48b825a0329a82482798ec90b5d46",
"cmd/migrate/postgres_test.go": "37401cd60657af560d52e727b08861f444ab18355d9decd9ceeeddb009d079ca",
"cmd/migrate/roundtrip_test.go": "25b3425447b1dbafec83db2e5a7fc8172031908d5f5d57ce7b0d36a97850f8fa",
"compose.yaml": "e67836432e988a1035a91ed028471e801fcf0827fb6ff4070f1e583abc06d787",
"db/migrations/00001_init_schema.sql": "32223afbc24f8250de055690c9e86299ebf2cd7a0d3ef2c59d3b2a5aaa41e84e",
"db/migrations/00002_add_created_at_defaults.sql": "e0680838294b93f916d727a3a20d83aca7c3f74077202c9696b1fc73cda14649",
"db/migrations/00003_drop_session_table.sql": "1b666cee95b9eb1210b0a303de95994bd3c24628e9d9fdb1e1f3318edb756c3d",
"db/migrations/00004_message_idempotency.sql": "e7b3dd5a056a702dfde7acba6666430477db0c7e03ae03fb270e145d6303b9b2",
"db/migrations/00005_attachments.sql": "c28ca64fb84d6c60dc181e4cb0022461894274b85970df84bc38bb9984585bb4",
"db/migrations/00006_allow_attachment_only_messages.sql": "ad56ecd0e8b88e0665cdc144aaa5d934ea7f48047cd702ce6f8832b6cf0bded3",
"db/migrations/embed.go": "a42e52ab05bc89b557a5bc6b3f0215883d99c4a039a8b32d6c19e66b2f769508",
"db/queries/attachments.sql": "15beeee95faf5b608179b0977a7dd45cd14a1c14df022e4c313194cb307646a0",
"db/queries/chat_members.sql": "e70940e35f325550041c290a571901ead4a6f93dece857ffa229f2f1c83eddf8",
"db/queries/chats.sql": "8c52c762da78eb3a3eb7402b17ec87bd7fc2e1387f6ddb8e297b4f2d75beca7b",
"db/queries/message_idempotence.sql": "34259e4beee9cc07de4dec95c1d588222c7d756d02ac6564b54d6dbec36678db",
"db/queries/messages.sql": "022c9f7aade55345f6cebd31f38084e73cb9a1419d56fa5316eaa36c9da52860",
"db/queries/messages_attachments.sql": "753e5b46dbe94b1043fbe19c83850e697feef7ea9ccc348496b71cb6f9906ddb",
"db/queries/users.sql": "0d73a6d46f4d4a1b0af37b750a208c07a38249f76974b41748f32c1949f990df",
"go.mod": "0dbf79ae49966e763868e20b453addaecbb74085dd7b0579271f0839ca8a17cd",
"go.sum": "30264a0cbc88f40ac455abb621624a54aa956bedd390c07deef4f67812efc982",
"infra/README.md": "0e804fa51debba4c38d3cc84abf4704897665e0335b272060363f03aadd6354f",
"infra/haproxy.cfg": "895a178e7354de61a5d40a2fe18b7b7c6d1d3d5faf0e5a5faa72783e7b983cb8",
"infra/s3/bootstrap.sh": "baa4b0b6fa4bd2a76b194b672ef2ca8575858ef6abc7ddfbede3eec3534bc8ce",
"internal/config/config.go": "142318dac05e3070358e7642c4b09813fe6bdb4d1b80e1154954d5e4e5f3fa59",
"internal/database/postgres.go": "3925c0bc9ae01bff9a37138a0dd19b38074bc2a70536b3a148f568b952516a21",
"internal/database/redis.go": "5b85a60bb0d1cb156c61bcd3c518c41c955f53c1d12da265b5e3a5dd7a397602",
"internal/database/sqlc/attachments.sql.go": "15c2c687f98031d4f360ca9db10670f9dca3d7188d3a5b7df9d7705d55632871",
"internal/database/sqlc/chat_members.sql.go": "9e8cd177f6f888c72d19857fd4748fbb2020770c1af772a463fe3d1b813b37ee",
"internal/database/sqlc/chats.sql.go": "3dccf4b0ffa918ec5a3b66026b3dab4027456b499486359881000d184944802c",
"internal/database/sqlc/db.go": "3ca71a7f007a39987cae838da64303419dd804d2d1b6227676f47177971d76cf",
"internal/database/sqlc/message_idempotence.sql.go": "ebd31e6f369483eed8d6978dab1687552eed742a612b5ee4090499fe8a48a73f",
"internal/database/sqlc/messages.sql.go": "224d6d8bd21426759ca6500e0eb3563564d51e639f1b6c64db4b3a43a5241c2a",
"internal/database/sqlc/messages_attachments.sql.go": "821d863a01079edae53805769821f1dc5eb3e32620ce61efd7326bb0253d2204",
"internal/database/sqlc/models.go": "df06356e705031dfc6226efd736f432853e026c26c30424675b99179819d422e",
"internal/database/sqlc/users.sql.go": "b954abd5b5964df39c20d857a60105513af84c8df68ce1ac9421da541a7800f4",
"internal/model/attachment.go": "877de511f5996c263c63c3e406ed67ee6609833f4946074594e1d438f8dce53a",
"internal/model/chat.go": "e0213f8360ea106b3627aa50cc87710153ff8e353475b42c2becdb9a32592660",
"internal/model/message.go": "4cebc4963e2eb104e85a1df6e8a93d6745c9ae42425df2d04bf6319e1d17b12d",
"internal/model/session.go": "b455d730e4e3efc95c6d1fe74ab43d86153e4bf863c09efd36bfe57efc669bc2",
"internal/model/user.go": "4bfd2d78e72f1c80b12541f2abc8500613e8f4d101575bd5d74fac22b52e3617",
"internal/repository/attachment.go": "75069a930484f0c464a3d237c42aedaa5cc58b606068b8834bc78184426f7d46",
"internal/repository/attachment_tx.go": "28fa001ae601fcc9b54e1e097b6e895d2f6ffb40b006c48a8862e0c822a2d896",
"internal/repository/chat.go": "7ef8dca39ff371f16d351b073199c25f719db22c7a525ab76ea7aca948efa157",
"internal/repository/errors.go": "9e3f77340ff87e41edc87380f795759701980685478737e7e4e1f9737e2634fa",
"internal/repository/message.go": "2f92e2808317850ca5873cc26f8e6c4d83104e8fc957a89304fc74ba2f64cbf2",
"internal/repository/postgres/attachment.go": "c37254509a9bf24158b73b8ae805c229a28b2aa2a203f7106d6c8a1ac69b10cd",
"internal/repository/postgres/chat.go": "d757c579e5b6599ab9366aef46fd59f9652247f3d947615aa46c919746b65ea5",
"internal/repository/postgres/errors.go": "580e5e599f7af8a403404c6850986e88efceda6393d21e772ac108be34004ba9",
"internal/repository/postgres/message.go": "a2057f6fe6ba2a93a7f98bdbd799baf043999811e9fa7bb649e4f0d60138b6c2",
"internal/repository/postgres/user.go": "eb6770e5cdd96b16a6e9d2116af89a6dd466dfbca4f4c179fe06c30c487e53c4",
"internal/repository/postgres/utils.go": "6c5c83a8ad8ecf611812fbf8c90735e2590a09dec88cd914a10f6b36a9f041b7",
"internal/repository/redis/errors.go": "ab56571d1d0b7d13b7d7ff868916f7e506c5a54c29e6bb211b3021ad4bf601ab",
"internal/repository/redis/session.go": "c1654fea2ba47734a5403d6f6cbb2fe70fbe7e6e37166ae75f338d3490981de1",
"internal/repository/session.go": "071285ff4d8033a6512a7256583a1cb32de782d6fd9f1696fac109927f7db699",
"internal/repository/user.go": "bee65136137d97bea2240411c96a93cd945b1b9145452416f7a83733a1fd28e7",
"internal/security/password.go": "8f74bef9216a2d12aa734e04fa786eac6b31f1378fe42f729fe1b97495c10487",
"internal/service/attachment.go": "4b901d85d93d6dffde1bf76dd532b4b6845afd55ea7a8f9aaea983eaead1e9da",
"internal/service/attachment_cleanup.go": "8e0112bacba6d90447c2be9884f34e5ca0be808af172e4cdaa4e6dea0f906dbb",
"internal/service/auth.go": "71b8960640866e515e9060159af750c418d97a6373d2616c3b59b764172aa54c",
"internal/service/chat.go": "bc7c8bea0066d8bbd926f78d2c100f0bd9a48c29fb542c5cfa3f2ae415299ef5",
"internal/service/errors.go": "702b41496201ec42a69591eaa7c5154bf2aaf52aecbc1318a87d4a32ccc5d5df",
"internal/service/helper.go": "390320fa296d9637630e6ac0d90c1229002b578a29186cad414896d5f4d1b8a6",
"internal/service/message.go": "9556a18a5679d7e44b5cdcf329488185d2b90cadb3180af926df4bb6ab3f9d0d",
"internal/service/user.go": "542051ef3eeff15d340979a615a7e0da3d21ed7ed93bea2547ab52d8e303b5e2",
"internal/storage/errors.go": "9cc85df576c98b29c9cc9ed244bdae2aa3d03361c3bdb0307c2940202e99c377",
"internal/storage/object_cleanup_storage.go": "ef50236baefe3e3aea8ca64264ae3c2e5fc89c245b9b1d7cfd207d354f0be2c9",
"internal/storage/object_writer.go": "15ca1257b9c42f64a42f19a4bbb487e9d2950aed2fdf87959fe6c2d763998b64",
"internal/storage/s3/client.go": "c6939f4dfd9ca8a7380010ff8551480cbff200b621af6f8b9ce444bd2a3f8a32",
"internal/storage/s3/errors.go": "f5e058597d8d3cba2e5db7e81eb4c0a87f7d8b1c1d17a0aff2409801be0cd701",
"internal/transport/http/handler/attachment.go": "885b2c4d24cbf0f5bfa9def4f6b5b4a7621919b47c3906d89a297abcf0c73da8",
"internal/transport/http/handler/attachment_types.go": "90210075401601bacb25ae70dea2e19a203f537cf0f5aff1369679619bb87811",
"internal/transport/http/handler/auth.go": "f175a570d47ca38ce627d0bc88b6f6a2adbf648345229fa961ea0144717744f1",
"internal/transport/http/handler/auth_types.go": "5ffcd182606a77d538efb5f1a51df23b8a324a605b5d226026048d34c409ebdb",
"internal/transport/http/handler/chat.go": "e5214ebf94ac9b8f37537e1a3fc844a2e8ae4e9b5aa159fe2b6e4da9ab2342f0",
"internal/transport/http/handler/chat_types.go": "d181bce0f003749d813c1254df4c8ebcc1b99ff506b61efa6899da4a499e95f4",
"internal/transport/http/handler/health.go": "1abf04652ea0b69bd87321b95e1a921d3c5ad3c04207232c2a9408bf722e550a",
"internal/transport/http/handler/health_types.go": "02ee3031e10fbb279e020f409fbd905ae2640aa306b2c558c3f8e6dcfe9efa80",
"internal/transport/http/handler/message.go": "540065ffc9090848fc25d560f1ad9734d4f0d45cdf0b5907a0f196f9efa8ede3",
"internal/transport/http/handler/message_types.go": "270a9504a3001b1dc02a1f62cdaed12d69af2471340674e50022c609643dcb34",
"internal/transport/http/handler/user.go": "86fd65ed176e9a83dc27c1112ff8384bc5aa32a92410290fb505d715ca167775",
"internal/transport/http/handler/user_types.go": "0c4450be649afe5edf4e8d3ac9baada79deb9e1423ace56b87aa227ebd0eb998",
"internal/transport/http/httputils/helper.go": "59c092950b69f5390427d0c6c1f90d2c33630d87a42bef363aed704960cb3a22",
"internal/transport/http/middleware/auth.go": "413d7fb5f8573a808c2eb4e36becd29b4d7edffe5b096b26fc07ac22a0285430",
"internal/transport/http/middleware/context.go": "f6db0e807659f30b05cae91f73c70d87f359760c57110e0d25b49a65bd0394a5",
"internal/transport/http/middleware/logging.go": "a477fda512953eb9f46266537c12ace9cc992ba8f036a5b041f9f8295bfd2097",
"internal/transport/http/router.go": "2841bac8988517591539336ba20718b4da7bae31ab81bf56e6b2b9bb59b0a297",
"scripts/restart.sh": "5f70beb4337aed16881a0728229f02dce0e95d42028e0088acd52aa0a76eb019",
"sqlc.yaml": "4e72de8f0b44d6d2b6a44da89f86dd5326e85077a5e7a8ae71816a5dff1fbaa3"
},
"source_tree_sha256": "f1dcdad773a3fb8de430ab9f85825dc61eba2954255dc5e0400c6c30ab9f84da",
"submission_whitespace_only": {
"db/migrations/00001_init_schema.sql": "48db014273f78be0f6eb97fc521cccc54f851dc99e3688d480d3a6ff7b9b6bbe",
"db/migrations/00005_attachments.sql": "033d8884cecb50b5067453a97f2d7c51e51331a1911d9ddf8687437afa59443c",
"db/queries/chat_members.sql": "c80b6e50c321ff94317545ea2f9fdee1dfd69f564d8638f7b111c8ec091ef03a",
"db/queries/messages.sql": "f6f6382f27d698b1c2af2305d45699226ef10806b938b3dca78ec9087a622b7b",
"internal/database/sqlc/messages.sql.go": "9836433d9645e6dd3bd66273ce15a53691f79093016c416fb24ffd085e62831e"
}
}
@@ -0,0 +1,323 @@
package main
import (
"bufio"
"bytes"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"image"
"image/jpeg"
"image/png"
"net"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"sync/atomic"
"time"
)
type settings struct {
Address, Instance, Token, Chat, CancelChat, EmptyCancelChat string
}
type status struct {
HTTP int `json:"http_status"`
Instance string `json:"instance"`
Code string `json:"error_code,omitempty"`
Retry string `json:"retry_after,omitempty"`
SHA string `json:"sha256,omitempty"`
Size int `json:"size_bytes,omitempty"`
RequestID string `json:"request_id,omitempty"`
Elapsed float64 `json:"elapsed_seconds"`
}
type event map[string]any
var events []event
func record(name string, data any) {
events = append(events, event{"event": name, "time_utc": time.Now().UTC().Format(time.RFC3339Nano), "data": data})
}
func check(ok bool, description string) error {
if !ok {
return errors.New(description)
}
return nil
}
func decode(resp *http.Response, start time.Time) (status, error) {
defer resp.Body.Close()
var value struct {
Error struct {
Code string `json:"code"`
} `json:"error"`
SHA string `json:"sha256"`
Size int `json:"size_bytes"`
}
err := json.NewDecoder(resp.Body).Decode(&value)
return status{HTTP: resp.StatusCode, Instance: resp.Header.Get("X-Instance-Id"), Retry: resp.Header.Get("Retry-After"),
Code: value.Error.Code, SHA: value.SHA, Size: value.Size, RequestID: resp.Header.Get("X-Request-Id"), Elapsed: time.Since(start).Seconds()}, err
}
func path(chat, filename string) string {
return "/api/v1/chats/" + chat + "/attachments?filename=" + filename
}
func post(c settings, filename, mime string, body []byte) (status, error) {
start := time.Now()
req, err := http.NewRequest("POST", "http://"+c.Address+path(c.Chat, filename), bytes.NewReader(body))
if err != nil {
return status{}, err
}
req.Header.Set("Content-Type", mime)
req.Header.Set("Authorization", "Bearer "+c.Token)
// One connection per upload makes the simultaneous requests independent.
req.Close = true
resp, err := (&http.Client{Timeout: 12 * time.Second}).Do(req)
if err != nil {
return status{Elapsed: time.Since(start).Seconds()}, err
}
return decode(resp, start)
}
type pending struct {
conn net.Conn
reader *bufio.Reader
start time.Time
}
func begin(c settings, chat, filename string, length int, expect bool) (*pending, error) {
conn, err := net.DialTimeout("tcp", c.Address, 2*time.Second)
if err != nil {
return nil, err
}
conn.SetDeadline(time.Now().Add(8 * time.Second))
expectation := ""
if expect {
expectation = "Expect: 100-continue\r\n"
}
_, err = fmt.Fprintf(conn, "POST %s HTTP/1.1\r\nHost: %s\r\nAuthorization: Bearer %s\r\nContent-Type: image/png\r\nContent-Length: %d\r\n%sConnection: close\r\n\r\n", path(chat, filename), c.Address, c.Token, length, expectation)
if err != nil {
conn.Close()
return nil, err
}
return &pending{conn, bufio.NewReader(conn), time.Now()}, nil
}
func (p *pending) provisional() error {
response, err := http.ReadResponse(p.reader, nil)
if err != nil {
return err
}
response.Body.Close()
return check(response.StatusCode == 100, fmt.Sprintf("expected provisional 100, got %d", response.StatusCode))
}
func (p *pending) finish(body []byte) (status, error) {
if _, err := p.conn.Write(body); err != nil {
return status{}, err
}
response, err := http.ReadResponse(p.reader, nil)
if err != nil {
return status{}, err
}
return decode(response, p.start)
}
func valid(value status, expectedInstance string, payload []byte) error {
return check(value.HTTP == 201 && (expectedInstance == "" || value.Instance == expectedInstance) &&
value.SHA == fmt.Sprintf("%x", sha256.Sum256(payload)) && value.Size == len(payload) &&
value.RequestID != "", fmt.Sprintf("expected valid 201 from %s, got %d from %s", expectedInstance, value.HTTP, value.Instance))
}
func controlled(c settings) error {
body, err := os.ReadFile("/input/sample.png")
if err != nil {
return err
}
first, err := begin(c, c.Chat, "held-a.png", len(body), true)
if err != nil {
return err
}
defer first.conn.Close()
if err := first.provisional(); err != nil {
return err
}
second, err := begin(c, c.Chat, "held-b.png", len(body), true)
if err != nil {
return err
}
defer second.conn.Close()
if err := second.provisional(); err != nil {
return err
}
record("both_slots_held", event{"provisional_statuses": []int{100, 100}, "body_bytes_sent": 0})
limited, err := post(c, "limited.png", "image/png", body)
record("third_upload", limited)
if err != nil {
return err
}
if err := check(limited.HTTP == 429 && limited.Code == "rate_limited" && limited.Retry == "1" &&
limited.Instance == c.Instance && limited.RequestID != "" && limited.Elapsed >= .9 && limited.Elapsed <= 1.5,
"third upload did not return the expected 429, Retry-After and one-second wait"); err != nil {
return err
}
for _, cancel := range []struct {
chat string
size int
}{
{c.CancelChat, len(body)}, {c.EmptyCancelChat, 0},
} {
waiter, err := begin(c, cancel.chat, "cancel.png", cancel.size, false)
if err != nil {
return err
}
time.Sleep(220 * time.Millisecond)
waiter.conn.Close()
record("waiter_disconnected", event{"chat_id": cancel.chat, "declared_bytes": cancel.size, "body_bytes_sent": 0,
"disconnect_after_seconds": time.Since(waiter.start).Seconds()})
}
// Both slots stay occupied for longer than the wait timeout, so server logs
// distinguish cancellation from a later slot grant.
time.Sleep(1150 * time.Millisecond)
r, err := first.finish(body)
record("first_holder_finished", r)
if err != nil {
return err
}
if err := valid(r, c.Instance, body); err != nil {
return err
}
r, err = post(c, "after-release.png", "image/png", body)
record("upload_after_release", r)
if err != nil {
return err
}
if err := valid(r, c.Instance, body); err != nil {
return err
}
r, err = second.finish(body)
record("second_holder_finished", r)
if err != nil {
return err
}
return valid(r, c.Instance, body)
}
func load(c settings) error {
pngData, err := os.ReadFile("/input/load.png")
if err != nil {
return err
}
jpegData, err := os.ReadFile("/input/load.jpg")
if err != nil {
return err
}
payloads := []struct {
name, mime string
data []byte
}{{"load.png", "image/png", pngData}, {"load.jpg", "image/jpeg", jpegData}}
const count = 12
var wg, ready sync.WaitGroup
var active, peak atomic.Int32
start := make(chan struct{})
done := make(chan event, count)
for i := 0; i < count; i++ {
wg.Add(1)
ready.Add(1)
go func(i int) {
defer wg.Done()
p := payloads[i%len(payloads)]
ready.Done()
<-start
n := active.Add(1)
for current := peak.Load(); n > current && !peak.CompareAndSwap(current, n); current = peak.Load() {
}
value, err := post(c, p.name, p.mime, p.data)
active.Add(-1)
detail := event{"request": i + 1, "file": p.name, "response": value}
if err == nil {
if value.HTTP == 201 {
err = valid(value, "", p.data)
} else if value.HTTP == 429 {
err = check(value.Code == "rate_limited" && value.Retry == "1" && value.Instance != "" && value.RequestID != "", "invalid rate limit response")
} else {
err = fmt.Errorf("unexpected status %d", value.HTTP)
}
}
if err != nil {
detail["failure"] = err.Error()
}
done <- detail
}(i)
}
ready.Wait()
close(start)
wg.Wait()
close(done)
failures := 0
for detail := range done {
record("concurrent_response", detail)
if detail["failure"] != nil {
failures++
}
}
record("concurrent_peak", event{"workers": count, "client_in_flight_peak": peak.Load()})
return check(peak.Load() >= 10 && failures == 0, "load had fewer than 10 overlapping requests or contained unexpected responses")
}
func generate(directory string) error {
const width, height = 6000, 6000
img := image.NewNRGBA(image.Rect(0, 0, width, height))
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
i := y*img.Stride + x*4
img.Pix[i] = byte(x/12 + y/36)
img.Pix[i+1] = byte(x/24 - y/9)
img.Pix[i+2] = byte((x / 8) ^ (y / 16))
img.Pix[i+3] = 255
}
}
for _, name := range []string{"load.png", "load.jpg"} {
f, err := os.Create(filepath.Join(directory, name))
if err != nil {
return err
}
if strings.HasSuffix(name, "png") {
err = png.Encode(f, img)
} else {
err = jpeg.Encode(f, img, &jpeg.Options{Quality: 90})
}
f.Close()
if err != nil {
return err
}
}
return nil
}
func main() {
if len(os.Args) != 2 {
os.Exit(2)
}
if os.Args[1] == "generate" {
if err := generate("."); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
return
}
var c settings
if err := json.NewDecoder(os.Stdin).Decode(&c); err != nil {
os.Exit(2)
}
var err error
switch os.Args[1] {
case "controlled":
err = controlled(c)
case "load":
err = load(c)
default:
err = errors.New("unknown mode")
}
report := event{"events": events}
if err != nil {
report["failure"] = err.Error()
}
json.NewEncoder(os.Stdout).Encode(report)
if err != nil {
os.Exit(1)
}
}
+30
View File
@@ -0,0 +1,30 @@
# Проверка двусторонней сверки
Понадобится новая копия проекта, Docker с Compose, Python 3.9+ и OpenSSL.
Перед экспериментом убедитесь, что выбранные порты свободны.
```sh
export LAB4_HTTP_PORT=18096 LAB4_POSTGRES_PORT=15496
export LAB4_CLEANUP_DRY_RUN=true
sh scripts/init-local.sh
docker compose -p lab4-final-grade5 --env-file .env up -d --build --wait
python3 student-tests/lab4/reconcile_experiment.py \
--stack "$PWD" --project lab4-final-grade5 \
--base-url http://127.0.0.1:18096 --output /tmp/lab4-reconcile
docker compose -p lab4-final-grade5 --env-file .env down --volumes
```
Эксперимент требует отдельный свежий стенд: он удаляет строки и файлы
синтетических участников, затем заполняет тестовые данные для замера. Рабочий
grace составляет 10 минут. Для воспроизводимости тест сокращает его до
4 секунд в сценариях ошибок и 1 секунды в замерах без ошибок.
[Журнал эксперимента](experiment.txt) · [Счётчики и время всех проходов](reconcile.json)
· [Сборка и запуск](compose-build.txt) · [Миграции 1–6](migrate.txt)
Повторная проверка на Go 1.27.1 / Docker Compose 2.39.4:
[make check](check.txt) · [smoke](smoke.txt) · [перезапуск](persistence.txt)
· [failover](failover.txt) · [Go с PostgreSQL и Redis](go.txt).
Полный Go-прогон выполнен на чистой копии со всеми настройками PostgreSQL и Redis. Для Go-тестов дополнительный Compose override публиковал Redis только на loopback. Хуки перезапуска обращались к выделенному Redis и этому же проекту.
Повтор аварийной уборки на чистой версии: [результаты](compensation.json) · [события](compensation-events.jsonl).
+2
View File
@@ -0,0 +1,2 @@
python3 scripts/check_contract.py
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
@@ -0,0 +1,26 @@
{"event": "sql_isolation", "time_utc": "2026-11-15T08:49:07Z", "time_utc_ms": "2026-11-15T08:49:07.400+00:00", "value": "read committed"}
{"detail": "", "elapsed_seconds": 1.369, "event": "case", "name": "setup", "requirement": "requested_operational_check", "status": "PASS", "time_utc": "2026-11-15T08:49:07Z", "time_utc_ms": "2026-11-15T08:49:07.556+00:00"}
{"dry_run": true, "event": "one_shot", "exit_code": 0, "grace_seconds": 8, "time_utc": "2026-11-15T08:49:18Z", "time_utc_ms": "2026-11-15T08:49:18.502+00:00"}
{"dry_run": false, "event": "one_shot", "exit_code": 0, "grace_seconds": 8, "time_utc": "2026-11-15T08:49:20Z", "time_utc_ms": "2026-11-15T08:49:20.726+00:00"}
{"dry_run": false, "event": "one_shot", "exit_code": 0, "grace_seconds": 8, "time_utc": "2026-11-15T08:49:22Z", "time_utc_ms": "2026-11-15T08:49:22.489+00:00"}
{"dry_run": false, "event": "one_shot", "exit_code": 0, "grace_seconds": 8, "time_utc": "2026-11-15T08:49:23Z", "time_utc_ms": "2026-11-15T08:49:23.325+00:00"}
{"applied": true, "dry_run_kept": true, "event": "retention", "fresh_preserved": true, "held_keys_preserved": true, "keys": {"commit": "attachments/cleanup-2020d04dc6-busy-commit", "fresh": "attachments/cleanup-2020d04dc6-fresh", "linked": "attachments/2b6a237d-3e88-44fe-bd1b-d50f36be2e29", "orphan": "attachments/cleanup-2020d04dc6-orphan", "outside": "other/cleanup-2020d04dc6", "rowless": "attachments/cleanup-2020d04dc6-busy-rollback", "unlinked": "attachments/c7c9db8e-3ccd-4499-985d-ffd1858f5a58"}, "last_modified": {"attachments/2b6a237d-3e88-44fe-bd1b-d50f36be2e29": "2026-11-15T08:49:07.589000+00:00", "attachments/c7c9db8e-3ccd-4499-985d-ffd1858f5a58": "2026-11-15T08:49:07.567000+00:00", "attachments/cleanup-2020d04dc6-busy-commit": "2026-11-15T08:49:08.408000+00:00", "attachments/cleanup-2020d04dc6-busy-rollback": "2026-11-15T08:49:08.555000+00:00", "attachments/cleanup-2020d04dc6-orphan": "2026-11-15T08:49:08.081000+00:00", "other/cleanup-2020d04dc6": "2026-11-15T08:49:08.227000+00:00"}, "referenced_after_lock": true, "referenced_preserved": true, "repeated": true, "rowless_after_lock_deleted": true, "time_utc": "2026-11-15T08:49:23Z", "time_utc_ms": "2026-11-15T08:49:23.557+00:00"}
{"detail": "", "elapsed_seconds": 16.001, "event": "case", "name": "retention", "requirement": "requested_operational_check", "status": "PASS", "time_utc": "2026-11-15T08:49:23Z", "time_utc_ms": "2026-11-15T08:49:23.557+00:00"}
{"dry_run": false, "event": "one_shot", "exit_code": 0, "grace_seconds": 8, "time_utc": "2026-11-15T08:49:24Z", "time_utc_ms": "2026-11-15T08:49:24.957+00:00"}
{"dry_run": false, "event": "one_shot", "exit_code": 0, "grace_seconds": 8, "time_utc": "2026-11-15T08:49:33Z", "time_utc_ms": "2026-11-15T08:49:33.961+00:00"}
{"deleted_after_grace": true, "event": "after_s3_before_db", "http_status": 500, "key": "attachments/0623c85b-59c3-4915-9ce9-b9deb8ca257a", "last_modified": "2026-11-15T08:49:23.855000+00:00", "rows": 0, "survived_young": true, "time_utc": "2026-11-15T08:49:34Z", "time_utc_ms": "2026-11-15T08:49:34.174+00:00"}
{"detail": "", "elapsed_seconds": 10.617, "event": "case", "name": "fault_after_put", "requirement": "requested_operational_check", "status": "PASS", "time_utc": "2026-11-15T08:49:34Z", "time_utc_ms": "2026-11-15T08:49:34.175+00:00"}
{"bytes_declared": 111817, "bytes_sent": 256, "event": "client_disconnect", "objects_created": 0, "rows": 0, "subsequent_status": 201, "time_utc": "2026-11-15T08:49:35Z", "time_utc_ms": "2026-11-15T08:49:35.279+00:00"}
{"detail": "", "elapsed_seconds": 1.104, "event": "case", "name": "aborted_body", "requirement": "requested_operational_check", "status": "PASS", "time_utc": "2026-11-15T08:49:35Z", "time_utc_ms": "2026-11-15T08:49:35.279+00:00"}
{"api": "f9bf1d922ba338cfa81fce5a3e4dae7707e6003329d91fe8fd2fa16afed68fa6", "api_ip": "172.19.0.5", "backend_pid": 46, "event": "kill_boundary", "insert_waiting": true, "key": "attachments/7675f167-66f7-44aa-bb3f-d82345b21919", "row_count": 0, "time_utc": "2026-11-15T08:49:36Z", "time_utc_ms": "2026-11-15T08:49:36.588+00:00"}
{"api": "f9bf1d922ba338cfa81fce5a3e4dae7707e6003329d91fe8fd2fa16afed68fa6", "backend_wait_before_unblock": "advisory", "client_connection_check_interval": "0", "event": "sigkill", "exit_code": 137, "signal": 9, "time_utc": "2026-11-15T08:49:37Z", "time_utc_ms": "2026-11-15T08:49:37.153+00:00"}
{"dry_run": false, "event": "one_shot", "exit_code": 0, "grace_seconds": 8, "time_utc": "2026-11-15T08:49:38Z", "time_utc_ms": "2026-11-15T08:49:38.156+00:00"}
{"dry_run": false, "event": "one_shot", "exit_code": 0, "grace_seconds": 8, "time_utc": "2026-11-15T08:49:46Z", "time_utc_ms": "2026-11-15T08:49:46.142+00:00"}
{"api": "f9bf1d922ba338cfa81fce5a3e4dae7707e6003329d91fe8fd2fa16afed68fa6", "backend_released": true, "client": {"error": "Failure"}, "event": "killed_api", "key": "attachments/7675f167-66f7-44aa-bb3f-d82345b21919", "old_deleted": true, "rows": 0, "time_utc": "2026-11-15T08:49:46Z", "time_utc_ms": "2026-11-15T08:49:46.321+00:00", "young_preserved": true}
{"detail": "", "elapsed_seconds": 11.341, "event": "case", "name": "killed_api_after_put", "requirement": "requested_operational_check", "status": "PASS", "time_utc": "2026-11-15T08:49:46Z", "time_utc_ms": "2026-11-15T08:49:46.622+00:00"}
{"dry_run": false, "event": "one_shot", "exit_code": 0, "grace_seconds": 8, "time_utc": "2026-11-15T08:49:56Z", "time_utc_ms": "2026-11-15T08:49:56.506+00:00"}
{"downstream_bytes": 0, "event": "lost_response", "key": "attachments/c52011a2-c360-458e-a806-c1d37a1a9ffd", "rows": 1, "sha256": "99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75", "survived_apply": true, "time_utc": "2026-11-15T08:49:56Z", "time_utc_ms": "2026-11-15T08:49:56.539+00:00", "upstream_status": 201}
{"detail": "", "elapsed_seconds": 9.917, "event": "case", "name": "lost_response", "requirement": "requested_operational_check", "status": "PASS", "time_utc": "2026-11-15T08:49:56Z", "time_utc_ms": "2026-11-15T08:49:56.539+00:00"}
{"deleted_seconds": 8.679, "event": "periodic", "key": "attachments/cleanup-2020d04dc6-periodic", "time_utc": "2026-11-15T08:50:05Z", "time_utc_ms": "2026-11-15T08:50:05.441+00:00"}
{"during": "EXISTS", "event": "sigterm", "exit_code": 0, "key": "attachments/cleanup-2020d04dc6-signal", "lock_released": true, "restart_deleted": true, "time_utc": "2026-11-15T08:50:16Z", "time_utc_ms": "2026-11-15T08:50:16.194+00:00"}
{"detail": "", "elapsed_seconds": 19.777, "event": "case", "name": "periodic_sigterm", "requirement": "requested_operational_check", "status": "PASS", "time_utc": "2026-11-15T08:50:16Z", "time_utc_ms": "2026-11-15T08:50:16.319+00:00"}
+44
View File
@@ -0,0 +1,44 @@
{
"aborted_body": {
"detail": "",
"elapsed_seconds": 1.104,
"requirement": "requested_operational_check",
"status": "PASS"
},
"fault_after_put": {
"detail": "",
"elapsed_seconds": 10.617,
"requirement": "requested_operational_check",
"status": "PASS"
},
"killed_api_after_put": {
"detail": "",
"elapsed_seconds": 11.341,
"requirement": "requested_operational_check",
"status": "PASS"
},
"lost_response": {
"detail": "",
"elapsed_seconds": 9.917,
"requirement": "requested_operational_check",
"status": "PASS"
},
"periodic_sigterm": {
"detail": "",
"elapsed_seconds": 19.777,
"requirement": "requested_operational_check",
"status": "PASS"
},
"retention": {
"detail": "",
"elapsed_seconds": 16.001,
"requirement": "requested_operational_check",
"status": "PASS"
},
"setup": {
"detail": "",
"elapsed_seconds": 1.369,
"requirement": "requested_operational_check",
"status": "PASS"
}
}
+236
View File
@@ -0,0 +1,236 @@
#1 [internal] load local bake definitions
#1 reading from stdin 1.51kB done
#1 DONE 0.0s
#2 [migrate internal] load build definition from Dockerfile.migrate
#2 transferring dockerfile: 448B done
#2 DONE 0.0s
#3 [attachment-cleanup internal] load build definition from Dockerfile
#3 transferring dockerfile: 855B done
#3 DONE 0.0s
#4 [migrate internal] load metadata for docker.io/library/golang:1.27.1-alpine
#4 DONE 2.6s
#5 [attachment-cleanup internal] load metadata for docker.io/library/alpine:3.22
#5 DONE 2.6s
#6 [api internal] load .dockerignore
#6 transferring context: 196B done
#6 DONE 0.0s
#7 [migrate builder 1/9] FROM docker.io/library/golang:1.27.1-alpine@sha256:8a5910f31396cd4d89662f56c68b3ae31d374308270a1c3bd96672ee5ed43414
#7 resolve docker.io/library/golang:1.27.1-alpine@sha256:8a5910f31396cd4d89662f56c68b3ae31d374308270a1c3bd96672ee5ed43414 0.0s done
#7 DONE 0.0s
#8 [migrate runtime 1/3] FROM docker.io/library/alpine:3.22@sha256:49340b346798875b5592e5d420241f2f9cfba5f47e62ebb1446a1b34734e898f
#8 resolve docker.io/library/alpine:3.22@sha256:49340b346798875b5592e5d420241f2f9cfba5f47e62ebb1446a1b34734e898f 0.0s done
#8 DONE 0.0s
#9 [migrate internal] load build context
#9 transferring context: 41.50kB done
#9 DONE 0.0s
#10 [migrate builder 4/7] RUN go mod download
#10 CACHED
#11 [migrate builder 3/7] COPY go.mod go.sum ./
#11 CACHED
#12 [migrate builder 5/7] COPY cmd/migrate/ ./cmd/migrate/
#12 CACHED
#13 [attachment-cleanup internal] load build context
#13 transferring context: 138.51kB 0.0s done
#13 DONE 0.0s
#14 [api builder 2/9] WORKDIR /app
#14 CACHED
#15 [api builder 3/9] COPY go.mod go.sum ./
#15 CACHED
#16 [api builder 4/9] RUN go mod download
#16 CACHED
#17 [api builder 5/9] COPY cmd/api/ ./cmd/api/
#17 CACHED
#18 [migrate builder 6/7] COPY db/migrations/ ./db/migrations/
#18 DONE 0.0s
#19 [api builder 6/9] COPY cmd/attachment-cleanup/ ./cmd/attachment-cleanup/
#19 DONE 0.0s
#20 [api builder 7/9] COPY internal/ ./internal/
#20 DONE 0.0s
#21 [api builder 8/9] RUN CGO_ENABLED=0 GOOS=linux go build -o /app/server ./cmd/api
#21 ...
#22 [migrate builder 7/7] RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -o /app/migrate ./cmd/migrate
#22 DONE 19.9s
#21 [api builder 8/9] RUN CGO_ENABLED=0 GOOS=linux go build -o /app/server ./cmd/api
#21 ...
#23 [migrate runtime 2/3] RUN addgroup -S app && adduser -S app -G app
#23 CACHED
#24 [migrate runtime 3/3] WORKDIR /app
#24 CACHED
#23 [attachment-cleanup runtime 2/3] RUN addgroup -S app && adduser -S app -G app
#23 CACHED
#25 [migrate stage-1 4/4] COPY --from=builder /app/migrate ./migrate
#25 DONE 0.3s
#21 [api builder 8/9] RUN CGO_ENABLED=0 GOOS=linux go build -o /app/server ./cmd/api
#21 ...
#26 [migrate] exporting to image
#26 exporting layers
#26 exporting layers 1.1s done
#26 exporting manifest sha256:f4b196e5c4d235b5593dfdf9fcc9f7f3943bdf78a380d9cbf703449ec1fd730a 0.0s done
#26 exporting config sha256:4194b73a1ba046593f5e0d4a8dc79c9f0a1ac5ced1c2ff9f29fbf6d765ebebe3 0.1s done
#26 exporting attestation manifest sha256:5c34cb23b4efd9767c126fb43a0a87b63a436ee2299436d38a65183fba7f47e0 0.0s done
#26 exporting manifest list sha256:f78bc60117392a76f0f177361994171d58d74cf218a30eae6cb6a998de7654bf
#26 exporting manifest list sha256:f78bc60117392a76f0f177361994171d58d74cf218a30eae6cb6a998de7654bf done
#26 naming to docker.io/library/lab4-final-grade5-migrate:latest done
#26 unpacking to docker.io/library/lab4-final-grade5-migrate:latest 0.1s done
#26 DONE 1.4s
#21 [api builder 8/9] RUN CGO_ENABLED=0 GOOS=linux go build -o /app/server ./cmd/api
#21 ...
#27 [migrate] resolving provenance for metadata file
#27 DONE 0.1s
#21 [api builder 8/9] RUN CGO_ENABLED=0 GOOS=linux go build -o /app/server ./cmd/api
#21 DONE 39.9s
#28 [attachment-cleanup builder 9/9] RUN CGO_ENABLED=0 GOOS=linux go build -o /app/attachment-cleanup ./cmd/attachment-cleanup
#28 DONE 1.5s
#29 [attachment-cleanup cleanup 1/1] COPY --from=builder /app/attachment-cleanup ./attachment-cleanup
#29 DONE 0.0s
#30 [api api 1/1] COPY --from=builder /app/server ./server
#30 DONE 0.1s
#31 [attachment-cleanup] exporting to image
#31 exporting layers
#31 exporting layers 0.6s done
#31 exporting manifest sha256:2ac7bb862c98bfad9f7c17ef0df831bcaff7de5aa8448fa674e823fdc4e45606
#31 exporting manifest sha256:2ac7bb862c98bfad9f7c17ef0df831bcaff7de5aa8448fa674e823fdc4e45606 done
#31 exporting config sha256:1d3c8c064add7ba3996ddaf974bbf81ae95af4cea677816405d7002dbd2c1577 done
#31 exporting attestation manifest sha256:a578b63ee12ddd77fef902967a1c135df5e179486c652f31ca589124d398cfd7 0.0s done
#31 exporting manifest list sha256:720d441943e7f9a8c89dd2e1e275c1a5c0e748973b2e00f0d5f80dae0f375883 0.0s done
#31 naming to docker.io/library/lab4-final-grade5-attachment-cleanup:latest done
#31 unpacking to docker.io/library/lab4-final-grade5-attachment-cleanup:latest 0.1s done
#31 DONE 0.7s
#32 [attachment-cleanup] resolving provenance for metadata file
#32 DONE 0.0s
#33 [api] exporting to image
#33 exporting layers 0.6s done
#33 exporting manifest sha256:09bff2b9862296c85b67d43968c0ac6b704bcdae387f9d57a90b60373ba7789b done
#33 exporting config sha256:fbd04f42a1ccb2f2cd4e4c51686ac0bf3ef02cd4d37a252cb0236858a476f511 done
#33 exporting attestation manifest sha256:2185a5dd97b2281771aafe47e1c3ad0a097eb6d1c14f2782e0c8021d1fb17a65 0.0s done
#33 exporting manifest list sha256:4cb2ecb3a51c6ad66a581c9169662d17eb5c6f9fd183ebcefdd87d3b7caf51c8 0.0s done
#33 naming to docker.io/library/lab4-final-grade5-api:latest done
#33 unpacking to docker.io/library/lab4-final-grade5-api:latest 0.1s done
#33 DONE 0.8s
#34 [api] resolving provenance for metadata file
#34 DONE 0.0s
lab4-final-grade5-migrate Built
lab4-final-grade5-api Built
lab4-final-grade5-attachment-cleanup Built
Network lab4-final-grade5_default Creating
Network lab4-final-grade5_default Created
Volume lab4-final-grade5_postgres_data Creating
Volume lab4-final-grade5_postgres_data Created
Volume lab4-final-grade5_redis_data Creating
Volume lab4-final-grade5_redis_data Created
Volume lab4-final-grade5_rustfs_data Creating
Volume lab4-final-grade5_rustfs_data Created
Container lab4-final-grade5-redis-1 Creating
Container lab4-final-grade5-s3-1 Creating
Container lab4-final-grade5-postgres-1 Creating
Container lab4-final-grade5-s3-1 Created
Container lab4-final-grade5-s3-init-1 Creating
Container lab4-final-grade5-redis-1 Created
Container lab4-final-grade5-postgres-1 Created
Container lab4-final-grade5-migrate-1 Creating
Container lab4-final-grade5-s3-init-1 Created
Container lab4-final-grade5-migrate-1 Created
Container lab4-final-grade5-attachment-cleanup-1 Creating
Container lab4-final-grade5-api-2 Creating
Container lab4-final-grade5-api-1 Creating
Container lab4-final-grade5-api-1 Created
Container lab4-final-grade5-api-2 Created
Container lab4-final-grade5-lb-1 Creating
Container lab4-final-grade5-attachment-cleanup-1 Created
Container lab4-final-grade5-lb-1 Created
Container lab4-final-grade5-s3-1 Starting
Container lab4-final-grade5-postgres-1 Starting
Container lab4-final-grade5-redis-1 Starting
Container lab4-final-grade5-postgres-1 Started
Container lab4-final-grade5-postgres-1 Waiting
Container lab4-final-grade5-redis-1 Started
Container lab4-final-grade5-s3-1 Started
Container lab4-final-grade5-s3-1 Waiting
Container lab4-final-grade5-postgres-1 Healthy
Container lab4-final-grade5-migrate-1 Starting
Container lab4-final-grade5-s3-1 Healthy
Container lab4-final-grade5-s3-init-1 Starting
Container lab4-final-grade5-migrate-1 Started
Container lab4-final-grade5-s3-init-1 Started
Container lab4-final-grade5-redis-1 Waiting
Container lab4-final-grade5-postgres-1 Waiting
Container lab4-final-grade5-migrate-1 Waiting
Container lab4-final-grade5-s3-init-1 Waiting
Container lab4-final-grade5-migrate-1 Waiting
Container lab4-final-grade5-s3-init-1 Waiting
Container lab4-final-grade5-postgres-1 Waiting
Container lab4-final-grade5-redis-1 Healthy
Container lab4-final-grade5-s3-init-1 Exited
Container lab4-final-grade5-migrate-1 Exited
Container lab4-final-grade5-postgres-1 Healthy
Container lab4-final-grade5-s3-init-1 Exited
Container lab4-final-grade5-migrate-1 Exited
Container lab4-final-grade5-attachment-cleanup-1 Starting
Container lab4-final-grade5-postgres-1 Healthy
Container lab4-final-grade5-api-2 Starting
Container lab4-final-grade5-api-2 Started
Container lab4-final-grade5-api-1 Starting
Container lab4-final-grade5-attachment-cleanup-1 Started
Container lab4-final-grade5-api-1 Started
Container lab4-final-grade5-api-2 Waiting
Container lab4-final-grade5-api-1 Waiting
Container lab4-final-grade5-api-2 Healthy
Container lab4-final-grade5-api-1 Healthy
Container lab4-final-grade5-lb-1 Starting
Container lab4-final-grade5-lb-1 Started
Container lab4-final-grade5-redis-1 Waiting
Container lab4-final-grade5-api-2 Waiting
Container lab4-final-grade5-api-1 Waiting
Container lab4-final-grade5-lb-1 Waiting
Container lab4-final-grade5-attachment-cleanup-1 Waiting
Container lab4-final-grade5-s3-init-1 Waiting
Container lab4-final-grade5-s3-1 Waiting
Container lab4-final-grade5-postgres-1 Waiting
Container lab4-final-grade5-migrate-1 Waiting
Container lab4-final-grade5-attachment-cleanup-1 Healthy
Container lab4-final-grade5-redis-1 Healthy
Container lab4-final-grade5-lb-1 Healthy
Container lab4-final-grade5-migrate-1 Exited
Container lab4-final-grade5-postgres-1 Healthy
Container lab4-final-grade5-s3-init-1 Exited
Container lab4-final-grade5-s3-1 Healthy
Container lab4-final-grade5-api-2 Healthy
Container lab4-final-grade5-api-1 Healthy
+1
View File
@@ -0,0 +1 @@
PASS: bidirectional reconciliation, preservation, repeated apply, and cost profiles
+4
View File
@@ -0,0 +1,4 @@
python3 tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://127.0.0.1:18096"
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
{"observed_instances_before": 2, "successful_writes": 114, "failed_attempts": 1, "first_recovery_seconds": 2.788}
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
+507
View File
@@ -0,0 +1,507 @@
? highload/cmd/api [no test files]
? highload/cmd/attachment-cleanup [no test files]
=== RUN TestConnectionSecretErrorsAreSafe
=== RUN TestConnectionSecretErrorsAreSafe/missing
=== RUN TestConnectionSecretErrorsAreSafe/empty
=== RUN TestConnectionSecretErrorsAreSafe/malformed
--- PASS: TestConnectionSecretErrorsAreSafe (0.01s)
--- PASS: TestConnectionSecretErrorsAreSafe/missing (0.00s)
--- PASS: TestConnectionSecretErrorsAreSafe/empty (0.00s)
--- PASS: TestConnectionSecretErrorsAreSafe/malformed (0.00s)
=== RUN TestEmbeddedMigrationsAreVersionedAndCollectable
--- PASS: TestEmbeddedMigrationsAreVersionedAndCollectable (0.00s)
=== RUN TestUnlockHasBudgetEvenWithDetachedContext
--- PASS: TestUnlockHasBudgetEvenWithDetachedContext (0.00s)
=== RUN TestPostgresMigrationsRepeatWithoutChangingDataOrLoggingSQL
--- PASS: TestPostgresMigrationsRepeatWithoutChangingDataOrLoggingSQL (0.14s)
=== RUN TestPostgresConcurrentMigratorsApplyEachVersionOnce
--- PASS: TestPostgresConcurrentMigratorsApplyEachVersionOnce (1.06s)
=== RUN TestPostgresMigrationFailureRollsBackAndPreservesCause
--- PASS: TestPostgresMigrationFailureRollsBackAndPreservesCause (0.04s)
=== RUN TestPostgresMigrationThreeDownTwoDownOneAndReapply
--- PASS: TestPostgresMigrationThreeDownTwoDownOneAndReapply (0.12s)
PASS
ok highload/cmd/migrate 2.263s
? highload/db/migrations [no test files]
? highload/internal/config [no test files]
? highload/internal/database [no test files]
? highload/internal/database/sqlc [no test files]
? highload/internal/model [no test files]
? highload/internal/repository [no test files]
? highload/internal/repository/postgres [no test files]
? highload/internal/repository/redis [no test files]
? highload/internal/security [no test files]
? highload/internal/service [no test files]
? highload/internal/storage [no test files]
? highload/internal/storage/s3 [no test files]
? highload/internal/transport/http [no test files]
? highload/internal/transport/http/handler [no test files]
? highload/internal/transport/http/httputils [no test files]
? highload/internal/transport/http/middleware [no test files]
=== RUN TestAttachmentCleanupPagesAndCutoff
--- PASS: TestAttachmentCleanupPagesAndCutoff (0.00s)
=== RUN TestAttachmentCleanupFaults
=== RUN TestAttachmentCleanupFaults/list
=== RUN TestAttachmentCleanupFaults/exists
=== RUN TestAttachmentCleanupFaults/delete
=== RUN TestAttachmentCleanupFaults/list_timeout
=== RUN TestAttachmentCleanupFaults/pass_timeout
=== RUN TestAttachmentCleanupFaults/canceled
--- PASS: TestAttachmentCleanupFaults (0.00s)
--- PASS: TestAttachmentCleanupFaults/list (0.00s)
--- PASS: TestAttachmentCleanupFaults/exists (0.00s)
--- PASS: TestAttachmentCleanupFaults/delete (0.00s)
--- PASS: TestAttachmentCleanupFaults/list_timeout (0.00s)
--- PASS: TestAttachmentCleanupFaults/pass_timeout (0.00s)
--- PASS: TestAttachmentCleanupFaults/canceled (0.00s)
=== RUN TestAttachmentCleanupS3InvalidNextToken
=== RUN TestAttachmentCleanupS3InvalidNextToken/missing
=== RUN TestAttachmentCleanupS3InvalidNextToken/empty
=== RUN TestAttachmentCleanupS3InvalidNextToken/repeat
--- PASS: TestAttachmentCleanupS3InvalidNextToken (0.00s)
--- PASS: TestAttachmentCleanupS3InvalidNextToken/missing (0.00s)
--- PASS: TestAttachmentCleanupS3InvalidNextToken/empty (0.00s)
--- PASS: TestAttachmentCleanupS3InvalidNextToken/repeat (0.00s)
=== RUN TestAttachmentTxLockAndRollback
=== RUN TestAttachmentTxLockAndRollback/callback_error
attachment_tx_test.go:160: INSERT ran at read committed, rollback left zero rows and released the lock
=== RUN TestAttachmentTxLockAndRollback/context_canceled
attachment_tx_test.go:160: INSERT ran at read committed, rollback left zero rows and released the lock
--- PASS: TestAttachmentTxLockAndRollback (0.10s)
--- PASS: TestAttachmentTxLockAndRollback/callback_error (0.01s)
--- PASS: TestAttachmentTxLockAndRollback/context_canceled (0.00s)
=== RUN TestAttachmentTxHTTP
=== RUN TestAttachmentTxHTTP/s3_unavailable
2026/11/15 11:47:25 ERROR request failed error="service unavailable: object storage unavailable: operation error S3: PutObject, https response error StatusCode: 503, RequestID: , HostID: , api error SlowDown: Synthetic outage" request_id=4410ba4d-b703-4050-beba-e9c9ddb4942c status=503 code=unavailable
attachment_tx_test.go:288: direct handler HTTP 503; committed rows=0; DELETE=0
=== RUN TestAttachmentTxHTTP/insert_after_put
2026/11/15 11:47:25 ERROR request failed error="server error: database error: ERROR: new row for relation \"attachments\" violates check constraint \"attachments_filename_check1\" (SQLSTATE 23514)" request_id=55830874-e46a-4622-ba4a-24bd2712a3d7 status=500 code=internal_error
attachment_tx_test.go:288: direct handler HTTP 500; committed rows=0; DELETE=0
=== RUN TestAttachmentTxHTTP/commit_after_insert
2026/11/15 11:47:25 ERROR request failed error="server error: database error: ERROR: synthetic deferred failure (SQLSTATE P0001)" request_id=61ebcff4-daf1-43ed-98b0-b305c659a3e4 status=500 code=internal_error
attachment_tx_test.go:288: direct handler HTTP 500; committed rows=0; DELETE=0
=== RUN TestAttachmentTxHTTP/committed_upload_and_get
attachment_tx_test.go:315: HTTP 201, committed rows=1, GET metadata/content=200, SHA-256=99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75
--- PASS: TestAttachmentTxHTTP (0.23s)
--- PASS: TestAttachmentTxHTTP/s3_unavailable (0.06s)
--- PASS: TestAttachmentTxHTTP/insert_after_put (0.05s)
--- PASS: TestAttachmentTxHTTP/commit_after_insert (0.05s)
--- PASS: TestAttachmentTxHTTP/committed_upload_and_get (0.06s)
=== RUN TestStorageAuthHandlerRejectsJSONBody
--- PASS: TestStorageAuthHandlerRejectsJSONBody (0.12s)
=== RUN TestPostgresOperationsHaveBoundedWaits
budgets_test.go:96: budget_policy=provisional_3000ms; teacher has no numeric threshold; approval remains pending
=== RUN TestPostgresOperationsHaveBoundedWaits/blocked_query/application_budget
2026/11/15 11:47:27 ERROR request failed error="service unavailable: database unavailable: timeout: context deadline exceeded" request_id=af0ade91-2c05-446b-881f-5085da4a638a status=503 code=unavailable
2026/11/15 11:47:27 INFO http request method=GET path=/api/v1/users/me status=503 duration=1.002822167s request_id=af0ade91-2c05-446b-881f-5085da4a638a
budgets_test.go:130: mode=blocked_query scenario=application_budget elapsed_ms=1003.015 caller_deadline_ms=5000 budget_ms=3000 caller_canceled=false
2026/11/15 11:47:27 INFO http request method=GET path=/api/v1/users/me status=200 duration=18.338542ms request_id=2bccce82-b04e-48c0-b75d-10a5ea07e12d
=== RUN TestPostgresOperationsHaveBoundedWaits/blocked_query/shorter_caller_deadline
2026/11/15 11:47:27 ERROR request failed error="service unavailable: database unavailable: timeout: context deadline exceeded" request_id=767c833b-b621-4e15-b2ca-18b9a3165ef5 status=503 code=unavailable
2026/11/15 11:47:27 INFO http request method=GET path=/api/v1/users/me status=503 duration=150.605583ms request_id=767c833b-b621-4e15-b2ca-18b9a3165ef5
budgets_test.go:130: mode=blocked_query scenario=shorter_caller_deadline elapsed_ms=150.686 caller_deadline_ms=150 budget_ms=3000 caller_canceled=true
2026/11/15 11:47:27 INFO http request method=GET path=/api/v1/users/me status=200 duration=5.120542ms request_id=71588141-78a7-4c05-b5f4-92282e58feb7
=== RUN TestPostgresOperationsHaveBoundedWaits/blocked_query/released_before_budget
2026/11/15 11:47:27 INFO http request method=GET path=/api/v1/users/me status=200 duration=103.494542ms request_id=e42ec7f1-3162-4691-ad92-8b3b6b78821f
budgets_test.go:130: mode=blocked_query scenario=released_before_budget elapsed_ms=103.631 caller_deadline_ms=5000 budget_ms=3000 caller_canceled=false
2026/11/15 11:47:27 INFO http request method=GET path=/api/v1/users/me status=200 duration=845.375µs request_id=8c8f48e9-e829-4355-8d25-987223cf4d2e
=== RUN TestPostgresOperationsHaveBoundedWaits/exhausted_pool/application_budget
2026/11/15 11:47:29 ERROR request failed error="service unavailable: database unavailable: context deadline exceeded" request_id=6c0366cf-55fe-42d7-9f74-f64646887a94 status=503 code=unavailable
2026/11/15 11:47:29 INFO http request method=GET path=/api/v1/users/me status=503 duration=1.003118167s request_id=6c0366cf-55fe-42d7-9f74-f64646887a94
budgets_test.go:130: mode=exhausted_pool scenario=application_budget elapsed_ms=1003.262 caller_deadline_ms=5000 budget_ms=3000 caller_canceled=false
2026/11/15 11:47:29 INFO http request method=GET path=/api/v1/users/me status=200 duration=15.015958ms request_id=9b2a8a8d-e656-4c03-9bd0-376a97d1521e
=== RUN TestPostgresOperationsHaveBoundedWaits/exhausted_pool/shorter_caller_deadline
2026/11/15 11:47:29 ERROR request failed error="service unavailable: database unavailable: context deadline exceeded" request_id=c5e50fb3-3d35-4b17-9d06-552520839462 status=503 code=unavailable
2026/11/15 11:47:29 INFO http request method=GET path=/api/v1/users/me status=503 duration=150.302291ms request_id=c5e50fb3-3d35-4b17-9d06-552520839462
budgets_test.go:130: mode=exhausted_pool scenario=shorter_caller_deadline elapsed_ms=150.378 caller_deadline_ms=150 budget_ms=3000 caller_canceled=true
2026/11/15 11:47:29 INFO http request method=GET path=/api/v1/users/me status=200 duration=4.709083ms request_id=c6d568e7-75c2-473f-83db-d4feb7004ad7
=== RUN TestPostgresOperationsHaveBoundedWaits/exhausted_pool/released_before_budget
2026/11/15 11:47:29 INFO http request method=GET path=/api/v1/users/me status=200 duration=104.233375ms request_id=e5c4c586-9fd8-499c-9478-66077e6d72b4
budgets_test.go:130: mode=exhausted_pool scenario=released_before_budget elapsed_ms=104.332 caller_deadline_ms=5000 budget_ms=3000 caller_canceled=false
2026/11/15 11:47:29 INFO http request method=GET path=/api/v1/users/me status=200 duration=1.092834ms request_id=140de8ac-41b5-4d0d-9cc7-d6f4d1bc4b18
--- PASS: TestPostgresOperationsHaveBoundedWaits (4.02s)
--- PASS: TestPostgresOperationsHaveBoundedWaits/blocked_query/application_budget (1.24s)
--- PASS: TestPostgresOperationsHaveBoundedWaits/blocked_query/shorter_caller_deadline (0.40s)
--- PASS: TestPostgresOperationsHaveBoundedWaits/blocked_query/released_before_budget (0.33s)
--- PASS: TestPostgresOperationsHaveBoundedWaits/exhausted_pool/application_budget (1.27s)
--- PASS: TestPostgresOperationsHaveBoundedWaits/exhausted_pool/shorter_caller_deadline (0.42s)
--- PASS: TestPostgresOperationsHaveBoundedWaits/exhausted_pool/released_before_budget (0.36s)
=== RUN TestPostgresChatCancellationBeforeCommitIsAtomic
--- PASS: TestPostgresChatCancellationBeforeCommitIsAtomic (0.35s)
=== RUN TestErrorDiagnosticsRecordedOnceAndPublicResponseIsSafe
--- PASS: TestErrorDiagnosticsRecordedOnceAndPublicResponseIsSafe (0.00s)
=== RUN TestErrorDatabaseCategoryReachesHTTP
=== RUN TestErrorDatabaseCategoryReachesHTTP/no_rows
=== RUN TestErrorDatabaseCategoryReachesHTTP/duplicate
=== RUN TestErrorDatabaseCategoryReachesHTTP/eof
=== RUN TestErrorDatabaseCategoryReachesHTTP/deadline
=== RUN TestErrorDatabaseCategoryReachesHTTP/unexpected_eof
=== RUN TestErrorDatabaseCategoryReachesHTTP/closed_connection
=== RUN TestErrorDatabaseCategoryReachesHTTP/connection_refused
=== RUN TestErrorDatabaseCategoryReachesHTTP/database_dns_missing
=== RUN TestErrorDatabaseCategoryReachesHTTP/sql_syntax
=== RUN TestErrorDatabaseCategoryReachesHTTP/unexpected
=== RUN TestErrorDatabaseCategoryReachesHTTP/sqlstate_53300
=== RUN TestErrorDatabaseCategoryReachesHTTP/sqlstate_57P03
=== RUN TestErrorDatabaseCategoryReachesHTTP/sqlstate_08006
=== RUN TestErrorDatabaseCategoryReachesHTTP/sqlstate_08001
=== RUN TestErrorDatabaseCategoryReachesHTTP/sqlstate_57P01
=== RUN TestErrorDatabaseCategoryReachesHTTP/sqlstate_57P02
=== RUN TestErrorDatabaseCategoryReachesHTTP/wrapped_no_rows
=== RUN TestErrorDatabaseCategoryReachesHTTP/wrapped_duplicate
=== RUN TestErrorDatabaseCategoryReachesHTTP/wrapped_eof
=== RUN TestErrorDatabaseCategoryReachesHTTP/wrapped_deadline
=== RUN TestErrorDatabaseCategoryReachesHTTP/wrapped_unexpected_eof
=== RUN TestErrorDatabaseCategoryReachesHTTP/wrapped_closed_connection
=== RUN TestErrorDatabaseCategoryReachesHTTP/wrapped_connection_refused
=== RUN TestErrorDatabaseCategoryReachesHTTP/wrapped_database_dns_missing
=== RUN TestErrorDatabaseCategoryReachesHTTP/wrapped_sql_syntax
=== RUN TestErrorDatabaseCategoryReachesHTTP/wrapped_unexpected
=== RUN TestErrorDatabaseCategoryReachesHTTP/wrapped_sqlstate_53300
=== RUN TestErrorDatabaseCategoryReachesHTTP/wrapped_sqlstate_57P03
=== RUN TestErrorDatabaseCategoryReachesHTTP/wrapped_sqlstate_08006
=== RUN TestErrorDatabaseCategoryReachesHTTP/wrapped_sqlstate_08001
=== RUN TestErrorDatabaseCategoryReachesHTTP/wrapped_sqlstate_57P01
=== RUN TestErrorDatabaseCategoryReachesHTTP/wrapped_sqlstate_57P02
--- PASS: TestErrorDatabaseCategoryReachesHTTP (0.00s)
--- PASS: TestErrorDatabaseCategoryReachesHTTP/no_rows (0.00s)
--- PASS: TestErrorDatabaseCategoryReachesHTTP/duplicate (0.00s)
--- PASS: TestErrorDatabaseCategoryReachesHTTP/eof (0.00s)
--- PASS: TestErrorDatabaseCategoryReachesHTTP/deadline (0.00s)
--- PASS: TestErrorDatabaseCategoryReachesHTTP/unexpected_eof (0.00s)
--- PASS: TestErrorDatabaseCategoryReachesHTTP/closed_connection (0.00s)
--- PASS: TestErrorDatabaseCategoryReachesHTTP/connection_refused (0.00s)
--- PASS: TestErrorDatabaseCategoryReachesHTTP/database_dns_missing (0.00s)
--- PASS: TestErrorDatabaseCategoryReachesHTTP/sql_syntax (0.00s)
--- PASS: TestErrorDatabaseCategoryReachesHTTP/unexpected (0.00s)
--- PASS: TestErrorDatabaseCategoryReachesHTTP/sqlstate_53300 (0.00s)
--- PASS: TestErrorDatabaseCategoryReachesHTTP/sqlstate_57P03 (0.00s)
--- PASS: TestErrorDatabaseCategoryReachesHTTP/sqlstate_08006 (0.00s)
--- PASS: TestErrorDatabaseCategoryReachesHTTP/sqlstate_08001 (0.00s)
--- PASS: TestErrorDatabaseCategoryReachesHTTP/sqlstate_57P01 (0.00s)
--- PASS: TestErrorDatabaseCategoryReachesHTTP/sqlstate_57P02 (0.00s)
--- PASS: TestErrorDatabaseCategoryReachesHTTP/wrapped_no_rows (0.00s)
--- PASS: TestErrorDatabaseCategoryReachesHTTP/wrapped_duplicate (0.00s)
--- PASS: TestErrorDatabaseCategoryReachesHTTP/wrapped_eof (0.00s)
--- PASS: TestErrorDatabaseCategoryReachesHTTP/wrapped_deadline (0.00s)
--- PASS: TestErrorDatabaseCategoryReachesHTTP/wrapped_unexpected_eof (0.00s)
--- PASS: TestErrorDatabaseCategoryReachesHTTP/wrapped_closed_connection (0.00s)
--- PASS: TestErrorDatabaseCategoryReachesHTTP/wrapped_connection_refused (0.00s)
--- PASS: TestErrorDatabaseCategoryReachesHTTP/wrapped_database_dns_missing (0.00s)
--- PASS: TestErrorDatabaseCategoryReachesHTTP/wrapped_sql_syntax (0.00s)
--- PASS: TestErrorDatabaseCategoryReachesHTTP/wrapped_unexpected (0.00s)
--- PASS: TestErrorDatabaseCategoryReachesHTTP/wrapped_sqlstate_53300 (0.00s)
--- PASS: TestErrorDatabaseCategoryReachesHTTP/wrapped_sqlstate_57P03 (0.00s)
--- PASS: TestErrorDatabaseCategoryReachesHTTP/wrapped_sqlstate_08006 (0.00s)
--- PASS: TestErrorDatabaseCategoryReachesHTTP/wrapped_sqlstate_08001 (0.00s)
--- PASS: TestErrorDatabaseCategoryReachesHTTP/wrapped_sqlstate_57P01 (0.00s)
--- PASS: TestErrorDatabaseCategoryReachesHTTP/wrapped_sqlstate_57P02 (0.00s)
=== RUN TestErrorUnavailableSQLLoginKeepsCategory
=== RUN TestErrorUnavailableSQLLoginKeepsCategory/eof
=== RUN TestErrorUnavailableSQLLoginKeepsCategory/deadline
=== RUN TestErrorUnavailableSQLLoginKeepsCategory/unexpected_eof
=== RUN TestErrorUnavailableSQLLoginKeepsCategory/closed_connection
=== RUN TestErrorUnavailableSQLLoginKeepsCategory/connection_refused
=== RUN TestErrorUnavailableSQLLoginKeepsCategory/database_dns_missing
=== RUN TestErrorUnavailableSQLLoginKeepsCategory/sqlstate_53300
=== RUN TestErrorUnavailableSQLLoginKeepsCategory/sqlstate_57P03
=== RUN TestErrorUnavailableSQLLoginKeepsCategory/sqlstate_08006
=== RUN TestErrorUnavailableSQLLoginKeepsCategory/sqlstate_08001
=== RUN TestErrorUnavailableSQLLoginKeepsCategory/sqlstate_57P01
=== RUN TestErrorUnavailableSQLLoginKeepsCategory/sqlstate_57P02
=== RUN TestErrorUnavailableSQLLoginKeepsCategory/wrapped_eof
=== RUN TestErrorUnavailableSQLLoginKeepsCategory/wrapped_deadline
=== RUN TestErrorUnavailableSQLLoginKeepsCategory/wrapped_unexpected_eof
=== RUN TestErrorUnavailableSQLLoginKeepsCategory/wrapped_closed_connection
=== RUN TestErrorUnavailableSQLLoginKeepsCategory/wrapped_connection_refused
=== RUN TestErrorUnavailableSQLLoginKeepsCategory/wrapped_database_dns_missing
=== RUN TestErrorUnavailableSQLLoginKeepsCategory/wrapped_sqlstate_53300
=== RUN TestErrorUnavailableSQLLoginKeepsCategory/wrapped_sqlstate_57P03
=== RUN TestErrorUnavailableSQLLoginKeepsCategory/wrapped_sqlstate_08006
=== RUN TestErrorUnavailableSQLLoginKeepsCategory/wrapped_sqlstate_08001
=== RUN TestErrorUnavailableSQLLoginKeepsCategory/wrapped_sqlstate_57P01
=== RUN TestErrorUnavailableSQLLoginKeepsCategory/wrapped_sqlstate_57P02
--- PASS: TestErrorUnavailableSQLLoginKeepsCategory (0.00s)
--- PASS: TestErrorUnavailableSQLLoginKeepsCategory/eof (0.00s)
--- PASS: TestErrorUnavailableSQLLoginKeepsCategory/deadline (0.00s)
--- PASS: TestErrorUnavailableSQLLoginKeepsCategory/unexpected_eof (0.00s)
--- PASS: TestErrorUnavailableSQLLoginKeepsCategory/closed_connection (0.00s)
--- PASS: TestErrorUnavailableSQLLoginKeepsCategory/connection_refused (0.00s)
--- PASS: TestErrorUnavailableSQLLoginKeepsCategory/database_dns_missing (0.00s)
--- PASS: TestErrorUnavailableSQLLoginKeepsCategory/sqlstate_53300 (0.00s)
--- PASS: TestErrorUnavailableSQLLoginKeepsCategory/sqlstate_57P03 (0.00s)
--- PASS: TestErrorUnavailableSQLLoginKeepsCategory/sqlstate_08006 (0.00s)
--- PASS: TestErrorUnavailableSQLLoginKeepsCategory/sqlstate_08001 (0.00s)
--- PASS: TestErrorUnavailableSQLLoginKeepsCategory/sqlstate_57P01 (0.00s)
--- PASS: TestErrorUnavailableSQLLoginKeepsCategory/sqlstate_57P02 (0.00s)
--- PASS: TestErrorUnavailableSQLLoginKeepsCategory/wrapped_eof (0.00s)
--- PASS: TestErrorUnavailableSQLLoginKeepsCategory/wrapped_deadline (0.00s)
--- PASS: TestErrorUnavailableSQLLoginKeepsCategory/wrapped_unexpected_eof (0.00s)
--- PASS: TestErrorUnavailableSQLLoginKeepsCategory/wrapped_closed_connection (0.00s)
--- PASS: TestErrorUnavailableSQLLoginKeepsCategory/wrapped_connection_refused (0.00s)
--- PASS: TestErrorUnavailableSQLLoginKeepsCategory/wrapped_database_dns_missing (0.00s)
--- PASS: TestErrorUnavailableSQLLoginKeepsCategory/wrapped_sqlstate_53300 (0.00s)
--- PASS: TestErrorUnavailableSQLLoginKeepsCategory/wrapped_sqlstate_57P03 (0.00s)
--- PASS: TestErrorUnavailableSQLLoginKeepsCategory/wrapped_sqlstate_08006 (0.00s)
--- PASS: TestErrorUnavailableSQLLoginKeepsCategory/wrapped_sqlstate_08001 (0.00s)
--- PASS: TestErrorUnavailableSQLLoginKeepsCategory/wrapped_sqlstate_57P01 (0.00s)
--- PASS: TestErrorUnavailableSQLLoginKeepsCategory/wrapped_sqlstate_57P02 (0.00s)
=== RUN TestErrorChatMemberLookupKeepsOperationMeaning
=== RUN TestErrorChatMemberLookupKeepsOperationMeaning/no_rows
=== RUN TestErrorChatMemberLookupKeepsOperationMeaning/eof
=== RUN TestErrorChatMemberLookupKeepsOperationMeaning/deadline
=== RUN TestErrorChatMemberLookupKeepsOperationMeaning/unexpected_eof
=== RUN TestErrorChatMemberLookupKeepsOperationMeaning/closed_connection
=== RUN TestErrorChatMemberLookupKeepsOperationMeaning/connection_refused
=== RUN TestErrorChatMemberLookupKeepsOperationMeaning/database_dns_missing
=== RUN TestErrorChatMemberLookupKeepsOperationMeaning/sql_syntax
=== RUN TestErrorChatMemberLookupKeepsOperationMeaning/unexpected
=== RUN TestErrorChatMemberLookupKeepsOperationMeaning/sqlstate_53300
=== RUN TestErrorChatMemberLookupKeepsOperationMeaning/sqlstate_57P03
=== RUN TestErrorChatMemberLookupKeepsOperationMeaning/sqlstate_08006
=== RUN TestErrorChatMemberLookupKeepsOperationMeaning/sqlstate_08001
=== RUN TestErrorChatMemberLookupKeepsOperationMeaning/sqlstate_57P01
=== RUN TestErrorChatMemberLookupKeepsOperationMeaning/sqlstate_57P02
=== RUN TestErrorChatMemberLookupKeepsOperationMeaning/wrapped_no_rows
=== RUN TestErrorChatMemberLookupKeepsOperationMeaning/wrapped_eof
=== RUN TestErrorChatMemberLookupKeepsOperationMeaning/wrapped_deadline
=== RUN TestErrorChatMemberLookupKeepsOperationMeaning/wrapped_unexpected_eof
=== RUN TestErrorChatMemberLookupKeepsOperationMeaning/wrapped_closed_connection
=== RUN TestErrorChatMemberLookupKeepsOperationMeaning/wrapped_connection_refused
=== RUN TestErrorChatMemberLookupKeepsOperationMeaning/wrapped_database_dns_missing
=== RUN TestErrorChatMemberLookupKeepsOperationMeaning/wrapped_sql_syntax
=== RUN TestErrorChatMemberLookupKeepsOperationMeaning/wrapped_unexpected
=== RUN TestErrorChatMemberLookupKeepsOperationMeaning/wrapped_sqlstate_53300
=== RUN TestErrorChatMemberLookupKeepsOperationMeaning/wrapped_sqlstate_57P03
=== RUN TestErrorChatMemberLookupKeepsOperationMeaning/wrapped_sqlstate_08006
=== RUN TestErrorChatMemberLookupKeepsOperationMeaning/wrapped_sqlstate_08001
=== RUN TestErrorChatMemberLookupKeepsOperationMeaning/wrapped_sqlstate_57P01
=== RUN TestErrorChatMemberLookupKeepsOperationMeaning/wrapped_sqlstate_57P02
--- PASS: TestErrorChatMemberLookupKeepsOperationMeaning (0.00s)
--- PASS: TestErrorChatMemberLookupKeepsOperationMeaning/no_rows (0.00s)
--- PASS: TestErrorChatMemberLookupKeepsOperationMeaning/eof (0.00s)
--- PASS: TestErrorChatMemberLookupKeepsOperationMeaning/deadline (0.00s)
--- PASS: TestErrorChatMemberLookupKeepsOperationMeaning/unexpected_eof (0.00s)
--- PASS: TestErrorChatMemberLookupKeepsOperationMeaning/closed_connection (0.00s)
--- PASS: TestErrorChatMemberLookupKeepsOperationMeaning/connection_refused (0.00s)
--- PASS: TestErrorChatMemberLookupKeepsOperationMeaning/database_dns_missing (0.00s)
--- PASS: TestErrorChatMemberLookupKeepsOperationMeaning/sql_syntax (0.00s)
--- PASS: TestErrorChatMemberLookupKeepsOperationMeaning/unexpected (0.00s)
--- PASS: TestErrorChatMemberLookupKeepsOperationMeaning/sqlstate_53300 (0.00s)
--- PASS: TestErrorChatMemberLookupKeepsOperationMeaning/sqlstate_57P03 (0.00s)
--- PASS: TestErrorChatMemberLookupKeepsOperationMeaning/sqlstate_08006 (0.00s)
--- PASS: TestErrorChatMemberLookupKeepsOperationMeaning/sqlstate_08001 (0.00s)
--- PASS: TestErrorChatMemberLookupKeepsOperationMeaning/sqlstate_57P01 (0.00s)
--- PASS: TestErrorChatMemberLookupKeepsOperationMeaning/sqlstate_57P02 (0.00s)
--- PASS: TestErrorChatMemberLookupKeepsOperationMeaning/wrapped_no_rows (0.00s)
--- PASS: TestErrorChatMemberLookupKeepsOperationMeaning/wrapped_eof (0.00s)
--- PASS: TestErrorChatMemberLookupKeepsOperationMeaning/wrapped_deadline (0.00s)
--- PASS: TestErrorChatMemberLookupKeepsOperationMeaning/wrapped_unexpected_eof (0.00s)
--- PASS: TestErrorChatMemberLookupKeepsOperationMeaning/wrapped_closed_connection (0.00s)
--- PASS: TestErrorChatMemberLookupKeepsOperationMeaning/wrapped_connection_refused (0.00s)
--- PASS: TestErrorChatMemberLookupKeepsOperationMeaning/wrapped_database_dns_missing (0.00s)
--- PASS: TestErrorChatMemberLookupKeepsOperationMeaning/wrapped_sql_syntax (0.00s)
--- PASS: TestErrorChatMemberLookupKeepsOperationMeaning/wrapped_unexpected (0.00s)
--- PASS: TestErrorChatMemberLookupKeepsOperationMeaning/wrapped_sqlstate_53300 (0.00s)
--- PASS: TestErrorChatMemberLookupKeepsOperationMeaning/wrapped_sqlstate_57P03 (0.00s)
--- PASS: TestErrorChatMemberLookupKeepsOperationMeaning/wrapped_sqlstate_08006 (0.00s)
--- PASS: TestErrorChatMemberLookupKeepsOperationMeaning/wrapped_sqlstate_08001 (0.00s)
--- PASS: TestErrorChatMemberLookupKeepsOperationMeaning/wrapped_sqlstate_57P01 (0.00s)
--- PASS: TestErrorChatMemberLookupKeepsOperationMeaning/wrapped_sqlstate_57P02 (0.00s)
=== RUN TestErrorMissingBasicPrecedesForbiddenBody
--- PASS: TestErrorMissingBasicPrecedesForbiddenBody (0.00s)
=== RUN TestRedisLogoutPersistenceDiagnostic
logout_diagnostic_test.go:133: logout samples=51 pool_wait_count=0 wait_ns=0 pool_timeouts=0 final_busy=0
--- PASS: TestRedisLogoutPersistenceDiagnostic (13.24s)
=== RUN TestMessageAttachmentsSingleIDServiceRegression
=== RUN TestMessageAttachmentsSingleIDServiceRegression/single_attachment_without_panic
=== RUN TestMessageAttachmentsSingleIDServiceRegression/identical_uppercase_body_replay
--- PASS: TestMessageAttachmentsSingleIDServiceRegression (0.00s)
--- PASS: TestMessageAttachmentsSingleIDServiceRegression/single_attachment_without_panic (0.00s)
--- PASS: TestMessageAttachmentsSingleIDServiceRegression/identical_uppercase_body_replay (0.00s)
=== RUN TestMessageAttachmentsTextCompatibility
--- PASS: TestMessageAttachmentsTextCompatibility (0.10s)
=== RUN TestMessageAttachmentsOnlyAndOrderZeroToTen
=== RUN TestMessageAttachmentsOnlyAndOrderZeroToTen/count_0
=== RUN TestMessageAttachmentsOnlyAndOrderZeroToTen/count_1
=== RUN TestMessageAttachmentsOnlyAndOrderZeroToTen/count_2
=== RUN TestMessageAttachmentsOnlyAndOrderZeroToTen/count_3
=== RUN TestMessageAttachmentsOnlyAndOrderZeroToTen/count_4
=== RUN TestMessageAttachmentsOnlyAndOrderZeroToTen/count_5
=== RUN TestMessageAttachmentsOnlyAndOrderZeroToTen/count_6
=== RUN TestMessageAttachmentsOnlyAndOrderZeroToTen/count_7
=== RUN TestMessageAttachmentsOnlyAndOrderZeroToTen/count_8
=== RUN TestMessageAttachmentsOnlyAndOrderZeroToTen/count_9
=== RUN TestMessageAttachmentsOnlyAndOrderZeroToTen/count_10
--- PASS: TestMessageAttachmentsOnlyAndOrderZeroToTen (0.64s)
--- PASS: TestMessageAttachmentsOnlyAndOrderZeroToTen/count_0 (0.08s)
--- PASS: TestMessageAttachmentsOnlyAndOrderZeroToTen/count_1 (0.06s)
--- PASS: TestMessageAttachmentsOnlyAndOrderZeroToTen/count_2 (0.06s)
--- PASS: TestMessageAttachmentsOnlyAndOrderZeroToTen/count_3 (0.05s)
--- PASS: TestMessageAttachmentsOnlyAndOrderZeroToTen/count_4 (0.05s)
--- PASS: TestMessageAttachmentsOnlyAndOrderZeroToTen/count_5 (0.06s)
--- PASS: TestMessageAttachmentsOnlyAndOrderZeroToTen/count_6 (0.05s)
--- PASS: TestMessageAttachmentsOnlyAndOrderZeroToTen/count_7 (0.06s)
--- PASS: TestMessageAttachmentsOnlyAndOrderZeroToTen/count_8 (0.06s)
--- PASS: TestMessageAttachmentsOnlyAndOrderZeroToTen/count_9 (0.06s)
--- PASS: TestMessageAttachmentsOnlyAndOrderZeroToTen/count_10 (0.05s)
=== RUN TestMessageAttachmentsInvalidReferencesAreAtomic
=== RUN TestMessageAttachmentsInvalidReferencesAreAtomic/unknown_id
=== RUN TestMessageAttachmentsInvalidReferencesAreAtomic/attachment_from_other_chat
=== RUN TestMessageAttachmentsInvalidReferencesAreAtomic/foreign_chat
--- PASS: TestMessageAttachmentsInvalidReferencesAreAtomic (0.21s)
--- PASS: TestMessageAttachmentsInvalidReferencesAreAtomic/unknown_id (0.05s)
--- PASS: TestMessageAttachmentsInvalidReferencesAreAtomic/attachment_from_other_chat (0.05s)
--- PASS: TestMessageAttachmentsInvalidReferencesAreAtomic/foreign_chat (0.10s)
=== RUN TestMessageAttachmentsInvalidListLeavesNoRows
=== RUN TestMessageAttachmentsInvalidListLeavesNoRows/duplicate
=== RUN TestMessageAttachmentsInvalidListLeavesNoRows/eleven
--- PASS: TestMessageAttachmentsInvalidListLeavesNoRows (0.11s)
--- PASS: TestMessageAttachmentsInvalidListLeavesNoRows/duplicate (0.05s)
--- PASS: TestMessageAttachmentsInvalidListLeavesNoRows/eleven (0.05s)
=== RUN TestMessageAttachmentsIdempotencyIncludesListAndOrder
--- PASS: TestMessageAttachmentsIdempotencyIncludesListAndOrder (0.12s)
=== RUN TestMessageAttachmentsConcurrentReplayDoesNotMultiplyLinks
--- PASS: TestMessageAttachmentsConcurrentReplayDoesNotMultiplyLinks (0.08s)
=== RUN TestMessageAttachmentsLinkFailureRollsBackEverything
--- PASS: TestMessageAttachmentsLinkFailureRollsBackEverything (0.07s)
=== RUN TestPasswordRoundTripAndRandomSalt
--- PASS: TestPasswordRoundTripAndRandomSalt (0.54s)
=== RUN TestPasswordDoesNotTruncateLongUTF8
--- PASS: TestPasswordDoesNotTruncateLongUTF8 (0.22s)
=== RUN TestPasswordWhitespaceIsSignificant
--- PASS: TestPasswordWhitespaceIsSignificant (0.15s)
=== RUN TestMalformedPasswordHashRejectedWithoutPanic
=== RUN TestMalformedPasswordHashRejectedWithoutPanic/plaintext
=== RUN TestMalformedPasswordHashRejectedWithoutPanic/wrong_algorithm
=== RUN TestMalformedPasswordHashRejectedWithoutPanic/huge_iterations
=== RUN TestMalformedPasswordHashRejectedWithoutPanic/missing_param
=== RUN TestMalformedPasswordHashRejectedWithoutPanic/hash_empty
=== RUN TestMalformedPasswordHashRejectedWithoutPanic/hash_wrong_size
=== RUN TestMalformedPasswordHashRejectedWithoutPanic/salt_wrong_size
=== RUN TestMalformedPasswordHashRejectedWithoutPanic/salt_empty
=== RUN TestMalformedPasswordHashRejectedWithoutPanic/extra_field
=== RUN TestMalformedPasswordHashRejectedWithoutPanic/zero_iterations
=== RUN TestMalformedPasswordHashRejectedWithoutPanic/negative
=== RUN TestMalformedPasswordHashRejectedWithoutPanic/non_numeric
=== RUN TestMalformedPasswordHashRejectedWithoutPanic/empty
=== RUN TestMalformedPasswordHashRejectedWithoutPanic/missing_version
=== RUN TestMalformedPasswordHashRejectedWithoutPanic/tiny_memory
=== RUN TestMalformedPasswordHashRejectedWithoutPanic/huge_threads
=== RUN TestMalformedPasswordHashRejectedWithoutPanic/zero_threads
=== RUN TestMalformedPasswordHashRejectedWithoutPanic/huge_memory
=== RUN TestMalformedPasswordHashRejectedWithoutPanic/hash_bad_base64
=== RUN TestMalformedPasswordHashRejectedWithoutPanic/wrong_version
=== RUN TestMalformedPasswordHashRejectedWithoutPanic/salt_bad_base64
--- PASS: TestMalformedPasswordHashRejectedWithoutPanic (0.07s)
--- PASS: TestMalformedPasswordHashRejectedWithoutPanic/plaintext (0.00s)
--- PASS: TestMalformedPasswordHashRejectedWithoutPanic/wrong_algorithm (0.00s)
--- PASS: TestMalformedPasswordHashRejectedWithoutPanic/huge_iterations (0.00s)
--- PASS: TestMalformedPasswordHashRejectedWithoutPanic/missing_param (0.00s)
--- PASS: TestMalformedPasswordHashRejectedWithoutPanic/hash_empty (0.00s)
--- PASS: TestMalformedPasswordHashRejectedWithoutPanic/hash_wrong_size (0.00s)
--- PASS: TestMalformedPasswordHashRejectedWithoutPanic/salt_wrong_size (0.00s)
--- PASS: TestMalformedPasswordHashRejectedWithoutPanic/salt_empty (0.00s)
--- PASS: TestMalformedPasswordHashRejectedWithoutPanic/extra_field (0.00s)
--- PASS: TestMalformedPasswordHashRejectedWithoutPanic/zero_iterations (0.00s)
--- PASS: TestMalformedPasswordHashRejectedWithoutPanic/negative (0.00s)
--- PASS: TestMalformedPasswordHashRejectedWithoutPanic/non_numeric (0.00s)
--- PASS: TestMalformedPasswordHashRejectedWithoutPanic/empty (0.00s)
--- PASS: TestMalformedPasswordHashRejectedWithoutPanic/missing_version (0.00s)
--- PASS: TestMalformedPasswordHashRejectedWithoutPanic/tiny_memory (0.00s)
--- PASS: TestMalformedPasswordHashRejectedWithoutPanic/huge_threads (0.00s)
--- PASS: TestMalformedPasswordHashRejectedWithoutPanic/zero_threads (0.00s)
--- PASS: TestMalformedPasswordHashRejectedWithoutPanic/huge_memory (0.00s)
--- PASS: TestMalformedPasswordHashRejectedWithoutPanic/hash_bad_base64 (0.00s)
--- PASS: TestMalformedPasswordHashRejectedWithoutPanic/wrong_version (0.00s)
--- PASS: TestMalformedPasswordHashRejectedWithoutPanic/salt_bad_base64 (0.00s)
=== RUN TestRedisAddressIsAnAddressNotAFile
--- PASS: TestRedisAddressIsAnAddressNotAFile (0.00s)
=== RUN TestRedisUnconfirmedAOFNeverReturnsSuccessfulLogout
=== RUN TestRedisUnconfirmedAOFNeverReturnsSuccessfulLogout/local_not_fsynced
=== RUN TestRedisUnconfirmedAOFNeverReturnsSuccessfulLogout/only_replica_fsynced
=== RUN TestRedisUnconfirmedAOFNeverReturnsSuccessfulLogout/malformed_reply
=== RUN TestRedisUnconfirmedAOFNeverReturnsSuccessfulLogout/command_failure
--- PASS: TestRedisUnconfirmedAOFNeverReturnsSuccessfulLogout (0.83s)
--- PASS: TestRedisUnconfirmedAOFNeverReturnsSuccessfulLogout/local_not_fsynced (0.21s)
--- PASS: TestRedisUnconfirmedAOFNeverReturnsSuccessfulLogout/only_replica_fsynced (0.21s)
--- PASS: TestRedisUnconfirmedAOFNeverReturnsSuccessfulLogout/malformed_reply (0.21s)
--- PASS: TestRedisUnconfirmedAOFNeverReturnsSuccessfulLogout/command_failure (0.21s)
=== RUN TestRedisSessionHasTTLAndReadsDoNotExtendIt
--- PASS: TestRedisSessionHasTTLAndReadsDoNotExtendIt (0.13s)
=== RUN TestRedisExpiredAndUnknownSessionsAreNotFound
--- PASS: TestRedisExpiredAndUnknownSessionsAreNotFound (0.36s)
=== RUN TestRedisLogoutOnlyRemovesSelectedSession
--- PASS: TestRedisLogoutOnlyRemovesSelectedSession (1.03s)
=== RUN TestRedisSessionsSurviveActualRestartWithoutExtendingTTL
--- PASS: TestRedisSessionsSurviveActualRestartWithoutExtendingTTL (7.59s)
=== RUN TestRedisActiveSessionSurvivesRestartIndependentlyOfLogout
--- PASS: TestRedisActiveSessionSurvivesRestartIndependentlyOfLogout (6.18s)
=== RUN TestRedisUnavailableKeepsUnavailableCategory
=== RUN TestRedisUnavailableKeepsUnavailableCategory/create
redis: 2026/11/15 11:48:02 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp: connection refused
=== RUN TestRedisUnavailableKeepsUnavailableCategory/read
redis: 2026/11/15 11:48:02 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp: connection refused
=== RUN TestRedisUnavailableKeepsUnavailableCategory/logout
redis: 2026/11/15 11:48:03 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp: connection refused
redis: 2026/11/15 11:48:03 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp: connection refused
--- PASS: TestRedisUnavailableKeepsUnavailableCategory (1.71s)
--- PASS: TestRedisUnavailableKeepsUnavailableCategory/create (0.42s)
--- PASS: TestRedisUnavailableKeepsUnavailableCategory/read (0.41s)
--- PASS: TestRedisUnavailableKeepsUnavailableCategory/logout (0.41s)
=== RUN TestRedisCriticalOutageHTTPAndReadiness
=== RUN TestRedisCriticalOutageHTTPAndReadiness/liveness
=== RUN TestRedisCriticalOutageHTTPAndReadiness/readiness
redis: 2026/11/15 11:48:04 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp: connection refused
=== RUN TestRedisCriticalOutageHTTPAndReadiness/protected_request
redis: 2026/11/15 11:48:04 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp: connection refused
--- PASS: TestRedisCriticalOutageHTTPAndReadiness (0.97s)
--- PASS: TestRedisCriticalOutageHTTPAndReadiness/liveness (0.00s)
--- PASS: TestRedisCriticalOutageHTTPAndReadiness/readiness (0.41s)
--- PASS: TestRedisCriticalOutageHTTPAndReadiness/protected_request (0.41s)
=== RUN TestRedisPublicHTTPContractIsUnchanged
=== RUN TestRedisPublicHTTPContractIsUnchanged/teacher_smoke
=== RUN TestRedisPublicHTTPContractIsUnchanged/auth_normal
--- PASS: TestRedisPublicHTTPContractIsUnchanged (7.92s)
--- PASS: TestRedisPublicHTTPContractIsUnchanged/teacher_smoke (1.58s)
--- PASS: TestRedisPublicHTTPContractIsUnchanged/auth_normal (6.28s)
=== RUN TestRedisHTTPFullRestartKeepsDeadlineAndRevocation
=== RUN TestRedisHTTPFullRestartKeepsDeadlineAndRevocation/active_only
=== RUN TestRedisHTTPFullRestartKeepsDeadlineAndRevocation/active_and_revoked
--- PASS: TestRedisHTTPFullRestartKeepsDeadlineAndRevocation (33.04s)
--- PASS: TestRedisHTTPFullRestartKeepsDeadlineAndRevocation/active_only (15.91s)
--- PASS: TestRedisHTTPFullRestartKeepsDeadlineAndRevocation/active_and_revoked (17.13s)
=== RUN TestStoragePasswordIsKDFAndDuplicateCannotOverwrite
--- PASS: TestStoragePasswordIsKDFAndDuplicateCannotOverwrite (0.38s)
=== RUN TestStorageSessionHashAndReconnectPreserveDeadline
--- PASS: TestStorageSessionHashAndReconnectPreserveDeadline (0.20s)
=== RUN TestStorageWrongCredentialsNeverCreateSession
--- PASS: TestStorageWrongCredentialsNeverCreateSession (0.18s)
=== RUN TestStorageRevocationIsPersistentAndSessionSpecific
--- PASS: TestStorageRevocationIsPersistentAndSessionSpecific (1.35s)
=== RUN TestStorageExpiredSessionCannotRevive
--- PASS: TestStorageExpiredSessionCannotRevive (0.25s)
=== RUN TestStorageShortTTLExpiresAndReLoginWorks
--- PASS: TestStorageShortTTLExpiresAndReLoginWorks (2.43s)
=== RUN TestStorageChatRollbackAfterMemberInsertFailure
--- PASS: TestStorageChatRollbackAfterMemberInsertFailure (0.14s)
=== RUN TestSQLUnavailableLoginStopsBeforeSessionCreation
--- PASS: TestSQLUnavailableLoginStopsBeforeSessionCreation (0.00s)
=== RUN TestMiddlewareMalformedAuthWritesExactlyOneError
=== RUN TestMiddlewareMalformedAuthWritesExactlyOneError/malformed
=== RUN TestMiddlewareMalformedAuthWritesExactlyOneError/malformed#01
=== RUN TestMiddlewareMalformedAuthWritesExactlyOneError/malformed#02
=== RUN TestMiddlewareMalformedAuthWritesExactlyOneError/malformed#03
=== RUN TestMiddlewareMalformedAuthWritesExactlyOneError/malformed#04
=== RUN TestMiddlewareMalformedAuthWritesExactlyOneError/malformed#05
--- PASS: TestMiddlewareMalformedAuthWritesExactlyOneError (0.00s)
--- PASS: TestMiddlewareMalformedAuthWritesExactlyOneError/malformed (0.00s)
--- PASS: TestMiddlewareMalformedAuthWritesExactlyOneError/malformed#01 (0.00s)
--- PASS: TestMiddlewareMalformedAuthWritesExactlyOneError/malformed#02 (0.00s)
--- PASS: TestMiddlewareMalformedAuthWritesExactlyOneError/malformed#03 (0.00s)
--- PASS: TestMiddlewareMalformedAuthWritesExactlyOneError/malformed#04 (0.00s)
--- PASS: TestMiddlewareMalformedAuthWritesExactlyOneError/malformed#05 (0.00s)
=== RUN TestStorageHTTPAuthRoutingAndLogout
--- PASS: TestStorageHTTPAuthRoutingAndLogout (0.86s)
PASS
ok highload/student-tests/auth_tests 86.386s
+7
View File
@@ -0,0 +1,7 @@
migrate-1 | {"time":"2026-11-15T08:47:02.287184423Z","level":"INFO","msg":"migration applied","file":"00001_init_schema.sql","version":1,"duration_seconds":0.010336709}
migrate-1 | {"time":"2026-11-15T08:47:02.287565965Z","level":"INFO","msg":"migration applied","file":"00002_add_created_at_defaults.sql","version":2,"duration_seconds":0.000999708}
migrate-1 | {"time":"2026-11-15T08:47:02.28757859Z","level":"INFO","msg":"migration applied","file":"00003_drop_session_table.sql","version":3,"duration_seconds":0.003952625}
migrate-1 | {"time":"2026-11-15T08:47:02.287583923Z","level":"INFO","msg":"migration applied","file":"00004_message_idempotency.sql","version":4,"duration_seconds":0.0022235}
migrate-1 | {"time":"2026-11-15T08:47:02.287588381Z","level":"INFO","msg":"migration applied","file":"00005_attachments.sql","version":5,"duration_seconds":0.008834}
migrate-1 | {"time":"2026-11-15T08:47:02.287592048Z","level":"INFO","msg":"migration applied","file":"00006_allow_attachment_only_messages.sql","version":6,"duration_seconds":0.0015365}
migrate-1 | {"time":"2026-11-15T08:47:02.287596715Z","level":"INFO","msg":"migrations completed","applied":6,"duration_seconds":0.060165208}
+3
View File
@@ -0,0 +1,3 @@
python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://127.0.0.1:18096"
Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
+6
View File
@@ -0,0 +1,6 @@
{
"width": 6500,
"height": 6200,
"encoded_bytes": 39249,
"upload_status": 413
}
+333
View File
@@ -0,0 +1,333 @@
{
"faults": {
"injected": [
"delete PostgreSQL row",
"delete S3 object",
"S3 connection refused"
],
"dry_run": {
"dry_run": true,
"duration_ms": 31,
"complete": true,
"orphan_scan": {
"s3_pages": 1,
"objects_scanned": 4,
"checked_in_db": 3,
"busy": 0,
"orphan_candidates": 1,
"objects_deleted": 0
},
"missing_scan": {
"db_pages": 1,
"rows_scanned": 3,
"s3_head_attempts": 3,
"missing_objects": 1,
"missing_attachment_ids": [
"0d3b9f77-50a9-4d31-90fa-2d25b4cd613e"
]
},
"wall_ms": 607.98
},
"bad_backup_rejected": {
"dry_run": false,
"duration_ms": 8,
"complete": false,
"wall_ms": 685.91
},
"restore_dry_run": {
"dry_run": true,
"duration_ms": 14,
"complete": true,
"restore": "would_restore",
"wall_ms": 603.21
},
"restore": {
"dry_run": false,
"duration_ms": 22,
"complete": true,
"restore": "restored",
"wall_ms": 431.95
},
"repeat_restore": {
"dry_run": false,
"duration_ms": 12,
"complete": true,
"restore": "already_present",
"wall_ms": 430.94
},
"conditional_existing_put_status": 412,
"apply": {
"dry_run": false,
"duration_ms": 190,
"complete": true,
"orphan_scan": {
"s3_pages": 1,
"objects_scanned": 5,
"checked_in_db": 3,
"busy": 0,
"orphan_candidates": 1,
"objects_deleted": 1
},
"missing_scan": {
"db_pages": 1,
"rows_scanned": 3,
"s3_head_attempts": 3,
"missing_objects": 0,
"missing_attachment_ids": []
},
"wall_ms": 689.99
},
"repeat": {
"dry_run": false,
"duration_ms": 59,
"complete": true,
"orphan_scan": {
"s3_pages": 1,
"objects_scanned": 3,
"checked_in_db": 2,
"busy": 0,
"orphan_candidates": 0,
"objects_deleted": 0
},
"missing_scan": {
"db_pages": 1,
"rows_scanned": 3,
"s3_head_attempts": 3,
"missing_objects": 0,
"missing_attachment_ids": []
},
"wall_ms": 653.52
},
"s3_outage": {
"dry_run": true,
"duration_ms": 107,
"complete": false,
"missing_scan": {
"db_pages": 0,
"rows_scanned": 0,
"s3_head_attempts": 0,
"missing_objects": 0,
"missing_attachment_ids": []
},
"wall_ms": 869.21
},
"fresh_preserved": true,
"linked_preserved": true,
"unlinked_preserved": true,
"sha256": "99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75",
"message_and_metadata_preserved": true,
"outsider_status": 404
},
"cost": [
{
"synthetic_pairs": 30,
"measured_pairs": 33,
"runs": [
{
"dry_run": true,
"duration_ms": 82,
"complete": true,
"orphan_scan": {
"s3_pages": 1,
"objects_scanned": 33,
"checked_in_db": 33,
"busy": 0,
"orphan_candidates": 0,
"objects_deleted": 0
},
"missing_scan": {
"db_pages": 1,
"rows_scanned": 33,
"s3_head_attempts": 33,
"missing_objects": 0,
"missing_attachment_ids": []
},
"wall_ms": 1791.83
},
{
"dry_run": true,
"duration_ms": 100,
"complete": true,
"orphan_scan": {
"s3_pages": 1,
"objects_scanned": 33,
"checked_in_db": 33,
"busy": 0,
"orphan_candidates": 0,
"objects_deleted": 0
},
"missing_scan": {
"db_pages": 1,
"rows_scanned": 33,
"s3_head_attempts": 33,
"missing_objects": 0,
"missing_attachment_ids": []
},
"wall_ms": 588.31
},
{
"dry_run": true,
"duration_ms": 32,
"complete": true,
"orphan_scan": {
"s3_pages": 1,
"objects_scanned": 33,
"checked_in_db": 33,
"busy": 0,
"orphan_candidates": 0,
"objects_deleted": 0
},
"missing_scan": {
"db_pages": 1,
"rows_scanned": 33,
"s3_head_attempts": 33,
"missing_objects": 0,
"missing_attachment_ids": []
},
"wall_ms": 572.91
}
],
"median_scan_ms": 82
},
{
"synthetic_pairs": 230,
"measured_pairs": 233,
"runs": [
{
"dry_run": true,
"duration_ms": 227,
"complete": true,
"orphan_scan": {
"s3_pages": 1,
"objects_scanned": 233,
"checked_in_db": 233,
"busy": 0,
"orphan_candidates": 0,
"objects_deleted": 0
},
"missing_scan": {
"db_pages": 2,
"rows_scanned": 233,
"s3_head_attempts": 233,
"missing_objects": 0,
"missing_attachment_ids": []
},
"wall_ms": 826.69
},
{
"dry_run": true,
"duration_ms": 132,
"complete": true,
"orphan_scan": {
"s3_pages": 1,
"objects_scanned": 233,
"checked_in_db": 233,
"busy": 0,
"orphan_candidates": 0,
"objects_deleted": 0
},
"missing_scan": {
"db_pages": 2,
"rows_scanned": 233,
"s3_head_attempts": 233,
"missing_objects": 0,
"missing_attachment_ids": []
},
"wall_ms": 591.24
},
{
"dry_run": true,
"duration_ms": 147,
"complete": true,
"orphan_scan": {
"s3_pages": 1,
"objects_scanned": 233,
"checked_in_db": 233,
"busy": 0,
"orphan_candidates": 0,
"objects_deleted": 0
},
"missing_scan": {
"db_pages": 2,
"rows_scanned": 233,
"s3_head_attempts": 233,
"missing_objects": 0,
"missing_attachment_ids": []
},
"wall_ms": 699.16
}
],
"median_scan_ms": 147
},
{
"synthetic_pairs": 1050,
"measured_pairs": 1053,
"runs": [
{
"dry_run": true,
"duration_ms": 629,
"complete": true,
"orphan_scan": {
"s3_pages": 2,
"objects_scanned": 1053,
"checked_in_db": 1053,
"busy": 0,
"orphan_candidates": 0,
"objects_deleted": 0
},
"missing_scan": {
"db_pages": 6,
"rows_scanned": 1053,
"s3_head_attempts": 1053,
"missing_objects": 0,
"missing_attachment_ids": []
},
"wall_ms": 1885.23
},
{
"dry_run": true,
"duration_ms": 587,
"complete": true,
"orphan_scan": {
"s3_pages": 2,
"objects_scanned": 1053,
"checked_in_db": 1053,
"busy": 0,
"orphan_candidates": 0,
"objects_deleted": 0
},
"missing_scan": {
"db_pages": 6,
"rows_scanned": 1053,
"s3_head_attempts": 1053,
"missing_objects": 0,
"missing_attachment_ids": []
},
"wall_ms": 1125.28
},
{
"dry_run": true,
"duration_ms": 3070,
"complete": true,
"orphan_scan": {
"s3_pages": 2,
"objects_scanned": 1053,
"checked_in_db": 1053,
"busy": 0,
"orphan_candidates": 0,
"objects_deleted": 0
},
"missing_scan": {
"db_pages": 6,
"rows_scanned": 1053,
"s3_head_attempts": 1053,
"missing_objects": 0,
"missing_attachment_ids": []
},
"wall_ms": 4871.64
}
],
"median_scan_ms": 629
}
]
}
+68
View File
@@ -0,0 +1,68 @@
{
"s3_api_bucket_scope_and_iam_denials": {
"detail": "",
"elapsed_seconds": 9.24,
"requirement": "requested_operational_check",
"status": "PASS"
},
"s3_api_delete_effect": {
"detail": "",
"elapsed_seconds": 2.247,
"requirement": "requested_operational_check",
"status": "PASS"
},
"s3_api_effective_policy": {
"detail": "",
"elapsed_seconds": 2.677,
"requirement": "requested_operational_check",
"status": "PASS"
},
"s3_bootstrap_preserves_object_and_volume": {
"detail": "",
"elapsed_seconds": 4.152,
"requirement": "requested_operational_check",
"status": "PASS"
},
"s3_console_closed_on_shared_network": {
"detail": "",
"elapsed_seconds": 1.491,
"requirement": "requested_operational_check",
"status": "PASS"
},
"s3_fixture_cleanup": {
"detail": "",
"elapsed_seconds": 4.488,
"requirement": "requested_operational_check",
"status": "PASS"
},
"s3_restart_preserves_original_bytes": {
"detail": "",
"elapsed_seconds": 6.478,
"requirement": "requested_operational_check",
"status": "PASS"
},
"s3_setup_and_scenarios": {
"detail": "",
"elapsed_seconds": 33.571,
"requirement": "requested_operational_check",
"status": "PASS"
},
"s3_two_replicas_after_restart": {
"detail": "",
"elapsed_seconds": 0.718,
"requirement": "requested_operational_check",
"status": "PASS"
},
"s3_two_replicas_before": {
"detail": "",
"elapsed_seconds": 1.003,
"requirement": "requested_operational_check",
"status": "PASS"
},
"s3_unsigned_known_object_403": {
"detail": "",
"elapsed_seconds": 0.453,
"requirement": "requested_operational_check",
"status": "PASS"
}
}
+2
View File
@@ -0,0 +1,2 @@
python3 tests/smoke.py --base-url "http://127.0.0.1:18097"
PASS: lab4 public API scenario; infrastructure and grading evidence remain separate.
+100
View File
@@ -0,0 +1,100 @@
{
"files": {
"Dockerfile": "17e5c9fa261dbe5395791213f605787824957da6567586ce990e44e3fd5a1236",
"Dockerfile.migrate": "0ab04cba4c2bb033c4d7c7d3a5984d8a27e8f355ff90e6023c3b7912d2211dec",
"cmd/api/main.go": "e9568a3a1098b01fded10cdc502dac652bf6a3a5761914c6860c9f45b56ed269",
"cmd/api/poolstats.go": "75c0ca950b85eaee3c0802e3001c61ba6436874b3be12cd38a5feb907b91528e",
"cmd/attachment-cleanup/main.go": "2658e84d6a062552303e04e41e45bd0b5fa1f6a75e90158f616d60fedbef3b3d",
"cmd/migrate/main.go": "6e9d413a86f53cf5f2e83d5ed0308af9b2c33e69fcb3d061b19bf480e352f257",
"cmd/migrate/main_test.go": "a88f2252d70caf644904331182dc3afd4df48b825a0329a82482798ec90b5d46",
"cmd/migrate/postgres_test.go": "37401cd60657af560d52e727b08861f444ab18355d9decd9ceeeddb009d079ca",
"cmd/migrate/roundtrip_test.go": "25b3425447b1dbafec83db2e5a7fc8172031908d5f5d57ce7b0d36a97850f8fa",
"compose.yaml": "e67836432e988a1035a91ed028471e801fcf0827fb6ff4070f1e583abc06d787",
"db/migrations/00001_init_schema.sql": "48db014273f78be0f6eb97fc521cccc54f851dc99e3688d480d3a6ff7b9b6bbe",
"db/migrations/00002_add_created_at_defaults.sql": "e0680838294b93f916d727a3a20d83aca7c3f74077202c9696b1fc73cda14649",
"db/migrations/00003_drop_session_table.sql": "1b666cee95b9eb1210b0a303de95994bd3c24628e9d9fdb1e1f3318edb756c3d",
"db/migrations/00004_message_idempotency.sql": "e7b3dd5a056a702dfde7acba6666430477db0c7e03ae03fb270e145d6303b9b2",
"db/migrations/00005_attachments.sql": "033d8884cecb50b5067453a97f2d7c51e51331a1911d9ddf8687437afa59443c",
"db/migrations/00006_allow_attachment_only_messages.sql": "ad56ecd0e8b88e0665cdc144aaa5d934ea7f48047cd702ce6f8832b6cf0bded3",
"db/migrations/embed.go": "a42e52ab05bc89b557a5bc6b3f0215883d99c4a039a8b32d6c19e66b2f769508",
"db/queries/attachments.sql": "cc030be1e7ed01d5895d37f0cf10d66821ef2c30dfd7fba4fb155488b8c96ebc",
"db/queries/chat_members.sql": "c80b6e50c321ff94317545ea2f9fdee1dfd69f564d8638f7b111c8ec091ef03a",
"db/queries/chats.sql": "8c52c762da78eb3a3eb7402b17ec87bd7fc2e1387f6ddb8e297b4f2d75beca7b",
"db/queries/message_idempotence.sql": "34259e4beee9cc07de4dec95c1d588222c7d756d02ac6564b54d6dbec36678db",
"db/queries/messages.sql": "f6f6382f27d698b1c2af2305d45699226ef10806b938b3dca78ec9087a622b7b",
"db/queries/messages_attachments.sql": "753e5b46dbe94b1043fbe19c83850e697feef7ea9ccc348496b71cb6f9906ddb",
"db/queries/users.sql": "0d73a6d46f4d4a1b0af37b750a208c07a38249f76974b41748f32c1949f990df",
"go.mod": "0dbf79ae49966e763868e20b453addaecbb74085dd7b0579271f0839ca8a17cd",
"go.sum": "30264a0cbc88f40ac455abb621624a54aa956bedd390c07deef4f67812efc982",
"infra/README.md": "0e804fa51debba4c38d3cc84abf4704897665e0335b272060363f03aadd6354f",
"infra/haproxy.cfg": "895a178e7354de61a5d40a2fe18b7b7c6d1d3d5faf0e5a5faa72783e7b983cb8",
"infra/s3/bootstrap.sh": "baa4b0b6fa4bd2a76b194b672ef2ca8575858ef6abc7ddfbede3eec3534bc8ce",
"internal/config/config.go": "142318dac05e3070358e7642c4b09813fe6bdb4d1b80e1154954d5e4e5f3fa59",
"internal/database/postgres.go": "3925c0bc9ae01bff9a37138a0dd19b38074bc2a70536b3a148f568b952516a21",
"internal/database/redis.go": "5b85a60bb0d1cb156c61bcd3c518c41c955f53c1d12da265b5e3a5dd7a397602",
"internal/database/sqlc/attachments.sql.go": "607a42ff362b4e69880a58b7eb989a0aac5d8e1b39df005dfae0c607dec659c5",
"internal/database/sqlc/chat_members.sql.go": "9e8cd177f6f888c72d19857fd4748fbb2020770c1af772a463fe3d1b813b37ee",
"internal/database/sqlc/chats.sql.go": "3dccf4b0ffa918ec5a3b66026b3dab4027456b499486359881000d184944802c",
"internal/database/sqlc/db.go": "3ca71a7f007a39987cae838da64303419dd804d2d1b6227676f47177971d76cf",
"internal/database/sqlc/message_idempotence.sql.go": "ebd31e6f369483eed8d6978dab1687552eed742a612b5ee4090499fe8a48a73f",
"internal/database/sqlc/messages.sql.go": "9836433d9645e6dd3bd66273ce15a53691f79093016c416fb24ffd085e62831e",
"internal/database/sqlc/messages_attachments.sql.go": "821d863a01079edae53805769821f1dc5eb3e32620ce61efd7326bb0253d2204",
"internal/database/sqlc/models.go": "df06356e705031dfc6226efd736f432853e026c26c30424675b99179819d422e",
"internal/database/sqlc/users.sql.go": "b954abd5b5964df39c20d857a60105513af84c8df68ce1ac9421da541a7800f4",
"internal/model/attachment.go": "877de511f5996c263c63c3e406ed67ee6609833f4946074594e1d438f8dce53a",
"internal/model/chat.go": "e0213f8360ea106b3627aa50cc87710153ff8e353475b42c2becdb9a32592660",
"internal/model/message.go": "4cebc4963e2eb104e85a1df6e8a93d6745c9ae42425df2d04bf6319e1d17b12d",
"internal/model/session.go": "b455d730e4e3efc95c6d1fe74ab43d86153e4bf863c09efd36bfe57efc669bc2",
"internal/model/user.go": "4bfd2d78e72f1c80b12541f2abc8500613e8f4d101575bd5d74fac22b52e3617",
"internal/repository/attachment.go": "62e75dbb4ed1cb966abbafc74530a676409ffa0b016a7943be5281cab1dce45c",
"internal/repository/attachment_tx.go": "c24c5b61a8cf07e823835c6f09110ac2334892dc53746b4039688a7250f80e87",
"internal/repository/chat.go": "7ef8dca39ff371f16d351b073199c25f719db22c7a525ab76ea7aca948efa157",
"internal/repository/errors.go": "9e3f77340ff87e41edc87380f795759701980685478737e7e4e1f9737e2634fa",
"internal/repository/message.go": "2f92e2808317850ca5873cc26f8e6c4d83104e8fc957a89304fc74ba2f64cbf2",
"internal/repository/postgres/attachment.go": "5ca7ed710c8dc07473376810e2bf746d2f2d60435b220e2e1cf20ee82c0c3e57",
"internal/repository/postgres/chat.go": "d757c579e5b6599ab9366aef46fd59f9652247f3d947615aa46c919746b65ea5",
"internal/repository/postgres/errors.go": "580e5e599f7af8a403404c6850986e88efceda6393d21e772ac108be34004ba9",
"internal/repository/postgres/message.go": "a2057f6fe6ba2a93a7f98bdbd799baf043999811e9fa7bb649e4f0d60138b6c2",
"internal/repository/postgres/user.go": "eb6770e5cdd96b16a6e9d2116af89a6dd466dfbca4f4c179fe06c30c487e53c4",
"internal/repository/postgres/utils.go": "6c5c83a8ad8ecf611812fbf8c90735e2590a09dec88cd914a10f6b36a9f041b7",
"internal/repository/redis/errors.go": "ab56571d1d0b7d13b7d7ff868916f7e506c5a54c29e6bb211b3021ad4bf601ab",
"internal/repository/redis/session.go": "c1654fea2ba47734a5403d6f6cbb2fe70fbe7e6e37166ae75f338d3490981de1",
"internal/repository/session.go": "071285ff4d8033a6512a7256583a1cb32de782d6fd9f1696fac109927f7db699",
"internal/repository/user.go": "bee65136137d97bea2240411c96a93cd945b1b9145452416f7a83733a1fd28e7",
"internal/security/password.go": "8f74bef9216a2d12aa734e04fa786eac6b31f1378fe42f729fe1b97495c10487",
"internal/service/attachment.go": "4b901d85d93d6dffde1bf76dd532b4b6845afd55ea7a8f9aaea983eaead1e9da",
"internal/service/attachment_cleanup.go": "c4e66bb48f7cd26de6fedf4ca7cf751c815dcab30e9442de61f7fe000d3c2367",
"internal/service/attachment_reconcile.go": "f7dbf60b14f9e82cc018bdd29482d71786a30c32bfb5d1ed1808342c9fd36cdb",
"internal/service/auth.go": "71b8960640866e515e9060159af750c418d97a6373d2616c3b59b764172aa54c",
"internal/service/chat.go": "bc7c8bea0066d8bbd926f78d2c100f0bd9a48c29fb542c5cfa3f2ae415299ef5",
"internal/service/errors.go": "fb5aa4f7be76af14ab81629eceb02e1c3a1bafd8bfeb036e7ee5ccd3940d64da",
"internal/service/helper.go": "390320fa296d9637630e6ac0d90c1229002b578a29186cad414896d5f4d1b8a6",
"internal/service/message.go": "9556a18a5679d7e44b5cdcf329488185d2b90cadb3180af926df4bb6ab3f9d0d",
"internal/service/user.go": "542051ef3eeff15d340979a615a7e0da3d21ed7ed93bea2547ab52d8e303b5e2",
"internal/storage/errors.go": "21a5622cd2655663fc24ee485013c1a917279990fd128a5d406b59f5f0ee67cd",
"internal/storage/object_cleanup_storage.go": "ef50236baefe3e3aea8ca64264ae3c2e5fc89c245b9b1d7cfd207d354f0be2c9",
"internal/storage/object_writer.go": "15ca1257b9c42f64a42f19a4bbb487e9d2950aed2fdf87959fe6c2d763998b64",
"internal/storage/s3/client.go": "032ec339cb91fc1f049a4f92ac61d0404199be059d452307cd12455386f68134",
"internal/storage/s3/errors.go": "6f1f5a7dbd93f65f8101ba80b6fa17bab964744d237746b3ae4ac3650cc9b52c",
"internal/transport/http/handler/attachment.go": "885b2c4d24cbf0f5bfa9def4f6b5b4a7621919b47c3906d89a297abcf0c73da8",
"internal/transport/http/handler/attachment_types.go": "90210075401601bacb25ae70dea2e19a203f537cf0f5aff1369679619bb87811",
"internal/transport/http/handler/auth.go": "f175a570d47ca38ce627d0bc88b6f6a2adbf648345229fa961ea0144717744f1",
"internal/transport/http/handler/auth_types.go": "5ffcd182606a77d538efb5f1a51df23b8a324a605b5d226026048d34c409ebdb",
"internal/transport/http/handler/chat.go": "e5214ebf94ac9b8f37537e1a3fc844a2e8ae4e9b5aa159fe2b6e4da9ab2342f0",
"internal/transport/http/handler/chat_types.go": "d181bce0f003749d813c1254df4c8ebcc1b99ff506b61efa6899da4a499e95f4",
"internal/transport/http/handler/health.go": "1abf04652ea0b69bd87321b95e1a921d3c5ad3c04207232c2a9408bf722e550a",
"internal/transport/http/handler/health_types.go": "02ee3031e10fbb279e020f409fbd905ae2640aa306b2c558c3f8e6dcfe9efa80",
"internal/transport/http/handler/message.go": "540065ffc9090848fc25d560f1ad9734d4f0d45cdf0b5907a0f196f9efa8ede3",
"internal/transport/http/handler/message_types.go": "270a9504a3001b1dc02a1f62cdaed12d69af2471340674e50022c609643dcb34",
"internal/transport/http/handler/user.go": "86fd65ed176e9a83dc27c1112ff8384bc5aa32a92410290fb505d715ca167775",
"internal/transport/http/handler/user_types.go": "0c4450be649afe5edf4e8d3ac9baada79deb9e1423ace56b87aa227ebd0eb998",
"internal/transport/http/httputils/helper.go": "59c092950b69f5390427d0c6c1f90d2c33630d87a42bef363aed704960cb3a22",
"internal/transport/http/middleware/auth.go": "413d7fb5f8573a808c2eb4e36becd29b4d7edffe5b096b26fc07ac22a0285430",
"internal/transport/http/middleware/context.go": "f6db0e807659f30b05cae91f73c70d87f359760c57110e0d25b49a65bd0394a5",
"internal/transport/http/middleware/logging.go": "a477fda512953eb9f46266537c12ace9cc992ba8f036a5b041f9f8295bfd2097",
"internal/transport/http/router.go": "2841bac8988517591539336ba20718b4da7bae31ab81bf56e6b2b9bb59b0a297",
"scripts/restart.sh": "5f70beb4337aed16881a0728229f02dce0e95d42028e0088acd52aa0a76eb019",
"sqlc.yaml": "4e72de8f0b44d6d2b6a44da89f86dd5326e85077a5e7a8ae71816a5dff1fbaa3"
},
"sha256": "4c7aff2f3780fd4df0ebb4ab7b380e9c8540bbcb45ae64fb0e2894bb650109e5"
}
+43
View File
@@ -0,0 +1,43 @@
module highload
go 1.27.1
require (
github.com/google/uuid v1.6.0
github.com/jackc/pgx/v5 v5.11.0
github.com/pressly/goose/v3 v3.26.0
github.com/redis/go-redis/v9 v9.23.0
golang.org/x/crypto v0.57.0
)
require (
github.com/aws/aws-sdk-go-v2 v1.48.0 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.27 // indirect
github.com/aws/aws-sdk-go-v2/config v1.33.18 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.21.0 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.20.9 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.12 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.8.12 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.5.12 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.25 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.11.13 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.14.12 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.20.12 // indirect
github.com/aws/aws-sdk-go-v2/service/s3 v1.114.10 // indirect
github.com/aws/aws-sdk-go-v2/service/signin v1.13.1 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.39.5 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.44.5 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.52.0 // indirect
github.com/aws/smithy-go v1.28.9 // indirect
github.com/cespare/xxhash/v2 v2.3.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/mfridman/interpolate v0.0.2 // indirect
github.com/sethvargo/go-retry v0.3.0 // indirect
go.uber.org/atomic v1.12.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/sync v0.23.0 // indirect
golang.org/x/sys v0.48.0 // indirect
golang.org/x/text v0.42.0 // indirect
)
+105
View File
@@ -0,0 +1,105 @@
github.com/aws/aws-sdk-go-v2 v1.48.0 h1:tFU7EJVm7MfyaXLsXdYTZ0Vd6vwt9OljCyqq0gqk0Mc=
github.com/aws/aws-sdk-go-v2 v1.48.0/go.mod h1:oeKU5p+TQBnpeLmU42WZLWvzv+ORLn+74n4QVLnm/Z0=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.27 h1:3dMP9eEmHVosFN1QiD24IJBKVY8m6iUfVYXqRCT118Q=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.27/go.mod h1:KH4PKcqtjy1m0656eqQV2v53xE04ddJzuNloEKqlHkI=
github.com/aws/aws-sdk-go-v2/config v1.33.18 h1:WC0C4jM/tewXG8DWXnoxXnusJPZTryPazqk2k/zgd2s=
github.com/aws/aws-sdk-go-v2/config v1.33.18/go.mod h1:0kRpFgsnD59mC2bbbyOTVpaZ9RnXW/pFkqlfiwbgZaU=
github.com/aws/aws-sdk-go-v2/credentials v1.21.0 h1:OViwhqeipkQzSHuO2eQDUCdLrI6r3nzGxFUyM8GqnCs=
github.com/aws/aws-sdk-go-v2/credentials v1.21.0/go.mod h1:OffaVQod3fyZzhZJyPtw11WpHPF1uIHADc+/MYlXmbA=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.20.9 h1:oH1E0tjN0g+ZRusbX1dsuat4FJ0z05l8hWaycSbDuNc=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.20.9/go.mod h1:3ZaQGigIoaS45x9HgnLZ4sP475Fs7QoxU3IsZX/J3h4=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.12 h1:W0yUUzKBA8SofGQ0yZafEUuZq9UxfvijRgdc6vcRQPA=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.12/go.mod h1:xwKv6vwBNdpPFOxEm/ld4IODZK2s/1ksHvdBqLfH6Gs=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.8.12 h1:lqSi8qvAnKC1vfosImdmOsuiJLr/jdybZX5299beB6Q=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.8.12/go.mod h1:rqPQE2vqkW9YHx5+Gp55sx1wsTM4xtGQLl+TKMjxxrs=
github.com/aws/aws-sdk-go-v2/internal/v4a v1.5.12 h1:uvX2yaL+aMjkmnrLPPlR/zwjoKFRMoNhhJfeGjfk6qc=
github.com/aws/aws-sdk-go-v2/internal/v4a v1.5.12/go.mod h1:dicT+2eNhHHqGkxHO7LoXw3QghIUGuzgdmFqaSTopgI=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.25 h1:rKwkY3Z+fvt2W0msjdNs6uNewPmXzPayQ961oyuWhes=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.25/go.mod h1:dBuAadQKd1F4Q4ZOg8gnFmuSHMRE40Pe3IfFwbtC4lE=
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.11.13 h1:U5HZ8FpTr6tyvulf1nT11Nw4KG1CAsXkuQGdUrIjZ/E=
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.11.13/go.mod h1:dUfvCU1NaE+b3vqNENDiJncUQW7Djl5pmgUlcjesVqk=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.14.12 h1:wzAqMZ2O4+ij9HQqIrk+P9PBGacE0wOTuWB7Mo97//I=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.14.12/go.mod h1:/0TfyruByHsOlphzP26xDTRe2DKD0J1I1Uyy2TyP8EI=
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.20.12 h1:H0IaQayT3TLiGIHiNnleQZTbndDlx7LGZ0NWZ5mDwjA=
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.20.12/go.mod h1:YuJJNxaOjnknLIrscBmkLhIpnmg9LzHd25sQRKUboGU=
github.com/aws/aws-sdk-go-v2/service/s3 v1.114.10 h1:WBJb2VxRzzKnuN94YWqZPiu5a5zc3IleFqgZnmVCyvU=
github.com/aws/aws-sdk-go-v2/service/s3 v1.114.10/go.mod h1:8NhAIBVYkVHq0hIbbzujdoou0VkKwMdG+De9e5oX6qI=
github.com/aws/aws-sdk-go-v2/service/signin v1.13.1 h1:ssrDA6AKfXEc3h0Pf4EI4vQ8sN5Zd136SrU9Tjr0XPc=
github.com/aws/aws-sdk-go-v2/service/signin v1.13.1/go.mod h1:LdwHjKqPE9VCC7sTlWcOAizZx3ZJYjmfwC/EW2tIIAE=
github.com/aws/aws-sdk-go-v2/service/sso v1.39.5 h1:ANAoa+qt3/9kXnvQVgUyxcTzhzZf4DqaBS+M2DQrELU=
github.com/aws/aws-sdk-go-v2/service/sso v1.39.5/go.mod h1:vsMi+SSR7xGQXtV430LVi49qbv/R+U+upHHSp1W/+QI=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.44.5 h1:uIOBLMhef4N16WguadoHKuBy9etdz+RKTeqzchXErAI=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.44.5/go.mod h1:NwWmkFLtsTVyXQpbzPOlYLwEoANkdCFjJRtrNcbg/vY=
github.com/aws/aws-sdk-go-v2/service/sts v1.52.0 h1:IaBLmJWiIEtKkjXMgMbYGZk7WKXbYA8148wCMCXq9hU=
github.com/aws/aws-sdk-go-v2/service/sts v1.52.0/go.mod h1:MG6N8U9+lY4LqchrlHPqCOSr7hyxw70XU0Et2YR3eNA=
github.com/aws/smithy-go v1.28.9 h1:zkZ058OpNX0iGTK24b+ZUEQSMc11CBA6oO7xtKZfzkY=
github.com/aws/smithy-go v1.28.9/go.mod h1:YE2RhdIuDbA5E5bTdciG9KrW3+TiEONeUWCqxX9i1Fc=
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/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/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.11.0 h1:IzBBtyK9AHqf98cctWFifYSci2hgQR/cd56wB4p+ogg=
github.com/jackc/pgx/v5 v5.11.0/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4=
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/klauspost/cpuid/v2 v2.4.0 h1:S6Hrbc7+ywsr0r+RLapfGBHfyefhCTwEh3A0tV913Dw=
github.com/klauspost/cpuid/v2 v2.4.0/go.mod h1:19jmZ9mjzoF//ddRSUsv0zfBTJWh3QJh9FNxZTMrGxU=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mfridman/interpolate v0.0.2 h1:pnuTK7MQIxxFz1Gr+rjSIx9u7qVjf5VOoM/u6BbAxPY=
github.com/mfridman/interpolate v0.0.2/go.mod h1:p+7uk6oE07mpE/Ik1b8EckO0O4ZXiGAfshKBWLUM9Xg=
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pressly/goose/v3 v3.26.0 h1:KJakav68jdH0WDvoAcj8+n61WqOIaPGgH0bJWS6jpmM=
github.com/pressly/goose/v3 v3.26.0/go.mod h1:4hC1KrritdCxtuFsqgs1R4AU5bWtTAf+cnWvfhf2DNY=
github.com/redis/go-redis/v9 v9.23.0 h1:/TTVdJa8BRNsybwwGGnmiYktHNlUXtbOQxlWpHBPL3s=
github.com/redis/go-redis/v9 v9.23.0/go.mod h1:EWP2UUk+XDaI1s5nQB5mablZN6vccXlmBQTyfnF87D0=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/sethvargo/go-retry v0.3.0 h1:EEt31A35QhrcRZtrYFDTBg91cqZVnFL2navjDrah2SE=
github.com/sethvargo/go-retry v0.3.0/go.mod h1:mNX17F0C/HguQMyMyJxcnU471gOZGxCLyYaFyAZraas=
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.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE=
github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg=
github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs=
github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s=
go.uber.org/atomic v1.12.0 h1:BvcXdFKuviU4fTL/f+SxdQ5qJX/Jix8pAkgdUcb3XOE=
go.uber.org/atomic v1.12.0/go.mod h1:I6c4cg+6HCxRjfjSsYtApoFILnpc0CGUdGkXVqbYVNk=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M=
golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA=
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b h1:M2rDM6z3Fhozi9O7NWsxAkg/yqS/lQJ6PmkyIV3YP+o=
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b/go.mod h1:3//PLf8L/X+8b4vuAfHzxeRUl04Adcb341+IGKfnqS8=
golang.org/x/sync v0.23.0 h1:KameEIfc1IkluZyXWLn39Wd4tURc6GbCiISGiZm2bQk=
golang.org/x/sync v0.23.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0=
golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo=
golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
golang.org/x/text v0.42.0 h1:JbOZXgfeCPU9gacVtYliJqOhD+zhrEqK4LfdpmlUZqI=
golang.org/x/text v0.42.0/go.mod h1:ojzP1Z+2QtioaF8DTtO8K5q7JWVVYwZKenzujK0Zd0E=
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=
modernc.org/libc v1.66.3 h1:cfCbjTUcdsKyyZZfEUKfoHcP3S0Wkvz3jgSzByEWVCQ=
modernc.org/libc v1.66.3/go.mod h1:XD9zO8kt59cANKvHPXpx7yS2ELPheAey0vjIuZOhOU8=
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI=
modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
modernc.org/sqlite v1.38.2 h1:Aclu7+tgjgcQVShZqim41Bbw9Cho0y/7WzYptXqkEek=
modernc.org/sqlite v1.38.2/go.mod h1:cPTJYSlgg3Sfg046yBShXENNtPrWrDX8bsbAQBzgQ5E=
+28
View File
@@ -0,0 +1,28 @@
global
log stdout format raw local0
stats socket ipv4@127.0.0.1:9999 level admin
defaults
mode http
log global
option httplog
timeout connect 2s
timeout client 30s
timeout server 30s
timeout http-request 10s
resolvers docker
nameserver docker 127.0.0.11:53
accepted_payload_size 8192
hold valid 1s
frontend api
bind *:8080
default_backend api_replicas
backend api_replicas
balance roundrobin
timeout check 3s
option httpchk GET /health/ready
http-check expect status 200
server-template api 4 api:8080 check inter 1s fall 2 rise 2 resolvers docker init-addr none
+65
View File
@@ -0,0 +1,65 @@
#!/bin/sh
set -eu
for name in rustfs_root_access_key rustfs_root_secret_key s3_access_key_id s3_secret_access_key; do
if [ ! -s "/run/secrets/$name" ]; then
echo "missing S3 secret: $name" >&2
exit 1
fi
done
root_key=$(cat /run/secrets/rustfs_root_access_key)
root_secret=$(cat /run/secrets/rustfs_root_secret_key)
app_key=$(cat /run/secrets/s3_access_key_id)
app_secret=$(cat /run/secrets/s3_secret_access_key)
if [ "$root_key" = "$app_key" ]; then
echo 'API and RustFS root access keys must differ' >&2
exit 1
fi
# These values become path and JSON fragments below.
case "$S3_BUCKET" in
messenger-attachments) ;;
*) echo 'unsupported S3_BUCKET in bootstrap' >&2; exit 1 ;;
esac
rc=/usr/bin/rc
policy=messenger-attachments-api-v1
"$rc" alias set root http://s3:9000 "$root_key" "$root_secret" \
--region "$S3_REGION" --bucket-lookup path >/dev/null
"$rc" bucket create "root/$S3_BUCKET" --ignore-existing
# A new bucket is private by default; this also removes a stale public policy.
"$rc" bucket anonymous set private "root/$S3_BUCKET"
cat > /tmp/api-policy.json <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["s3:ListBucket", "s3:GetBucketLocation"],
"Resource": "arn:aws:s3:::$S3_BUCKET"
},
{
"Effect": "Allow",
"Action": ["s3:PutObject", "s3:GetObject", "s3:DeleteObject"],
"Resource": "arn:aws:s3:::$S3_BUCKET/*"
}
]
}
EOF
if ! "$rc" admin policy info root/ "$policy" >/dev/null 2>&1; then
"$rc" admin policy create root/ "$policy" /tmp/api-policy.json
fi
if ! "$rc" admin user info root/ "$app_key" >/dev/null 2>&1; then
"$rc" admin user add root/ "$app_key" "$app_secret" >/dev/null
fi
"$rc" admin policy attach root/ "$policy" --user "$app_key" >/dev/null
"$rc" alias set app http://s3:9000 "$app_key" "$app_secret" \
--region "$S3_REGION" --bucket-lookup path >/dev/null
"$rc" bucket list "app/$S3_BUCKET" >/dev/null
echo "S3 bucket and API identity ready: $S3_BUCKET"
+175
View File
@@ -0,0 +1,175 @@
package config
import (
"fmt"
"os"
"strconv"
"strings"
"time"
)
type Database struct {
PostgresURL string
RedisAddr string
}
type S3 struct {
Endpoint string
Region string
Bucket string
AccessKeyID string
SecretAccessKey string
}
type Config struct {
HTTP struct {
Host string
Port string
}
Database Database
Session struct {
TTL time.Duration
Store string
}
S3 S3
AppEnv string
CacheURL string
}
func Load() (*Config, error) {
config := &Config{}
sessionTTL, err := getSessionTTL()
if err != nil {
return nil, err
}
config.Session.TTL = sessionTTL
config.HTTP.Host = os.Getenv("APP_HOST")
if config.HTTP.Host == "" {
config.HTTP.Host = "0.0.0.0"
}
config.HTTP.Port = os.Getenv("APP_PORT")
if config.HTTP.Port == "" {
config.HTTP.Port = "8080"
}
config.AppEnv = os.Getenv("APP_ENV")
if config.AppEnv == "" {
config.AppEnv = "lab"
}
store := os.Getenv("SESSION_STORE")
config.Session.Store = strings.TrimSpace(string(store))
if config.Session.Store == "" {
return nil, fmt.Errorf("empty session store data")
}
config.Database, err = getDatabaseURL()
if err != nil {
return nil, err
}
config.S3, err = getS3Data()
if err != nil {
return nil, err
}
return config, nil
}
func getSessionTTL() (time.Duration, error) {
value, ok := os.LookupEnv("SESSION_TTL_SECONDS")
if !ok {
return time.Hour, nil
}
seconds, err := strconv.ParseInt(value, 10, 64)
if err != nil || seconds <= 0 || seconds > int64((1<<63-1)/int64(time.Second)) {
return 0, fmt.Errorf("invalid SESSION_TTL_SECONDS: %q", value)
}
return time.Duration(seconds) * time.Second, nil
}
func LoadAttachmentCleanup() (postgresURL string, objectStorage S3, err error) {
postgresURL, err = loadPostgresURL()
if err != nil {
return "", S3{}, err
}
objectStorage, err = getS3Data()
return postgresURL, objectStorage, err
}
func loadPostgresURL() (string, error) {
postgresData, err := os.ReadFile(os.Getenv("DATABASE_URL_FILE"))
if err != nil {
return "", fmt.Errorf("read database URL: %w", err)
}
postgresURL := strings.TrimSpace(string(postgresData))
if postgresURL == "" {
return "", fmt.Errorf("empty database URL")
}
return postgresURL, nil
}
func getDatabaseURL() (Database, error) {
postgresURL, err := loadPostgresURL()
if err != nil {
return Database{}, err
}
db := Database{PostgresURL: postgresURL}
redisData := os.Getenv("REDIS_ADDR")
db.RedisAddr = strings.TrimSpace(string(redisData))
if db.RedisAddr == "" {
return db, fmt.Errorf("redis addres is empty")
}
return db, nil
}
func getS3Data() (S3, error) {
s3 := S3{
Endpoint: os.Getenv("S3_ENDPOINT"),
Region: os.Getenv("S3_REGION"),
Bucket: os.Getenv("S3_BUCKET"),
}
if s3.Endpoint == "" {
return s3, fmt.Errorf("empty s3 endpoint")
}
if s3.Region == "" {
return s3, fmt.Errorf("empty s3 region")
}
if s3.Bucket == "" {
return s3, fmt.Errorf("emtpy s3 bucket")
}
accessKeyIDPath := os.Getenv("S3_ACCESS_KEY_ID_FILE")
accessKeyID, err := os.ReadFile(accessKeyIDPath)
if err != nil {
return s3, fmt.Errorf("empty s3 access_key_id")
}
s3.AccessKeyID = strings.TrimSpace(string(accessKeyID))
secretAccessKeyPath := os.Getenv("S3_SECRET_ACCESS_KEY_FILE")
secretAccessKey, err := os.ReadFile(secretAccessKeyPath)
if err != nil {
return s3, fmt.Errorf("empty s3 secret_access_key_file")
}
s3.SecretAccessKey = strings.TrimSpace(string(secretAccessKey))
return s3, nil
}
+21
View File
@@ -0,0 +1,21 @@
package database
import (
"context"
"github.com/jackc/pgx/v5/pgxpool"
)
func NewPostgres(ctx context.Context, databaseURL string) (*pgxpool.Pool, error) {
pool, err := pgxpool.New(ctx, databaseURL)
if err != nil {
return nil, err
}
if err = pool.Ping(ctx); err != nil {
pool.Close()
return nil, err
}
return pool, nil
}
+31
View File
@@ -0,0 +1,31 @@
package database
import (
"context"
"time"
"github.com/redis/go-redis/v9"
)
func NewRedis(ctx context.Context, addr string) (*redis.Client, error) {
options := &redis.Options{
Addr: addr,
DialTimeout: 3 * time.Second,
ReadTimeout: 3 * time.Second,
WriteTimeout: 2 * time.Second,
PoolSize: 10,
PoolTimeout: 2 * time.Second,
ContextTimeoutEnabled: true,
}
client := redis.NewClient(options)
if err := client.Ping(ctx).Err(); err != nil {
_ = client.Close()
return nil, err
}
return client, nil
}
+207
View File
@@ -0,0 +1,207 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: attachments.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const attachmentExistsByObjectKey = `-- name: AttachmentExistsByObjectKey :one
SELECT EXISTS (
SELECT 1 FROM attachments
WHERE object_key = $1
)
`
func (q *Queries) AttachmentExistsByObjectKey(ctx context.Context, objectKey string) (bool, error) {
row := q.db.QueryRow(ctx, attachmentExistsByObjectKey, objectKey)
var exists bool
err := row.Scan(&exists)
return exists, err
}
const createAttachment = `-- name: CreateAttachment :one
INSERT INTO attachments
(chat_id, owner_id, filename, object_key, content_type, size_bytes, sha256)
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id, chat_id, owner_id, filename, object_key, content_type, size_bytes, sha256, created_at, updated_at
`
type CreateAttachmentParams struct {
ChatID pgtype.UUID
OwnerID pgtype.UUID
Filename string
ObjectKey string
ContentType string
SizeBytes int64
Sha256 string
}
func (q *Queries) CreateAttachment(ctx context.Context, arg CreateAttachmentParams) (Attachment, error) {
row := q.db.QueryRow(ctx, createAttachment,
arg.ChatID,
arg.OwnerID,
arg.Filename,
arg.ObjectKey,
arg.ContentType,
arg.SizeBytes,
arg.Sha256,
)
var i Attachment
err := row.Scan(
&i.ID,
&i.ChatID,
&i.OwnerID,
&i.Filename,
&i.ObjectKey,
&i.ContentType,
&i.SizeBytes,
&i.Sha256,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const getAttachmentByID = `-- name: GetAttachmentByID :one
SELECT id, chat_id, owner_id, filename, object_key, content_type, size_bytes, sha256, created_at, updated_at FROM attachments WHERE id = $1
`
func (q *Queries) GetAttachmentByID(ctx context.Context, attachmentID pgtype.UUID) (Attachment, error) {
row := q.db.QueryRow(ctx, getAttachmentByID, attachmentID)
var i Attachment
err := row.Scan(
&i.ID,
&i.ChatID,
&i.OwnerID,
&i.Filename,
&i.ObjectKey,
&i.ContentType,
&i.SizeBytes,
&i.Sha256,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const getAttachmentForUser = `-- name: GetAttachmentForUser :one
SELECT a.id, a.chat_id, a.owner_id, a.filename, a.object_key, a.content_type, a.size_bytes, a.sha256, a.created_at, a.updated_at
FROM attachments AS a
JOIN chat_members AS cm ON cm.chat_id = a.chat_id
WHERE a.id = $1
AND cm.user_id = $2
`
type GetAttachmentForUserParams struct {
AttachmentID pgtype.UUID
UserID pgtype.UUID
}
func (q *Queries) GetAttachmentForUser(ctx context.Context, arg GetAttachmentForUserParams) (Attachment, error) {
row := q.db.QueryRow(ctx, getAttachmentForUser, arg.AttachmentID, arg.UserID)
var i Attachment
err := row.Scan(
&i.ID,
&i.ChatID,
&i.OwnerID,
&i.Filename,
&i.ObjectKey,
&i.ContentType,
&i.SizeBytes,
&i.Sha256,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const getAttachmentsForMessages = `-- name: GetAttachmentsForMessages :many
SELECT message_id, attachment_id, position
FROM messages_attachments
WHERE message_id = ANY($1::uuid[])
ORDER BY message_id, position
`
type GetAttachmentsForMessagesRow struct {
MessageID pgtype.UUID
AttachmentID pgtype.UUID
Position int16
}
func (q *Queries) GetAttachmentsForMessages(ctx context.Context, messageIds []pgtype.UUID) ([]GetAttachmentsForMessagesRow, error) {
rows, err := q.db.Query(ctx, getAttachmentsForMessages, messageIds)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetAttachmentsForMessagesRow
for rows.Next() {
var i GetAttachmentsForMessagesRow
if err := rows.Scan(&i.MessageID, &i.AttachmentID, &i.Position); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listAttachmentKeys = `-- name: ListAttachmentKeys :many
SELECT id, object_key, created_at
FROM attachments
WHERE object_key > $1
ORDER BY object_key
LIMIT $2
`
type ListAttachmentKeysParams struct {
AfterKey string
PageSize int32
}
type ListAttachmentKeysRow struct {
ID pgtype.UUID
ObjectKey string
CreatedAt pgtype.Timestamptz
}
func (q *Queries) ListAttachmentKeys(ctx context.Context, arg ListAttachmentKeysParams) ([]ListAttachmentKeysRow, error) {
rows, err := q.db.Query(ctx, listAttachmentKeys, arg.AfterKey, arg.PageSize)
if err != nil {
return nil, err
}
defer rows.Close()
var items []ListAttachmentKeysRow
for rows.Next() {
var i ListAttachmentKeysRow
if err := rows.Scan(&i.ID, &i.ObjectKey, &i.CreatedAt); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const tryLockAttachmentObjectKey = `-- name: TryLockAttachmentObjectKey :one
SELECT pg_try_advisory_xact_lock(
hashtextextended($1::text, 0)
)::boolean AS locked
`
func (q *Queries) TryLockAttachmentObjectKey(ctx context.Context, objectKey string) (bool, error) {
row := q.db.QueryRow(ctx, tryLockAttachmentObjectKey, objectKey)
var locked bool
err := row.Scan(&locked)
return locked, err
}
@@ -0,0 +1,27 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: chat_members.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const createChatMember = `-- name: CreateChatMember :exec
INSERT INTO chat_members(chat_id, user_id)
VALUES ($1, $2)
`
type CreateChatMemberParams struct {
ChatID pgtype.UUID
UserID pgtype.UUID
}
func (q *Queries) CreateChatMember(ctx context.Context, arg CreateChatMemberParams) error {
_, err := q.db.Exec(ctx, createChatMember, arg.ChatID, arg.UserID)
return err
}
+118
View File
@@ -0,0 +1,118 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: chats.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const createChat = `-- name: CreateChat :one
INSERT INTO chats (title)
VALUES ($1)
RETURNING id, title, created_at
`
func (q *Queries) CreateChat(ctx context.Context, title string) (Chat, error) {
row := q.db.QueryRow(ctx, createChat, title)
var i Chat
err := row.Scan(&i.ID, &i.Title, &i.CreatedAt)
return i, err
}
const getChatByID = `-- name: GetChatByID :one
SELECT c.id, c.title, c.created_at, array_agg(cm.user_id)::uuid[] AS member_ids
FROM chats c
JOIN chat_members cm ON cm.chat_id = c.id
WHERE c.id = $1
GROUP BY c.id
`
type GetChatByIDRow struct {
ID pgtype.UUID
Title string
CreatedAt pgtype.Timestamptz
MemberIds []pgtype.UUID
}
func (q *Queries) GetChatByID(ctx context.Context, id pgtype.UUID) (GetChatByIDRow, error) {
row := q.db.QueryRow(ctx, getChatByID, id)
var i GetChatByIDRow
err := row.Scan(
&i.ID,
&i.Title,
&i.CreatedAt,
&i.MemberIds,
)
return i, err
}
const getChatsByUserID = `-- name: GetChatsByUserID :many
SELECT c.id, c.title, c.created_at, array_agg(cm.user_id)::uuid[] AS member_ids
FROM chats c
JOIN chat_members cm ON cm.chat_id = c.id
WHERE EXISTS (SELECT 1
FROM chat_members membership
WHERE membership.chat_id = c.id
AND membership.user_id = $1)
AND (
$2::timestamptz IS NULL
OR (c.created_at, c.id) >
(
$2::timestamptz,
$3::uuid
)
)
GROUP BY c.id
ORDER BY c.created_at ASC, c.id ASC
LIMIT $4
`
type GetChatsByUserIDParams struct {
UserID pgtype.UUID
AfterCreatedAt pgtype.Timestamptz
AfterID pgtype.UUID
LimitCount int32
}
type GetChatsByUserIDRow struct {
ID pgtype.UUID
Title string
CreatedAt pgtype.Timestamptz
MemberIds []pgtype.UUID
}
func (q *Queries) GetChatsByUserID(ctx context.Context, arg GetChatsByUserIDParams) ([]GetChatsByUserIDRow, error) {
rows, err := q.db.Query(ctx, getChatsByUserID,
arg.UserID,
arg.AfterCreatedAt,
arg.AfterID,
arg.LimitCount,
)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetChatsByUserIDRow
for rows.Next() {
var i GetChatsByUserIDRow
if err := rows.Scan(
&i.ID,
&i.Title,
&i.CreatedAt,
&i.MemberIds,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
+32
View File
@@ -0,0 +1,32 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
package sqlc
import (
"context"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)
type DBTX interface {
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
QueryRow(context.Context, string, ...interface{}) pgx.Row
}
func New(db DBTX) *Queries {
return &Queries{db: db}
}
type Queries struct {
db DBTX
}
func (q *Queries) WithTx(tx pgx.Tx) *Queries {
return &Queries{
db: tx,
}
}
@@ -0,0 +1,66 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: message_idempotence.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const getIdempotentMessage = `-- name: GetIdempotentMessage :one
SELECT m.id, m.chat_id, m.sender_id, m.text, m.created_at
FROM message_idempotency AS i
JOIN messages AS m ON m.id = i.message_id
WHERE i.user_id = $1
AND i.chat_id = $2
AND i.idempotency_key = $3
`
type GetIdempotentMessageParams struct {
UserID pgtype.UUID
ChatID pgtype.UUID
IdempotencyKey string
}
func (q *Queries) GetIdempotentMessage(ctx context.Context, arg GetIdempotentMessageParams) (Message, error) {
row := q.db.QueryRow(ctx, getIdempotentMessage, arg.UserID, arg.ChatID, arg.IdempotencyKey)
var i Message
err := row.Scan(
&i.ID,
&i.ChatID,
&i.SenderID,
&i.Text,
&i.CreatedAt,
)
return i, err
}
const insertMessageIdempotency = `-- name: InsertMessageIdempotency :execrows
INSERT INTO message_idempotency(user_id, chat_id, idempotency_key, message_id)
VALUES ($1, $2, $3, $4)
ON CONFLICT (user_id, chat_id, idempotency_key) DO NOTHING
`
type InsertMessageIdempotencyParams struct {
UserID pgtype.UUID
ChatID pgtype.UUID
IdempotencyKey string
MessageID pgtype.UUID
}
func (q *Queries) InsertMessageIdempotency(ctx context.Context, arg InsertMessageIdempotencyParams) (int64, error) {
result, err := q.db.Exec(ctx, insertMessageIdempotency,
arg.UserID,
arg.ChatID,
arg.IdempotencyKey,
arg.MessageID,
)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
+91
View File
@@ -0,0 +1,91 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: messages.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const createMessage = `-- name: CreateMessage :one
INSERT INTO messages (chat_id, sender_id, text)
VALUES ($1, $2, $3)
RETURNING id, chat_id, sender_id, text, created_at
`
type CreateMessageParams struct {
ChatID pgtype.UUID
SenderID pgtype.UUID
Text string
}
func (q *Queries) CreateMessage(ctx context.Context, arg CreateMessageParams) (Message, error) {
row := q.db.QueryRow(ctx, createMessage, arg.ChatID, arg.SenderID, arg.Text)
var i Message
err := row.Scan(
&i.ID,
&i.ChatID,
&i.SenderID,
&i.Text,
&i.CreatedAt,
)
return i, err
}
const getMessagesByChatID = `-- name: GetMessagesByChatID :many
SELECT id, chat_id, sender_id, text, created_at
FROM messages
WHERE chat_id = $1
AND (
$2::timestamptz IS NULL
OR (created_at, id) >
($2::timestamptz,
$3::uuid
)
)
ORDER BY created_at ASC, id ASC
LIMIT $4
`
type GetMessagesByChatIDParams struct {
ChatID pgtype.UUID
AfterCreatedAt pgtype.Timestamptz
AfterID pgtype.UUID
LimitCount int32
}
func (q *Queries) GetMessagesByChatID(ctx context.Context, arg GetMessagesByChatIDParams) ([]Message, error) {
rows, err := q.db.Query(ctx, getMessagesByChatID,
arg.ChatID,
arg.AfterCreatedAt,
arg.AfterID,
arg.LimitCount,
)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Message
for rows.Next() {
var i Message
if err := rows.Scan(
&i.ID,
&i.ChatID,
&i.SenderID,
&i.Text,
&i.CreatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
@@ -0,0 +1,71 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: messages_attachments.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const createMessagesAttachments = `-- name: CreateMessagesAttachments :execrows
INSERT INTO messages_attachments (message_id, attachment_id, chat_id, position)
SELECT $1, $2, $3, $4
FROM attachments
WHERE id = $2 AND chat_id = $3
`
type CreateMessagesAttachmentsParams struct {
MessageID pgtype.UUID
AttachmentID pgtype.UUID
ChatID pgtype.UUID
Position int16
}
func (q *Queries) CreateMessagesAttachments(ctx context.Context, arg CreateMessagesAttachmentsParams) (int64, error) {
result, err := q.db.Exec(ctx, createMessagesAttachments,
arg.MessageID,
arg.AttachmentID,
arg.ChatID,
arg.Position,
)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const getMessagesAttachments = `-- name: GetMessagesAttachments :many
SELECT attachment_id, position
FROM messages_attachments
WHERE message_id = $1
ORDER BY position ASC
`
type GetMessagesAttachmentsRow struct {
AttachmentID pgtype.UUID
Position int16
}
func (q *Queries) GetMessagesAttachments(ctx context.Context, messageID pgtype.UUID) ([]GetMessagesAttachmentsRow, error) {
rows, err := q.db.Query(ctx, getMessagesAttachments, messageID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetMessagesAttachmentsRow
for rows.Next() {
var i GetMessagesAttachmentsRow
if err := rows.Scan(&i.AttachmentID, &i.Position); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
+65
View File
@@ -0,0 +1,65 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
package sqlc
import (
"github.com/jackc/pgx/v5/pgtype"
)
type Attachment struct {
ID pgtype.UUID
ChatID pgtype.UUID
OwnerID pgtype.UUID
Filename string
ObjectKey string
ContentType string
SizeBytes int64
Sha256 string
CreatedAt pgtype.Timestamptz
UpdatedAt pgtype.Timestamptz
}
type Chat struct {
ID pgtype.UUID
Title string
CreatedAt pgtype.Timestamptz
}
type ChatMember struct {
ChatID pgtype.UUID
UserID pgtype.UUID
}
type Message struct {
ID pgtype.UUID
ChatID pgtype.UUID
SenderID pgtype.UUID
Text string
CreatedAt pgtype.Timestamptz
}
type MessageIdempotency struct {
UserID pgtype.UUID
ChatID pgtype.UUID
IdempotencyKey string
MessageID pgtype.UUID
CreatedAt pgtype.Timestamptz
ExpiresAt pgtype.Timestamptz
}
type MessagesAttachment struct {
MessageID pgtype.UUID
AttachmentID pgtype.UUID
ChatID pgtype.UUID
Position int16
}
type User struct {
ID pgtype.UUID
Username string
DisplayName string
PasswordHash string
CreatedAt pgtype.Timestamptz
}
+77
View File
@@ -0,0 +1,77 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: users.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const createUser = `-- name: CreateUser :one
INSERT INTO users (username, display_name, password_hash)
VALUES ($1, $2, $3)
RETURNING id, username, display_name, password_hash, created_at
`
type CreateUserParams struct {
Username string
DisplayName string
PasswordHash string
}
func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, error) {
row := q.db.QueryRow(ctx, createUser, arg.Username, arg.DisplayName, arg.PasswordHash)
var i User
err := row.Scan(
&i.ID,
&i.Username,
&i.DisplayName,
&i.PasswordHash,
&i.CreatedAt,
)
return i, err
}
const getUserByID = `-- name: GetUserByID :one
SELECT id, username, display_name, password_hash, created_at
FROM users
WHERE id = $1
`
func (q *Queries) GetUserByID(ctx context.Context, id pgtype.UUID) (User, error) {
row := q.db.QueryRow(ctx, getUserByID, id)
var i User
err := row.Scan(
&i.ID,
&i.Username,
&i.DisplayName,
&i.PasswordHash,
&i.CreatedAt,
)
return i, err
}
const getUserByUsername = `-- name: GetUserByUsername :one
SELECT id, username, display_name, password_hash, created_at
FROM users
WHERE username = $1
`
func (q *Queries) GetUserByUsername(ctx context.Context, username string) (User, error) {
row := q.db.QueryRow(ctx, getUserByUsername, username)
var i User
err := row.Scan(
&i.ID,
&i.Username,
&i.DisplayName,
&i.PasswordHash,
&i.CreatedAt,
)
return i, err
}
+16
View File
@@ -0,0 +1,16 @@
package model
import "time"
type Attachment struct {
ID string
ChatID string
OwnerID string
Filename string
ObjectKey string
ContentType string
SizeBytes int64
SHA256 string
CreatedAt time.Time
UpdatedAt time.Time
}
+10
View File
@@ -0,0 +1,10 @@
package model
import "time"
type Chat struct {
ID string
Title string
MemberIDs []string
CreatedAt time.Time
}
+12
View File
@@ -0,0 +1,12 @@
package model
import "time"
type Message struct {
ID string
ChatID string
SenderID string
Text string
AttachmentIDs []string
CreatedAt time.Time
}
+11
View File
@@ -0,0 +1,11 @@
package model
import "time"
type Session struct {
TokenHash []byte
UserID string
CreatedAt time.Time
ExpiresAt time.Time
RevokedAt *time.Time
}
+11
View File
@@ -0,0 +1,11 @@
package model
import "time"
type User struct {
ID string
Username string
DisplayName string
PasswordHash string
CreatedAt time.Time
}
+36
View File
@@ -0,0 +1,36 @@
package repository
import (
"context"
"highload/internal/model"
"time"
)
type AttachmentRepository interface {
CreateAttachment(ctx context.Context, params CreateAttachmentParams) (*model.Attachment, error)
GetAttachmentForUser(ctx context.Context, userID, attachmentID string) (*model.Attachment, error)
ExistsByObjectKey(ctx context.Context, key string) (bool, error)
WithObjectKeyLock(ctx context.Context, key string, fn func(context.Context, AttachmentTx) error) (bool, error)
}
type AttachmentKey struct {
ID string
ObjectKey string
CreatedAt time.Time
}
type AttachmentReconcileRepository interface {
ListAttachmentKeys(ctx context.Context, afterKey string, pageSize int32) ([]AttachmentKey, error)
GetAttachmentByID(ctx context.Context, id string) (*model.Attachment, error)
WithObjectKeyLock(ctx context.Context, key string, fn func(context.Context, AttachmentTx) error) (bool, error)
}
type CreateAttachmentParams struct {
ChatID string
OwnerID string
Filename string
ObjectKey string
ContentType string
Sha256 string
SizeBytes int64
}
+12
View File
@@ -0,0 +1,12 @@
package repository
import (
"context"
"highload/internal/model"
)
type AttachmentTx interface {
CreateAttachment(ctx context.Context, params CreateAttachmentParams) (*model.Attachment, error)
ExistsByObjectKey(ctx context.Context, key string) (bool, error)
GetAttachmentByID(ctx context.Context, id string) (*model.Attachment, error)
}
+18
View File
@@ -0,0 +1,18 @@
package repository
import (
"context"
"highload/internal/model"
"time"
)
type ChatRepository interface {
CreateChat(ctx context.Context, title string, memberIDs []string) (*model.Chat, error)
GetChatByID(ctx context.Context, id string) (*model.Chat, error)
GetChatsByUserID(ctx context.Context, userID string, limit int, after *ChatPosition) ([]*model.Chat, error)
}
type ChatPosition struct {
CreatedAt time.Time
ID string
}
+16
View File
@@ -0,0 +1,16 @@
package repository
import (
"errors"
)
var (
ErrNotFound = errors.New("not found")
ErrAlreadyExists = errors.New("already exists")
ErrDatabase = errors.New("database error")
ErrInvalidInput = errors.New("invalid input")
ErrServiceUnavailable = errors.New("database unavailable")
ErrServerError = errors.New("server error")
ErrRedisNotAnswer = errors.New("redis not not answer")
ErrConflict = errors.New("data conflict")
)
+17
View File
@@ -0,0 +1,17 @@
package repository
import (
"context"
"highload/internal/model"
"time"
)
type MessageRepository interface {
CreateMessage(ctx context.Context, chatID, senderID, text, key string, attachmentIDs []string) (*model.Message, error)
GetMessagesByChatID(ctx context.Context, chatID string, limit int, after *MessagePosition) ([]*model.Message, error)
}
type MessagePosition struct {
CreatedAt time.Time
ID string
}
+188
View File
@@ -0,0 +1,188 @@
package postgres
import (
"context"
"highload/internal/database/sqlc"
"highload/internal/model"
"highload/internal/repository"
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)
const attachmentTransactionTimeout = 5 * time.Second
type AttachmentRepository struct {
queries *sqlc.Queries
pool *pgxpool.Pool
}
func NewAttachmentRepository(queries *sqlc.Queries, pool *pgxpool.Pool) *AttachmentRepository {
return &AttachmentRepository{
queries: queries,
pool: pool,
}
}
func (r *AttachmentRepository) CreateAttachment(ctx context.Context, params repository.CreateAttachmentParams) (*model.Attachment, error) {
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
chatUUID, err := getUUIDFromString(params.ChatID)
if err != nil {
return nil, err
}
ownerUUID, err := getUUIDFromString(params.OwnerID)
if err != nil {
return nil, err
}
sqlcParams := sqlc.CreateAttachmentParams{
ChatID: *chatUUID,
OwnerID: *ownerUUID,
Filename: params.Filename,
ObjectKey: params.ObjectKey,
ContentType: params.ContentType,
SizeBytes: params.SizeBytes,
Sha256: params.Sha256,
}
sqlcAttachment, err := r.queries.CreateAttachment(dbCtx, sqlcParams)
if err != nil {
return nil, convertDBError(err)
}
attachment := mapAttachmentSqlcToModel(sqlcAttachment)
return attachment, nil
}
func (r *AttachmentRepository) GetAttachmentForUser(ctx context.Context, userID, attachmentID string) (*model.Attachment, error) {
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
userUUID, err := getUUIDFromString(userID)
if err != nil {
return nil, err
}
attachmentUUID, err := getUUIDFromString(attachmentID)
if err != nil {
return nil, err
}
params := sqlc.GetAttachmentForUserParams{
AttachmentID: *attachmentUUID,
UserID: *userUUID,
}
sqlcAttachment, err := r.queries.GetAttachmentForUser(dbCtx, params)
if err != nil {
return nil, convertDBError(err)
}
attachment := mapAttachmentSqlcToModel(sqlcAttachment)
return attachment, nil
}
func (r *AttachmentRepository) ExistsByObjectKey(ctx context.Context, key string) (bool, error) {
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
exists, err := r.queries.AttachmentExistsByObjectKey(dbCtx, key)
if err != nil {
return false, convertDBError(err)
}
return exists, nil
}
func (r *AttachmentRepository) ListAttachmentKeys(ctx context.Context, afterKey string, pageSize int32) ([]repository.AttachmentKey, error) {
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
rows, err := r.queries.ListAttachmentKeys(dbCtx, sqlc.ListAttachmentKeysParams{AfterKey: afterKey, PageSize: pageSize})
if err != nil {
return nil, convertDBError(err)
}
keys := make([]repository.AttachmentKey, 0, len(rows))
for _, row := range rows {
keys = append(keys, repository.AttachmentKey{
ID: row.ID.String(), ObjectKey: row.ObjectKey, CreatedAt: row.CreatedAt.Time,
})
}
return keys, nil
}
func (r *AttachmentRepository) GetAttachmentByID(ctx context.Context, id string) (*model.Attachment, error) {
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
attachmentID, err := getUUIDFromString(id)
if err != nil {
return nil, err
}
row, err := r.queries.GetAttachmentByID(dbCtx, *attachmentID)
if err != nil {
return nil, convertDBError(err)
}
return mapAttachmentSqlcToModel(row), nil
}
func (r *AttachmentRepository) WithObjectKeyLock(ctx context.Context, key string, fn func(context.Context, repository.AttachmentTx) error) (bool, error) {
txCtx, cancel := context.WithTimeout(ctx, attachmentTransactionTimeout)
defer cancel()
tx, err := r.pool.BeginTx(txCtx, pgx.TxOptions{IsoLevel: pgx.ReadCommitted})
if err != nil {
return false, convertDBError(err)
}
defer func() {
rollbackCtx, rollbackCancel := context.WithTimeout(
context.WithoutCancel(ctx),
time.Second,
)
defer rollbackCancel()
_ = tx.Rollback(rollbackCtx)
}()
txQueries := r.queries.WithTx(tx)
locked, err := txQueries.TryLockAttachmentObjectKey(txCtx, key)
if err != nil {
return false, convertDBError(err)
}
if !locked {
return false, nil
}
txRepo := &AttachmentRepository{queries: txQueries}
if err = fn(txCtx, txRepo); err != nil {
return true, err
}
if err = tx.Commit(txCtx); err != nil {
return true, convertDBError(err)
}
return true, nil
}
func mapAttachmentSqlcToModel(sa sqlc.Attachment) *model.Attachment {
return &model.Attachment{
ID: sa.ID.String(),
ChatID: sa.ChatID.String(),
OwnerID: sa.OwnerID.String(),
Filename: sa.Filename,
ObjectKey: sa.ObjectKey,
ContentType: sa.ContentType,
SizeBytes: sa.SizeBytes,
SHA256: sa.Sha256,
CreatedAt: sa.CreatedAt.Time,
UpdatedAt: sa.UpdatedAt.Time,
}
}
+163
View File
@@ -0,0 +1,163 @@
package postgres
import (
"context"
"highload/internal/database/sqlc"
"highload/internal/model"
"highload/internal/repository"
"slices"
"time"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
)
type ChatRepository struct {
queries *sqlc.Queries
pool *pgxpool.Pool
}
func NewChatRepository(queries *sqlc.Queries, pool *pgxpool.Pool) *ChatRepository {
return &ChatRepository{
queries: queries,
pool: pool,
}
}
func (r *ChatRepository) CreateChat(ctx context.Context, title string, memberIDs []string) (*model.Chat, error) {
txCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
tx, err := r.pool.Begin(txCtx)
if err != nil {
return nil, convertDBError(err)
}
defer func() {
rollbackCtx, rollbackCancel := context.WithTimeout(
context.WithoutCancel(ctx),
time.Second,
)
defer rollbackCancel()
_ = tx.Rollback(rollbackCtx)
}()
txQueries := r.queries.WithTx(tx)
sqlcChat, err := txQueries.CreateChat(txCtx, title)
if err != nil {
return nil, convertDBError(err)
}
chat := &model.Chat{
ID: sqlcChat.ID.String(),
Title: sqlcChat.Title,
CreatedAt: sqlcChat.CreatedAt.Time,
}
for _, id := range memberIDs {
userID, err := getUUIDFromString(id)
if err != nil {
return nil, convertDBError(err)
}
err = txQueries.CreateChatMember(txCtx, sqlc.CreateChatMemberParams{ChatID: sqlcChat.ID, UserID: *userID})
if err != nil {
return nil, convertDBError(err)
}
}
chat.MemberIDs = slices.Clone(memberIDs)
err = tx.Commit(txCtx)
if err != nil {
return nil, convertDBError(err)
}
return chat, nil
}
func (r *ChatRepository) GetChatByID(ctx context.Context, id string) (*model.Chat, error) {
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
uuidID, err := getUUIDFromString(id)
if err != nil {
return nil, convertDBError(err)
}
sqlcChat, err := r.queries.GetChatByID(dbCtx, *uuidID)
if err != nil {
return nil, convertDBError(err)
}
chat := mapChatSqlcToModel(&sqlcChat)
return chat, nil
}
func (r *ChatRepository) GetChatsByUserID(ctx context.Context, userID string, limit int, after *repository.ChatPosition) ([]*model.Chat, error) {
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
uuidUserID, err := getUUIDFromString(userID)
if err != nil {
return nil, convertDBError(err)
}
afterCreatedAt := pgtype.Timestamptz{Valid: false}
afterID := pgtype.UUID{Valid: false}
if after != nil {
afterCreatedAt = pgtype.Timestamptz{Time: after.CreatedAt, Valid: true}
if uuidFromString, err := getUUIDFromString(after.ID); err == nil {
afterID = *uuidFromString
} else {
return nil, convertDBError(err)
}
}
chats, err := r.queries.GetChatsByUserID(dbCtx, sqlc.GetChatsByUserIDParams{UserID: *uuidUserID, AfterCreatedAt: afterCreatedAt, AfterID: afterID, LimitCount: int32(limit)})
if err != nil {
return nil, convertDBError(err)
}
resultChats := make([]*model.Chat, len(chats))
for index, sqlcChat := range chats {
memberIDs := make([]string, len(sqlcChat.MemberIds))
for i, memberID := range sqlcChat.MemberIds {
memberIDs[i] = memberID.String()
}
resultChats[index] = &model.Chat{
ID: sqlcChat.ID.String(),
MemberIDs: memberIDs,
CreatedAt: sqlcChat.CreatedAt.Time,
Title: sqlcChat.Title,
}
}
return resultChats, nil
}
func mapChatSqlcToModel(sqlcChat *sqlc.GetChatByIDRow) *model.Chat {
memberIDs := make([]string, len(sqlcChat.MemberIds))
for i, memberID := range sqlcChat.MemberIds {
memberIDs[i] = memberID.String()
}
return &model.Chat{
ID: sqlcChat.ID.String(),
MemberIDs: memberIDs,
CreatedAt: sqlcChat.CreatedAt.Time,
Title: sqlcChat.Title,
}
}
+46
View File
@@ -0,0 +1,46 @@
package postgres
import (
"context"
"errors"
"fmt"
"highload/internal/repository"
"io"
"net"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)
func convertDBError(err error) error {
var pgErr *pgconn.PgError
var netErr *net.OpError
var dnsErr *net.DNSError
switch {
case errors.Is(err, context.DeadlineExceeded):
return fmt.Errorf("%w: %v", repository.ErrServiceUnavailable, err)
case errors.Is(err, pgx.ErrNoRows):
return repository.ErrNotFound
case errors.As(err, &pgErr):
switch pgErr.Code {
case "23505":
return repository.ErrAlreadyExists
case "53300", "57P03", "08006", "08001", "57P01", "57P02":
return repository.ErrServiceUnavailable
}
case errors.Is(err, io.EOF) ||
errors.Is(err, io.ErrUnexpectedEOF) ||
errors.Is(err, pgconn.ErrConnClosed):
return repository.ErrServiceUnavailable
case errors.Is(err, repository.ErrInvalidInput):
return repository.ErrInvalidInput
case errors.As(err, &netErr):
return repository.ErrServiceUnavailable
case errors.As(err, &dnsErr):
return repository.ErrServiceUnavailable
}
return fmt.Errorf("%w: %v", repository.ErrDatabase, err)
}
+205
View File
@@ -0,0 +1,205 @@
package postgres
import (
"context"
"highload/internal/database/sqlc"
"highload/internal/model"
"highload/internal/repository"
"slices"
"time"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
)
type MessageRepository struct {
queries *sqlc.Queries
pool *pgxpool.Pool
}
func NewMessageRepository(queries *sqlc.Queries, pool *pgxpool.Pool) *MessageRepository {
return &MessageRepository{
queries: queries,
pool: pool,
}
}
func (r *MessageRepository) CreateMessage(ctx context.Context, chatID, senderID, text, key string, attachmentIDs []string) (*model.Message, error) {
uuidChatID, err := getUUIDFromString(chatID)
if err != nil {
return nil, convertDBError(err)
}
txCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
uuidSenderID, err := getUUIDFromString(senderID)
if err != nil {
return nil, convertDBError(err)
}
tx, err := r.pool.Begin(txCtx)
if err != nil {
return nil, convertDBError(err)
}
defer func() {
rollbackCtx, rollbackCancel := context.WithTimeout(
context.WithoutCancel(ctx),
time.Second,
)
defer rollbackCancel()
_ = tx.Rollback(rollbackCtx)
}()
txQueries := r.queries.WithTx(tx)
sqlcMessage, err := txQueries.CreateMessage(txCtx, sqlc.CreateMessageParams{ChatID: *uuidChatID, SenderID: *uuidSenderID, Text: text})
if err != nil {
return nil, convertDBError(err)
}
requestedIDs := make([]string, 0, len(attachmentIDs))
for index, attID := range attachmentIDs {
attUUID, err := getUUIDFromString(attID)
if err != nil {
return nil, convertDBError(err)
}
requestedIDs = append(requestedIDs, attUUID.String())
cmaParams := sqlc.CreateMessagesAttachmentsParams{MessageID: sqlcMessage.ID, AttachmentID: *attUUID, ChatID: sqlcMessage.ChatID, Position: int16(index)}
rowsAffected, err := txQueries.CreateMessagesAttachments(txCtx, cmaParams)
if err != nil {
return nil, convertDBError(err)
}
if rowsAffected == 0 {
return nil, repository.ErrNotFound
}
}
rowsAffected, err := txQueries.InsertMessageIdempotency(txCtx, sqlc.InsertMessageIdempotencyParams{UserID: sqlcMessage.SenderID, ChatID: sqlcMessage.ChatID, IdempotencyKey: key, MessageID: sqlcMessage.ID})
if err != nil {
return nil, convertDBError(err)
}
resultIDs := requestedIDs
switch rowsAffected {
case 1:
err := tx.Commit(txCtx)
if err != nil {
return nil, convertDBError(err)
}
case 0:
rollbackCtx, rollbackCancel := context.WithTimeout(context.WithoutCancel(ctx), time.Second)
err := tx.Rollback(rollbackCtx)
rollbackCancel()
if err != nil {
return nil, convertDBError(err)
}
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
sqlcMessage, err = r.queries.GetIdempotentMessage(dbCtx, sqlc.GetIdempotentMessageParams{UserID: sqlcMessage.SenderID, ChatID: sqlcMessage.ChatID, IdempotencyKey: key})
if err != nil {
return nil, convertDBError(err)
}
rows, err := r.queries.GetMessagesAttachments(dbCtx, sqlcMessage.ID)
if err != nil {
return nil, convertDBError(err)
}
savedIDs := make([]string, 0, len(rows))
for _, row := range rows {
savedIDs = append(savedIDs, row.AttachmentID.String())
}
if sqlcMessage.Text != text || !slices.Equal(savedIDs, requestedIDs) {
return nil, repository.ErrConflict
}
resultIDs = savedIDs
default:
return nil, repository.ErrServerError
}
message := &model.Message{
ID: sqlcMessage.ID.String(),
ChatID: sqlcMessage.ChatID.String(),
SenderID: sqlcMessage.SenderID.String(),
Text: sqlcMessage.Text,
AttachmentIDs: resultIDs,
CreatedAt: sqlcMessage.CreatedAt.Time,
}
return message, nil
}
func (r *MessageRepository) GetMessagesByChatID(ctx context.Context, chatID string, limit int, after *repository.MessagePosition) ([]*model.Message, error) {
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
uuidChatID, err := getUUIDFromString(chatID)
if err != nil {
return nil, convertDBError(err)
}
afterCreatedAt := pgtype.Timestamptz{Valid: false}
afterID := pgtype.UUID{Valid: false}
if after != nil {
afterCreatedAt = pgtype.Timestamptz{Time: after.CreatedAt, Valid: true}
if uuidFromString, err := getUUIDFromString(after.ID); err == nil {
afterID = *uuidFromString
} else {
return nil, convertDBError(err)
}
}
sqlcMessages, err := r.queries.GetMessagesByChatID(dbCtx, sqlc.GetMessagesByChatIDParams{ChatID: *uuidChatID, AfterID: afterID, AfterCreatedAt: afterCreatedAt, LimitCount: int32(limit)})
if err != nil {
return nil, convertDBError(err)
}
messageIDs := make([]pgtype.UUID, len(sqlcMessages))
for index, _ := range sqlcMessages {
messageIDs[index] = sqlcMessages[index].ID
}
messageRow, err := r.queries.GetAttachmentsForMessages(dbCtx, messageIDs)
if err != nil {
return nil, convertDBError(err)
}
mapMessagesAtt := make(map[string][]string)
for index, _ := range messageRow {
messageID := messageRow[index].MessageID.String()
attachmentID := messageRow[index].AttachmentID.String()
mapMessagesAtt[messageID] = append(mapMessagesAtt[messageID], attachmentID)
}
messages := make([]*model.Message, len(sqlcMessages))
for index, sqlcMessage := range sqlcMessages {
messages[index] = &model.Message{
ID: sqlcMessage.ID.String(),
ChatID: sqlcMessage.ChatID.String(),
SenderID: sqlcMessage.SenderID.String(),
Text: sqlcMessage.Text,
AttachmentIDs: mapMessagesAtt[sqlcMessage.ID.String()],
CreatedAt: sqlcMessage.CreatedAt.Time}
}
return messages, nil
}
+85
View File
@@ -0,0 +1,85 @@
package postgres
import (
"context"
"highload/internal/database/sqlc"
"highload/internal/model"
"time"
)
type UserRepository struct {
queries *sqlc.Queries
}
func NewUserRepository(queries *sqlc.Queries) *UserRepository {
return &UserRepository{queries: queries}
}
func (r *UserRepository) Create(ctx context.Context, username, displayName string, passwordHash string) (*model.User, error) {
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
sqlcUser, err := r.queries.CreateUser(dbCtx, sqlc.CreateUserParams{Username: username, DisplayName: displayName, PasswordHash: passwordHash})
if err != nil {
return nil, convertDBError(err)
}
user := &model.User{
ID: sqlcUser.ID.String(),
Username: sqlcUser.Username,
DisplayName: sqlcUser.DisplayName,
CreatedAt: sqlcUser.CreatedAt.Time,
PasswordHash: sqlcUser.PasswordHash,
}
return user, nil
}
func (r *UserRepository) GetUserByID(ctx context.Context, id string) (*model.User, error) {
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
uuidID, err := getUUIDFromString(id)
if err != nil {
return nil, convertDBError(err)
}
sqlcUser, err := r.queries.GetUserByID(dbCtx, *uuidID)
if err != nil {
return nil, convertDBError(err)
}
user := &model.User{
ID: sqlcUser.ID.String(),
Username: sqlcUser.Username,
DisplayName: sqlcUser.DisplayName,
CreatedAt: sqlcUser.CreatedAt.Time,
PasswordHash: sqlcUser.PasswordHash,
}
return user, nil
}
func (r *UserRepository) GetUserByUsername(ctx context.Context, username string) (*model.User, error) {
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
sqlcUser, err := r.queries.GetUserByUsername(dbCtx, username)
if err != nil {
return nil, convertDBError(err)
}
user := &model.User{
ID: sqlcUser.ID.String(),
Username: sqlcUser.Username,
DisplayName: sqlcUser.DisplayName,
CreatedAt: sqlcUser.CreatedAt.Time,
PasswordHash: sqlcUser.PasswordHash,
}
return user, nil
}
+17
View File
@@ -0,0 +1,17 @@
package postgres
import (
"highload/internal/repository"
"github.com/jackc/pgx/v5/pgtype"
)
func getUUIDFromString(id string) (*pgtype.UUID, error) {
var uuidID pgtype.UUID
if err := uuidID.Scan(id); err != nil {
return nil, repository.ErrInvalidInput
}
return &uuidID, nil
}
+38
View File
@@ -0,0 +1,38 @@
package redis
import (
"context"
"errors"
"fmt"
"highload/internal/repository"
"io"
"net"
"github.com/redis/go-redis/v9"
)
func convertDBError(err error) error {
category := repository.ErrDatabase
var netErr *net.OpError
if errors.Is(err, redis.Nil) {
category = repository.ErrNotFound
}
if errors.As(err, &netErr) {
category = repository.ErrServiceUnavailable
}
if errors.Is(err, context.DeadlineExceeded) {
category = repository.ErrServiceUnavailable
}
if errors.Is(err, redis.ErrPoolTimeout) {
category = repository.ErrServiceUnavailable
}
if errors.Is(err, io.EOF) ||
errors.Is(err, io.ErrUnexpectedEOF) {
category = repository.ErrServiceUnavailable
}
return fmt.Errorf("%w: %v", category, err)
}
+127
View File
@@ -0,0 +1,127 @@
package redis
import (
"context"
"encoding/hex"
"encoding/json/v2"
"fmt"
"highload/internal/model"
"highload/internal/repository"
"time"
"github.com/redis/go-redis/v9"
)
type SessionRepository struct {
client *redis.Client
}
func NewSessionRepository(client *redis.Client) *SessionRepository {
return &SessionRepository{client: client}
}
type sessionData struct {
UserID string `json:"user_id"`
CreatedAt time.Time `json:"created_at"`
ExpiresAt time.Time `json:"expires_at"`
}
func (r *SessionRepository) Create(ctx context.Context, tokenHash []byte, userID string, sessionTTL time.Duration) (*model.Session, error) {
key := "session:" + hex.EncodeToString(tokenHash)
now := time.Now().UTC()
data := sessionData{
UserID: userID,
CreatedAt: now,
ExpiresAt: now.Add(sessionTTL),
}
value, err := json.Marshal(data)
if err != nil {
return nil, fmt.Errorf("%w: encoding session %v", repository.ErrDatabase, err)
}
err = r.client.Set(ctx, key, value, sessionTTL).Err()
if err != nil {
return nil, convertDBError(err)
}
session := &model.Session{
TokenHash: tokenHash,
UserID: data.UserID,
CreatedAt: data.CreatedAt,
ExpiresAt: data.ExpiresAt,
RevokedAt: nil,
}
return session, nil
}
func (r *SessionRepository) GetSessionByTokenHash(ctx context.Context, tokenHash []byte) (*model.Session, error) {
key := "session:" + hex.EncodeToString(tokenHash)
value, err := r.client.Get(ctx, key).Result()
if err != nil {
return nil, convertDBError(err)
}
var data sessionData
err = json.Unmarshal([]byte(value), &data)
if err != nil {
return nil, fmt.Errorf("%w: decode session %v", repository.ErrDatabase, err)
}
session := &model.Session{
TokenHash: tokenHash,
UserID: data.UserID,
CreatedAt: data.CreatedAt,
ExpiresAt: data.ExpiresAt,
RevokedAt: nil,
}
return session, nil
}
func (r *SessionRepository) RevokeSession(
ctx context.Context,
tokenHash []byte,
) (int, error) {
key := "session:" + hex.EncodeToString(tokenHash)
conn := r.client.Conn()
defer conn.Close()
deleted, err := conn.Del(ctx, key).Result()
if err != nil {
return 0, convertDBError(err)
}
result, err := conn.Do(ctx, "WAITAOF", 1, 0, 2000).Slice()
if err != nil {
return 0, convertDBError(err)
}
if len(result) != 2 {
return 0, fmt.Errorf(
"%w: WAITAOF: expected 2 counters, got %d",
repository.ErrDatabase, len(result),
)
}
local, ok := result[0].(int64)
if !ok {
return 0, fmt.Errorf("%w: expected int64, got %T", repository.ErrDatabase, result[0])
}
if local != 1 {
return 0, fmt.Errorf("%w: WAITAOF expected local fsync = 1, got %d", repository.ErrServiceUnavailable, local)
}
return int(deleted), nil
}
+13
View File
@@ -0,0 +1,13 @@
package repository
import (
"context"
"highload/internal/model"
"time"
)
type SessionRepository interface {
Create(ctx context.Context, tokenHash []byte, userID string, sessionTTL time.Duration) (*model.Session, error)
GetSessionByTokenHash(ctx context.Context, tokenHash []byte) (*model.Session, error)
RevokeSession(ctx context.Context, tokenHash []byte) (int, error)
}
+12
View File
@@ -0,0 +1,12 @@
package repository
import (
"context"
"highload/internal/model"
)
type UserRepository interface {
Create(ctx context.Context, username, displayName string, passwordHash string) (*model.User, error)
GetUserByID(ctx context.Context, id string) (*model.User, error)
GetUserByUsername(ctx context.Context, username string) (*model.User, error)
}
+143
View File
@@ -0,0 +1,143 @@
package security
import (
"crypto/rand"
"crypto/subtle"
"encoding/base64"
"errors"
"fmt"
"strconv"
"strings"
"golang.org/x/crypto/argon2"
)
const (
memory = 64 * 1024
iterations = 3
parallelism = 2
saltLength = 16
hashLength = 32
)
var ErrInvalidHash = errors.New("invalid password hash")
func HashPassword(password string) (string, error) {
salt := make([]byte, saltLength)
if _, err := rand.Read(salt); err != nil {
return "", err
}
encodedSalt := base64.RawStdEncoding.EncodeToString(salt)
hash := argon2.IDKey(
[]byte(password),
salt,
iterations,
memory,
parallelism,
hashLength,
)
encodedHash := base64.RawStdEncoding.EncodeToString(hash)
hashPassword := fmt.Sprintf(
"$argon2id$v=%d$m=%d,t=%d,p=%d$%s$%s",
argon2.Version,
memory,
iterations,
parallelism,
encodedSalt,
encodedHash,
)
return hashPassword, nil
}
func VerifyPassword(password, encodedHash string) (bool, error) {
parts := strings.Split(encodedHash, "$")
if len(parts) != 6 || parts[0] != "" || parts[1] != "argon2id" {
return false, ErrInvalidHash
}
versionStr, ok := strings.CutPrefix(parts[2], "v=")
if !ok {
return false, ErrInvalidHash
}
version, err := strconv.ParseUint(versionStr, 10, 32)
if err != nil || version != uint64(argon2.Version) {
return false, ErrInvalidHash
}
params := strings.Split(parts[3], ",")
if len(params) != 3 {
return false, ErrInvalidHash
}
parseParam := func(value, prefix string, bits int) (uint64, error) {
number, ok := strings.CutPrefix(value, prefix)
if !ok {
return 0, ErrInvalidHash
}
return strconv.ParseUint(number, 10, bits)
}
m, err := parseParam(params[0], "m=", 32)
if err != nil {
return false, ErrInvalidHash
}
t, err := parseParam(params[1], "t=", 32)
if err != nil {
return false, ErrInvalidHash
}
p, err := parseParam(params[2], "p=", 8)
if err != nil {
return false, ErrInvalidHash
}
memory := uint32(m)
iterations := uint32(t)
parallelism := uint8(p)
if memory < 8*uint32(parallelism) ||
memory > 256*1024 ||
iterations < 1 || iterations > 10 ||
parallelism < 1 || parallelism > 8 {
return false, ErrInvalidHash
}
salt, err := base64.RawStdEncoding.DecodeString(parts[4])
if err != nil {
return false, ErrInvalidHash
}
if len(salt) != saltLength {
return false, ErrInvalidHash
}
expectedHash, err := base64.RawStdEncoding.DecodeString(parts[5])
if err != nil {
return false, ErrInvalidHash
}
if len(expectedHash) != hashLength {
return false, ErrInvalidHash
}
computedHash := argon2.IDKey(
[]byte(password),
salt,
iterations,
memory,
parallelism,
uint32(len(expectedHash)),
)
return subtle.ConstantTimeCompare(computedHash, expectedHash) == 1, nil
}
+204
View File
@@ -0,0 +1,204 @@
package service
import (
"context"
"crypto/sha256"
"encoding/hex"
"highload/internal/model"
"highload/internal/repository"
"highload/internal/storage"
"image"
_ "image/jpeg"
_ "image/png"
"io"
"strings"
"unicode/utf8"
"github.com/google/uuid"
)
const MaxAttachmentBytes = 10 * 1024 * 1024
const maxImagePixels = 40_000_000
const attachmentObjectPrefix = "attachments/"
type AttachmentService struct {
repo repository.AttachmentRepository
chatService *ChatService
objects storage.ObjectWriter
}
func NewAttachmentService(repo repository.AttachmentRepository, chatService *ChatService, objects storage.ObjectWriter) *AttachmentService {
return &AttachmentService{repo: repo, chatService: chatService, objects: objects}
}
func (s *AttachmentService) CreateAttachment(ctx context.Context, chatID, userID, filename, declaredContentType string, body io.ReadSeeker, size int64) (*model.Attachment, error) {
chat, err := s.chatService.GetChatByID(ctx, chatID, userID)
if err != nil {
return nil, err
}
mimeType, err := validateAttachment(filename, declaredContentType, body, size)
if err != nil {
return nil, err
}
key := attachmentObjectPrefix + uuid.NewString()
if err := ctx.Err(); err != nil {
return nil, err
}
sum := sha256.New()
n, err := io.Copy(sum, io.LimitReader(body, size+1))
if err != nil || n != size {
return nil, ErrServerError
}
if _, err := body.Seek(0, io.SeekStart); err != nil {
return nil, ErrServerError
}
sumString := hex.EncodeToString(sum.Sum(nil))
var attachment *model.Attachment
fn := func(ctx context.Context, tx repository.AttachmentTx) error {
err := s.objects.Put(ctx, key, mimeType, body, size)
if err != nil {
return convertStorageError(err)
}
params := repository.CreateAttachmentParams{
ChatID: chat.ID,
OwnerID: userID,
Filename: filename,
ObjectKey: key,
ContentType: mimeType,
Sha256: sumString,
SizeBytes: size,
}
attachment, err = tx.CreateAttachment(ctx, params)
if err != nil {
return err
}
return nil
}
acquired, err := s.repo.WithObjectKeyLock(ctx, key, fn)
if err != nil {
return nil, convertRepoError(err)
}
if !acquired {
return nil, ErrServiceUnavailable
}
return attachment, nil
}
func (s *AttachmentService) GetAttachment(ctx context.Context, userID, attachmentID string) (*model.Attachment, error) {
_, err := uuid.Parse(attachmentID)
if err != nil {
return nil, ErrInvalidInput
}
_, err = uuid.Parse(userID)
if err != nil {
return nil, ErrInvalidInput
}
attachment, err := s.repo.GetAttachmentForUser(ctx, userID, attachmentID)
if err != nil {
return nil, convertRepoError(err)
}
return attachment, nil
}
func (s *AttachmentService) GetAttachmentContent(ctx context.Context, userID, attachmentID string) (*model.Attachment, io.ReadCloser, error) {
attachment, err := s.GetAttachment(ctx, userID, attachmentID)
if err != nil {
return nil, nil, err
}
contentReader, err := s.objects.Get(ctx, attachment.ObjectKey)
if err != nil {
return nil, nil, convertStorageError(err)
}
return attachment, contentReader, nil
}
func validateAttachment(filename, declaredContentType string, body io.ReadSeeker, size int64) (string, error) {
if !utf8.ValidString(filename) || strings.TrimSpace(filename) == "" || utf8.RuneCountInString(filename) > 255 {
return "", ErrInvalidInput
}
if strings.ContainsAny(filename, "/\\") || containsControlChars(filename) {
return "", ErrInvalidInput
}
if size > MaxAttachmentBytes {
return "", ErrPayloadTooLarge
}
if size <= 0 {
return "", ErrUnsupportMediaType
}
if _, err := body.Seek(0, io.SeekStart); err != nil {
return "", ErrServerError
}
imageConfig, format, err := image.DecodeConfig(io.LimitReader(body, size))
if err != nil {
return "", ErrUnsupportMediaType
}
if imageConfig.Height <= 0 || imageConfig.Width <= 0 {
return "", ErrUnsupportMediaType
}
if imageConfig.Width > maxImagePixels/imageConfig.Height {
return "", ErrPayloadTooLarge
}
var mimeType string
switch format {
case "png":
mimeType = "image/png"
case "jpeg":
mimeType = "image/jpeg"
default:
return "", ErrUnsupportMediaType
}
if mimeType != declaredContentType {
return "", ErrUnsupportMediaType
}
if _, err := body.Seek(0, io.SeekStart); err != nil {
return "", ErrServerError
}
_, _, err = image.Decode(io.LimitReader(body, size))
if err != nil {
return "", ErrUnsupportMediaType
}
if _, err := body.Seek(0, io.SeekStart); err != nil {
return "", ErrServerError
}
return mimeType, nil
}
func containsControlChars(s string) bool {
for _, r := range s {
if (r >= 0 && r <= 31) || r == 127 {
return true
}
}
return false
}
+98
View File
@@ -0,0 +1,98 @@
package service
import (
"context"
"highload/internal/repository"
"highload/internal/storage"
"strings"
"time"
)
type AttachmentCleanupOptions struct {
GracePeriod time.Duration
DryRun bool
ListTimeout time.Duration
}
type AttachmentCleanupService struct {
repo repository.AttachmentRepository
objects storage.ObjectCleanup
options *AttachmentCleanupOptions
}
type OrphanReport struct {
Pages int `json:"s3_pages"`
Objects int `json:"objects_scanned"`
Checked int `json:"checked_in_db"`
Busy int `json:"busy"`
Candidates int `json:"orphan_candidates"`
Deleted int `json:"objects_deleted"`
}
func NewAttachmentCleanupService(repo repository.AttachmentRepository, objects storage.ObjectCleanup, options *AttachmentCleanupOptions) *AttachmentCleanupService {
return &AttachmentCleanupService{repo: repo, objects: objects, options: options}
}
func (s *AttachmentCleanupService) RunOnce(ctx context.Context) error {
_, err := s.ScanOrphans(ctx)
return err
}
func (s *AttachmentCleanupService) ScanOrphans(ctx context.Context) (OrphanReport, error) {
var report OrphanReport
cutoff := time.Now().Add(-s.options.GracePeriod)
var token *string
for {
if err := ctx.Err(); err != nil {
return report, err
}
listCtx, cancel := context.WithTimeout(ctx, s.options.ListTimeout)
objects, nextToken, err := s.objects.ListPage(listCtx, token, attachmentObjectPrefix)
cancel()
if err != nil {
return report, err
}
report.Pages++
for _, obj := range objects {
report.Objects++
if !strings.HasPrefix(obj.Key, attachmentObjectPrefix) ||
obj.LastModified.IsZero() || !obj.LastModified.Before(cutoff) {
continue
}
acquired, err := s.repo.WithObjectKeyLock(ctx, obj.Key, func(txCtx context.Context, txRepo repository.AttachmentTx) error {
exists, err := txRepo.ExistsByObjectKey(txCtx, obj.Key)
if err != nil {
return err
}
report.Checked++
if exists {
return nil
}
report.Candidates++
if s.options.DryRun {
return nil
}
if err := s.objects.Delete(txCtx, obj.Key); err != nil {
return err
}
report.Deleted++
return nil
})
if err != nil {
return report, err
}
if !acquired {
report.Busy++
}
}
if nextToken == nil {
return report, ctx.Err()
}
token = nextToken
}
}
+162
View File
@@ -0,0 +1,162 @@
package service
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"highload/internal/repository"
"highload/internal/storage"
"io"
"strings"
"time"
)
const attachmentReconcilePageSize = 200
const missingSampleLimit = 32
type attachmentReconcileStorage interface {
Check(context.Context) error
Head(context.Context, string) error
PutIfAbsent(context.Context, string, string, io.ReadSeeker, int64) error
}
type AttachmentReconciler struct {
repo repository.AttachmentReconcileRepository
objects attachmentReconcileStorage
options AttachmentCleanupOptions
}
type MissingReport struct {
Pages int `json:"db_pages"`
Rows int `json:"rows_scanned"`
Heads int `json:"s3_head_attempts"`
Missing int `json:"missing_objects"`
SampleIDs []string `json:"missing_attachment_ids"`
}
func NewAttachmentReconciler(repo repository.AttachmentReconcileRepository, objects attachmentReconcileStorage, options AttachmentCleanupOptions) *AttachmentReconciler {
return &AttachmentReconciler{repo: repo, objects: objects, options: options}
}
// ScanMissing checks old committed attachment rows using S3 metadata requests.
func (s *AttachmentReconciler) ScanMissing(ctx context.Context) (MissingReport, error) {
report := MissingReport{SampleIDs: []string{}}
cutoff := time.Now().Add(-s.options.GracePeriod)
if err := s.checkBucket(ctx); err != nil {
return report, err
}
var afterKey string
for {
rows, err := s.repo.ListAttachmentKeys(ctx, afterKey, attachmentReconcilePageSize)
if err != nil {
return report, err
}
report.Pages++
for _, row := range rows {
report.Rows++
if !strings.HasPrefix(row.ObjectKey, attachmentObjectPrefix) ||
row.CreatedAt.IsZero() || !row.CreatedAt.Before(cutoff) {
continue
}
report.Heads++
present, err := s.objectPresent(ctx, row.ObjectKey)
if err != nil {
return report, err
}
if !present {
report.Missing++
if len(report.SampleIDs) < missingSampleLimit {
report.SampleIDs = append(report.SampleIDs, row.ID)
}
}
}
if len(rows) < attachmentReconcilePageSize {
return report, ctx.Err()
}
afterKey = rows[len(rows)-1].ObjectKey
}
}
// Restore verifies the original bytes before writing a missing object under its existing key.
func (s *AttachmentReconciler) Restore(ctx context.Context, id string, original io.Reader) (string, error) {
attachment, err := s.repo.GetAttachmentByID(ctx, id)
if err != nil {
return "", err
}
if !strings.HasPrefix(attachment.ObjectKey, attachmentObjectPrefix) ||
attachment.CreatedAt.IsZero() || !attachment.CreatedAt.Before(time.Now().Add(-s.options.GracePeriod)) {
return "", errors.New("attachment is outside the restore scope or still within grace")
}
if attachment.SizeBytes <= 0 || attachment.SizeBytes > MaxAttachmentBytes {
return "", errors.New("invalid original size in attachment metadata")
}
if err := s.checkBucket(ctx); err != nil {
return "", err
}
// Read at most one original. Keep the verified bytes stable until Put completes.
data, err := io.ReadAll(io.LimitReader(original, attachment.SizeBytes+1))
if err != nil {
return "", fmt.Errorf("read backup original: %w", err)
}
sum := sha256.Sum256(data)
if int64(len(data)) != attachment.SizeBytes || hex.EncodeToString(sum[:]) != attachment.SHA256 {
return "", errors.New("backup original does not match the stored size and SHA-256")
}
var result string
acquired, err := s.repo.WithObjectKeyLock(ctx, attachment.ObjectKey, func(txCtx context.Context, txRepo repository.AttachmentTx) error {
current, err := txRepo.GetAttachmentByID(txCtx, id)
if err != nil {
return err
}
if current.ObjectKey != attachment.ObjectKey || current.SizeBytes != attachment.SizeBytes ||
current.SHA256 != attachment.SHA256 || current.ContentType != attachment.ContentType ||
!current.CreatedAt.Equal(attachment.CreatedAt) {
return errors.New("attachment metadata changed during restore")
}
present, err := s.objectPresent(txCtx, current.ObjectKey)
if err != nil {
return err
}
if present {
result = "already_present"
return nil
}
if s.options.DryRun {
result = "would_restore"
return nil
}
if err := s.objects.PutIfAbsent(txCtx, current.ObjectKey, current.ContentType, bytes.NewReader(data), current.SizeBytes); err != nil {
return err
}
result = "restored"
return nil
})
if err != nil {
return "", err
}
if !acquired {
return "", errors.New("attachment key is busy; retry the restore")
}
return result, nil
}
func (s *AttachmentReconciler) checkBucket(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, s.options.ListTimeout)
defer cancel()
return s.objects.Check(ctx)
}
func (s *AttachmentReconciler) objectPresent(ctx context.Context, key string) (bool, error) {
headCtx, cancel := context.WithTimeout(ctx, s.options.ListTimeout)
defer cancel()
err := s.objects.Head(headCtx, key)
if errors.Is(err, storage.ErrObjectNotFound) {
return false, nil
}
return err == nil, err
}
+142
View File
@@ -0,0 +1,142 @@
package service
import (
"context"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"errors"
"highload/internal/model"
"highload/internal/repository"
"highload/internal/security"
"time"
)
const tokenType = "Bearer"
type AuthService struct {
userRepo repository.UserRepository
sessionRepo repository.SessionRepository
sessionTTL time.Duration
}
func NewAuthService(userRepo repository.UserRepository, sessionRepo repository.SessionRepository, sessionTTL time.Duration) *AuthService {
return &AuthService{
userRepo: userRepo,
sessionRepo: sessionRepo,
sessionTTL: sessionTTL,
}
}
func (s *AuthService) Authenticate(ctx context.Context, username, password string) (*model.User, error) {
userOK := checkUsername(username)
passwordOK := checkPassword(password)
if !userOK || !passwordOK {
return nil, ErrUnauthorized
}
user, err := s.userRepo.GetUserByUsername(ctx, username)
if err != nil {
if errors.Is(err, repository.ErrNotFound) {
return nil, ErrUnauthorized
}
return nil, convertRepoError(err)
}
isVerify, err := security.VerifyPassword(password, user.PasswordHash)
if err != nil {
return nil, ErrServerError
}
if !isVerify {
return nil, ErrUnauthorized
}
return user, nil
}
type CreatedSession struct {
Token string
TokenType string
ExpiresAt time.Time
User *model.User
}
func (s *AuthService) CreateSession(ctx context.Context, username, password string) (*CreatedSession, error) {
user, err := s.Authenticate(ctx, username, password)
if err != nil {
return nil, err
}
if s.sessionTTL <= 0 {
return nil, ErrServerError
}
tokenBytes := make([]byte, 32)
_, err = rand.Read(tokenBytes)
if err != nil {
return nil, ErrServerError
}
token := base64.RawURLEncoding.EncodeToString(tokenBytes)
tokenHash := sha256.Sum256([]byte(token))
session, err := s.sessionRepo.Create(ctx, tokenHash[:], user.ID, s.sessionTTL)
if err != nil {
return nil, convertRepoError(err)
}
createdSession := &CreatedSession{
Token: token,
TokenType: tokenType,
ExpiresAt: session.ExpiresAt,
User: user,
}
return createdSession, nil
}
func (s *AuthService) ValidateSession(ctx context.Context, token string) (string, error) {
if token == "" {
return "", ErrUnauthorized
}
tokenHash := sha256.Sum256([]byte(token))
session, err := s.sessionRepo.GetSessionByTokenHash(ctx, tokenHash[:])
if err != nil {
if errors.Is(err, repository.ErrNotFound) {
return "", ErrUnauthorized
}
return "", convertRepoError(err)
}
if session.RevokedAt != nil {
return "", ErrUnauthorized
}
if !time.Now().Before(session.ExpiresAt) {
return "", ErrUnauthorized
}
return session.UserID, nil
}
func (s *AuthService) RevokeSession(ctx context.Context, token string) error {
tokenHash := sha256.Sum256([]byte(token))
_, err := s.sessionRepo.RevokeSession(ctx, tokenHash[:])
if err != nil {
return convertRepoError(err)
}
return nil
}
+194
View File
@@ -0,0 +1,194 @@
package service
import (
"context"
"errors"
"highload/internal/model"
"highload/internal/repository"
"slices"
"strings"
"time"
"unicode/utf8"
"github.com/google/uuid"
)
type ChatService struct {
repo repository.ChatRepository
userRepo repository.UserRepository
userService *UserService
}
func NewChatService(repo repository.ChatRepository, userRepo repository.UserRepository, userService *UserService) *ChatService {
return &ChatService{repo: repo, userRepo: userRepo, userService: userService}
}
func (s *ChatService) Create(ctx context.Context, title string, memberIDs []string, userID string) (*model.Chat, error) {
trimTitle := strings.TrimSpace(title)
lenTitle := utf8.RuneCountInString(title)
if lenTitle < 1 || lenTitle > 100 {
return nil, ErrInvalidInput
}
if trimTitle == "" {
return nil, ErrInvalidInput
}
if memberIDs == nil || len(memberIDs) == 0 || len(memberIDs) > 100 {
return nil, ErrInvalidInput
}
seen := make(map[string]struct{})
for _, memberID := range memberIDs {
parsedID, err := uuid.Parse(memberID)
if err != nil {
return nil, ErrInvalidInput
}
normalizedID := parsedID.String()
if _, exist := seen[normalizedID]; exist {
return nil, ErrInvalidInput
}
seen[normalizedID] = struct{}{}
_, err = s.userRepo.GetUserByID(ctx, memberID)
if err != nil {
// NOTE: MemberNotFound –> InvalidInput
if errors.Is(err, repository.ErrNotFound) {
return nil, ErrInvalidInput
}
return nil, convertRepoError(err)
}
}
parsedUserID, err := uuid.Parse(userID)
if err != nil {
return nil, ErrServerError
}
normalizedUserID := parsedUserID.String()
_, creatorExists := seen[normalizedUserID]
if !creatorExists {
return nil, ErrInvalidInput
}
chat, err := s.repo.CreateChat(ctx, title, memberIDs)
if err != nil {
return nil, convertRepoError(err)
}
return chat, nil
}
func (s *ChatService) GetChatsByUserID(ctx context.Context, userID string, limit *int, cursor *string) ([]*model.Chat, *string, error) {
actualLimit := 50
if limit != nil {
actualLimit = *limit
}
if actualLimit <= 0 || actualLimit > 100 {
return nil, nil, ErrInvalidInput
}
var chatPosition *repository.ChatPosition
if cursor != nil {
chCursor, err := decodeCursor[chatCursor](*cursor)
if err != nil {
return nil, nil, ErrInvalidInput
}
_, err = uuid.Parse(chCursor.ChatID)
if err != nil {
return nil, nil, ErrInvalidInput
}
if chCursor.CreatedAt.IsZero() {
return nil, nil, ErrInvalidInput
}
_, err = uuid.Parse(chCursor.UserID)
if err != nil {
return nil, nil, ErrInvalidInput
}
if chCursor.UserID != userID {
return nil, nil, ErrInvalidInput
}
chatPosition = &repository.ChatPosition{
CreatedAt: chCursor.CreatedAt,
ID: chCursor.ChatID,
}
}
chats, err := s.repo.GetChatsByUserID(ctx, userID, actualLimit+1, chatPosition)
if err != nil {
return nil, nil, convertRepoError(err)
}
var chCursor *chatCursor
if len(chats) > actualLimit {
chCursor = &chatCursor{
ChatID: chats[actualLimit-1].ID,
UserID: userID,
CreatedAt: chats[actualLimit-1].CreatedAt,
}
cursor, err = encodeCursor[chatCursor](*chCursor)
if err != nil {
return nil, nil, ErrServerError
}
return chats[:actualLimit], cursor, nil
}
return chats, nil, nil
}
func (s *ChatService) GetChatByID(ctx context.Context, chatID, userID string) (*model.Chat, error) {
_, err := uuid.Parse(chatID)
if err != nil {
return nil, ErrInvalidInput
}
_, err = uuid.Parse(userID)
if err != nil {
return nil, ErrInvalidInput
}
chat, err := s.repo.GetChatByID(ctx, chatID)
if err != nil {
return nil, convertRepoError(err)
}
index := slices.Index(chat.MemberIDs, userID)
if index == -1 {
return nil, ErrNotFound
}
return chat, nil
}
type chatCursor struct {
CreatedAt time.Time `json:"created_at"`
ChatID string `json:"chat_id"`
UserID string `json:"user_id"`
}

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