From 4bba40d098d2cc38f9ce3466ee6c086e926fc2b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Fri, 6 Nov 2026 20:21:23 +0300 Subject: [PATCH 01/46] chore: lab import lab4 backend and infrastructure as baseline --- .gitattributes | 16 + Dockerfile | 29 ++ backend/cmd/server/main.go | 205 +++++++++++ backend/go.mod | 65 ++++ backend/go.sum | 165 +++++++++ backend/internal/config/config.go | 230 +++++++++++++ backend/internal/domain/entity.go | 61 ++++ backend/internal/domain/errors.go | 197 +++++++++++ backend/internal/domain/idempotency.go | 33 ++ backend/internal/domain/repository.go | 47 +++ backend/internal/domain/storage.go | 21 ++ backend/internal/httpapi/dto/attachment.go | 50 +++ backend/internal/httpapi/dto/chat.go | 39 +++ backend/internal/httpapi/dto/error.go | 86 +++++ backend/internal/httpapi/dto/health.go | 5 + backend/internal/httpapi/dto/message.go | 47 +++ backend/internal/httpapi/dto/session.go | 21 ++ backend/internal/httpapi/dto/user.go | 29 ++ .../internal/httpapi/handler/attachment.go | 98 ++++++ backend/internal/httpapi/handler/auth.go | 77 +++++ backend/internal/httpapi/handler/bind.go | 52 +++ backend/internal/httpapi/handler/chat.go | 88 +++++ backend/internal/httpapi/handler/health.go | 54 +++ backend/internal/httpapi/handler/message.go | 88 +++++ backend/internal/httpapi/handler/response.go | 44 +++ backend/internal/httpapi/handler/user.go | 43 +++ .../httpapi/middleware/auth_legacy.go | 38 +++ backend/internal/httpapi/middleware/bearer.go | 70 ++++ .../internal/httpapi/middleware/context.go | 30 ++ .../internal/httpapi/middleware/instance.go | 31 ++ .../internal/httpapi/middleware/requestid.go | 32 ++ backend/internal/httpapi/router.go | 57 ++++ backend/internal/service/attachment.go | 209 ++++++++++++ backend/internal/service/chat.go | 95 ++++++ backend/internal/service/message.go | 122 +++++++ backend/internal/service/password.go | 112 ++++++ backend/internal/service/reconciler.go | 176 ++++++++++ backend/internal/service/session.go | 127 +++++++ backend/internal/service/user.go | 78 +++++ .../internal/storage/_legacy/memory/chat.go | 102 ++++++ .../internal/storage/_legacy/memory/cursor.go | 47 +++ .../storage/_legacy/memory/message.go | 93 +++++ .../storage/_legacy/memory/storage.go | 34 ++ .../internal/storage/_legacy/memory/user.go | 59 ++++ .../internal/storage/postgres/attachments.go | 200 +++++++++++ backend/internal/storage/postgres/chat.go | 195 +++++++++++ backend/internal/storage/postgres/cursor.go | 46 +++ backend/internal/storage/postgres/db.go | 75 ++++ backend/internal/storage/postgres/errors.go | 71 ++++ backend/internal/storage/postgres/message.go | 321 ++++++++++++++++++ backend/internal/storage/postgres/session.go | 59 ++++ backend/internal/storage/postgres/storage.go | 19 ++ backend/internal/storage/postgres/user.go | 76 +++++ backend/internal/storage/redis/client.go | 33 ++ backend/internal/storage/redis/errors.go | 31 ++ backend/internal/storage/redis/session.go | 80 +++++ backend/internal/storage/s3/client.go | 98 ++++++ backend/internal/storage/s3/errors.go | 42 +++ backend/migrations/0001_init.sql | 67 ++++ backend/migrations/0002_idempotency.sql | 35 ++ backend/migrations/0003_attachments.sql | 73 ++++ .../migrations/0004_messages_text_relaxed.sql | 13 + backend/migrations/embed.go | 6 + compose.yaml | 241 +++++++++++++ infra/minio-init.sh | 48 +++ infra/nginx.conf | 48 +++ scripts/restart.sh | 14 + scripts/stop-one.sh | 22 ++ 68 files changed, 5315 insertions(+) create mode 100644 .gitattributes create mode 100644 Dockerfile create mode 100644 backend/cmd/server/main.go create mode 100644 backend/go.mod create mode 100644 backend/go.sum create mode 100644 backend/internal/config/config.go create mode 100644 backend/internal/domain/entity.go create mode 100644 backend/internal/domain/errors.go create mode 100644 backend/internal/domain/idempotency.go create mode 100644 backend/internal/domain/repository.go create mode 100644 backend/internal/domain/storage.go create mode 100644 backend/internal/httpapi/dto/attachment.go create mode 100644 backend/internal/httpapi/dto/chat.go create mode 100644 backend/internal/httpapi/dto/error.go create mode 100644 backend/internal/httpapi/dto/health.go create mode 100644 backend/internal/httpapi/dto/message.go create mode 100644 backend/internal/httpapi/dto/session.go create mode 100644 backend/internal/httpapi/dto/user.go create mode 100644 backend/internal/httpapi/handler/attachment.go create mode 100644 backend/internal/httpapi/handler/auth.go create mode 100644 backend/internal/httpapi/handler/bind.go create mode 100644 backend/internal/httpapi/handler/chat.go create mode 100644 backend/internal/httpapi/handler/health.go create mode 100644 backend/internal/httpapi/handler/message.go create mode 100644 backend/internal/httpapi/handler/response.go create mode 100644 backend/internal/httpapi/handler/user.go create mode 100644 backend/internal/httpapi/middleware/auth_legacy.go create mode 100644 backend/internal/httpapi/middleware/bearer.go create mode 100644 backend/internal/httpapi/middleware/context.go create mode 100644 backend/internal/httpapi/middleware/instance.go create mode 100644 backend/internal/httpapi/middleware/requestid.go create mode 100644 backend/internal/httpapi/router.go create mode 100644 backend/internal/service/attachment.go create mode 100644 backend/internal/service/chat.go create mode 100644 backend/internal/service/message.go create mode 100644 backend/internal/service/password.go create mode 100644 backend/internal/service/reconciler.go create mode 100644 backend/internal/service/session.go create mode 100644 backend/internal/service/user.go create mode 100644 backend/internal/storage/_legacy/memory/chat.go create mode 100644 backend/internal/storage/_legacy/memory/cursor.go create mode 100644 backend/internal/storage/_legacy/memory/message.go create mode 100644 backend/internal/storage/_legacy/memory/storage.go create mode 100644 backend/internal/storage/_legacy/memory/user.go create mode 100644 backend/internal/storage/postgres/attachments.go create mode 100644 backend/internal/storage/postgres/chat.go create mode 100644 backend/internal/storage/postgres/cursor.go create mode 100644 backend/internal/storage/postgres/db.go create mode 100644 backend/internal/storage/postgres/errors.go create mode 100644 backend/internal/storage/postgres/message.go create mode 100644 backend/internal/storage/postgres/session.go create mode 100644 backend/internal/storage/postgres/storage.go create mode 100644 backend/internal/storage/postgres/user.go create mode 100644 backend/internal/storage/redis/client.go create mode 100644 backend/internal/storage/redis/errors.go create mode 100644 backend/internal/storage/redis/session.go create mode 100644 backend/internal/storage/s3/client.go create mode 100644 backend/internal/storage/s3/errors.go create mode 100644 backend/migrations/0001_init.sql create mode 100644 backend/migrations/0002_idempotency.sql create mode 100644 backend/migrations/0003_attachments.sql create mode 100644 backend/migrations/0004_messages_text_relaxed.sql create mode 100644 backend/migrations/embed.go create mode 100644 compose.yaml create mode 100644 infra/minio-init.sh create mode 100644 infra/nginx.conf create mode 100644 scripts/restart.sh create mode 100644 scripts/stop-one.sh diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..4adaa807 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,16 @@ +# Контейнеры Linux; shell-скрипты, Dockerfile, конфиги nginx/yaml +# должны иметь LF независимо от core.autocrlf на стороне разработчика. +*.sh text eol=lf +*.bash text eol=lf +Dockerfile text eol=lf +Dockerfile.* text eol=lf +*.yml text eol=lf +*.yaml text eol=lf +*.go text eol=lf +*.sql text eol=lf +*.md text eol=lf +*.conf text eol=lf + +# Windows-специфичные - CRLF +*.bat text eol=crlf +*.cmd text eol=crlf \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..16229253 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,29 @@ +FROM golang:1.26.1-alpine AS builder + +WORKDIR /src + +COPY backend/go.mod backend/go.sum ./ +RUN go mod download + +COPY backend/ . + +RUN CGO_ENABLED=0 GOOS=linux go build \ + -trimpath \ + -ldflags="-s -w" \ + -o /out/server \ + ./cmd/server + +FROM alpine:3.20 + +RUN adduser -D -u 1000 app + +COPY --from=builder /out/server /server + +EXPOSE 8080 + +USER app + +HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \ + CMD wget -q -O - http://127.0.0.1:8080/health/live >/dev/null || exit 1 + +ENTRYPOINT ["/server"] \ No newline at end of file diff --git a/backend/cmd/server/main.go b/backend/cmd/server/main.go new file mode 100644 index 00000000..c0c74af0 --- /dev/null +++ b/backend/cmd/server/main.go @@ -0,0 +1,205 @@ +package main + +import ( + "context" + "errors" + "fmt" + "log" + "net/http" + "os" + "os/signal" + "strings" + "sync/atomic" + "syscall" + "time" + + "high-loud-apps-labs/internal/config" + "high-loud-apps-labs/internal/domain" + "high-loud-apps-labs/internal/httpapi" + "high-loud-apps-labs/internal/httpapi/handler" + "high-loud-apps-labs/internal/httpapi/middleware" + "high-loud-apps-labs/internal/service" + "high-loud-apps-labs/internal/storage/postgres" + redisstore "high-loud-apps-labs/internal/storage/redis" + s3store "high-loud-apps-labs/internal/storage/s3" +) + +func main() { + if len(os.Args) > 1 && os.Args[1] == "migrate" { + if err := runMigrate(); err != nil { + log.Fatalf("миграции: %v", err) + } + return + } + + cfg, err := config.Load() + if err != nil { + log.Fatalf("конфигурация: %v", err) + } + + addr := cfg.Host + ":" + cfg.Port + + ctx := context.Background() + pool, err := postgres.Connect(ctx, postgres.Config{ + URL: cfg.DatabaseURL, + MaxConns: 10, + MinConns: 1, + MaxConnLifetime: time.Hour, + MaxConnIdleTime: 30 * time.Minute, + ConnectTimeout: 5 * time.Second, + }) + if err != nil { + log.Fatalf("БД: %v", err) + } + defer pool.Close() + + store := postgres.New(pool) + + sessionRepo, cachePing, closeSession := newSessionRepo(cfg, store) + defer closeSession() + + hasher := service.NewArgon2idHasher() + + userSvc := service.NewUserService(store.Users(), hasher) + sessionSvc := service.NewSessionService(store.Users(), sessionRepo, hasher, cfg.SessionTTL) + chatSvc := service.NewChatService(store.Chats(), store.Users()) + messageSvc := service.NewMessageService(store.Messages(), store.Chats(), cfg.IdempotencyTTL) + + if !cfg.S3Configured() { + log.Fatal("S3 не настроен: задайте S3_ENDPOINT, S3_BUCKET, S3_ACCESS_KEY_ID_FILE/S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY_FILE/S3_SECRET_ACCESS_KEY") + } + + objectStorage, err := s3store.New(ctx, s3store.Config{ + Endpoint: cfg.S3Endpoint, + Bucket: cfg.S3Bucket, + AccessKey: cfg.S3AccessKey, + SecretKey: cfg.S3SecretKey, + UseSSL: cfg.S3UseSSL, + Region: cfg.S3Region, + }) + if err != nil { + var unavailable *domain.UnavailableError + if errors.As(err, &unavailable) { + log.Fatalf("s3: %v (reason: %s)", err, unavailable.Reason) + } + log.Fatalf("s3: %v", err) + } + + attachmentSvc := service.NewAttachmentService( + store.Attachments(), + store.Chats(), + objectStorage, + cfg.MaxUploadBytes, + cfg.MaxImagePixels, + ) + + healthChecks := []handler.HealthCheck{ + func(ctx context.Context) error { return pool.Ping(ctx) }, + func(ctx context.Context) error { return objectStorage.Ping(ctx) }, + } + if cachePing != nil { + healthChecks = append(healthChecks, cachePing) + } + + var ready atomic.Bool + ready.Store(true) + + router := httpapi.NewRouter( + httpapi.Handlers{ + Health: handler.NewHealthHandler(healthChecks...).WithReadyFlag(&ready), + User: handler.NewUserHandler(userSvc), + Chat: handler.NewChatHandler(chatSvc), + Message: handler.NewMessageHandler(messageSvc), + Auth: handler.NewAuthHandler(sessionSvc), + Attachment: handler.NewAttachmentHandler(attachmentSvc), + }, + middleware.BearerAuth(sessionSvc), + ) + + srv := &http.Server{Addr: addr, Handler: router} + + reconcilerCtx, reconcilerCancel := context.WithCancel(context.Background()) + defer reconcilerCancel() + if cfg.ReconcilerEnabled { + reconciler := service.NewReconciler( + store.Attachments(), + objectStorage, + pool, + cfg.ReconcilerGracePeriod, + ) + go reconciler.Run(reconcilerCtx, cfg.ReconcilerInterval) + } + + go func() { + log.Printf("Запускаем на %s (SESSION_STORE=%s, RECONCILER_ENABLED=%v)", addr, cfg.SessionStore, cfg.ReconcilerEnabled) + if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { + log.Fatalf("Ошибка запуска: %v", err) + } + }() + + quit := make(chan os.Signal, 1) + signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) + <-quit + + log.Printf("Получен сигнал завершения; readiness выключен, drain %s", cfg.ShutdownDrain) + ready.Store(false) + reconcilerCancel() + + if cfg.ShutdownDrain > 0 { + time.Sleep(cfg.ShutdownDrain) + } + + log.Println("Drain завершён, останавливаем HTTP-сервер...") + shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + if err := srv.Shutdown(shutdownCtx); err != nil { + log.Fatalf("Ошибка при остановке: %v", err) + } + log.Println("Сервер остановлен") +} + +func runMigrate() error { + cfg, err := config.Load() + if err != nil { + return fmt.Errorf("конфигурация: %w", err) + } + + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) + defer cancel() + + pool, err := postgres.Connect(ctx, postgres.Config{ + URL: cfg.DatabaseURL, + ConnectTimeout: 5 * time.Second, + }) + if err != nil { + return fmt.Errorf("БД: %w", err) + } + defer pool.Close() + + if err := postgres.RunMigrations(ctx, pool); err != nil { + return fmt.Errorf("применение миграций: %w", err) + } + return nil +} + +func newSessionRepo(cfg *config.Config, store *postgres.Storage) (domain.SessionRepository, handler.HealthCheck, func()) { + switch strings.ToLower(cfg.SessionStore) { + case "", "postgres": + return store.Sessions(), nil, func() {} + case "redis": + if cfg.CacheURL == "" { + log.Fatalf("SESSION_STORE=%s требует CACHE_URL_FILE или CACHE_URL", cfg.SessionStore) + } + client, err := redisstore.NewClient(cfg.CacheURL) + if err != nil { + log.Fatalf("redis: %v", err) + } + ping := func(ctx context.Context) error { + return client.Ping(ctx).Err() + } + return redisstore.NewSessionRepo(client), ping, func() { _ = client.Close() } + default: + log.Fatalf("SESSION_STORE=%q не поддерживается", cfg.SessionStore) + return nil, nil, nil + } +} diff --git a/backend/go.mod b/backend/go.mod new file mode 100644 index 00000000..f4390989 --- /dev/null +++ b/backend/go.mod @@ -0,0 +1,65 @@ +module high-loud-apps-labs + +go 1.26.1 + +require ( + github.com/gin-gonic/gin v1.12.0 + github.com/google/uuid v1.6.0 + github.com/jackc/pgerrcode v0.0.0-20250907135507-afb5586c32a6 + github.com/jackc/pgx/v5 v5.7.1 + github.com/minio/minio-go/v7 v7.4.1 + github.com/pressly/goose/v3 v3.22.1 + github.com/redis/go-redis/v9 v9.22.0 + golang.org/x/crypto v0.57.0 +) + +require ( + github.com/bytedance/gopkg v0.1.3 // indirect + github.com/bytedance/sonic v1.15.0 // indirect + github.com/bytedance/sonic/loader v0.5.0 // indirect + github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/cloudwego/base64x v0.1.6 // indirect + github.com/dustin/go-humanize v1.0.1 // indirect + github.com/gabriel-vasile/mimetype v1.4.12 // indirect + github.com/gin-contrib/sse v1.1.0 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.30.1 // indirect + github.com/goccy/go-json v0.10.5 // indirect + github.com/goccy/go-yaml v1.19.2 // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect + github.com/jackc/puddle/v2 v2.2.2 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/compress v1.19.2 // indirect + github.com/klauspost/cpuid/v2 v2.4.0 // indirect + github.com/klauspost/crc32 v1.3.0 // indirect + github.com/leodido/go-urn v1.4.0 // indirect + github.com/mattn/go-isatty v0.0.22 // indirect + github.com/mfridman/interpolate v0.0.2 // indirect + github.com/minio/crc64nvme v1.1.1 // indirect + github.com/minio/md5-simd v1.1.2 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.4.3 // indirect + github.com/philhofer/fwd v1.2.0 // indirect + github.com/quic-go/qpack v0.6.0 // indirect + github.com/quic-go/quic-go v0.59.0 // indirect + github.com/rs/xid v1.6.0 // indirect + github.com/sethvargo/go-retry v0.3.0 // indirect + github.com/tinylib/msgp v1.6.4 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.3.1 // indirect + github.com/zeebo/xxh3 v1.1.0 // indirect + go.mongodb.org/mongo-driver/v2 v2.5.0 // indirect + go.uber.org/atomic v1.11.0 // indirect + go.uber.org/multierr v1.11.0 // indirect + go.yaml.in/yaml/v3 v3.0.5 // indirect + golang.org/x/arch v0.22.0 // indirect + golang.org/x/net v0.60.0 // indirect + golang.org/x/sync v0.23.0 // indirect + golang.org/x/sys v0.48.0 // indirect + golang.org/x/text v0.42.0 // indirect + google.golang.org/protobuf v1.36.12 // indirect + gopkg.in/ini.v1 v1.67.3 // indirect +) diff --git a/backend/go.sum b/backend/go.sum new file mode 100644 index 00000000..898b3e14 --- /dev/null +++ b/backend/go.sum @@ -0,0 +1,165 @@ +github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= +github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c= +github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA= +github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0= +github.com/bytedance/gopkg v0.1.3 h1:TPBSwH8RsouGCBcMBktLt1AymVo2TVsBVCY4b6TnZ/M= +github.com/bytedance/gopkg v0.1.3/go.mod h1:576VvJ+eJgyCzdjS+c4+77QF3p7ubbtiKARP3TxducM= +github.com/bytedance/sonic v1.15.0 h1:/PXeWFaR5ElNcVE84U0dOHjiMHQOwNIx3K4ymzh/uSE= +github.com/bytedance/sonic v1.15.0/go.mod h1:tFkWrPz0/CUCLEF4ri4UkHekCIcdnkqXw9VduqpJh0k= +github.com/bytedance/sonic/loader v0.5.0 h1:gXH3KVnatgY7loH5/TkeVyXPfESoqSBSBEiDd5VjlgE= +github.com/bytedance/sonic/loader v0.5.0/go.mod h1:AR4NYCk5DdzZizZ5djGqQ92eEhCCcdf5x77udYiSJRo= +github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= +github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M= +github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/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/gabriel-vasile/mimetype v1.4.12 h1:e9hWvmLYvtp846tLHam2o++qitpguFiYCKbn0w9jyqw= +github.com/gabriel-vasile/mimetype v1.4.12/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s= +github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w= +github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM= +github.com/gin-gonic/gin v1.12.0 h1:b3YAbrZtnf8N//yjKeU2+MQsh2mY5htkZidOM7O0wG8= +github.com/gin-gonic/gin v1.12.0/go.mod h1:VxccKfsSllpKshkBWgVgRniFFAzFb9csfngsqANjnLc= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.30.1 h1:f3zDSN/zOma+w6+1Wswgd9fLkdwy06ntQJp0BBvFG0w= +github.com/go-playground/validator/v10 v10.30.1/go.mod h1:oSuBIQzuJxL//3MelwSLD5hc2Tu889bF0Idm9Dg26cM= +github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= +github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= +github.com/goccy/go-yaml v1.19.2 h1:PmFC1S6h8ljIz6gMRBopkjP1TVT7xuwrButHID66PoM= +github.com/goccy/go-yaml v1.19.2/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= +github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= +github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= +github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= +github.com/jackc/pgerrcode v0.0.0-20250907135507-afb5586c32a6 h1:D/V0gu4zQ3cL2WKeVNVM4r2gLxGGf6McLwgXzRTo2RQ= +github.com/jackc/pgerrcode v0.0.0-20250907135507-afb5586c32a6/go.mod h1:a/s9Lp5W7n/DD0VrVoyJ00FbP2ytTPDVOivvn2bMlds= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.7.1 h1:x7SYsPBYDkHDksogeSmZZ5xzThcTgRz++I5E+ePFUcs= +github.com/jackc/pgx/v5 v5.7.1/go.mod h1:e7O26IywZZ+naJtWWos6i6fvWK+29etgITqrqHLfoZA= +github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= +github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/klauspost/compress v1.19.2 h1:hMRETovs/pu/dVWN7zIT1PGG8t509MwT6bO7XSi26R8= +github.com/klauspost/compress v1.19.2/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ= +github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.4.0 h1:S6Hrbc7+ywsr0r+RLapfGBHfyefhCTwEh3A0tV913Dw= +github.com/klauspost/cpuid/v2 v2.4.0/go.mod h1:19jmZ9mjzoF//ddRSUsv0zfBTJWh3QJh9FNxZTMrGxU= +github.com/klauspost/crc32 v1.3.0 h1:sSmTt3gUt81RP655XGZPElI0PelVTZ6YwCRnPSupoFM= +github.com/klauspost/crc32 v1.3.0/go.mod h1:D7kQaZhnkX/Y0tstFGf8VUzv2UofNGqCjnC3zdHB0Hw= +github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= +github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= +github.com/mattn/go-isatty v0.0.22 h1:j8l17JJ9i6VGPUFUYoTUKPSgKe/83EYU2zBC7YNKMw4= +github.com/mattn/go-isatty v0.0.22/go.mod h1:ZXfXG4SQHsB/w3ZeOYbR0PrPwLy+n6xiMrJlRFqopa4= +github.com/mfridman/interpolate v0.0.2 h1:pnuTK7MQIxxFz1Gr+rjSIx9u7qVjf5VOoM/u6BbAxPY= +github.com/mfridman/interpolate v0.0.2/go.mod h1:p+7uk6oE07mpE/Ik1b8EckO0O4ZXiGAfshKBWLUM9Xg= +github.com/minio/crc64nvme v1.1.1 h1:8dwx/Pz49suywbO+auHCBpCtlW1OfpcLN7wYgVR6wAI= +github.com/minio/crc64nvme v1.1.1/go.mod h1:eVfm2fAzLlxMdUGc0EEBGSMmPwmXD5XiNRpnu9J3bvg= +github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34= +github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM= +github.com/minio/minio-go/v7 v7.4.1 h1:PtU/zIOfQRWZ+QydXAZysc+xZYHdG+BBR4Z6TUvy73w= +github.com/minio/minio-go/v7 v7.4.1/go.mod h1:nQnQi7zTfXR4lREtYXZQWqo5aw0Rqv+ZqCGrTORygRk= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4= +github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls= +github.com/pelletier/go-toml/v2 v2.4.3 h1:GTRvJQutkOSftxIFD5xw9aepkYNuPWmVJpffdDPYVpY= +github.com/pelletier/go-toml/v2 v2.4.3/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= +github.com/philhofer/fwd v1.2.0 h1:e6DnBTl7vGY+Gz322/ASL4Gyp1FspeMvx1RNDoToZuM= +github.com/philhofer/fwd v1.2.0/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pressly/goose/v3 v3.22.1 h1:2zICEfr1O3yTP9BRZMGPj7qFxQ+ik6yeo+z1LMuioLc= +github.com/pressly/goose/v3 v3.22.1/go.mod h1:xtMpbstWyCpyH+0cxLTMCENWBG+0CSxvTsXhW95d5eo= +github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8= +github.com/quic-go/qpack v0.6.0/go.mod h1:lUpLKChi8njB4ty2bFLX2x4gzDqXwUpaO1DP9qMDZII= +github.com/quic-go/quic-go v0.59.0 h1:OLJkp1Mlm/aS7dpKgTc6cnpynnD2Xg7C1pwL6vy/SAw= +github.com/quic-go/quic-go v0.59.0/go.mod h1:upnsH4Ju1YkqpLXC305eW3yDZ4NfnNbmQRCMWS58IKU= +github.com/redis/go-redis/v9 v9.22.0 h1:laDvpYXTJtZLloinw1fA5Kqd6HAEH2XKxOkG/PDq2F0= +github.com/redis/go-redis/v9 v9.22.0/go.mod h1:y2g0Wj8rQvuK0ELM+oxSudcLtC09JScs98I/X9gRWY4= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU= +github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0= +github.com/sethvargo/go-retry v0.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/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE= +github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg= +github.com/tinylib/msgp v1.6.4 h1:mOwYbyYDLPj35mkA2BjjYejgJk9BuHxDdvRnb6v2ZcQ= +github.com/tinylib/msgp v1.6.4/go.mod h1:RSp0LW9oSxFut3KzESt5Voq4GVWyS+PSulT77roAqEA= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/ugorji/go/codec v1.3.1 h1:waO7eEiFDwidsBN6agj1vJQ4AG7lh2yqXyOXqhgQuyY= +github.com/ugorji/go/codec v1.3.1/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4= +github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ= +github.com/zeebo/assert v1.3.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0= +github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs= +github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s= +go.mongodb.org/mongo-driver/v2 v2.5.0 h1:yXUhImUjjAInNcpTcAlPHiT7bIXhshCTL3jVBkF3xaE= +go.mongodb.org/mongo-driver/v2 v2.5.0/go.mod h1:yOI9kBsufol30iFsl1slpdq1I0eHPzybRWdyYUs8K/0= +go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE= +go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= +go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y= +go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU= +go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= +go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw= +go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg= +golang.org/x/arch v0.22.0 h1:c/Zle32i5ttqRXjdLyyHZESLD/bB90DCU1g9l/0YBDI= +golang.org/x/arch v0.22.0/go.mod h1:dNHoOeKiyja7GTvF9NJS1l3Z2yntpQNzgrjh1cU103A= +golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M= +golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA= +golang.org/x/net v0.60.0 h1:79p50tfZlm0J9YfoDsSi639qSXNGVwEzOPLCxM2FsYU= +golang.org/x/net v0.60.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg= +golang.org/x/sync v0.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= +google.golang.org/protobuf v1.36.12 h1:pJOKDDOyeXErUroCihFAd5LQuwXBSpVnKGrj5o/fwxc= +google.golang.org/protobuf v1.36.12/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/ini.v1 v1.67.3 h1:iM9Lhz5MRSGhHVGGwCuzG9KO8PoirCXj/m/qTmOJJQw= +gopkg.in/ini.v1 v1.67.3/go.mod h1:x/cyOwCgZqOkJoDIJ3c1KNHMo10+nLGAhh+kn3Zizss= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 h1:5D53IMaUuA5InSeMu9eJtlQXS2NxAhyWQvkKEgXZhHI= +modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6/go.mod h1:Qz0X07sNOR1jWYCrJMEnbW/X55x206Q7Vt4mz6/wHp4= +modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4= +modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo= +modernc.org/memory v1.8.0 h1:IqGTL6eFMaDZZhEWwcREgeMXYwmW83LYW8cROZYkg+E= +modernc.org/memory v1.8.0/go.mod h1:XPZ936zp5OMKGWPqbD3JShgd/ZoQ7899TUuQqxY+peU= +modernc.org/sqlite v1.33.0 h1:WWkA/T2G17okiLGgKAj4/RMIvgyMT19yQ038160IeYk= +modernc.org/sqlite v1.33.0/go.mod h1:9uQ9hF/pCZoYZK73D/ud5Z7cIRIILSZI8NdIemVMTX8= +modernc.org/strutil v1.2.0 h1:agBi9dp1I+eOnxXeiZawM8F4LawKv4NzGWSaLfyeNZA= +modernc.org/strutil v1.2.0/go.mod h1:/mdcBmfOibveCTBxUl5B5l6W+TTH1FXPLHZE6bTosX0= +modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y= +modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM= diff --git a/backend/internal/config/config.go b/backend/internal/config/config.go new file mode 100644 index 00000000..6419db3a --- /dev/null +++ b/backend/internal/config/config.go @@ -0,0 +1,230 @@ +package config + +import ( + "fmt" + "os" + "strconv" + "strings" + "time" +) + +type Config struct { + Host string + Port string + DatabaseURL string + SessionStore string + CacheURL string + SessionTTL time.Duration + IdempotencyTTL time.Duration + ShutdownDrain time.Duration + + S3Endpoint string + S3Bucket string + S3UseSSL bool + S3Region string + S3AccessKey string + S3SecretKey string + + MaxUploadBytes int64 + MaxImagePixels int64 + + ReconcilerEnabled bool + ReconcilerInterval time.Duration + ReconcilerGracePeriod time.Duration +} + +func Load() (*Config, error) { + dsn, err := readSecret("DATABASE_URL_FILE", "DATABASE_URL") + if err != nil { + return nil, fmt.Errorf("database url: %w", err) + } + + cacheURL, err := readSecretOptional("CACHE_URL_FILE", "CACHE_URL") + if err != nil { + return nil, fmt.Errorf("cache url: %w", err) + } + + ttlSeconds, err := readInt("SESSION_TTL_SECONDS", 3600) + if err != nil { + return nil, fmt.Errorf("SESSION_TTL_SECONDS: %w", err) + } + if ttlSeconds <= 0 { + return nil, fmt.Errorf("SESSION_TTL_SECONDS должен быть положительным") + } + + idemSeconds, err := readInt("IDEMPOTENCY_TTL_SECONDS", 86400) + if err != nil { + return nil, fmt.Errorf("IDEMPOTENCY_TTL_SECONDS: %w", err) + } + if idemSeconds <= 0 { + return nil, fmt.Errorf("IDEMPOTENCY_TTL_SECONDS должен быть положительным") + } + + drainSeconds, err := readInt("SHUTDOWN_DRAIN_SECONDS", 3) + if err != nil { + return nil, fmt.Errorf("SHUTDOWN_DRAIN_SECONDS: %w", err) + } + if drainSeconds < 0 { + return nil, fmt.Errorf("SHUTDOWN_DRAIN_SECONDS не может быть отрицательным") + } + + store := strings.ToLower(getenv("SESSION_STORE", "postgres")) + switch store { + case "postgres", "redis", "valkey": + default: + return nil, fmt.Errorf("SESSION_STORE=%q не поддерживается", store) + } + + s3Endpoint := getenv("S3_ENDPOINT", "") + s3Bucket := getenv("S3_BUCKET", "") + s3UseSSL, err := readBool("S3_USE_SSL", false) + if err != nil { + return nil, fmt.Errorf("S3_USE_SSL: %w", err) + } + s3Region := getenv("S3_REGION", "us-east-1") + + s3AccessKey, err := readSecretOptional("S3_ACCESS_KEY_ID_FILE", "S3_ACCESS_KEY_ID") + if err != nil { + return nil, fmt.Errorf("s3 access key: %w", err) + } + s3SecretKey, err := readSecretOptional("S3_SECRET_ACCESS_KEY_FILE", "S3_SECRET_ACCESS_KEY") + if err != nil { + return nil, fmt.Errorf("s3 secret key: %w", err) + } + + maxUploadBytes, err := readInt64("MAX_UPLOAD_BYTES", 10485760) + if err != nil { + return nil, fmt.Errorf("MAX_UPLOAD_BYTES: %w", err) + } + if maxUploadBytes <= 0 { + return nil, fmt.Errorf("MAX_UPLOAD_BYTES должен быть положительным") + } + + maxImagePixels, err := readInt64("MAX_IMAGE_PIXELS", 40000000) + if err != nil { + return nil, fmt.Errorf("MAX_IMAGE_PIXELS: %w", err) + } + if maxImagePixels <= 0 { + return nil, fmt.Errorf("MAX_IMAGE_PIXELS должен быть положительным") + } + + reconcilerEnabled, err := readBool("RECONCILER_ENABLED", true) + if err != nil { + return nil, fmt.Errorf("RECONCILER_ENABLED: %w", err) + } + reconcilerIntervalSec, err := readInt("RECONCILER_INTERVAL_SECONDS", 300) + if err != nil { + return nil, fmt.Errorf("RECONCILER_INTERVAL_SECONDS: %w", err) + } + if reconcilerIntervalSec <= 0 { + return nil, fmt.Errorf("RECONCILER_INTERVAL_SECONDS должен быть положительным") + } + reconcilerGraceSec, err := readInt("RECONCILER_GRACE_SECONDS", 3600) + if err != nil { + return nil, fmt.Errorf("RECONCILER_GRACE_SECONDS: %w", err) + } + if reconcilerGraceSec <= 0 { + return nil, fmt.Errorf("RECONCILER_GRACE_SECONDS должен быть положительным") + } + + return &Config{ + Host: getenv("APP_HOST", "0.0.0.0"), + Port: getenv("APP_PORT", "8080"), + DatabaseURL: dsn, + SessionStore: store, + CacheURL: cacheURL, + SessionTTL: time.Duration(ttlSeconds) * time.Second, + IdempotencyTTL: time.Duration(idemSeconds) * time.Second, + ShutdownDrain: time.Duration(drainSeconds) * time.Second, + + S3Endpoint: s3Endpoint, + S3Bucket: s3Bucket, + S3UseSSL: s3UseSSL, + S3Region: s3Region, + S3AccessKey: s3AccessKey, + S3SecretKey: s3SecretKey, + + MaxUploadBytes: maxUploadBytes, + MaxImagePixels: maxImagePixels, + + ReconcilerEnabled: reconcilerEnabled, + ReconcilerInterval: time.Duration(reconcilerIntervalSec) * time.Second, + ReconcilerGracePeriod: time.Duration(reconcilerGraceSec) * time.Second, + }, nil +} + +func (c *Config) S3Configured() bool { + return c.S3Endpoint != "" && + c.S3Bucket != "" && + c.S3AccessKey != "" && + c.S3SecretKey != "" +} + +func getenv(key, def string) string { + if v := os.Getenv(key); v != "" { + return v + } + return def +} + +func readInt(key string, def int) (int, error) { + v := os.Getenv(key) + if v == "" { + return def, nil + } + n, err := strconv.Atoi(v) + if err != nil { + return 0, fmt.Errorf("не целое число: %q", v) + } + return n, nil +} + +func readInt64(key string, def int64) (int64, error) { + v := os.Getenv(key) + if v == "" { + return def, nil + } + n, err := strconv.ParseInt(v, 10, 64) + if err != nil { + return 0, fmt.Errorf("не целое число: %q", v) + } + return n, nil +} + +func readBool(key string, def bool) (bool, error) { + v := os.Getenv(key) + if v == "" { + return def, nil + } + b, err := strconv.ParseBool(v) + if err != nil { + return false, fmt.Errorf("не bool: %q", v) + } + return b, nil +} + +func readSecret(fileEnv, envKey string) (string, error) { + v, err := readSecretOptional(fileEnv, envKey) + if err != nil { + return "", err + } + if v == "" { + return "", fmt.Errorf("не задан %s или %s", fileEnv, envKey) + } + return v, nil +} + +func readSecretOptional(fileEnv, envKey string) (string, error) { + if path := os.Getenv(fileEnv); path != "" { + b, err := os.ReadFile(path) + if err != nil { + return "", fmt.Errorf("чтение %s=%s: %w", fileEnv, path, err) + } + s := strings.TrimSpace(string(b)) + if s == "" { + return "", fmt.Errorf("файл %s пуст", path) + } + return s, nil + } + return os.Getenv(envKey), nil +} diff --git a/backend/internal/domain/entity.go b/backend/internal/domain/entity.go new file mode 100644 index 00000000..570ac609 --- /dev/null +++ b/backend/internal/domain/entity.go @@ -0,0 +1,61 @@ +package domain + +import "time" + +type Health struct { + Status string +} + +type User struct { + ID string + Username string + DisplayName string + PasswordHash string // в DTO UserResponse его не будет + CreatedAt time.Time +} + +type Chat struct { + ID string + Title string + MemberIDs []string + CreatedAt time.Time +} + +type Message struct { + ID string + ChatID string + SenderID string + Text string + AttachmentIDs []string + CreatedAt time.Time +} + +type Session struct { + TokenHash []byte + UserID string + ExpiresAt time.Time + CreatedAt time.Time +} + +type AttachmentStatus string + +const ( + AttachmentStatusPending AttachmentStatus = "pending" + AttachmentStatusReady AttachmentStatus = "ready" + AttachmentStatusFailed AttachmentStatus = "failed" +) + +type Attachment struct { + ID string + ChatID string + OwnerID string + Filename string + ContentType string + SizeBytes int64 + SHA256 []byte + ObjectKey string + Status AttachmentStatus + ErrorCode *string + CreatedAt time.Time + UpdatedAt time.Time +} diff --git a/backend/internal/domain/errors.go b/backend/internal/domain/errors.go new file mode 100644 index 00000000..0d196ac0 --- /dev/null +++ b/backend/internal/domain/errors.go @@ -0,0 +1,197 @@ +package domain + +// Этот файл — единственный источник доменных ошибок. +// Правила: +// 1. sentinel-ошибки (ErrXxx) нужны для errors.Is. +// 2. типы *XxxError нужны для errors.As и для поля code в JSON. +// 3. У каждого типа есть Code() ErrorCode — через него DTO маппит +// в HTTP-код (StatusFromError в httpapi/dto/error.go). +// 4. В Error() пишем только то, что безопасно показать клиенту: +// без SQL, stack trace, путей, credential'ов. + +import ( + "errors" + "fmt" +) + +// =================================================================== +// ErrorCode — машинный код для JSON {"error":{"code":"..."}} +// =================================================================== + +type ErrorCode string + +const ( + CodeInvalidRequest ErrorCode = "invalid_request" // 400 + CodeUnauthorized ErrorCode = "unauthorized" // 401 + CodeNotFound ErrorCode = "not_found" // 404 + CodeConflict ErrorCode = "conflict" // 409 + CodePayloadTooLarge ErrorCode = "payload_too_large" // 413 + CodeUnsupportedMediaType ErrorCode = "unsupported_media_type" // 415 + CodeUnavailable ErrorCode = "unavailable" // 503 + CodeInternalError ErrorCode = "internal_error" // 500 +) + +// =================================================================== +// Sentinel-ошибки (errors.Is) +// =================================================================== + +var ( + ErrNotFound = errors.New("не найдено") + ErrAlreadyExists = errors.New("уже существует") + ErrInvalidInput = errors.New("некорректные данные") + ErrUnauthorized = errors.New("требуется аутентификация") + ErrUnsupportedMediaType = errors.New("неподдерживаемый тип содержимого") + ErrPayloadTooLarge = errors.New("превышен допустимый размер") + ErrUnavailable = errors.New("сервис недоступен") + ErrConflict = errors.New("конфликт") +) + +// =================================================================== +// 404 Not Found +// =================================================================== + +type NotFoundError struct { + Entity string + ID string +} + +func (e *NotFoundError) Error() string { + return fmt.Sprintf("%s с id %q не найден", e.Entity, e.ID) +} +func (e *NotFoundError) Unwrap() error { return ErrNotFound } +func (e *NotFoundError) Code() ErrorCode { return CodeNotFound } + +// =================================================================== +// 409 Conflict +// =================================================================== + +// AlreadyExistsError — конфликт уникальности (например, username занят). +// Отдельно от ConflictError: другой смысл, но оба дают HTTP 409. +type AlreadyExistsError struct { + Entity string + Field string + Value string +} + +func (e *AlreadyExistsError) Error() string { + return fmt.Sprintf("%s с %s %q уже существует", e.Entity, e.Field, e.Value) +} +func (e *AlreadyExistsError) Unwrap() error { return ErrAlreadyExists } +func (e *AlreadyExistsError) Code() ErrorCode { return CodeConflict } + +// IdempotencyConflictError — тот же Idempotency-Key, но другое тело запроса. +type IdempotencyConflictError struct { + Key string +} + +func (e *IdempotencyConflictError) Error() string { + return "Idempotency-Key " + e.Key + " уже использован с другим телом запроса" +} +func (e *IdempotencyConflictError) Unwrap() error { return ErrAlreadyExists } +func (e *IdempotencyConflictError) Code() ErrorCode { return CodeConflict } + +// ConflictError — конфликт бизнес-состояния, а не уникальности. +// Примеры: "вложение ещё не готово", "нельзя перевести failed-вложение в ready". +// Reason — короткая безопасная фраза, идёт клиенту как message. +type ConflictError struct{ Reason string } + +func (e *ConflictError) Error() string { return e.Reason } +func (e *ConflictError) Unwrap() error { return ErrConflict } +func (e *ConflictError) Code() ErrorCode { return CodeConflict } + +// =================================================================== +// 400 Bad Request +// =================================================================== + +// ValidationError — неверное поле или формат. +type ValidationError struct { + Field string + Message string +} + +func (e *ValidationError) Error() string { + return fmt.Sprintf("ошибка валидации поля %q: %s", e.Field, e.Message) +} +func (e *ValidationError) Unwrap() error { return ErrInvalidInput } +func (e *ValidationError) Code() ErrorCode { return CodeInvalidRequest } + +// =================================================================== +// 401 Unauthorized +// =================================================================== + +// UnauthorizedError — нет/просрочен/отозван токен, неверные credentials. +// Reason — только для логов, в JSON не попадает (по контракту все 401 +// должны иметь одинаковое сообщение, чтобы не перебирать username). +type UnauthorizedError struct { + Reason string +} + +func (e *UnauthorizedError) Error() string { + return "требуется аутентификация" +} +func (e *UnauthorizedError) Unwrap() error { return ErrUnauthorized } +func (e *UnauthorizedError) Code() ErrorCode { return CodeUnauthorized } + +// =================================================================== +// 415 Unsupported Media Type +// =================================================================== + +// UnsupportedMediaTypeError — неверный Content-Type. +// +// ВАЖНО: сейчас текст жёстко про application/json. Для lab4 (вложения) +// ожидается image/png или image/jpeg. Прежде чем использовать этот тип +// в AttachmentHandler, добавить поле Expected string (дефолт application/json) +// или сделать отдельный тип — не переиспользовать как есть, иначе клиент +// увидит неверную подсказку. +type UnsupportedMediaTypeError struct { + MediaType string + Expected string +} + +func (e *UnsupportedMediaTypeError) Error() string { + expected := e.Expected + if expected == "" { + expected = "application/json" + } + if e.MediaType == "" { + return "требуется Content-Type: " + expected + } + return fmt.Sprintf("неподдерживаемый Content-Type %q; требуется %s", e.MediaType, expected) +} +func (e *UnsupportedMediaTypeError) Unwrap() error { return ErrUnsupportedMediaType } +func (e *UnsupportedMediaTypeError) Code() ErrorCode { return CodeUnsupportedMediaType } + +// =================================================================== +// 413 Payload Too Large +// =================================================================== + +// PayloadTooLargeError — тело больше лимита. +// Message опционален: если пуст — формируется из Limit. +type PayloadTooLargeError struct { + Limit int64 + Message string +} + +func (e *PayloadTooLargeError) Error() string { + if e.Message != "" { + return e.Message + } + return fmt.Sprintf("превышен лимит %d байт", e.Limit) +} +func (e *PayloadTooLargeError) Unwrap() error { return ErrPayloadTooLarge } +func (e *PayloadTooLargeError) Code() ErrorCode { return CodePayloadTooLarge } + +// =================================================================== +// 503 Service Unavailable +// =================================================================== + +// UnavailableError — недоступна критичная зависимость (БД, кэш, S3). +// Reason — только для логов; клиент видит общее сообщение по контракту, +// чтобы не раскрывать имена сервисов и топологию. +type UnavailableError struct{ Reason string } + +func (e *UnavailableError) Error() string { + return "сервис временно недоступен" +} +func (e *UnavailableError) Unwrap() error { return ErrUnavailable } +func (e *UnavailableError) Code() ErrorCode { return CodeUnavailable } diff --git a/backend/internal/domain/idempotency.go b/backend/internal/domain/idempotency.go new file mode 100644 index 00000000..65d43d59 --- /dev/null +++ b/backend/internal/domain/idempotency.go @@ -0,0 +1,33 @@ +package domain + +import ( + "crypto/sha256" +) + +type IdempotencyScope struct { + UserID string + Method string + Path string + Key string +} + +type CreateIdempotentResult struct { + Status int + Body []byte + Replayed bool +} + +func RequestHash(text string, attachmentIDs []string) []byte { + if len(attachmentIDs) == 0 { + sum := sha256.Sum256([]byte(text)) + return sum[:] + } + h := sha256.New() + h.Write([]byte(text)) + h.Write([]byte{0}) + for _, id := range attachmentIDs { + h.Write([]byte(id)) + h.Write([]byte{0}) + } + return h.Sum(nil) +} diff --git a/backend/internal/domain/repository.go b/backend/internal/domain/repository.go new file mode 100644 index 00000000..b5d6a619 --- /dev/null +++ b/backend/internal/domain/repository.go @@ -0,0 +1,47 @@ +package domain + +import ( + "context" + "time" +) + +type UserRepository interface { + Create(ctx context.Context, user *User) error + GetByID(ctx context.Context, id string) (*User, error) + GetByUsername(ctx context.Context, username string) (*User, error) +} + +type ChatRepository interface { + Create(ctx context.Context, chat *Chat) error + GetByID(ctx context.Context, id string) (*Chat, error) + ListByMember(ctx context.Context, userID string, limit int, cursor *string) ([]Chat, *string, error) +} + +type MessageRepository interface { + Create(ctx context.Context, message *Message) error + ListByChat(ctx context.Context, chatID string, limit int, cursor *string) ([]Message, *string, error) + CreateIdempotent( + ctx context.Context, + scope IdempotencyScope, + requestHash []byte, + fresh *Message, + freshResponse []byte, + ttl time.Duration, + ) (*CreateIdempotentResult, error) +} + +type SessionRepository interface { + Create(ctx context.Context, session *Session) error + GetByTokenHash(ctx context.Context, tokenHash []byte) (*Session, error) + DeleteByTokenHash(ctx context.Context, tokenHash []byte) error +} + +type AttachmentRepository interface { + Create(ctx context.Context, a *Attachment) error + GetByID(ctx context.Context, id string) (*Attachment, error) + MarkReady(ctx context.Context, id string) error + MarkFailed(ctx context.Context, id string, errorCode string) error + ListStalePending(ctx context.Context, olderThan time.Time, limit int) ([]Attachment, error) + ExistingKeys(ctx context.Context, keys []string) ([]string, error) + DeletePending(ctx context.Context, id string) error +} diff --git a/backend/internal/domain/storage.go b/backend/internal/domain/storage.go new file mode 100644 index 00000000..5df5ff2e --- /dev/null +++ b/backend/internal/domain/storage.go @@ -0,0 +1,21 @@ +package domain + +import ( + "context" + "io" + "time" +) + +type ObjectInfo struct { + Key string + LastModified time.Time + Size int64 +} + +type ObjectStorage interface { + Ping(ctx context.Context) error + Put(ctx context.Context, key string, r io.Reader, size int64, contentType string) error + Get(ctx context.Context, key string) (io.ReadCloser, int64, string, error) + Delete(ctx context.Context, key string) error + List(ctx context.Context, prefix string) ([]ObjectInfo, error) +} diff --git a/backend/internal/httpapi/dto/attachment.go b/backend/internal/httpapi/dto/attachment.go new file mode 100644 index 00000000..b608954a --- /dev/null +++ b/backend/internal/httpapi/dto/attachment.go @@ -0,0 +1,50 @@ +package dto + +import ( + "encoding/hex" + + "high-loud-apps-labs/internal/domain" +) + +type VariantResponse struct { + Name string `json:"name"` + ContentType string `json:"content_type"` + SizeBytes int64 `json:"size_bytes"` + Width int `json:"width"` + Height int `json:"height"` + SHA256 string `json:"sha256"` +} + +type AttachmentResponse struct { + ID string `json:"id"` + ChatID string `json:"chat_id"` + OwnerID string `json:"owner_id"` + Filename string `json:"filename"` + ContentType string `json:"content_type"` + SizeBytes int64 `json:"size_bytes"` + SHA256 string `json:"sha256"` + Status string `json:"status"` + Variants []VariantResponse `json:"variants"` + ErrorCode *string `json:"error_code"` + CreatedAt string `json:"created_at"` + UpdatedAt string `json:"updated_at"` +} + +func AttachmentFromDomain(a *domain.Attachment) AttachmentResponse { + variants := make([]VariantResponse, 0) + errCode := a.ErrorCode + return AttachmentResponse{ + ID: a.ID, + ChatID: a.ChatID, + OwnerID: a.OwnerID, + Filename: a.Filename, + ContentType: a.ContentType, + SizeBytes: a.SizeBytes, + SHA256: hex.EncodeToString(a.SHA256), + Status: string(a.Status), + Variants: variants, + ErrorCode: errCode, + CreatedAt: a.CreatedAt.UTC().Format(timeFormat), + UpdatedAt: a.UpdatedAt.UTC().Format(timeFormat), + } +} diff --git a/backend/internal/httpapi/dto/chat.go b/backend/internal/httpapi/dto/chat.go new file mode 100644 index 00000000..a9bf0425 --- /dev/null +++ b/backend/internal/httpapi/dto/chat.go @@ -0,0 +1,39 @@ +package dto + +import ( + "high-loud-apps-labs/internal/domain" +) + +type CreateChatRequest struct { + Title string `json:"title" binding:"required,min=1,max=100"` + MemberIDs []string `json:"member_ids" binding:"required,min=1,max=100,dive,uuid"` +} + +type ChatResponse struct { + ID string `json:"id"` + Title string `json:"title"` + MemberIDs []string `json:"member_ids"` + CreatedAt string `json:"created_at"` +} + +type ChatPageResponse struct { + Items []ChatResponse `json:"items"` + NextCursor *string `json:"next_cursor"` +} + +func ChatFromDomain(c *domain.Chat) ChatResponse { + return ChatResponse{ + ID: c.ID, + Title: c.Title, + MemberIDs: append([]string(nil), c.MemberIDs...), + CreatedAt: c.CreatedAt.UTC().Format(timeFormat), + } +} + +func ChatsFromDomain(items []domain.Chat) []ChatResponse { + out := make([]ChatResponse, len(items)) + for i := range items { + out[i] = ChatFromDomain(&items[i]) + } + return out +} diff --git a/backend/internal/httpapi/dto/error.go b/backend/internal/httpapi/dto/error.go new file mode 100644 index 00000000..03f62acb --- /dev/null +++ b/backend/internal/httpapi/dto/error.go @@ -0,0 +1,86 @@ +package dto + +import ( + "errors" + + "high-loud-apps-labs/internal/domain" +) + +// делаем вложенный ответ через вкладку ErrorBody в обертку. + +type ErrorBody struct { + Code string `json:"code"` + Message string `json:"message"` + RequestID string `json:"request_id"` +} + +type ErrorResponse struct { + Error ErrorBody `json:"error"` +} + +type coded interface { + Code() domain.ErrorCode +} + +func ErrorFromDomain(err error, requestID string) ErrorResponse { + var c coded + if errors.As(err, &c) { + code := c.Code() + message := err.Error() + if code == domain.CodeInternalError { + message = "внутренняя ошибка" + } + return ErrorResponse{Error: ErrorBody{ + Code: string(code), + Message: message, + RequestID: requestID, + }} + } + code := domain.CodeInternalError + switch { + case errors.Is(err, domain.ErrNotFound): + code = domain.CodeNotFound + case errors.Is(err, domain.ErrAlreadyExists): + code = domain.CodeConflict + case errors.Is(err, domain.ErrInvalidInput): + code = domain.CodeInvalidRequest + case errors.Is(err, domain.ErrUnauthorized): + code = domain.CodeUnauthorized + case errors.Is(err, domain.ErrUnsupportedMediaType): + code = domain.CodeUnsupportedMediaType + case errors.Is(err, domain.ErrUnavailable): + code = domain.CodeUnavailable + } + message := err.Error() + if code == domain.CodeInternalError { + message = "внутренняя ошибка" + } + return ErrorResponse{Error: ErrorBody{ + Code: string(code), + Message: message, + RequestID: requestID, + }} +} + +func StatusFromError(err error) int { + switch { + case errors.Is(err, domain.ErrNotFound): + return 404 + case errors.Is(err, domain.ErrAlreadyExists): + return 409 + case errors.Is(err, domain.ErrInvalidInput): + return 400 + case errors.Is(err, domain.ErrUnauthorized): + return 401 + case errors.Is(err, domain.ErrUnsupportedMediaType): + return 415 + case errors.Is(err, domain.ErrUnavailable): + return 503 + case errors.Is(err, domain.ErrPayloadTooLarge): + return 413 + case errors.Is(err, domain.ErrConflict): + return 409 + default: + return 500 + } +} diff --git a/backend/internal/httpapi/dto/health.go b/backend/internal/httpapi/dto/health.go new file mode 100644 index 00000000..8bb35034 --- /dev/null +++ b/backend/internal/httpapi/dto/health.go @@ -0,0 +1,5 @@ +package dto + +type HealthResponse struct { + Status string `json:"status"` +} diff --git a/backend/internal/httpapi/dto/message.go b/backend/internal/httpapi/dto/message.go new file mode 100644 index 00000000..6c9d6e1a --- /dev/null +++ b/backend/internal/httpapi/dto/message.go @@ -0,0 +1,47 @@ +package dto + +import ( + "high-loud-apps-labs/internal/domain" +) + +type CreateMessageRequest struct { + Text string `json:"text" binding:"max=4000"` + AttachmentIDs []string `json:"attachment_ids" binding:"max=10,dive,uuid"` +} + +type MessageResponse struct { + ID string `json:"id"` + ChatID string `json:"chat_id"` + SenderID string `json:"sender_id"` + Text string `json:"text"` + AttachmentIDs []string `json:"attachment_ids"` + CreatedAt string `json:"created_at"` +} + +type MessagePageResponse struct { + Items []MessageResponse `json:"items"` + NextCursor *string `json:"next_cursor"` +} + +func MessageFromDomain(m *domain.Message) MessageResponse { + ids := m.AttachmentIDs + if ids == nil { + ids = []string{} + } + return MessageResponse{ + ID: m.ID, + ChatID: m.ChatID, + SenderID: m.SenderID, + Text: m.Text, + AttachmentIDs: ids, + CreatedAt: m.CreatedAt.UTC().Format(timeFormat), + } +} + +func MessagesFromDomain(items []domain.Message) []MessageResponse { + out := make([]MessageResponse, len(items)) + for i := range items { + out[i] = MessageFromDomain(&items[i]) + } + return out +} diff --git a/backend/internal/httpapi/dto/session.go b/backend/internal/httpapi/dto/session.go new file mode 100644 index 00000000..174430cf --- /dev/null +++ b/backend/internal/httpapi/dto/session.go @@ -0,0 +1,21 @@ +package dto + +import ( + "high-loud-apps-labs/internal/domain" +) + +type SessionResponse struct { + Token string `json:"token"` + TokenType string `json:"token_type"` + ExpiresAt string `json:"expires_at"` + User UserResponse `json:"user"` +} + +func SessionResponseFromDomain(s *domain.Session, token string, u *domain.User) SessionResponse { + return SessionResponse{ + Token: token, + TokenType: "Bearer", + ExpiresAt: s.ExpiresAt.UTC().Format(timeFormat), + User: UserFromDomain(u), + } +} diff --git a/backend/internal/httpapi/dto/user.go b/backend/internal/httpapi/dto/user.go new file mode 100644 index 00000000..08519414 --- /dev/null +++ b/backend/internal/httpapi/dto/user.go @@ -0,0 +1,29 @@ +package dto + +import ( + "high-loud-apps-labs/internal/domain" +) + +const timeFormat = "2006-01-02T15:04:05.000000Z07:00" + +type CreateUserRequest struct { + Username string `json:"username" binding:"required,min=3,max=32"` + DisplayName string `json:"display_name" binding:"required,min=1,max=100"` + Password string `json:"password" binding:"required,min=12,max=512"` +} + +type UserResponse struct { + ID string `json:"id"` + Username string `json:"username"` + DisplayName string `json:"display_name"` + CreatedAt string `json:"created_at"` +} + +func UserFromDomain(u *domain.User) UserResponse { + return UserResponse{ + ID: u.ID, + Username: u.Username, + DisplayName: u.DisplayName, + CreatedAt: u.CreatedAt.UTC().Format(timeFormat), + } +} diff --git a/backend/internal/httpapi/handler/attachment.go b/backend/internal/httpapi/handler/attachment.go new file mode 100644 index 00000000..ad0f8186 --- /dev/null +++ b/backend/internal/httpapi/handler/attachment.go @@ -0,0 +1,98 @@ +package handler + +import ( + "mime" + "net/http" + + "github.com/gin-gonic/gin" + + "high-loud-apps-labs/internal/domain" + "high-loud-apps-labs/internal/httpapi/dto" + "high-loud-apps-labs/internal/httpapi/middleware" + "high-loud-apps-labs/internal/service" +) + +type AttachmentHandler struct { + svc *service.AttachmentService +} + +func NewAttachmentHandler(svc *service.AttachmentService) *AttachmentHandler { + return &AttachmentHandler{svc: svc} +} + +func (h *AttachmentHandler) Upload(c *gin.Context) { + me := middleware.CurrentUser(c) + + chatID, err := parseUUIDPathParam(c, "chat_id") + if err != nil { + respondError(c, err) + return + } + + filename := c.Query("filename") + if filename == "" { + respondError(c, &domain.ValidationError{ + Field: "filename", + Message: "обязателен", + }) + return + } + + mediaType, _, err := mime.ParseMediaType(c.GetHeader("Content-Type")) + if err != nil || (mediaType != "image/png" && mediaType != "image/jpeg") { + respondError(c, &domain.UnsupportedMediaTypeError{ + MediaType: c.GetHeader("Content-Type"), + Expected: "image/png или image/jpeg", + }) + return + } + + att, err := h.svc.Upload(c.Request.Context(), me.ID, chatID, filename, c.Request.Body) + if err != nil { + respondError(c, err) + return + } + c.JSON(http.StatusCreated, dto.AttachmentFromDomain(att)) +} + +func (h *AttachmentHandler) Get(c *gin.Context) { + me := middleware.CurrentUser(c) + + attachmentID, err := parseUUIDPathParam(c, "attachment_id") + if err != nil { + respondError(c, err) + return + } + + a, err := h.svc.Get(c.Request.Context(), me.ID, attachmentID) + if err != nil { + respondError(c, err) + return + } + c.JSON(http.StatusOK, dto.AttachmentFromDomain(a)) +} + +func (h *AttachmentHandler) Content(c *gin.Context) { + me := middleware.CurrentUser(c) + + attachmentID, err := parseUUIDPathParam(c, "attachment_id") + if err != nil { + respondError(c, err) + return + } + + variant := c.DefaultQuery("variant", "original") + if variant != "original" { + respondError(c, &domain.NotFoundError{Entity: "variant", ID: variant}) + return + } + + rc, size, contentType, err := h.svc.Download(c.Request.Context(), me.ID, attachmentID) + if err != nil { + respondError(c, err) + return + } + defer func() { _ = rc.Close() }() + + c.DataFromReader(http.StatusOK, size, contentType, rc, nil) +} diff --git a/backend/internal/httpapi/handler/auth.go b/backend/internal/httpapi/handler/auth.go new file mode 100644 index 00000000..b06a8727 --- /dev/null +++ b/backend/internal/httpapi/handler/auth.go @@ -0,0 +1,77 @@ +package handler + +import ( + "encoding/base64" + "net/http" + "strings" + + "github.com/gin-gonic/gin" + + "high-loud-apps-labs/internal/domain" + "high-loud-apps-labs/internal/httpapi/dto" + "high-loud-apps-labs/internal/httpapi/middleware" + "high-loud-apps-labs/internal/service" +) + +type AuthHandler struct { + sessions *service.SessionService +} + +func NewAuthHandler(sessions *service.SessionService) *AuthHandler { + return &AuthHandler{sessions: sessions} +} + +func (h *AuthHandler) CreateSession(c *gin.Context) { + username, password, err := parseBasicAuth(c) + if err != nil { + respondBasicChallenge(c, err) + return + } + + session, token, user, err := h.sessions.Login(c.Request.Context(), username, password) + if err != nil { + respondBasicChallenge(c, err) + return + } + + c.JSON(http.StatusCreated, dto.SessionResponseFromDomain(session, token, user)) +} + +// RevokeCurrent отзывает текущую сессию. Токен уже проверен BearerAuth, +// поэтому здесь его достаточно передать в SessionService. +func (h *AuthHandler) RevokeCurrent(c *gin.Context) { + token := middleware.CurrentToken(c) + if token == "" { + respondError(c, &domain.UnauthorizedError{Reason: "нет токена"}) + return + } + + if err := h.sessions.Revoke(c.Request.Context(), token); err != nil { + respondError(c, err) + return + } + + c.Status(http.StatusNoContent) +} + +// parseBasicAuth читает Authorization: Basic base64(username:password). +func parseBasicAuth(c *gin.Context) (string, string, error) { + const prefix = "Basic " + + auth := c.GetHeader("Authorization") + if len(auth) < len(prefix) || !strings.EqualFold(auth[:len(prefix)], prefix) { + return "", "", &domain.UnauthorizedError{Reason: "требуется Basic"} + } + + decoded, err := base64.StdEncoding.DecodeString(auth[len(prefix):]) + if err != nil { + return "", "", &domain.UnauthorizedError{Reason: "некорректный Base64"} + } + + username, password, ok := strings.Cut(string(decoded), ":") + if !ok || username == "" { + return "", "", &domain.UnauthorizedError{Reason: "некорректный Basic-формат"} + } + + return username, password, nil +} diff --git a/backend/internal/httpapi/handler/bind.go b/backend/internal/httpapi/handler/bind.go new file mode 100644 index 00000000..cca73874 --- /dev/null +++ b/backend/internal/httpapi/handler/bind.go @@ -0,0 +1,52 @@ +package handler + +import ( + "bytes" + "encoding/json" + "errors" + "io" + "mime" + "strings" + + "github.com/gin-gonic/gin" + "github.com/gin-gonic/gin/binding" + + "high-loud-apps-labs/internal/domain" +) + +func bindStrictJSON(c *gin.Context, dst any) error { + ct := c.GetHeader("Content-Type") + mediaType, _, err := mime.ParseMediaType(ct) + if ct == "" || err != nil || !strings.EqualFold(mediaType, "application/json") { + return &domain.UnsupportedMediaTypeError{MediaType: ct} + } + + body, err := io.ReadAll(c.Request.Body) + if err != nil { + return &badRequestError{msg: "не удалось прочитать тело запроса"} + } + _ = c.Request.Body.Close() + + dec := json.NewDecoder(bytes.NewReader(body)) + dec.DisallowUnknownFields() + + if err := dec.Decode(dst); err != nil { + if errors.Is(err, io.EOF) { + return &badRequestError{msg: "тело запроса обязательно"} + } + return &badRequestError{msg: err.Error()} + } + + // Отклоняем любой непустой остаток после первого JSON-значения. + var extra json.RawMessage + if err := dec.Decode(&extra); !errors.Is(err, io.EOF) { + return &badRequestError{msg: "тело запроса содержит данные после JSON"} + } + + if v := binding.Validator; v != nil { + if err := v.ValidateStruct(dst); err != nil { + return &badRequestError{msg: err.Error()} + } + } + return nil +} diff --git a/backend/internal/httpapi/handler/chat.go b/backend/internal/httpapi/handler/chat.go new file mode 100644 index 00000000..86fc2cd0 --- /dev/null +++ b/backend/internal/httpapi/handler/chat.go @@ -0,0 +1,88 @@ +package handler + +import ( + "net/http" + "strconv" + + "github.com/gin-gonic/gin" + + "high-loud-apps-labs/internal/httpapi/dto" + "high-loud-apps-labs/internal/httpapi/middleware" + "high-loud-apps-labs/internal/service" +) + +type ChatHandler struct { + chats *service.ChatService +} + +func NewChatHandler(chats *service.ChatService) *ChatHandler { + return &ChatHandler{chats: chats} +} + +func (h *ChatHandler) Create(c *gin.Context) { + var req dto.CreateChatRequest + if err := bindStrictJSON(c, &req); err != nil { + respondError(c, err) + return + } + me := middleware.CurrentUser(c) + + chat, err := h.chats.Create(c.Request.Context(), me.ID, req.Title, req.MemberIDs) + if err != nil { + respondError(c, err) + return + } + c.JSON(http.StatusCreated, dto.ChatFromDomain(chat)) +} + +func (h *ChatHandler) Get(c *gin.Context) { + me := middleware.CurrentUser(c) + + chatID, err := parseUUIDPathParam(c, "chat_id") + if err != nil { + respondError(c, err) + return + } + + chat, err := h.chats.Get(c.Request.Context(), me.ID, chatID) + if err != nil { + respondError(c, err) + return + } + c.JSON(http.StatusOK, dto.ChatFromDomain(chat)) +} + +func (h *ChatHandler) List(c *gin.Context) { + me := middleware.CurrentUser(c) + limit, cursor, err := parseListQuery(c) + if err != nil { + respondError(c, err) + return + } + + items, next, err := h.chats.List(c.Request.Context(), me.ID, limit, cursor) + if err != nil { + respondError(c, err) + return + } + c.JSON(http.StatusOK, dto.ChatPageResponse{ + Items: dto.ChatsFromDomain(items), + NextCursor: next, + }) +} + +func parseListQuery(c *gin.Context) (int, *string, error) { + limit := 50 + if v := c.Query("limit"); v != "" { + n, err := strconv.Atoi(v) + if err != nil || n < 1 || n > 100 { + return 0, nil, &badRequestError{msg: "limit должен быть целым 1..100"} + } + limit = n + } + var cursor *string + if v := c.Query("cursor"); v != "" { + cursor = &v + } + return limit, cursor, nil +} diff --git a/backend/internal/httpapi/handler/health.go b/backend/internal/httpapi/handler/health.go new file mode 100644 index 00000000..00995659 --- /dev/null +++ b/backend/internal/httpapi/handler/health.go @@ -0,0 +1,54 @@ +package handler + +import ( + "context" + "net/http" + "sync/atomic" + "time" + + "github.com/gin-gonic/gin" + + "high-loud-apps-labs/internal/domain" + "high-loud-apps-labs/internal/httpapi/dto" +) + +type HealthCheck func(ctx context.Context) error + +type HealthHandler struct { + checks []HealthCheck + ready *atomic.Bool +} + +func NewHealthHandler(checks ...HealthCheck) *HealthHandler { + return &HealthHandler{checks: checks} +} + +func (h *HealthHandler) WithReadyFlag(v *atomic.Bool) *HealthHandler { + h.ready = v + return h +} + +func (h *HealthHandler) Live(c *gin.Context) { + c.JSON(http.StatusOK, dto.HealthResponse{Status: "ok"}) +} + +func (h *HealthHandler) Ready(c *gin.Context) { + if h.ready != nil && !h.ready.Load() { + respondError(c, &domain.UnavailableError{Reason: "shutting down"}) + return + } + + ctx, cancel := context.WithTimeout(c.Request.Context(), 2*time.Second) + defer cancel() + + for _, check := range h.checks { + if check == nil { + continue + } + if err := check(ctx); err != nil { + respondError(c, &domain.UnavailableError{Reason: "зависимость недоступна"}) + return + } + } + c.JSON(http.StatusOK, dto.HealthResponse{Status: "ok"}) +} diff --git a/backend/internal/httpapi/handler/message.go b/backend/internal/httpapi/handler/message.go new file mode 100644 index 00000000..badf7f7b --- /dev/null +++ b/backend/internal/httpapi/handler/message.go @@ -0,0 +1,88 @@ +package handler + +import ( + "net/http" + "regexp" + + "github.com/gin-gonic/gin" + + "high-loud-apps-labs/internal/httpapi/dto" + "high-loud-apps-labs/internal/httpapi/middleware" + "high-loud-apps-labs/internal/service" +) + +var idempotencyKeyPattern = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`) + +type MessageHandler struct { + messages *service.MessageService +} + +func NewMessageHandler(messages *service.MessageService) *MessageHandler { + return &MessageHandler{messages: messages} +} + +func (h *MessageHandler) Create(c *gin.Context) { + var req dto.CreateMessageRequest + if err := bindStrictJSON(c, &req); err != nil { + respondError(c, err) + return + } + + key := c.GetHeader("Idempotency-Key") + if key == "" || !idempotencyKeyPattern.MatchString(key) { + respondError(c, &badRequestError{ + msg: "Idempotency-Key обязателен и должен соответствовать ^[A-Za-z0-9._:-]{1,128}$", + }) + return + } + + me := middleware.CurrentUser(c) + + chatID, err := parseUUIDPathParam(c, "chat_id") + if err != nil { + respondError(c, err) + return + } + + result, err := h.messages.Create( + c.Request.Context(), + me.ID, + chatID, + req.Text, + req.AttachmentIDs, + key, + c.Request.URL.Path, + ) + if err != nil { + respondError(c, err) + return + } + + c.Data(result.Status, "application/json", result.Body) +} + +func (h *MessageHandler) List(c *gin.Context) { + me := middleware.CurrentUser(c) + + chatID, err := parseUUIDPathParam(c, "chat_id") + if err != nil { + respondError(c, err) + return + } + + limit, cursor, err := parseListQuery(c) + if err != nil { + respondError(c, err) + return + } + + items, next, err := h.messages.List(c.Request.Context(), me.ID, chatID, limit, cursor) + if err != nil { + respondError(c, err) + return + } + c.JSON(http.StatusOK, dto.MessagePageResponse{ + Items: dto.MessagesFromDomain(items), + NextCursor: next, + }) +} diff --git a/backend/internal/httpapi/handler/response.go b/backend/internal/httpapi/handler/response.go new file mode 100644 index 00000000..8cad0b3e --- /dev/null +++ b/backend/internal/httpapi/handler/response.go @@ -0,0 +1,44 @@ +package handler + +import ( + "github.com/gin-gonic/gin" + "github.com/google/uuid" + + "high-loud-apps-labs/internal/domain" + "high-loud-apps-labs/internal/httpapi/dto" + "high-loud-apps-labs/internal/httpapi/middleware" +) + +type badRequestError struct{ msg string } + +func (e *badRequestError) Error() string { return e.msg } +func (e *badRequestError) Unwrap() error { return domain.ErrInvalidInput } +func (e *badRequestError) Code() domain.ErrorCode { return domain.CodeInvalidRequest } + +func parseUUIDPathParam(c *gin.Context, name string) (string, error) { + raw := c.Param(name) + if raw == "" { + return "", &badRequestError{msg: "параметр " + name + " обязателен"} + } + + id, err := uuid.Parse(raw) + if err != nil { + return "", &badRequestError{msg: "параметр " + name + " должен быть корректным UUID"} + } + + return id.String(), nil +} + +func respondError(c *gin.Context, err error) { + reqID := middleware.GetRequestID(c) + c.JSON(dto.StatusFromError(err), dto.ErrorFromDomain(err, reqID)) +} + +// respondBasicChallenge выставляет WWW-Authenticate для 401 на /auth/sessions. +// На не-401 ответах заголовок не ставится как по контракту нужно +func respondBasicChallenge(c *gin.Context, err error) { + if dto.StatusFromError(err) == 401 { + c.Header("WWW-Authenticate", `Basic realm="messenger", charset="UTF-8"`) + } + respondError(c, err) +} diff --git a/backend/internal/httpapi/handler/user.go b/backend/internal/httpapi/handler/user.go new file mode 100644 index 00000000..7697b504 --- /dev/null +++ b/backend/internal/httpapi/handler/user.go @@ -0,0 +1,43 @@ +package handler + +import ( + "net/http" + + "github.com/gin-gonic/gin" + + "high-loud-apps-labs/internal/httpapi/dto" + "high-loud-apps-labs/internal/httpapi/middleware" + "high-loud-apps-labs/internal/service" +) + +type UserHandler struct { + users *service.UserService +} + +func NewUserHandler(users *service.UserService) *UserHandler { + return &UserHandler{users: users} +} + +func (h *UserHandler) Create(c *gin.Context) { + var req dto.CreateUserRequest + if err := bindStrictJSON(c, &req); err != nil { + respondError(c, err) + return + } + + u, err := h.users.Create(c.Request.Context(), req.Username, req.DisplayName, req.Password) + if err != nil { + respondError(c, err) + return + } + c.JSON(http.StatusCreated, dto.UserFromDomain(u)) +} + +func (h *UserHandler) Current(c *gin.Context) { + u := middleware.CurrentUser(c) + if u == nil { + respondError(c, &badRequestError{msg: "нет текущего пользователя"}) + return + } + c.JSON(http.StatusOK, dto.UserFromDomain(u)) +} diff --git a/backend/internal/httpapi/middleware/auth_legacy.go b/backend/internal/httpapi/middleware/auth_legacy.go new file mode 100644 index 00000000..e3192dab --- /dev/null +++ b/backend/internal/httpapi/middleware/auth_legacy.go @@ -0,0 +1,38 @@ +package middleware + +import ( + "net/http" + + "github.com/gin-gonic/gin" + + "high-loud-apps-labs/internal/domain" + "high-loud-apps-labs/internal/httpapi/dto" +) + +func Auth(users domain.UserRepository) gin.HandlerFunc { + return func(c *gin.Context) { + id := c.GetHeader("X-User-Id") + if id == "" { + abortUnauthorized(c, "требуется X-User-Id") + return + } + + u, err := users.GetByID(c.Request.Context(), id) + if err != nil { + abortUnauthorized(c, "неизвестный пользователь") + return + } + + c.Set(CurrentUserKey, u) + c.Next() + } +} + +func abortUnauthorized(c *gin.Context, msg string) { + reqID := GetRequestID(c) + body := dto.ErrorFromDomain( + &domain.UnauthorizedError{Reason: msg}, + reqID, + ) + c.AbortWithStatusJSON(http.StatusUnauthorized, body) +} diff --git a/backend/internal/httpapi/middleware/bearer.go b/backend/internal/httpapi/middleware/bearer.go new file mode 100644 index 00000000..fc904aa2 --- /dev/null +++ b/backend/internal/httpapi/middleware/bearer.go @@ -0,0 +1,70 @@ +package middleware + +import ( + "context" + "strings" + "time" + + "github.com/gin-gonic/gin" + + "high-loud-apps-labs/internal/domain" + "high-loud-apps-labs/internal/httpapi/dto" + "high-loud-apps-labs/internal/service" +) + +const authTimeout = 3 * time.Second + +// BearerAuth читает Authorization: Bearer , проверяет сессию +// через SessionService и кладёт пользователя и токен в контекст. +func BearerAuth(sessions *service.SessionService) gin.HandlerFunc { + return func(c *gin.Context) { + token, ok := parseBearerToken(c) + if !ok { + abortBearer(c, "требуется Bearer") + return + } + + ctx, cancel := context.WithTimeout(c.Request.Context(), authTimeout) + defer cancel() + + user, err := sessions.Resolve(ctx, token) + if err != nil { + abortBearerWithErr(c, err) + return + } + + c.Set(CurrentUserKey, user) + c.Set(CurrentTokenKey, token) + c.Next() + } +} + +// parseBearerToken читает Authorization: Bearer . +func parseBearerToken(c *gin.Context) (string, bool) { + const prefix = "Bearer " + auth := c.GetHeader("Authorization") + if len(auth) < len(prefix) || !strings.EqualFold(auth[:len(prefix)], prefix) { + return "", false + } + token := strings.TrimSpace(auth[len(prefix):]) + if token == "" { + return "", false + } + return token, true +} + +func abortBearer(c *gin.Context, reason string) { + abortBearerWithErr(c, &domain.UnauthorizedError{Reason: reason}) +} + +// abortBearerWithErr формирует ответ на ошибку аутентификации. +// Заголовок WWW-Authenticate: Bearer выставляется только на 401; +// при 503 (недоступность БД/кэша) — обычный ответ ошибки без challenge. +func abortBearerWithErr(c *gin.Context, err error) { + reqID := GetRequestID(c) + status := dto.StatusFromError(err) + if status == 401 { + c.Header("WWW-Authenticate", `Bearer realm="messenger"`) + } + c.AbortWithStatusJSON(status, dto.ErrorFromDomain(err, reqID)) +} diff --git a/backend/internal/httpapi/middleware/context.go b/backend/internal/httpapi/middleware/context.go new file mode 100644 index 00000000..f467d367 --- /dev/null +++ b/backend/internal/httpapi/middleware/context.go @@ -0,0 +1,30 @@ +package middleware + +import ( + "github.com/gin-gonic/gin" + + "high-loud-apps-labs/internal/domain" +) + +const ( + CurrentUserKey = "current_user" + CurrentTokenKey = "current_token" +) + +func CurrentUser(c *gin.Context) *domain.User { + if v, ok := c.Get(CurrentUserKey); ok { + if u, ok := v.(*domain.User); ok { + return u + } + } + return nil +} + +func CurrentToken(c *gin.Context) string { + if v, ok := c.Get(CurrentTokenKey); ok { + if s, ok := v.(string); ok { + return s + } + } + return "" +} diff --git a/backend/internal/httpapi/middleware/instance.go b/backend/internal/httpapi/middleware/instance.go new file mode 100644 index 00000000..583488c1 --- /dev/null +++ b/backend/internal/httpapi/middleware/instance.go @@ -0,0 +1,31 @@ +package middleware + +import ( + "os" + + "github.com/gin-gonic/gin" + "github.com/google/uuid" +) + +const InstanceIDHeader = "X-Instance-Id" + +var instanceID = resolveInstanceID() + +func resolveInstanceID() string { + if v := os.Getenv("INSTANCE_ID"); v != "" { + return v + } + if v, err := os.Hostname(); err == nil && v != "" { + return v + } + return uuid.NewString() +} + +func Instance() gin.HandlerFunc { + return func(c *gin.Context) { + c.Writer.Header().Set(InstanceIDHeader, instanceID) + c.Next() + } +} + +func GetInstanceID() string { return instanceID } diff --git a/backend/internal/httpapi/middleware/requestid.go b/backend/internal/httpapi/middleware/requestid.go new file mode 100644 index 00000000..43c90113 --- /dev/null +++ b/backend/internal/httpapi/middleware/requestid.go @@ -0,0 +1,32 @@ +package middleware + +import ( + "github.com/gin-gonic/gin" + "github.com/google/uuid" +) + +const ( + RequestIDKey = "request_id" + RequestIDHeader = "X-Request-Id" +) + +func RequestID() gin.HandlerFunc { + return func(c *gin.Context) { + id := c.GetHeader(RequestIDHeader) + if id == "" { + id = uuid.NewString() + } + c.Set(RequestIDKey, id) + c.Writer.Header().Set(RequestIDHeader, id) + c.Next() + } +} + +func GetRequestID(c *gin.Context) string { + if v, ok := c.Get(RequestIDKey); ok { + if s, ok := v.(string); ok { + return s + } + } + return "" +} diff --git a/backend/internal/httpapi/router.go b/backend/internal/httpapi/router.go new file mode 100644 index 00000000..a614a017 --- /dev/null +++ b/backend/internal/httpapi/router.go @@ -0,0 +1,57 @@ +package httpapi + +import ( + "github.com/gin-gonic/gin" + + "high-loud-apps-labs/internal/httpapi/handler" + "high-loud-apps-labs/internal/httpapi/middleware" +) + +type Handlers struct { + Health *handler.HealthHandler + User *handler.UserHandler + Chat *handler.ChatHandler + Message *handler.MessageHandler + Auth *handler.AuthHandler + Attachment *handler.AttachmentHandler +} + +func NewRouter(h Handlers, auth gin.HandlerFunc) *gin.Engine { + gin.EnableJsonDecoderDisallowUnknownFields() + + r := gin.New() + r.Use(gin.Logger()) + r.Use(gin.Recovery()) + r.Use(middleware.RequestID()) + r.Use(middleware.Instance()) + + r.GET("/health/live", h.Health.Live) + r.GET("/health/ready", h.Health.Ready) + + api := r.Group("/api/v1") + { + api.POST("/users", h.User.Create) + api.POST("/auth/sessions", h.Auth.CreateSession) + } + + protected := api.Group("") + protected.Use(auth) + { + protected.DELETE("/auth/sessions/current", h.Auth.RevokeCurrent) + + protected.GET("/users/me", h.User.Current) + protected.POST("/chats", h.Chat.Create) + protected.GET("/chats", h.Chat.List) + protected.GET("/chats/:chat_id", h.Chat.Get) + protected.POST("/chats/:chat_id/messages", h.Message.Create) + protected.GET("/chats/:chat_id/messages", h.Message.List) + + if h.Attachment != nil { + protected.POST("/chats/:chat_id/attachments", h.Attachment.Upload) + protected.GET("/attachments/:attachment_id", h.Attachment.Get) + protected.GET("/attachments/:attachment_id/content", h.Attachment.Content) + } + } + + return r +} diff --git a/backend/internal/service/attachment.go b/backend/internal/service/attachment.go new file mode 100644 index 00000000..a9b1273d --- /dev/null +++ b/backend/internal/service/attachment.go @@ -0,0 +1,209 @@ +package service + +import ( + "bytes" + "context" + "crypto/sha256" + "image" + _ "image/jpeg" + _ "image/png" + "io" + "net/http" + "strings" + "time" + "unicode" + "unicode/utf8" + + "github.com/google/uuid" + + "high-loud-apps-labs/internal/domain" +) + +type AttachmentService struct { + attachments domain.AttachmentRepository + chats domain.ChatRepository + storage domain.ObjectStorage + maxBytes int64 + maxPixels int64 + decodeSem chan struct{} +} + +func NewAttachmentService( + attachments domain.AttachmentRepository, + chats domain.ChatRepository, + storage domain.ObjectStorage, + maxBytes, maxPixels int64, +) *AttachmentService { + return &AttachmentService{ + attachments: attachments, + chats: chats, + storage: storage, + maxBytes: maxBytes, + maxPixels: maxPixels, + // 40M pixels × 4 bytes (RGBA8) = 160 MB per full decode. + // API_MEMORY=256M → только один decode безопасен. + decodeSem: make(chan struct{}, 1), + } +} + +// Upload принимает тело, валидирует PNG/JPEG по содержимому, +// кладёт в pending, загружает в S3, переводит в ready. +// При ошибке Put компенсирует MarkFailed. +func (s *AttachmentService) Upload( + ctx context.Context, + ownerID, chatID, filename string, + body io.Reader, +) (*domain.Attachment, error) { + if err := s.ensureMember(ctx, ownerID, chatID); err != nil { + return nil, err + } + if err := validateFilename(filename); err != nil { + return nil, err + } + + limited := io.LimitReader(body, s.maxBytes+1) + buf, err := io.ReadAll(limited) + if err != nil { + return nil, &domain.UnavailableError{Reason: "не удалось прочитать тело загрузки"} + } + if int64(len(buf)) > s.maxBytes { + return nil, &domain.PayloadTooLargeError{Limit: s.maxBytes} + } + if len(buf) == 0 { + return nil, &domain.ValidationError{Field: "body", Message: "пустой файл"} + } + + contentType := http.DetectContentType(buf) + if contentType != "image/png" && contentType != "image/jpeg" { + return nil, &domain.UnsupportedMediaTypeError{ + MediaType: contentType, + Expected: "image/png или image/jpeg", + } + } + + cfg, _, err := image.DecodeConfig(bytes.NewReader(buf)) + if err != nil { + return nil, &domain.UnsupportedMediaTypeError{ + MediaType: contentType, + Expected: "целостный image/png или image/jpeg", + } + } + if int64(cfg.Width)*int64(cfg.Height) > s.maxPixels { + return nil, &domain.PayloadTooLargeError{ + Limit: s.maxPixels, + Message: "изображение превышает лимит пикселей", + } + } + + if err := s.verifyIntegrity(ctx, buf, contentType); err != nil { + return nil, err + } + + sha := sha256.Sum256(buf) + id := uuid.NewString() + objectKey := "chats/" + chatID + "/" + id + now := time.Now().UTC() + + a := &domain.Attachment{ + ID: id, + ChatID: chatID, + OwnerID: ownerID, + Filename: filename, + ContentType: contentType, + SizeBytes: int64(len(buf)), + SHA256: sha[:], + ObjectKey: objectKey, + Status: domain.AttachmentStatusPending, + CreatedAt: now, + UpdatedAt: now, + } + if err := s.attachments.Create(ctx, a); err != nil { + return nil, err + } + + if err := s.storage.Put(ctx, objectKey, bytes.NewReader(buf), int64(len(buf)), contentType); err != nil { + // Компенсация: pending → failed. Reconciler не будет трогать failed. + _ = s.attachments.MarkFailed(ctx, a.ID, "upload_failed") + return nil, err + } + + if err := s.attachments.MarkReady(ctx, a.ID); err != nil { + // S3 has the object, DB still pending. Reconciler очистит по grace period. + return nil, err + } + a.Status = domain.AttachmentStatusReady + return a, nil +} + +func (s *AttachmentService) verifyIntegrity(ctx context.Context, buf []byte, contentType string) error { + select { + case s.decodeSem <- struct{}{}: + case <-ctx.Done(): + return &domain.UnavailableError{Reason: "ожидание декодирования"} + } + defer func() { <-s.decodeSem }() + + if _, _, err := image.Decode(bytes.NewReader(buf)); err != nil { + return &domain.UnsupportedMediaTypeError{ + MediaType: contentType, + Expected: "целостный " + contentType + " (без обрезки/повреждений)", + } + } + return nil +} + +func (s *AttachmentService) Get(ctx context.Context, userID, attachmentID string) (*domain.Attachment, error) { + a, err := s.attachments.GetByID(ctx, attachmentID) + if err != nil { + return nil, err + } + if err := s.ensureMember(ctx, userID, a.ChatID); err != nil { + return nil, err + } + return a, nil +} + +func (s *AttachmentService) Download( + ctx context.Context, + userID, attachmentID string, +) (io.ReadCloser, int64, string, error) { + a, err := s.Get(ctx, userID, attachmentID) + if err != nil { + return nil, 0, "", err + } + if a.Status != domain.AttachmentStatusReady { + return nil, 0, "", &domain.ConflictError{Reason: "вложение не готово"} + } + return s.storage.Get(ctx, a.ObjectKey) +} + +func (s *AttachmentService) ensureMember(ctx context.Context, userID, chatID string) error { + c, err := s.chats.GetByID(ctx, chatID) + if err != nil { + return err + } + for _, m := range c.MemberIDs { + if m == userID { + return nil + } + } + return &domain.NotFoundError{Entity: "chat", ID: chatID} +} + +func validateFilename(name string) error { + if strings.TrimSpace(name) == "" { + return &domain.ValidationError{Field: "filename", Message: "не может быть пустым или состоять только из пробелов"} + } + if utf8.RuneCountInString(name) > 255 { + return &domain.ValidationError{Field: "filename", Message: "не более 255 символов"} + } + if strings.ContainsAny(name, "/\\\x00") { + return &domain.ValidationError{Field: "filename", Message: "содержит недопустимые символы"} + } + for _, r := range name { + if unicode.IsControl(r) { + return &domain.ValidationError{Field: "filename", Message: "содержит управляющие символы"} + } + } + return nil +} diff --git a/backend/internal/service/chat.go b/backend/internal/service/chat.go new file mode 100644 index 00000000..caa8dcec --- /dev/null +++ b/backend/internal/service/chat.go @@ -0,0 +1,95 @@ +package service + +import ( + "context" + "errors" + "strings" + "time" + "unicode/utf8" + + "github.com/google/uuid" + + "high-loud-apps-labs/internal/domain" +) + +type ChatService struct { + chats domain.ChatRepository + users domain.UserRepository +} + +func NewChatService(chats domain.ChatRepository, users domain.UserRepository) *ChatService { + return &ChatService{chats: chats, users: users} +} + +func (s *ChatService) Create(ctx context.Context, creatorID, title string, memberIDs []string) (*domain.Chat, error) { + if strings.TrimSpace(title) == "" { + return nil, &domain.ValidationError{ + Field: "title", + Message: "не может состоять только из пробелов", + } + } + if utf8.RuneCountInString(title) > 100 { + return nil, &domain.ValidationError{ + Field: "title", + Message: "не более 100 символов", + } + } + + seen := make(map[string]struct{}, len(memberIDs)) + for _, id := range memberIDs { + if _, dup := seen[id]; dup { + return nil, &domain.ValidationError{ + Field: "member_ids", + Message: "дубликаты запрещены", + } + } + seen[id] = struct{}{} + } + + if _, ok := seen[creatorID]; !ok { + return nil, &domain.ValidationError{ + Field: "member_ids", + Message: "создатель должен входить в список участников", + } + } + + for _, id := range memberIDs { + if _, err := s.users.GetByID(ctx, id); err != nil { + if errors.Is(err, domain.ErrNotFound) { + return nil, &domain.ValidationError{ + Field: "member_ids", + Message: "неизвестный участник " + id, + } + } + return nil, err + } + } + + c := &domain.Chat{ + ID: uuid.NewString(), + Title: title, + MemberIDs: append([]string(nil), memberIDs...), + CreatedAt: time.Now().UTC(), + } + if err := s.chats.Create(ctx, c); err != nil { + return nil, err + } + return c, nil +} + +func (s *ChatService) Get(ctx context.Context, userID, chatID string) (*domain.Chat, error) { + c, err := s.chats.GetByID(ctx, chatID) + if err != nil { + return nil, err + } + for _, m := range c.MemberIDs { + if m == userID { + return c, nil + } + } + return nil, &domain.NotFoundError{Entity: "chat", ID: chatID} +} + +func (s *ChatService) List(ctx context.Context, userID string, limit int, cursor *string) ([]domain.Chat, *string, error) { + return s.chats.ListByMember(ctx, userID, limit, cursor) +} diff --git a/backend/internal/service/message.go b/backend/internal/service/message.go new file mode 100644 index 00000000..5d51e815 --- /dev/null +++ b/backend/internal/service/message.go @@ -0,0 +1,122 @@ +package service + +import ( + "context" + "encoding/json" + "net/http" + "strings" + "time" + "unicode/utf8" + + "github.com/google/uuid" + + "high-loud-apps-labs/internal/domain" + "high-loud-apps-labs/internal/httpapi/dto" +) + +type MessageService struct { + messages domain.MessageRepository + chats domain.ChatRepository + idempotencyTTL time.Duration +} + +func NewMessageService( + messages domain.MessageRepository, + chats domain.ChatRepository, + idempotencyTTL time.Duration, +) *MessageService { + return &MessageService{ + messages: messages, + chats: chats, + idempotencyTTL: idempotencyTTL, + } +} + +func (s *MessageService) Create( + ctx context.Context, + senderID, chatID, text string, + attachmentIDs []string, + idempotencyKey, requestPath string, +) (*domain.CreateIdempotentResult, error) { + trimmed := strings.TrimSpace(text) + if trimmed == "" && len(attachmentIDs) == 0 { + return nil, &domain.ValidationError{ + Field: "text", + Message: "text или attachment_ids обязательны", + } + } + if utf8.RuneCountInString(text) > 4000 { + return nil, &domain.ValidationError{ + Field: "text", + Message: "не более 4000 символов", + } + } + if len(attachmentIDs) > 10 { + return nil, &domain.ValidationError{ + Field: "attachment_ids", + Message: "не более 10", + } + } + seen := make(map[string]struct{}, len(attachmentIDs)) + for _, id := range attachmentIDs { + if _, dup := seen[id]; dup { + return nil, &domain.ValidationError{ + Field: "attachment_ids", + Message: "дубликаты запрещены", + } + } + seen[id] = struct{}{} + } + + if err := s.ensureMember(ctx, senderID, chatID); err != nil { + return nil, err + } + + fresh := &domain.Message{ + ID: uuid.NewString(), + ChatID: chatID, + SenderID: senderID, + Text: text, + AttachmentIDs: append([]string(nil), attachmentIDs...), + CreatedAt: time.Now().UTC(), + } + + body, err := json.Marshal(dto.MessageFromDomain(fresh)) + if err != nil { + return nil, err + } + + return s.messages.CreateIdempotent( + ctx, + domain.IdempotencyScope{ + UserID: senderID, + Method: http.MethodPost, + Path: requestPath, + Key: idempotencyKey, + }, + domain.RequestHash(text, attachmentIDs), + fresh, + body, + s.idempotencyTTL, + ) +} + +func (s *MessageService) List(ctx context.Context, userID, chatID string, limit int, cursor *string) ([]domain.Message, *string, error) { + if err := s.ensureMember(ctx, userID, chatID); err != nil { + return nil, nil, err + } + return s.messages.ListByChat(ctx, chatID, limit, cursor) +} + +func (s *MessageService) ensureMember(ctx context.Context, userID, chatID string) error { + c, err := s.chats.GetByID(ctx, chatID) + if err != nil { + return err + } + for _, m := range c.MemberIDs { + if m == userID { + return nil + } + } + return &domain.NotFoundError{Entity: "chat", ID: chatID} +} diff --git a/backend/internal/service/password.go b/backend/internal/service/password.go new file mode 100644 index 00000000..1f0659f1 --- /dev/null +++ b/backend/internal/service/password.go @@ -0,0 +1,112 @@ +package service + +import ( + "crypto/rand" + "crypto/subtle" + "encoding/base64" + "errors" + "fmt" + "strings" + + "golang.org/x/crypto/argon2" +) + +// PasswordHasher хеширует пароли и проверяет пароль сохранённого хеша. +type PasswordHasher interface { + Hash(password string) (string, error) + Verify(password, encoded string) (bool, error) +} + +type Argon2idHasher struct { + memory uint32 + iterations uint32 + parallelism uint8 + saltLength uint32 + keyLength uint32 +} + +func NewArgon2idHasher() *Argon2idHasher { + return &Argon2idHasher{ + memory: 64 * 1024, + iterations: 3, + parallelism: 4, + saltLength: 16, + keyLength: 32, + } +} + +func (h *Argon2idHasher) Hash(password string) (string, error) { + salt := make([]byte, h.saltLength) + if _, err := rand.Read(salt); err != nil { + return "", fmt.Errorf("генерация соли: %w", err) + } + + key := argon2.IDKey( + []byte(password), + salt, + h.iterations, + h.memory, + h.parallelism, + h.keyLength, + ) + + return fmt.Sprintf( + "$argon2id$v=%d$m=%d,t=%d,p=%d$%s$%s", + argon2.Version, + h.memory, + h.iterations, + h.parallelism, + base64.RawStdEncoding.EncodeToString(salt), + base64.RawStdEncoding.EncodeToString(key), + ), nil +} + +// Verify сравнивает пароль с сохранённым PHC-хешем. +// Возвращает false и nil при неверном пароле. +// Возвращает false и ошибку, если строка хеша повреждена или несовместима. +func (h *Argon2idHasher) Verify(password, encoded string) (bool, error) { + parts := strings.Split(encoded, "$") + if len(parts) != 6 || parts[1] != "argon2id" { + return false, errors.New("некорректный формат хеша") + } + + var version int + if n, err := fmt.Sscanf(parts[2], "v=%d", &version); err != nil || n != 1 { + return false, errors.New("некорректная версия хеша") + } + if version != argon2.Version { + return false, errors.New("неподдерживаемая версия Argon2") + } + + var ( + memory uint32 + iterations uint32 + parallelism uint8 + ) + if n, err := fmt.Sscanf(parts[3], "m=%d,t=%d,p=%d", &memory, &iterations, ¶llelism); err != nil || n != 3 { + return false, errors.New("некорректные параметры хеша") + } + + salt, err := base64.RawStdEncoding.DecodeString(parts[4]) + if err != nil { + return false, errors.New("некорректная соль") + } + + wantKey, err := base64.RawStdEncoding.DecodeString(parts[5]) + if err != nil { + return false, errors.New("некорректный ключ") + } + + gotKey := argon2.IDKey( + []byte(password), + salt, + iterations, + memory, + parallelism, + uint32(len(wantKey)), + ) + + return subtle.ConstantTimeCompare(gotKey, wantKey) == 1, nil +} + +var _ PasswordHasher = (*Argon2idHasher)(nil) diff --git a/backend/internal/service/reconciler.go b/backend/internal/service/reconciler.go new file mode 100644 index 00000000..325b95d8 --- /dev/null +++ b/backend/internal/service/reconciler.go @@ -0,0 +1,176 @@ +package service + +import ( + "context" + "log" + "time" + + "github.com/jackc/pgx/v5/pgxpool" + + "high-loud-apps-labs/internal/domain" +) + +const advisoryLockKey int64 = 0x4D5455534904 + +type Reconciler struct { + attachments domain.AttachmentRepository + storage domain.ObjectStorage + pool *pgxpool.Pool + gracePeriod time.Duration + batchSize int +} + +func NewReconciler( + attachments domain.AttachmentRepository, + storage domain.ObjectStorage, + pool *pgxpool.Pool, + gracePeriod time.Duration, +) *Reconciler { + if gracePeriod <= 0 { + gracePeriod = time.Hour + } + return &Reconciler{ + attachments: attachments, + storage: storage, + pool: pool, + gracePeriod: gracePeriod, + batchSize: 100, + } +} + +func (r *Reconciler) Run(ctx context.Context, interval time.Duration) { + if interval <= 0 { + interval = 5 * time.Minute + } + ticker := time.NewTicker(interval) + defer ticker.Stop() + + log.Printf("reconciler: started, interval=%s grace=%s", interval, r.gracePeriod) + + r.tick(ctx) + + for { + select { + case <-ctx.Done(): + log.Println("reconciler: stopped") + return + case <-ticker.C: + r.tick(ctx) + } + } +} + +func (r *Reconciler) tick(ctx context.Context) { + acquired, release, err := r.acquireLock(ctx) + if err != nil { + log.Printf("reconciler: acquire lock: %v", err) + return + } + if !acquired { + return + } + defer release() + + pendingDeleted := r.sweepPending(ctx) + orphansDeleted := r.sweepOrphans(ctx) + + if pendingDeleted > 0 || orphansDeleted > 0 { + log.Printf("reconciler: sweep done pending=%d orphans=%d", pendingDeleted, orphansDeleted) + } +} + +func (r *Reconciler) acquireLock(ctx context.Context) (bool, func(), error) { + conn, err := r.pool.Acquire(ctx) + if err != nil { + return false, nil, err + } + + var ok bool + if err := conn.QueryRow(ctx, `SELECT pg_try_advisory_lock($1)`, advisoryLockKey).Scan(&ok); err != nil { + conn.Release() + return false, nil, err + } + if !ok { + conn.Release() + return false, nil, nil + } + + release := func() { + unlockCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + if _, err := conn.Exec(unlockCtx, `SELECT pg_advisory_unlock($1)`, advisoryLockKey); err != nil { + log.Printf("reconciler: unlock: %v", err) + } + conn.Release() + } + return true, release, nil +} + +func (r *Reconciler) sweepPending(ctx context.Context) int { + cutoff := time.Now().UTC().Add(-r.gracePeriod) + stale, err := r.attachments.ListStalePending(ctx, cutoff, r.batchSize) + if err != nil { + log.Printf("reconciler: list stale pending: %v", err) + return 0 + } + + deleted := 0 + for _, a := range stale { + if err := r.storage.Delete(ctx, a.ObjectKey); err != nil { + log.Printf("reconciler: delete object %s: %v", a.ObjectKey, err) + continue + } + if err := r.attachments.DeletePending(ctx, a.ID); err != nil { + log.Printf("reconciler: delete pending row %s: %v", a.ID, err) + continue + } + deleted++ + } + return deleted +} + +func (r *Reconciler) sweepOrphans(ctx context.Context) int { + objects, err := r.storage.List(ctx, "chats/") + if err != nil { + log.Printf("reconciler: list objects: %v", err) + return 0 + } + if len(objects) == 0 { + return 0 + } + + cutoff := time.Now().UTC().Add(-r.gracePeriod) + keys := make([]string, 0, len(objects)) + for _, o := range objects { + if o.LastModified.Before(cutoff) { + keys = append(keys, o.Key) + } + } + if len(keys) == 0 { + return 0 + } + + existing, err := r.attachments.ExistingKeys(ctx, keys) + if err != nil { + log.Printf("reconciler: existing keys: %v", err) + return 0 + } + + inDB := make(map[string]struct{}, len(existing)) + for _, k := range existing { + inDB[k] = struct{}{} + } + + deleted := 0 + for _, k := range keys { + if _, ok := inDB[k]; ok { + continue + } + if err := r.storage.Delete(ctx, k); err != nil { + log.Printf("reconciler: delete orphan %s: %v", k, err) + continue + } + deleted++ + } + return deleted +} diff --git a/backend/internal/service/session.go b/backend/internal/service/session.go new file mode 100644 index 00000000..ee7216bf --- /dev/null +++ b/backend/internal/service/session.go @@ -0,0 +1,127 @@ +package service + +import ( + "context" + "crypto/rand" + "crypto/sha256" + "encoding/base64" + "errors" + "fmt" + "time" + + "high-loud-apps-labs/internal/domain" +) + +// SessionService отвечает за вход, проверку и отзыв сессий. +type SessionService struct { + users domain.UserRepository + sessions domain.SessionRepository + hasher PasswordHasher + ttl time.Duration +} + +// NewSessionService создаёт сервис с заданным TTL сессий. +func NewSessionService( + users domain.UserRepository, + sessions domain.SessionRepository, + hasher PasswordHasher, + ttl time.Duration, +) *SessionService { + return &SessionService{ + users: users, + sessions: sessions, + hasher: hasher, + ttl: ttl, + } +} + +// Login проверяет username/password и создаёт новую сессию. +// Возвращает сессию, открытый токен и пользователя. +// Открытый токен уходит клиенту; в БД сохраняется только его SHA-256. +func (s *SessionService) Login(ctx context.Context, username, password string) (*domain.Session, string, *domain.User, error) { + user, err := s.users.GetByUsername(ctx, username) + if err != nil { + if errors.Is(err, domain.ErrNotFound) { + return nil, "", nil, &domain.UnauthorizedError{Reason: "неверные учётные данные"} + } + return nil, "", nil, err + } + + ok, err := s.hasher.Verify(password, user.PasswordHash) + if err != nil { + return nil, "", nil, err + } + if !ok { + return nil, "", nil, &domain.UnauthorizedError{Reason: "неверные учётные данные"} + } + + token, err := generateToken() + if err != nil { + return nil, "", nil, err + } + + tokenHash := sha256.Sum256([]byte(token)) + now := time.Now().UTC() + + session := &domain.Session{ + TokenHash: tokenHash[:], + UserID: user.ID, + ExpiresAt: now.Add(s.ttl), + CreatedAt: now, + } + if err := s.sessions.Create(ctx, session); err != nil { + return nil, "", nil, err + } + + return session, token, user, nil +} + +// Resolve проверяет токен и возвращает пользователя сессии. +// Истёкший, отозванный или неизвестный токен - UnauthorizedError по контракту +func (s *SessionService) Resolve(ctx context.Context, token string) (*domain.User, error) { + if token == "" { + return nil, &domain.UnauthorizedError{Reason: "пустой токен"} + } + + tokenHash := sha256.Sum256([]byte(token)) + + session, err := s.sessions.GetByTokenHash(ctx, tokenHash[:]) + if err != nil { + if errors.Is(err, domain.ErrNotFound) { + return nil, &domain.UnauthorizedError{Reason: "неизвестный токен"} + } + return nil, err + } + + if !session.ExpiresAt.After(time.Now().UTC()) { + return nil, &domain.UnauthorizedError{Reason: "сессия истекла"} + } + + user, err := s.users.GetByID(ctx, session.UserID) + if err != nil { + if errors.Is(err, domain.ErrNotFound) { + return nil, &domain.UnauthorizedError{Reason: "пользователь сессии не найден"} + } + return nil, err + } + + return user, nil +} + +// Revoke удаляет сессию по токену. +func (s *SessionService) Revoke(ctx context.Context, token string) error { + if token == "" { + return nil + } + tokenHash := sha256.Sum256([]byte(token)) + return s.sessions.DeleteByTokenHash(ctx, tokenHash[:]) +} + +// generateToken возвращает 32 случайных байта в base64url без паддинга=. +func generateToken() (string, error) { + buf := make([]byte, 32) + if _, err := rand.Read(buf); err != nil { + return "", fmt.Errorf("генерация токена: %w", err) + } + return base64.RawURLEncoding.EncodeToString(buf), nil +} diff --git a/backend/internal/service/user.go b/backend/internal/service/user.go new file mode 100644 index 00000000..e7b75479 --- /dev/null +++ b/backend/internal/service/user.go @@ -0,0 +1,78 @@ +package service + +import ( + "context" + "regexp" + "strings" + "time" + "unicode/utf8" + + "github.com/google/uuid" + + "high-loud-apps-labs/internal/domain" +) + +var usernamePattern = regexp.MustCompile(`^[a-z0-9_]{3,32}$`) + +type UserService struct { + users domain.UserRepository + hasher PasswordHasher +} + +func NewUserService(users domain.UserRepository, hasher PasswordHasher) *UserService { + return &UserService{users: users, hasher: hasher} +} + +func (s *UserService) Create(ctx context.Context, username, displayName, password string) (*domain.User, error) { + if !usernamePattern.MatchString(username) { + return nil, &domain.ValidationError{ + Field: "username", + Message: "должен соответствовать ^[a-z0-9_]{3,32}$", + } + } + if strings.TrimSpace(displayName) == "" { + return nil, &domain.ValidationError{ + Field: "display_name", + Message: "не может состоять только из пробелов", + } + } + if utf8.RuneCountInString(displayName) > 100 { + return nil, &domain.ValidationError{ + Field: "display_name", + Message: "не более 100 символов", + } + } + + passwordRunes := utf8.RuneCountInString(password) + if passwordRunes < 12 || passwordRunes > 128 { + return nil, &domain.ValidationError{ + Field: "password", + Message: "длина 12–128 символов", + } + } + + hash, err := s.hasher.Hash(password) + if err != nil { + return nil, err + } + + u := &domain.User{ + ID: uuid.NewString(), + Username: username, + DisplayName: displayName, + PasswordHash: hash, + CreatedAt: time.Now().UTC(), + } + if err := s.users.Create(ctx, u); err != nil { + return nil, err + } + return u, nil +} + +func (s *UserService) GetByID(ctx context.Context, id string) (*domain.User, error) { + return s.users.GetByID(ctx, id) +} + +func (s *UserService) GetByUsername(ctx context.Context, username string) (*domain.User, error) { + return s.users.GetByUsername(ctx, username) +} diff --git a/backend/internal/storage/_legacy/memory/chat.go b/backend/internal/storage/_legacy/memory/chat.go new file mode 100644 index 00000000..a2e18d07 --- /dev/null +++ b/backend/internal/storage/_legacy/memory/chat.go @@ -0,0 +1,102 @@ +package memory + +import ( + "context" + "slices" + + "high-loud-apps-labs/internal/domain" +) + +func (r *ChatRepo) Create(ctx context.Context, chat *domain.Chat) error { + if err := ctx.Err(); err != nil { + return err + } + r.mu.Lock() + defer r.mu.Unlock() + + stored := *chat + stored.MemberIDs = append([]string(nil), chat.MemberIDs...) + r.chats[chat.ID] = &stored + return nil +} + +func (r *ChatRepo) GetByID(ctx context.Context, id string) (*domain.Chat, error) { + if err := ctx.Err(); err != nil { + return nil, err + } + r.mu.RLock() + defer r.mu.RUnlock() + + c, ok := r.chats[id] + if !ok { + return nil, &domain.NotFoundError{Entity: "chat", ID: id} + } + copy := *c + copy.MemberIDs = append([]string(nil), c.MemberIDs...) + return ©, nil +} + +func (r *ChatRepo) ListByMember(ctx context.Context, userID string, limit int, cursor *string) ([]domain.Chat, *string, error) { + if err := ctx.Err(); err != nil { + return nil, nil, err + } + r.mu.RLock() + defer r.mu.RUnlock() + + scope := "chats:" + userID + + all := make([]domain.Chat, 0) + for _, c := range r.chats { + for _, m := range c.MemberIDs { + if m == userID { + copy := *c + copy.MemberIDs = append([]string(nil), c.MemberIDs...) + all = append(all, copy) + break + } + } + } + + slices.SortFunc(all, func(a, b domain.Chat) int { + if lessThan(a.CreatedAt, a.ID, b.CreatedAt, b.ID) { + return -1 + } + if lessThan(b.CreatedAt, b.ID, a.CreatedAt, a.ID) { + return 1 + } + return 0 + }) + + start := 0 + if cursor != nil { + t, id, err := decodeCursor(*cursor, scope) + if err != nil { + return nil, nil, err + } + start = len(all) + for i, c := range all { + if lessThan(t, id, c.CreatedAt, c.ID) { + start = i + break + } + } + } + + if start >= len(all) { + return []domain.Chat{}, nil, nil + } + end := start + limit + if end > len(all) { + end = len(all) + } + page := all[start:end] + + var next *string + if end < len(all) { + last := page[len(page)-1] + c := encodeCursor(scope, last.CreatedAt, last.ID) + next = &c + } + + return page, next, nil +} diff --git a/backend/internal/storage/_legacy/memory/cursor.go b/backend/internal/storage/_legacy/memory/cursor.go new file mode 100644 index 00000000..ef0ebac9 --- /dev/null +++ b/backend/internal/storage/_legacy/memory/cursor.go @@ -0,0 +1,47 @@ +package memory + +import ( + "encoding/base64" + "strings" + "time" + + "high-loud-apps-labs/internal/domain" +) + +func encodeCursor(scope string, createdAt time.Time, id string) string { + raw := scope + "|" + createdAt.UTC().Format(time.RFC3339Nano) + "|" + id + return base64.RawURLEncoding.EncodeToString([]byte(raw)) +} + +func decodeCursor(cursor, expectedScope string) (time.Time, string, error) { + data, err := base64.RawURLEncoding.DecodeString(cursor) + if err != nil { + return time.Time{}, "", badCursor() + } + parts := strings.SplitN(string(data), "|", 3) + if len(parts) != 3 { + return time.Time{}, "", badCursor() + } + if parts[0] != expectedScope { + return time.Time{}, "", badCursor() + } + t, err := time.Parse(time.RFC3339Nano, parts[1]) + if err != nil { + return time.Time{}, "", badCursor() + } + return t, parts[2], nil +} + +func badCursor() error { + return &domain.ValidationError{ + Field: "cursor", + Message: "некорректный формат", + } +} + +func lessThan(aTime time.Time, aID string, bTime time.Time, bID string) bool { + if aTime.Equal(bTime) { + return aID < bID + } + return aTime.Before(bTime) +} diff --git a/backend/internal/storage/_legacy/memory/message.go b/backend/internal/storage/_legacy/memory/message.go new file mode 100644 index 00000000..463b7578 --- /dev/null +++ b/backend/internal/storage/_legacy/memory/message.go @@ -0,0 +1,93 @@ +package memory + +import ( + "context" + "slices" + "time" + + "high-loud-apps-labs/internal/domain" +) + +func (r *MessageRepo) Create(ctx context.Context, message *domain.Message) error { + if err := ctx.Err(); err != nil { + return err + } + r.mu.Lock() + defer r.mu.Unlock() + + stored := *message + r.messages[message.ChatID] = append(r.messages[message.ChatID], stored) + return nil +} + +func (r *MessageRepo) ListByChat(ctx context.Context, chatID string, limit int, cursor *string) ([]domain.Message, *string, error) { + if err := ctx.Err(); err != nil { + return nil, nil, err + } + r.mu.RLock() + defer r.mu.RUnlock() + + scope := "messages:" + chatID + + var ( + afterTime time.Time + afterID string + hasCursor bool + ) + if cursor != nil { + t, id, err := decodeCursor(*cursor, scope) + if err != nil { + return nil, nil, err + } + afterTime = t + afterID = id + hasCursor = true + } + + src := r.messages[chatID] + if len(src) == 0 { + return []domain.Message{}, nil, nil + } + + all := make([]domain.Message, len(src)) + copy(all, src) + + slices.SortFunc(all, func(a, b domain.Message) int { + if lessThan(a.CreatedAt, a.ID, b.CreatedAt, b.ID) { + return -1 + } + if lessThan(b.CreatedAt, b.ID, a.CreatedAt, a.ID) { + return 1 + } + return 0 + }) + + start := 0 + if hasCursor { + start = len(all) + for i, m := range all { + if lessThan(afterTime, afterID, m.CreatedAt, m.ID) { + start = i + break + } + } + } + + if start >= len(all) { + return []domain.Message{}, nil, nil + } + end := start + limit + if end > len(all) { + end = len(all) + } + page := all[start:end] + + var next *string + if end < len(all) { + last := page[len(page)-1] + c := encodeCursor(scope, last.CreatedAt, last.ID) + next = &c + } + + return page, next, nil +} diff --git a/backend/internal/storage/_legacy/memory/storage.go b/backend/internal/storage/_legacy/memory/storage.go new file mode 100644 index 00000000..5b967732 --- /dev/null +++ b/backend/internal/storage/_legacy/memory/storage.go @@ -0,0 +1,34 @@ +package memory + +import ( + "sync" + + "high-loud-apps-labs/internal/domain" +) + +type Storage struct { + mu sync.RWMutex + users map[string]*domain.User + chats map[string]*domain.Chat + messages map[string][]domain.Message +} + +func New() *Storage { + return &Storage{ + users: make(map[string]*domain.User), + chats: make(map[string]*domain.Chat), + messages: make(map[string][]domain.Message), + } +} + +func (s *Storage) Users() *UserRepo { return &UserRepo{s} } +func (s *Storage) Chats() *ChatRepo { return &ChatRepo{s} } +func (s *Storage) Messages() *MessageRepo { return &MessageRepo{s} } + +type UserRepo struct{ *Storage } +type ChatRepo struct{ *Storage } +type MessageRepo struct{ *Storage } + +var _ domain.UserRepository = (*UserRepo)(nil) +var _ domain.ChatRepository = (*ChatRepo)(nil) +var _ domain.MessageRepository = (*MessageRepo)(nil) diff --git a/backend/internal/storage/_legacy/memory/user.go b/backend/internal/storage/_legacy/memory/user.go new file mode 100644 index 00000000..713162b5 --- /dev/null +++ b/backend/internal/storage/_legacy/memory/user.go @@ -0,0 +1,59 @@ +package memory + +import ( + "context" + + "high-loud-apps-labs/internal/domain" +) + +func (r *UserRepo) Create(ctx context.Context, user *domain.User) error { + if err := ctx.Err(); err != nil { + return err + } + r.mu.Lock() + defer r.mu.Unlock() + + for _, u := range r.users { + if u.Username == user.Username { + return &domain.AlreadyExistsError{ + Entity: "user", + Field: "username", + Value: user.Username, + } + } + } + stored := *user + r.users[user.ID] = &stored + return nil +} + +func (r *UserRepo) GetByID(ctx context.Context, id string) (*domain.User, error) { + if err := ctx.Err(); err != nil { + return nil, err + } + r.mu.RLock() + defer r.mu.RUnlock() + + u, ok := r.users[id] + if !ok { + return nil, &domain.NotFoundError{Entity: "user", ID: id} + } + copy := *u + return ©, nil +} + +func (r *UserRepo) GetByUsername(ctx context.Context, username string) (*domain.User, error) { + if err := ctx.Err(); err != nil { + return nil, err + } + r.mu.RLock() + defer r.mu.RUnlock() + + for _, u := range r.users { + if u.Username == username { + copy := *u + return ©, nil + } + } + return nil, &domain.NotFoundError{Entity: "user", ID: username} +} diff --git a/backend/internal/storage/postgres/attachments.go b/backend/internal/storage/postgres/attachments.go new file mode 100644 index 00000000..915b1f13 --- /dev/null +++ b/backend/internal/storage/postgres/attachments.go @@ -0,0 +1,200 @@ +package postgres + +import ( + "context" + + "github.com/jackc/pgx/v5/pgxpool" + + "high-loud-apps-labs/internal/domain" + "time" +) + +// pending -> ready (успешный upload) +// pending -> failed (отказ upload или reconciler) +type AttachmentRepo struct { + pool *pgxpool.Pool +} + +func NewAttachmentRepo(pool *pgxpool.Pool) *AttachmentRepo { + return &AttachmentRepo{pool: pool} +} + +func (r *AttachmentRepo) Create(ctx context.Context, a *domain.Attachment) error { + const q = ` + INSERT INTO attachments + (id, chat_id, owner_id, filename, content_type, size_bytes, sha256, + object_key, status, error_code, created_at, updated_at) + VALUES ($1::uuid, $2::uuid, $3::uuid, $4, $5, $6, $7, $8, $9, NULL, $10, $10)` + _, err := r.pool.Exec(ctx, q, + a.ID, a.ChatID, a.OwnerID, a.Filename, a.ContentType, a.SizeBytes, + a.SHA256, a.ObjectKey, string(a.Status), a.CreatedAt, + ) + if err != nil { + if uniqueViolation(err) { + return &domain.AlreadyExistsError{ + Entity: "attachment", + Field: "object_key", + Value: a.ObjectKey, + } + } + if foreignKeyViolation(err) { + return &domain.NotFoundError{Entity: "chat", ID: a.ChatID} + } + if checkViolation(err) { + return &domain.ValidationError{ + Field: "attachment", + Message: "нарушено ограничение целостности", + } + } + return mapError(err) + } + return nil +} + +func (r *AttachmentRepo) GetByID(ctx context.Context, id string) (*domain.Attachment, error) { + const q = ` + SELECT id, chat_id, owner_id, filename, content_type, size_bytes, sha256, + object_key, status, error_code, created_at, updated_at + FROM attachments + WHERE id = $1::uuid + ` + a := &domain.Attachment{} + var status string + err := r.pool.QueryRow(ctx, q, id). + Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.ContentType, + &a.SizeBytes, &a.SHA256, &a.ObjectKey, &status, &a.ErrorCode, + &a.CreatedAt, &a.UpdatedAt) + if err != nil { + return nil, notFound(err, "attachment", id) + } + a.Status = domain.AttachmentStatus(status) + return a, nil +} + +// MarkReady переводит pending -> ready. Идемпотентна. +func (r *AttachmentRepo) MarkReady(ctx context.Context, id string) error { + const q = ` + UPDATE attachments + SET status = 'ready', updated_at = now() + WHERE id = $1::uuid AND status = 'pending' + ` + tag, err := r.pool.Exec(ctx, q, id) + if err != nil { + return mapError(err) + } + if tag.RowsAffected() > 0 { + return nil + } + + var status string + err = r.pool.QueryRow(ctx, `SELECT status FROM attachments WHERE id = $1::uuid`, id). + Scan(&status) + if err != nil { + return notFound(err, "attachment", id) + } + switch domain.AttachmentStatus(status) { + case domain.AttachmentStatusReady: + return nil // idempotent no-op + case domain.AttachmentStatusFailed: + return &domain.ConflictError{Reason: "нельзя перевести failed-вложение в ready"} + default: + return &domain.ConflictError{Reason: "некорректное состояние вложения"} + } +} + +// MarkFailed переводит pending -> failed. Идемпотентна. +func (r *AttachmentRepo) MarkFailed(ctx context.Context, id, errorCode string) error { + if errorCode == "" { + return &domain.ValidationError{Field: "error_code", Message: "не может быть пустым"} + } + const q = ` + UPDATE attachments + SET status = 'failed', error_code = $2, updated_at = now() + WHERE id = $1::uuid AND status = 'pending' + ` + tag, err := r.pool.Exec(ctx, q, id, errorCode) + if err != nil { + return mapError(err) + } + if tag.RowsAffected() > 0 { + return nil + } + + var status string + err = r.pool.QueryRow(ctx, `SELECT status FROM attachments WHERE id = $1::uuid`, id). + Scan(&status) + if err != nil { + return notFound(err, "attachment", id) + } + switch domain.AttachmentStatus(status) { + case domain.AttachmentStatusFailed, domain.AttachmentStatusReady: + return nil + default: + return &domain.ConflictError{Reason: "некорректное состояние вложения"} + } +} + +func (r *AttachmentRepo) ListStalePending(ctx context.Context, olderThan time.Time, limit int) ([]domain.Attachment, error) { + if limit <= 0 { + return []domain.Attachment{}, nil + } + const q = ` + SELECT id, chat_id, owner_id, filename, content_type, size_bytes, sha256, + object_key, status, error_code, created_at, updated_at + FROM attachments + WHERE status = 'pending' AND created_at < $1 + ORDER BY created_at + LIMIT $2 + ` + rows, err := r.pool.Query(ctx, q, olderThan, limit) + if err != nil { + return nil, mapError(err) + } + defer rows.Close() + + out := make([]domain.Attachment, 0, limit) + for rows.Next() { + var a domain.Attachment + var status string + if err := rows.Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, + &a.ContentType, &a.SizeBytes, &a.SHA256, &a.ObjectKey, + &status, &a.ErrorCode, &a.CreatedAt, &a.UpdatedAt); err != nil { + return nil, mapError(err) + } + a.Status = domain.AttachmentStatus(status) + out = append(out, a) + } + return out, mapError(rows.Err()) +} + +func (r *AttachmentRepo) DeletePending(ctx context.Context, id string) error { + const q = `DELETE FROM attachments WHERE id = $1::uuid AND status = 'pending'` + if _, err := r.pool.Exec(ctx, q, id); err != nil { + return mapError(err) + } + return nil +} + +func (r *AttachmentRepo) ExistingKeys(ctx context.Context, keys []string) ([]string, error) { + if len(keys) == 0 { + return []string{}, nil + } + const q = `SELECT object_key FROM attachments WHERE object_key = ANY($1::text[])` + rows, err := r.pool.Query(ctx, q, keys) + if err != nil { + return nil, mapError(err) + } + defer rows.Close() + + out := make([]string, 0, len(keys)) + for rows.Next() { + var k string + if err := rows.Scan(&k); err != nil { + return nil, mapError(err) + } + out = append(out, k) + } + return out, mapError(rows.Err()) +} + +var _ domain.AttachmentRepository = (*AttachmentRepo)(nil) diff --git a/backend/internal/storage/postgres/chat.go b/backend/internal/storage/postgres/chat.go new file mode 100644 index 00000000..8afc6f29 --- /dev/null +++ b/backend/internal/storage/postgres/chat.go @@ -0,0 +1,195 @@ +package postgres + +import ( + "context" + "time" + + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgxpool" + + "high-loud-apps-labs/internal/domain" +) + +type ChatRepo struct { + pool *pgxpool.Pool +} + +func NewChatRepo(pool *pgxpool.Pool) *ChatRepo { + return &ChatRepo{pool: pool} +} + +// Create атомарно создаёт чат и его участников. +func (r *ChatRepo) Create(ctx context.Context, chat *domain.Chat) error { + tx, err := r.pool.Begin(ctx) + if err != nil { + return mapError(err) + } + defer func() { _ = tx.Rollback(ctx) }() + + const qChat = `INSERT INTO chats (id, title, created_at) VALUES ($1::uuid, $2, $3)` + if _, err := tx.Exec(ctx, qChat, chat.ID, chat.Title, chat.CreatedAt); err != nil { + if checkViolation(err) { + return &domain.ValidationError{Field: "chat", Message: "нарушено ограничение целостности"} + } + return mapError(err) + } + + const qMember = `INSERT INTO chat_members (chat_id, user_id) VALUES ($1::uuid, $2::uuid)` + for _, uid := range chat.MemberIDs { + if _, err := tx.Exec(ctx, qMember, chat.ID, uid); err != nil { + if foreignKeyViolation(err) { + return &domain.ValidationError{ + Field: "member_ids", + Message: "неизвестный участник " + uid, + } + } + if uniqueViolation(err) { + return &domain.ValidationError{ + Field: "member_ids", + Message: "дубликаты запрещены", + } + } + return mapError(err) + } + } + + return mapError(tx.Commit(ctx)) +} + +func (r *ChatRepo) GetByID(ctx context.Context, id string) (*domain.Chat, error) { + const q = `SELECT id, title, created_at FROM chats WHERE id = $1::uuid` + c := &domain.Chat{} + err := r.pool.QueryRow(ctx, q, id).Scan(&c.ID, &c.Title, &c.CreatedAt) + if err != nil { + return nil, notFound(err, "chat", id) + } + members, err := r.loadMembers(ctx, []string{c.ID}) + if err != nil { + return nil, err + } + c.MemberIDs = members[c.ID] + if c.MemberIDs == nil { + c.MemberIDs = []string{} + } + return c, nil +} + +func (r *ChatRepo) ListByMember(ctx context.Context, userID string, limit int, cursor *string) ([]domain.Chat, *string, error) { + scope := "chats:" + userID + + var ( + afterTime time.Time + afterID string + hasCursor bool + ) + if cursor != nil { + t, id, err := decodeCursor(*cursor, scope) + if err != nil { + return nil, nil, err + } + afterTime = t + afterID = id + hasCursor = true + } + + var ( + rows pgx.Rows + err error + ) + if hasCursor { + const q = ` + SELECT c.id, c.title, c.created_at + FROM chats c + JOIN chat_members m ON m.chat_id = c.id + WHERE m.user_id = $1::uuid + AND (c.created_at, c.id) > ($2, $3::uuid) + ORDER BY c.created_at, c.id + LIMIT $4 + ` + rows, err = r.pool.Query(ctx, q, userID, afterTime, afterID, limit+1) + } else { + const q = ` + SELECT c.id, c.title, c.created_at + FROM chats c + JOIN chat_members m ON m.chat_id = c.id + WHERE m.user_id = $1::uuid + ORDER BY c.created_at, c.id + LIMIT $2 + ` + rows, err = r.pool.Query(ctx, q, userID, limit+1) + } + if err != nil { + return nil, nil, mapError(err) + } + defer rows.Close() + + page := make([]domain.Chat, 0, limit) + for rows.Next() { + var c domain.Chat + if err := rows.Scan(&c.ID, &c.Title, &c.CreatedAt); err != nil { + return nil, nil, mapError(err) + } + page = append(page, c) + } + if err := rows.Err(); err != nil { + return nil, nil, mapError(err) + } + + var next *string + if len(page) > limit { + page = page[:limit] + last := page[len(page)-1] + c := encodeCursor(scope, last.CreatedAt, last.ID) + next = &c + } + + if len(page) > 0 { + ids := make([]string, len(page)) + for i := range page { + ids[i] = page[i].ID + } + members, err := r.loadMembers(ctx, ids) + if err != nil { + return nil, nil, err + } + for i := range page { + if m, ok := members[page[i].ID]; ok { + page[i].MemberIDs = m + } else { + page[i].MemberIDs = []string{} + } + } + } + + return page, next, nil +} + +// loadMembers одним запросом собирает участников для указанных чатов. +func (r *ChatRepo) loadMembers(ctx context.Context, chatIDs []string) (map[string][]string, error) { + if len(chatIDs) == 0 { + return map[string][]string{}, nil + } + const q = ` + SELECT chat_id, user_id + FROM chat_members + WHERE chat_id = ANY($1::uuid[]) + ORDER BY chat_id, user_id + ` + rows, err := r.pool.Query(ctx, q, chatIDs) + if err != nil { + return nil, mapError(err) + } + defer rows.Close() + + result := make(map[string][]string, len(chatIDs)) + for rows.Next() { + var chatID, userID string + if err := rows.Scan(&chatID, &userID); err != nil { + return nil, mapError(err) + } + result[chatID] = append(result[chatID], userID) + } + return result, mapError(rows.Err()) +} + +var _ domain.ChatRepository = (*ChatRepo)(nil) diff --git a/backend/internal/storage/postgres/cursor.go b/backend/internal/storage/postgres/cursor.go new file mode 100644 index 00000000..044ffeea --- /dev/null +++ b/backend/internal/storage/postgres/cursor.go @@ -0,0 +1,46 @@ +package postgres + +import ( + "encoding/base64" + "strings" + "time" + + "high-loud-apps-labs/internal/domain" + + "github.com/google/uuid" +) + +// Взял за основу что в inmemery делал +func encodeCursor(scope string, createdAt time.Time, id string) string { + raw := scope + "|" + createdAt.UTC().Format(time.RFC3339Nano) + "|" + id + return base64.RawURLEncoding.EncodeToString([]byte(raw)) +} + +func decodeCursor(cursor, expectedScope string) (time.Time, string, error) { + data, err := base64.RawURLEncoding.DecodeString(cursor) + if err != nil { + return time.Time{}, "", badCursor() + } + parts := strings.SplitN(string(data), "|", 3) + if len(parts) != 3 { + return time.Time{}, "", badCursor() + } + if parts[0] != expectedScope { + return time.Time{}, "", badCursor() + } + t, err := time.Parse(time.RFC3339Nano, parts[1]) + if err != nil { + return time.Time{}, "", badCursor() + } + if _, err := uuid.Parse(parts[2]); err != nil { + return time.Time{}, "", badCursor() + } + return t, parts[2], nil +} + +func badCursor() error { + return &domain.ValidationError{ + Field: "cursor", + Message: "некорректный формат", + } +} diff --git a/backend/internal/storage/postgres/db.go b/backend/internal/storage/postgres/db.go new file mode 100644 index 00000000..ad27e83e --- /dev/null +++ b/backend/internal/storage/postgres/db.go @@ -0,0 +1,75 @@ +package postgres + +import ( + "context" + "fmt" + "time" + + "github.com/jackc/pgx/v5/pgxpool" + "github.com/jackc/pgx/v5/stdlib" + "github.com/pressly/goose/v3" + + "high-loud-apps-labs/migrations" +) + +type Config struct { + URL string + MaxConns int32 + MinConns int32 + MaxConnLifetime time.Duration + MaxConnIdleTime time.Duration + ConnectTimeout time.Duration +} + +func Connect(ctx context.Context, cfg Config) (*pgxpool.Pool, error) { + if cfg.URL == "" { + return nil, fmt.Errorf("DATABASE_URL пустой") + } + + poolCfg, err := pgxpool.ParseConfig(cfg.URL) + if err != nil { + return nil, fmt.Errorf("разбор DATABASE_URL: %w", err) + } + if cfg.MaxConns > 0 { + poolCfg.MaxConns = cfg.MaxConns + } + if cfg.MinConns > 0 { + poolCfg.MinConns = cfg.MinConns + } + if cfg.MaxConnLifetime > 0 { + poolCfg.MaxConnLifetime = cfg.MaxConnLifetime + } + if cfg.MaxConnIdleTime > 0 { + poolCfg.MaxConnIdleTime = cfg.MaxConnIdleTime + } + if cfg.ConnectTimeout > 0 { + poolCfg.ConnConfig.ConnectTimeout = cfg.ConnectTimeout + } + poolCfg.HealthCheckPeriod = 10 * time.Second + + pool, err := pgxpool.NewWithConfig(ctx, poolCfg) + if err != nil { + return nil, fmt.Errorf("создание пула: %w", err) + } + + pingCtx, cancel := context.WithTimeout(ctx, 5*time.Second) + defer cancel() + if err := pool.Ping(pingCtx); err != nil { + pool.Close() + return nil, fmt.Errorf("ping БД: %w", err) + } + return pool, nil +} + +func RunMigrations(ctx context.Context, pool *pgxpool.Pool) error { + db := stdlib.OpenDBFromPool(pool) + + goose.SetBaseFS(migrations.FS) + if err := goose.SetDialect("postgres"); err != nil { + return fmt.Errorf("goose dialect: %w", err) + } + if err := goose.UpContext(ctx, db, "."); err != nil { + return fmt.Errorf("применение миграций: %w", err) + } + return nil +} diff --git a/backend/internal/storage/postgres/errors.go b/backend/internal/storage/postgres/errors.go new file mode 100644 index 00000000..63861286 --- /dev/null +++ b/backend/internal/storage/postgres/errors.go @@ -0,0 +1,71 @@ +package postgres + +import ( + "context" + "errors" + + "github.com/jackc/pgerrcode" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + + "high-loud-apps-labs/internal/domain" +) + +type coded interface { + Code() domain.ErrorCode +} + +// mapError переводит ошибки драйвера в доменные. +func mapError(err error) error { + if err == nil { + return nil + } + var c coded + if errors.As(err, &c) { + return err + } + if errors.Is(err, pgx.ErrNoRows) { + return err + } + if errors.Is(err, context.DeadlineExceeded) { + return &domain.UnavailableError{Reason: "превышено время ожидания БД"} + } + if errors.Is(err, context.Canceled) { + return &domain.UnavailableError{Reason: "запрос отменён"} + } + return &domain.UnavailableError{Reason: err.Error()} +} + +func notFound(err error, entity, id string) error { + if err == nil { + return nil + } + if errors.Is(err, pgx.ErrNoRows) { + return &domain.NotFoundError{Entity: entity, ID: id} + } + return mapError(err) +} + +func uniqueViolation(err error) bool { + var pgErr *pgconn.PgError + if errors.As(err, &pgErr) { + return pgErr.Code == pgerrcode.UniqueViolation + } + return false +} + +func foreignKeyViolation(err error) bool { + var pgErr *pgconn.PgError + if errors.As(err, &pgErr) { + return pgErr.Code == pgerrcode.ForeignKeyViolation + } + return false +} + +func checkViolation(err error) bool { + var pgErr *pgconn.PgError + if errors.As(err, &pgErr) { + return pgErr.Code == pgerrcode.CheckViolation + } + return false +} diff --git a/backend/internal/storage/postgres/message.go b/backend/internal/storage/postgres/message.go new file mode 100644 index 00000000..67df6cd0 --- /dev/null +++ b/backend/internal/storage/postgres/message.go @@ -0,0 +1,321 @@ +package postgres + +import ( + "bytes" + "context" + "net/http" + "time" + + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgxpool" + + "high-loud-apps-labs/internal/domain" +) + +type MessageRepo struct { + pool *pgxpool.Pool +} + +func NewMessageRepo(pool *pgxpool.Pool) *MessageRepo { + return &MessageRepo{pool: pool} +} + +func (r *MessageRepo) Create(ctx context.Context, m *domain.Message) error { + const q = ` + INSERT INTO messages (id, chat_id, sender_id, text, created_at) + VALUES ($1::uuid, $2::uuid, $3::uuid, $4, $5) + ` + _, err := r.pool.Exec(ctx, q, m.ID, m.ChatID, m.SenderID, m.Text, m.CreatedAt) + if err != nil { + if foreignKeyViolation(err) { + return &domain.NotFoundError{Entity: "chat", ID: m.ChatID} + } + if checkViolation(err) { + return &domain.ValidationError{Field: "text", Message: "нарушено ограничение"} + } + return mapError(err) + } + return nil +} + +func (r *MessageRepo) CreateIdempotent( + ctx context.Context, + scope domain.IdempotencyScope, + requestHash []byte, + fresh *domain.Message, + freshResponse []byte, + ttl time.Duration, +) (*domain.CreateIdempotentResult, error) { + tx, err := r.pool.Begin(ctx) + if err != nil { + return nil, mapError(err) + } + defer func() { _ = tx.Rollback(ctx) }() + + // 1) Убираем просроченную запись, чтобы ON CONFLICT сработал + // только на живой, а не на истёкшей строке. + if _, err := tx.Exec(ctx, ` + DELETE FROM idempotency_keys + WHERE user_id = $1::uuid AND method = $2 AND path = $3 AND key = $4 + AND expires_at <= now() + `, scope.UserID, scope.Method, scope.Path, scope.Key); err != nil { + return nil, mapError(err) + } + + // 2) Резервируем слот. Если параллельная реплика уже вставила — + // Postgres заблокирует конфликтный INSERT и мы увидим RowsAffected=0 + // только после того, как конкурент закоммитит или откатится. + expiresAt := time.Now().UTC().Add(ttl) + tag, err := tx.Exec(ctx, ` + INSERT INTO idempotency_keys + (user_id, method, path, key, request_hash, response_status, response_body, expires_at) + VALUES ($1::uuid, $2, $3, $4, $5, $6, $7, $8) + ON CONFLICT (user_id, method, path, key) DO NOTHING + `, scope.UserID, scope.Method, scope.Path, scope.Key, + requestHash, http.StatusCreated, freshResponse, expiresAt) + if err != nil { + return nil, mapError(err) + } + + // 3) Слот занят — читаем сохранённый ответ в этой же транзакции. + if tag.RowsAffected() == 0 { + var ( + storedHash []byte + storedStatus int + storedBody []byte + ) + err := tx.QueryRow(ctx, ` + SELECT request_hash, response_status, response_body + FROM idempotency_keys + WHERE user_id = $1::uuid AND method = $2 AND path = $3 AND key = $4 + `, scope.UserID, scope.Method, scope.Path, scope.Key). + Scan(&storedHash, &storedStatus, &storedBody) + if err != nil { + return nil, mapError(err) + } + + if !bytes.Equal(storedHash, requestHash) { + return nil, &domain.IdempotencyConflictError{Key: scope.Key} + } + + if err := tx.Commit(ctx); err != nil { + return nil, mapError(err) + } + return &domain.CreateIdempotentResult{ + Status: storedStatus, + Body: storedBody, + Replayed: true, + }, nil + } + + // 4) Слот наш — валидируем вложения, вставляем сообщение, линкуем + // вложения. Всё в одной транзакции: при ошибке на любом шаге + // откатывается и idempotency-ключ, и сообщение, и связи. + if err := r.validateAttachments(ctx, tx, fresh.ChatID, fresh.AttachmentIDs); err != nil { + return nil, err + } + + if _, err := tx.Exec(ctx, ` + INSERT INTO messages (id, chat_id, sender_id, text, created_at) + VALUES ($1::uuid, $2::uuid, $3::uuid, $4, $5) + `, fresh.ID, fresh.ChatID, fresh.SenderID, fresh.Text, fresh.CreatedAt); err != nil { + if foreignKeyViolation(err) { + return nil, &domain.NotFoundError{Entity: "chat", ID: fresh.ChatID} + } + if checkViolation(err) { + return nil, &domain.ValidationError{Field: "text", Message: "нарушено ограничение"} + } + return nil, mapError(err) + } + + if err := r.linkAttachments(ctx, tx, fresh.ID, fresh.AttachmentIDs); err != nil { + return nil, err + } + + if err := tx.Commit(ctx); err != nil { + return nil, mapError(err) + } + return &domain.CreateIdempotentResult{ + Status: http.StatusCreated, + Body: freshResponse, + Replayed: false, + }, nil +} + +func (r *MessageRepo) ListByChat(ctx context.Context, chatID string, limit int, cursor *string) ([]domain.Message, *string, error) { + scope := "messages:" + chatID + + var ( + afterTime time.Time + afterID string + hasCursor bool + ) + if cursor != nil { + t, id, err := decodeCursor(*cursor, scope) + if err != nil { + return nil, nil, err + } + afterTime = t + afterID = id + hasCursor = true + } + + var ( + rows pgx.Rows + err error + ) + if hasCursor { + const q = ` + SELECT id, chat_id, sender_id, text, created_at + FROM messages + WHERE chat_id = $1::uuid + AND (created_at, id) > ($2, $3::uuid) + ORDER BY created_at, id + LIMIT $4 + ` + rows, err = r.pool.Query(ctx, q, chatID, afterTime, afterID, limit+1) + } else { + const q = ` + SELECT id, chat_id, sender_id, text, created_at + FROM messages + WHERE chat_id = $1::uuid + ORDER BY created_at, id + LIMIT $2 + ` + rows, err = r.pool.Query(ctx, q, chatID, limit+1) + } + if err != nil { + return nil, nil, err + } + defer rows.Close() + + page := make([]domain.Message, 0, limit) + for rows.Next() { + var m domain.Message + if err := rows.Scan(&m.ID, &m.ChatID, &m.SenderID, &m.Text, &m.CreatedAt); err != nil { + return nil, nil, err + } + page = append(page, m) + } + if err := rows.Err(); err != nil { + return nil, nil, err + } + + var next *string + if len(page) > limit { + page = page[:limit] + last := page[len(page)-1] + c := encodeCursor(scope, last.CreatedAt, last.ID) + next = &c + } + + if len(page) > 0 { + msgIDs := make([]string, len(page)) + for i := range page { + msgIDs[i] = page[i].ID + } + links, err := r.loadAttachmentIDs(ctx, msgIDs) + if err != nil { + return nil, nil, err + } + for i := range page { + if ids, ok := links[page[i].ID]; ok { + page[i].AttachmentIDs = ids + } else { + page[i].AttachmentIDs = []string{} + } + } + } + + return page, next, nil +} + +func (r *MessageRepo) validateAttachments(ctx context.Context, tx pgx.Tx, chatID string, ids []string) error { + if len(ids) == 0 { + return nil + } + const q = `SELECT id, chat_id, status FROM attachments WHERE id = ANY($1::uuid[])` + rows, err := tx.Query(ctx, q, ids) + if err != nil { + return mapError(err) + } + defer rows.Close() + + type info struct { + chatID string + status domain.AttachmentStatus + } + found := make(map[string]info, len(ids)) + for rows.Next() { + var id, chID, st string + if err := rows.Scan(&id, &chID, &st); err != nil { + return mapError(err) + } + found[id] = info{chatID: chID, status: domain.AttachmentStatus(st)} + } + if err := rows.Err(); err != nil { + return mapError(err) + } + + for _, id := range ids { + a, ok := found[id] + if !ok || a.chatID != chatID { + return &domain.NotFoundError{Entity: "attachment", ID: id} + } + if a.status != domain.AttachmentStatusReady { + return &domain.ConflictError{Reason: "вложение " + id + " не готово"} + } + } + return nil +} + +func (r *MessageRepo) linkAttachments(ctx context.Context, tx pgx.Tx, messageID string, ids []string) error { + if len(ids) == 0 { + return nil + } + const q = `INSERT INTO message_attachments (message_id, attachment_id, position) VALUES ($1::uuid, $2::uuid, $3)` + for i, id := range ids { + if _, err := tx.Exec(ctx, q, messageID, id, int16(i)); err != nil { + if foreignKeyViolation(err) { + return &domain.NotFoundError{Entity: "attachment", ID: id} + } + if uniqueViolation(err) { + return &domain.ValidationError{Field: "attachment_ids", Message: "дубликаты запрещены"} + } + if checkViolation(err) { + return &domain.ValidationError{Field: "attachment_ids", Message: "нарушено ограничение"} + } + return mapError(err) + } + } + return nil +} + +func (r *MessageRepo) loadAttachmentIDs(ctx context.Context, messageIDs []string) (map[string][]string, error) { + if len(messageIDs) == 0 { + return map[string][]string{}, nil + } + const q = ` + SELECT message_id, attachment_id + FROM message_attachments + WHERE message_id = ANY($1::uuid[]) + ORDER BY message_id, position + ` + rows, err := r.pool.Query(ctx, q, messageIDs) + if err != nil { + return nil, mapError(err) + } + defer rows.Close() + + result := make(map[string][]string, len(messageIDs)) + for rows.Next() { + var msgID, attID string + if err := rows.Scan(&msgID, &attID); err != nil { + return nil, mapError(err) + } + result[msgID] = append(result[msgID], attID) + } + return result, mapError(rows.Err()) +} + +var _ domain.MessageRepository = (*MessageRepo)(nil) diff --git a/backend/internal/storage/postgres/session.go b/backend/internal/storage/postgres/session.go new file mode 100644 index 00000000..430a9e6f --- /dev/null +++ b/backend/internal/storage/postgres/session.go @@ -0,0 +1,59 @@ +package postgres + +import ( + "context" + + "github.com/jackc/pgx/v5/pgxpool" + + "high-loud-apps-labs/internal/domain" +) + +type SessionRepo struct { + pool *pgxpool.Pool +} + +func NewSessionRepo(pool *pgxpool.Pool) *SessionRepo { + return &SessionRepo{pool: pool} +} + +func (r *SessionRepo) Create(ctx context.Context, s *domain.Session) error { + const q = ` + INSERT INTO sessions (token_hash, user_id, expires_at, created_at) + VALUES ($1, $2::uuid, $3, $4) + ` + _, err := r.pool.Exec(ctx, q, s.TokenHash, s.UserID, s.ExpiresAt, s.CreatedAt) + if err != nil { + if uniqueViolation(err) { + return &domain.AlreadyExistsError{ + Entity: "session", + Field: "token_hash", + Value: "", + } + } + return mapError(err) + } + return nil +} + +func (r *SessionRepo) GetByTokenHash(ctx context.Context, tokenHash []byte) (*domain.Session, error) { + const q = ` + SELECT token_hash, user_id, expires_at, created_at + FROM sessions + WHERE token_hash = $1 + ` + s := &domain.Session{} + err := r.pool.QueryRow(ctx, q, tokenHash). + Scan(&s.TokenHash, &s.UserID, &s.ExpiresAt, &s.CreatedAt) + if err != nil { + return nil, notFound(err, "session", "") + } + return s, nil +} + +func (r *SessionRepo) DeleteByTokenHash(ctx context.Context, tokenHash []byte) error { + const q = `DELETE FROM sessions WHERE token_hash = $1` + _, err := r.pool.Exec(ctx, q, tokenHash) + return mapError(err) +} + +var _ domain.SessionRepository = (*SessionRepo)(nil) diff --git a/backend/internal/storage/postgres/storage.go b/backend/internal/storage/postgres/storage.go new file mode 100644 index 00000000..07121091 --- /dev/null +++ b/backend/internal/storage/postgres/storage.go @@ -0,0 +1,19 @@ +package postgres + +import "github.com/jackc/pgx/v5/pgxpool" + +type Storage struct { + pool *pgxpool.Pool +} + +func New(pool *pgxpool.Pool) *Storage { + return &Storage{pool: pool} +} + +func (s *Storage) Pool() *pgxpool.Pool { return s.pool } + +func (s *Storage) Users() *UserRepo { return &UserRepo{pool: s.pool} } +func (s *Storage) Chats() *ChatRepo { return &ChatRepo{pool: s.pool} } +func (s *Storage) Messages() *MessageRepo { return &MessageRepo{pool: s.pool} } +func (s *Storage) Sessions() *SessionRepo { return &SessionRepo{pool: s.pool} } +func (s *Storage) Attachments() *AttachmentRepo { return &AttachmentRepo{pool: s.pool} } diff --git a/backend/internal/storage/postgres/user.go b/backend/internal/storage/postgres/user.go new file mode 100644 index 00000000..418f35a6 --- /dev/null +++ b/backend/internal/storage/postgres/user.go @@ -0,0 +1,76 @@ +package postgres + +import ( + "context" + + "github.com/jackc/pgx/v5/pgxpool" + + "high-loud-apps-labs/internal/domain" +) + +type UserRepo struct { + pool *pgxpool.Pool +} + +func NewUserRepo(pool *pgxpool.Pool) *UserRepo { + return &UserRepo{pool: pool} +} + +func (r *UserRepo) Create(ctx context.Context, user *domain.User) error { + const q = ` + INSERT INTO users (id, username, display_name, password_hash, created_at) + VALUES ($1::uuid, $2, $3, $4, $5) + ` + _, err := r.pool.Exec(ctx, q, + user.ID, user.Username, user.DisplayName, user.PasswordHash, user.CreatedAt, + ) + if err != nil { + if uniqueViolation(err) { + return &domain.AlreadyExistsError{ + Entity: "user", + Field: "username", + Value: user.Username, + } + } + if checkViolation(err) { + return &domain.ValidationError{ + Field: "user", + Message: "нарушено ограничение целостности", + } + } + return mapError(err) + } + return nil +} + +func (r *UserRepo) GetByID(ctx context.Context, id string) (*domain.User, error) { + const q = ` + SELECT id, username, display_name, password_hash, created_at + FROM users + WHERE id = $1::uuid + ` + u := &domain.User{} + err := r.pool.QueryRow(ctx, q, id). + Scan(&u.ID, &u.Username, &u.DisplayName, &u.PasswordHash, &u.CreatedAt) + if err != nil { + return nil, notFound(err, "user", id) + } + return u, nil +} + +func (r *UserRepo) GetByUsername(ctx context.Context, username string) (*domain.User, error) { + const q = ` + SELECT id, username, display_name, password_hash, created_at + FROM users + WHERE username = $1 + ` + u := &domain.User{} + err := r.pool.QueryRow(ctx, q, username). + Scan(&u.ID, &u.Username, &u.DisplayName, &u.PasswordHash, &u.CreatedAt) + if err != nil { + return nil, notFound(err, "user", username) + } + return u, nil +} + +var _ domain.UserRepository = (*UserRepo)(nil) diff --git a/backend/internal/storage/redis/client.go b/backend/internal/storage/redis/client.go new file mode 100644 index 00000000..0da66143 --- /dev/null +++ b/backend/internal/storage/redis/client.go @@ -0,0 +1,33 @@ +package redis + +import ( + "context" + "fmt" + "time" + + "github.com/redis/go-redis/v9" +) + +func NewClient(url string) (*redis.Client, error) { + opt, err := redis.ParseURL(url) + if err != nil { + return nil, fmt.Errorf("разбор URL: %w", err) + } + + opt.DialTimeout = 700 * time.Millisecond + opt.ReadTimeout = 700 * time.Millisecond + opt.WriteTimeout = 700 * time.Millisecond + opt.MaxRetries = 1 + opt.PoolSize = 10 + opt.ConnMaxIdleTime = time.Minute + + client := redis.NewClient(opt) + + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + if err := client.Ping(ctx).Err(); err != nil { + _ = client.Close() + return nil, fmt.Errorf("ping: %w", err) + } + return client, nil +} diff --git a/backend/internal/storage/redis/errors.go b/backend/internal/storage/redis/errors.go new file mode 100644 index 00000000..55e34226 --- /dev/null +++ b/backend/internal/storage/redis/errors.go @@ -0,0 +1,31 @@ +package redis + +import ( + "context" + "errors" + + "high-loud-apps-labs/internal/domain" +) + +type coded interface { + Code() domain.ErrorCode +} + +// mapError переводит ошибки драйвера в доменные. +// Любая недоступность redis (dial/read/write/context) → UnavailableError (503). +func mapError(err error) error { + if err == nil { + return nil + } + var c coded + if errors.As(err, &c) { + return err + } + if errors.Is(err, context.DeadlineExceeded) { + return &domain.UnavailableError{Reason: "превышено время ожидания redis"} + } + if errors.Is(err, context.Canceled) { + return &domain.UnavailableError{Reason: "запрос отменён"} + } + return &domain.UnavailableError{Reason: err.Error()} +} diff --git a/backend/internal/storage/redis/session.go b/backend/internal/storage/redis/session.go new file mode 100644 index 00000000..0aecfd59 --- /dev/null +++ b/backend/internal/storage/redis/session.go @@ -0,0 +1,80 @@ +package redis + +import ( + "context" + "encoding/hex" + "encoding/json" + "errors" + "fmt" + "time" + + "github.com/redis/go-redis/v9" + + "high-loud-apps-labs/internal/domain" +) + +const sessionKeyPrefix = "session:" + +type SessionRepo struct { + client *redis.Client +} + +func NewSessionRepo(client *redis.Client) *SessionRepo { + return &SessionRepo{client: client} +} + +type sessionValue struct { + UserID string `json:"user_id"` + ExpiresAt time.Time `json:"expires_at"` + CreatedAt time.Time `json:"created_at"` +} + +func (r *SessionRepo) key(tokenHash []byte) string { + return sessionKeyPrefix + hex.EncodeToString(tokenHash) +} + +func (r *SessionRepo) Create(ctx context.Context, s *domain.Session) error { + ttl := time.Until(s.ExpiresAt) + if ttl <= 0 { + return nil + } + + data, err := json.Marshal(sessionValue{ + UserID: s.UserID, + ExpiresAt: s.ExpiresAt.UTC(), + CreatedAt: s.CreatedAt.UTC(), + }) + if err != nil { + return fmt.Errorf("сериализация сессии: %w", err) + } + + return mapError(r.client.Set(ctx, r.key(s.TokenHash), data, ttl).Err()) +} + +func (r *SessionRepo) GetByTokenHash(ctx context.Context, tokenHash []byte) (*domain.Session, error) { + data, err := r.client.Get(ctx, r.key(tokenHash)).Bytes() + if err != nil { + if errors.Is(err, redis.Nil) { + return nil, &domain.NotFoundError{Entity: "session"} + } + return nil, mapError(err) + } + + var sv sessionValue + if err := json.Unmarshal(data, &sv); err != nil { + return nil, fmt.Errorf("разбор сессии: %w", err) + } + + return &domain.Session{ + TokenHash: tokenHash, + UserID: sv.UserID, + ExpiresAt: sv.ExpiresAt, + CreatedAt: sv.CreatedAt, + }, nil +} + +func (r *SessionRepo) DeleteByTokenHash(ctx context.Context, tokenHash []byte) error { + return mapError(r.client.Del(ctx, r.key(tokenHash)).Err()) +} + +var _ domain.SessionRepository = (*SessionRepo)(nil) diff --git a/backend/internal/storage/s3/client.go b/backend/internal/storage/s3/client.go new file mode 100644 index 00000000..75cbf954 --- /dev/null +++ b/backend/internal/storage/s3/client.go @@ -0,0 +1,98 @@ +package s3 + +import ( + "context" + "io" + + "github.com/minio/minio-go/v7" + "github.com/minio/minio-go/v7/pkg/credentials" + + "high-loud-apps-labs/internal/domain" +) + +type Config struct { + Endpoint string + Bucket string + AccessKey string + SecretKey string + UseSSL bool + Region string +} + +type MinioStorage struct { + client *minio.Client + bucket string +} + +func New(ctx context.Context, cfg Config) (*MinioStorage, error) { + client, err := minio.New(cfg.Endpoint, &minio.Options{ + Creds: credentials.NewStaticV4(cfg.AccessKey, cfg.SecretKey, ""), + Secure: cfg.UseSSL, + Region: cfg.Region, + }) + if err != nil { + return nil, err + } + + exists, err := client.BucketExists(ctx, cfg.Bucket) + if err != nil { + return nil, mapError(err) + } + if !exists { + return nil, &domain.UnavailableError{Reason: "s3 bucket отсутствует"} + } + + return &MinioStorage{client: client, bucket: cfg.Bucket}, nil +} + +func (s *MinioStorage) Ping(ctx context.Context) error { + _, err := s.client.BucketExists(ctx, s.bucket) + return mapError(err) +} + +func (s *MinioStorage) Put(ctx context.Context, key string, r io.Reader, size int64, contentType string) error { + _, err := s.client.PutObject(ctx, s.bucket, key, r, size, minio.PutObjectOptions{ + ContentType: contentType, + }) + return mapError(err) +} + +func (s *MinioStorage) Get(ctx context.Context, key string) (io.ReadCloser, int64, string, error) { + obj, err := s.client.GetObject(ctx, s.bucket, key, minio.GetObjectOptions{}) + if err != nil { + return nil, 0, "", mapError(err) + } + + stat, err := obj.Stat() + if err != nil { + _ = obj.Close() + return nil, 0, "", mapError(err) + } + + return obj, stat.Size, stat.ContentType, nil +} + +func (s *MinioStorage) Delete(ctx context.Context, key string) error { + err := s.client.RemoveObject(ctx, s.bucket, key, minio.RemoveObjectOptions{}) + return mapError(err) +} + +func (s *MinioStorage) List(ctx context.Context, prefix string) ([]domain.ObjectInfo, error) { + out := make([]domain.ObjectInfo, 0) + for obj := range s.client.ListObjects(ctx, s.bucket, minio.ListObjectsOptions{ + Prefix: prefix, + Recursive: true, + }) { + if obj.Err != nil { + return nil, mapError(obj.Err) + } + out = append(out, domain.ObjectInfo{ + Key: obj.Key, + LastModified: obj.LastModified, + Size: obj.Size, + }) + } + return out, nil +} + +var _ domain.ObjectStorage = (*MinioStorage)(nil) diff --git a/backend/internal/storage/s3/errors.go b/backend/internal/storage/s3/errors.go new file mode 100644 index 00000000..8558a6d8 --- /dev/null +++ b/backend/internal/storage/s3/errors.go @@ -0,0 +1,42 @@ +package s3 + +import ( + "context" + "errors" + + "github.com/minio/minio-go/v7" + + "high-loud-apps-labs/internal/domain" +) + +type coded interface { + Code() domain.ErrorCode +} + +func mapError(err error) error { + if err == nil { + return nil + } + var c coded + if errors.As(err, &c) { + return err + } + var resp minio.ErrorResponse + if errors.As(err, &resp) { + switch resp.Code { + case "NoSuchKey", "NotFound": + return &domain.NotFoundError{Entity: "object"} + case "NoSuchBucket": + return &domain.UnavailableError{Reason: "s3 bucket отсутствует"} + case "AccessDenied", "InvalidAccessKeyId", "SignatureDoesNotMatch": + return &domain.UnavailableError{Reason: "s3 доступ запрещён"} + } + } + if errors.Is(err, context.DeadlineExceeded) { + return &domain.UnavailableError{Reason: "превышено время ожидания s3"} + } + if errors.Is(err, context.Canceled) { + return &domain.UnavailableError{Reason: "запрос отменён"} + } + return &domain.UnavailableError{Reason: err.Error()} +} diff --git a/backend/migrations/0001_init.sql b/backend/migrations/0001_init.sql new file mode 100644 index 00000000..d9fad408 --- /dev/null +++ b/backend/migrations/0001_init.sql @@ -0,0 +1,67 @@ +-- +goose Up +-- +goose StatementBegin +CREATE TABLE users ( + id UUID PRIMARY KEY, + username TEXT NOT NULL, + display_name TEXT NOT NULL, + password_hash TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + CONSTRAINT users_username_format + CHECK (username ~ '^[a-z0-9_]{3,32}$'), + CONSTRAINT users_display_name_not_blank + CHECK (display_name !~ '^[[:space:]]*$'), + CONSTRAINT users_display_name_len + CHECK (char_length(display_name) <= 100), + CONSTRAINT users_password_hash_not_blank + CHECK (length(password_hash) > 0) +); +CREATE UNIQUE INDEX users_username_uniq ON users (username); + +CREATE TABLE chats ( + id UUID PRIMARY KEY, + title TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + CONSTRAINT chats_title_not_blank CHECK (title !~ '^[[:space:]]*$'), + CONSTRAINT chats_title_len CHECK (char_length(title) <= 100) +); +CREATE INDEX chats_created_id_idx ON chats (created_at, id); + +CREATE TABLE chat_members ( + chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + user_id UUID NOT NULL REFERENCES users(id) ON DELETE RESTRICT, + joined_at TIMESTAMPTZ NOT NULL DEFAULT now(), + PRIMARY KEY (chat_id, user_id) +); +CREATE INDEX chat_members_user_idx ON chat_members (user_id, chat_id); + +CREATE TABLE messages ( + id UUID PRIMARY KEY, + chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + sender_id UUID NOT NULL REFERENCES users(id) ON DELETE RESTRICT, + text TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + CONSTRAINT messages_text_not_blank CHECK (text !~ '^[[:space:]]*$'), + CONSTRAINT messages_text_len CHECK (char_length(text) <= 4000) +); +CREATE INDEX messages_chat_created_id_idx + ON messages (chat_id, created_at, id); + +CREATE TABLE sessions ( + token_hash BYTEA PRIMARY KEY, + user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, + expires_at TIMESTAMPTZ NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + CONSTRAINT sessions_token_hash_len CHECK (octet_length(token_hash) = 32) +); +CREATE INDEX sessions_user_idx ON sessions (user_id); +CREATE INDEX sessions_expires_idx ON sessions (expires_at); +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin +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; +-- +goose StatementEnd \ No newline at end of file diff --git a/backend/migrations/0002_idempotency.sql b/backend/migrations/0002_idempotency.sql new file mode 100644 index 00000000..0ad719c9 --- /dev/null +++ b/backend/migrations/0002_idempotency.sql @@ -0,0 +1,35 @@ +-- +goose Up +-- +goose StatementBegin +CREATE TABLE idempotency_keys ( + user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, + method TEXT NOT NULL, + path TEXT NOT NULL, + key TEXT NOT NULL, + request_hash BYTEA NOT NULL, + response_status INT NOT NULL, + response_body BYTEA NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + expires_at TIMESTAMPTZ NOT NULL, + + CONSTRAINT idempotency_keys_pkey + PRIMARY KEY (user_id, method, path, key), + CONSTRAINT idempotency_keys_key_len + CHECK (char_length(key) BETWEEN 1 AND 128), + CONSTRAINT idempotency_keys_method_len + CHECK (char_length(method) BETWEEN 1 AND 16), + CONSTRAINT idempotency_keys_path_len + CHECK (char_length(path) BETWEEN 1 AND 2048), + CONSTRAINT idempotency_keys_hash_len + CHECK (octet_length(request_hash) = 32), + CONSTRAINT idempotency_keys_status_range + CHECK (response_status BETWEEN 100 AND 599) +); + +CREATE INDEX idempotency_keys_expires_idx + ON idempotency_keys (expires_at); +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin +DROP TABLE IF EXISTS idempotency_keys; +-- +goose StatementEnd \ No newline at end of file diff --git a/backend/migrations/0003_attachments.sql b/backend/migrations/0003_attachments.sql new file mode 100644 index 00000000..40268573 --- /dev/null +++ b/backend/migrations/0003_attachments.sql @@ -0,0 +1,73 @@ +-- +goose Up +-- +goose StatementBegin + +CREATE TABLE attachments ( + id UUID PRIMARY KEY, + chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + owner_id UUID NOT NULL REFERENCES users(id) ON DELETE RESTRICT, + filename TEXT NOT NULL, + content_type TEXT NOT NULL, + size_bytes BIGINT NOT NULL, + sha256 BYTEA NOT NULL, + object_key TEXT NOT NULL, + status TEXT NOT NULL, + error_code TEXT, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + updated_at TIMESTAMPTZ NOT NULL DEFAULT now(), + + CONSTRAINT attachments_filename_len + CHECK (char_length(filename) BETWEEN 1 AND 255), + CONSTRAINT attachments_filename_no_slash + CHECK (filename !~ '[/\\]'), + CONSTRAINT attachments_filename_no_ctrl + CHECK (filename !~ '[[:cntrl:]]'), + CONSTRAINT attachments_content_type_chk + CHECK (content_type IN ('image/png', 'image/jpeg')), + CONSTRAINT attachments_size_chk + CHECK (size_bytes > 0 AND size_bytes <= 10485760), + CONSTRAINT attachments_sha256_len + CHECK (octet_length(sha256) = 32), + CONSTRAINT attachments_object_key_len + CHECK (char_length(object_key) BETWEEN 1 AND 1024), + CONSTRAINT attachments_status_chk + CHECK (status IN ('ready', 'pending', 'failed')), + CONSTRAINT attachments_error_code_consistency + CHECK ( + (status IN ('ready', 'pending') AND error_code IS NULL) OR + (status = 'failed' AND error_code IS NOT NULL) + ) +); + +CREATE UNIQUE INDEX attachments_object_key_uniq + ON attachments (object_key); + +CREATE INDEX attachments_chat_idx + ON attachments (chat_id, created_at, id); + +CREATE INDEX attachments_pending_idx + ON attachments (created_at) + WHERE status = 'pending'; + +CREATE TABLE message_attachments ( + message_id UUID NOT NULL REFERENCES messages(id) ON DELETE CASCADE, + attachment_id UUID NOT NULL REFERENCES attachments(id) ON DELETE RESTRICT, + position SMALLINT NOT NULL, + + CONSTRAINT message_attachments_pkey + PRIMARY KEY (message_id, attachment_id), + CONSTRAINT message_attachments_position_range + CHECK (position >= 0 AND position < 10), + CONSTRAINT message_attachments_position_uniq + UNIQUE (message_id, position) +); + +CREATE INDEX message_attachments_attachment_idx + ON message_attachments (attachment_id); + +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin +DROP TABLE IF EXISTS message_attachments; +DROP TABLE IF EXISTS attachments; +-- +goose StatementEnd \ No newline at end of file diff --git a/backend/migrations/0004_messages_text_relaxed.sql b/backend/migrations/0004_messages_text_relaxed.sql new file mode 100644 index 00000000..f4e1798e --- /dev/null +++ b/backend/migrations/0004_messages_text_relaxed.sql @@ -0,0 +1,13 @@ +-- +goose Up +-- +goose StatementBegin +ALTER TABLE messages DROP CONSTRAINT IF EXISTS messages_text_not_blank; +ALTER TABLE messages ADD CONSTRAINT messages_text_not_blank + CHECK (text !~ '^[[:space:]]+$'); +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin +ALTER TABLE messages DROP CONSTRAINT IF EXISTS messages_text_not_blank; +ALTER TABLE messages ADD CONSTRAINT messages_text_not_blank + CHECK (text !~ '^[[:space:]]*$'); +-- +goose StatementEnd \ No newline at end of file diff --git a/backend/migrations/embed.go b/backend/migrations/embed.go new file mode 100644 index 00000000..91cca1c3 --- /dev/null +++ b/backend/migrations/embed.go @@ -0,0 +1,6 @@ +package migrations + +import "embed" + +//go:embed *.sql +var FS embed.FS diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..bda9b132 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,241 @@ +name: golikovap-messenger-lab5 + +x-api-resources: &api-resources + limits: + cpus: "${API_CPUS:-0.5}" + memory: "${API_MEMORY:-256M}" + reservations: + cpus: "${API_CPU_RESERVATION:-0.1}" + memory: "${API_MEMORY_RESERVATION:-64M}" + +services: + postgres: + image: postgres:16.4-alpine + restart: unless-stopped + environment: + POSTGRES_USER: messenger + POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password + POSTGRES_DB: messenger + secrets: + - postgres_password + volumes: + - postgres_data:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"] + interval: 5s + timeout: 3s + retries: 10 + start_period: 5s + deploy: + resources: + limits: + cpus: "${POSTGRES_CPUS:-1.0}" + memory: ${POSTGRES_MEMORY:-1G} + reservations: + cpus: "${POSTGRES_CPU_RESERVATION:-0.25}" + memory: ${POSTGRES_MEMORY_RESERVATION:-256M} + networks: + - internal + + redis: + image: redis:7.4-alpine + restart: unless-stopped + command: + - redis-server + - --appendonly + - "yes" + - --appendfsync + - everysec + - --save + - "60 1000" + volumes: + - redis_data:/data + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 5s + timeout: 3s + retries: 10 + start_period: 5s + deploy: + resources: + limits: + cpus: "${REDIS_CPUS:-0.5}" + memory: ${REDIS_MEMORY:-256M} + networks: + - internal + + minio: + image: pgsty/silo:RELEASE.2026-09-16T00-00-00Z + restart: unless-stopped + command: ["server", "/data"] + secrets: + - minio_root_user + - minio_root_password + environment: + MINIO_ROOT_USER_FILE: /run/secrets/minio_root_user + MINIO_ROOT_PASSWORD_FILE: /run/secrets/minio_root_password + MINIO_BROWSER: "off" + volumes: + - minio_data:/data + healthcheck: + test: ["CMD", "mc", "ready", "local"] + interval: 5s + timeout: 3s + retries: 10 + start_period: 5s + deploy: + resources: + limits: + cpus: "${MINIO_CPUS:-0.5}" + memory: ${MINIO_MEMORY:-512M} + networks: + - internal + + mc-init: + image: pgsty/silo:RELEASE.2026-09-16T00-00-00Z + restart: "no" + depends_on: + minio: + condition: service_healthy + entrypoint: ["/bin/sh", "/init.sh"] + environment: + MINIO_ENDPOINT: "http://minio:9000" + MINIO_BUCKET: "${S3_BUCKET:-messenger-attachments}" + ROOT_USER_FILE: /run/secrets/minio_root_user + ROOT_PASSWORD_FILE: /run/secrets/minio_root_password + API_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id + API_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key + volumes: + - ./infra/minio-init.sh:/init.sh:ro + secrets: + - minio_root_user + - minio_root_password + - s3_access_key_id + - s3_secret_access_key + networks: + - internal + + migrate: + build: + context: . + dockerfile: Dockerfile + image: mtusi-messenger-api:lab3 + restart: "no" + command: ["migrate"] + environment: + DATABASE_URL_FILE: /run/secrets/database_url + secrets: + - database_url + depends_on: + postgres: + condition: service_healthy + healthcheck: + disable: true + deploy: + resources: + limits: + cpus: "0.5" + memory: 128M + networks: + - internal + + api: + build: + context: . + dockerfile: Dockerfile + image: mtusi-messenger-api:lab3 + restart: unless-stopped + environment: + APP_HOST: "0.0.0.0" + APP_PORT: "8080" + DATABASE_URL_FILE: /run/secrets/database_url + CACHE_URL_FILE: /run/secrets/cache_url + SESSION_STORE: "redis" + SESSION_TTL_SECONDS: "${SESSION_TTL_SECONDS:-3600}" + IDEMPOTENCY_TTL_SECONDS: "${IDEMPOTENCY_TTL_SECONDS:-86400}" + SHUTDOWN_DRAIN_SECONDS: "${SHUTDOWN_DRAIN_SECONDS:-3}" + GIN_MODE: "release" + S3_ENDPOINT: "http://minio:9000" + S3_BUCKET: "${S3_BUCKET:-messenger-attachments}" + S3_REGION: "${S3_REGION:-us-east-1}" + S3_USE_SSL: "false" + S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id + S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key + MAX_UPLOAD_BYTES: "${MAX_UPLOAD_BYTES:-10485760}" + MAX_IMAGE_PIXELS: "${MAX_IMAGE_PIXELS:-40000000}" + RECONCILER_ENABLED: "${RECONCILER_ENABLED:-true}" + RECONCILER_INTERVAL_SECONDS: "${RECONCILER_INTERVAL_SECONDS:-300}" + RECONCILER_GRACE_SECONDS: "${RECONCILER_GRACE_SECONDS:-3600}" + secrets: + - database_url + - cache_url + - s3_access_key_id + - s3_secret_access_key + depends_on: + postgres: + condition: service_healthy + redis: + condition: service_healthy + migrate: + condition: service_completed_successfully + minio: + condition: service_healthy + mc-init: + condition: service_completed_successfully + expose: + - "8080" + stop_grace_period: 15s + healthcheck: + test: ["CMD-SHELL", "wget -q -O - http://127.0.0.1:8080/health/ready >/dev/null || exit 1"] + interval: 5s + timeout: 3s + retries: 5 + start_period: 10s + deploy: + replicas: ${API_REPLICAS:-2} + resources: *api-resources + networks: + - internal + + lb: + image: nginx:1.27.4-alpine + restart: unless-stopped + ports: + - "${LB_PORT:-8080}:8080" + volumes: + - ./infra/nginx.conf:/etc/nginx/nginx.conf:ro + depends_on: + api: + condition: service_started + deploy: + resources: + limits: + cpus: "0.5" + memory: 128M + networks: + - internal + +secrets: + postgres_password: + file: ./secrets/postgres_password + database_url: + file: ./secrets/database_url + cache_url: + file: ./secrets/cache_url + minio_root_user: + file: ./secrets/minio_root_user + minio_root_password: + file: ./secrets/minio_root_password + 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: + minio_data: + +networks: + internal: + driver: bridge \ No newline at end of file diff --git a/infra/minio-init.sh b/infra/minio-init.sh new file mode 100644 index 00000000..3defe34d --- /dev/null +++ b/infra/minio-init.sh @@ -0,0 +1,48 @@ +#!/bin/sh +set -eu + +: "${MINIO_ENDPOINT:?MINIO_ENDPOINT не задан}" +: "${MINIO_BUCKET:?MINIO_BUCKET не задан}" +: "${ROOT_USER_FILE:?ROOT_USER_FILE не задан}" +: "${ROOT_PASSWORD_FILE:?ROOT_PASSWORD_FILE не задан}" +: "${API_ACCESS_KEY_ID_FILE:?API_ACCESS_KEY_ID_FILE не задан}" +: "${API_SECRET_ACCESS_KEY_FILE:?API_SECRET_ACCESS_KEY_FILE не задан}" + +ROOT_USER="$(cat "$ROOT_USER_FILE")" +ROOT_PASS="$(cat "$ROOT_PASSWORD_FILE")" +API_ID="$(cat "$API_ACCESS_KEY_ID_FILE")" +API_SECRET="$(cat "$API_SECRET_ACCESS_KEY_FILE")" + +mc alias set bootstrap "$MINIO_ENDPOINT" "$ROOT_USER" "$ROOT_PASS" --api S3v4 >/dev/null + +mc mb --ignore-existing "bootstrap/$MINIO_BUCKET" + +if ! mc admin user info bootstrap "$API_ID" >/dev/null 2>&1; then + mc admin user add bootstrap "$API_ID" "$API_SECRET" +fi + +POLICY_FILE="/tmp/minio-policy.json" +trap 'rm -f "$POLICY_FILE"' EXIT +cat >"$POLICY_FILE" </dev/null || true + +mc admin policy attach bootstrap messenger-api --user "$API_ID" 2>/dev/null || true + +echo "minio bootstrap в норме: bucket=${MINIO_BUCKET} user=${API_ID}" \ No newline at end of file diff --git a/infra/nginx.conf b/infra/nginx.conf new file mode 100644 index 00000000..e8f196c7 --- /dev/null +++ b/infra/nginx.conf @@ -0,0 +1,48 @@ +worker_processes auto; + +events { + worker_connections 1024; +} + +http { + access_log /dev/stdout; + error_log /dev/stderr warn; + + client_max_body_size 12m; + + resolver 127.0.0.11 valid=10s ipv6=off; + + upstream backend { + zone backend 64k; + + server api:8080 resolve max_fails=2 fail_timeout=5s; + + keepalive 32; + keepalive_requests 1000; + keepalive_timeout 60s; + } + + server { + listen 8080; + server_tokens off; + + location / { + proxy_pass http://backend; + + proxy_http_version 1.1; + proxy_set_header Connection ""; + + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + + proxy_connect_timeout 2s; + proxy_send_timeout 30s; + proxy_read_timeout 30s; + + proxy_next_upstream error timeout; + proxy_next_upstream_tries 2; + } + } +} \ No newline at end of file diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100644 index 00000000..e3f366d9 --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,14 @@ +#!/bin/sh +set -eu + +# Перезапускает API и все хранилища состояния (PostgreSQL, Redis, MinIO), +# сохраняя тома и данные. Используется в `make test-persistence`. +# +# - `docker compose restart` останавливает и запускает существующие контейнеры; +# - именованные тома не затрагиваются (без `down`, без `-v`, без `rm`); +# - `migrate`/`mc-init` не перезапускаются: миграции уже применены, +# bucket уже создан, и то и другое сохраняется благодаря томам и хранилищам состояния. + +cd "$(dirname "$0")/.." + +docker compose restart postgres redis minio api \ No newline at end of file diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100644 index 00000000..e7943594 --- /dev/null +++ b/scripts/stop-one.sh @@ -0,0 +1,22 @@ +#!/bin/sh +set -eu + +: "${TARGET_INSTANCE_ID:?test provides TARGET_INSTANCE_ID (X-Instance-Id header)}" + +cd "$(dirname "$0")/.." + +cid="" +for c in $(docker compose ps -q api); do + host=$(docker inspect -f '{{.Config.Hostname}}' "$c" 2>/dev/null || true) + if [ "$host" = "$TARGET_INSTANCE_ID" ]; then + cid="$c" + break + fi +done + +if [ -z "$cid" ]; then + echo "no api replica with X-Instance-Id=$TARGET_INSTANCE_ID" >&2 + exit 1 +fi + +docker stop "$cid" >/dev/null \ No newline at end of file -- 2.54.0 From f2940880f5f02966daac3cb2e108f75fb7762970 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Fri, 6 Nov 2026 20:53:22 +0300 Subject: [PATCH 02/46] feat(domain): lab5 add queued/processing statuses and job fields on Attachment --- backend/internal/domain/entity.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/backend/internal/domain/entity.go b/backend/internal/domain/entity.go index 570ac609..4f93e873 100644 --- a/backend/internal/domain/entity.go +++ b/backend/internal/domain/entity.go @@ -40,9 +40,12 @@ type Session struct { type AttachmentStatus string const ( - AttachmentStatusPending AttachmentStatus = "pending" - AttachmentStatusReady AttachmentStatus = "ready" - AttachmentStatusFailed AttachmentStatus = "failed" + // Удалим в Этапе 2 вместе с переходом upload на 202/queued. + AttachmentStatusPending AttachmentStatus = "pending" + AttachmentStatusQueued AttachmentStatus = "queued" + AttachmentStatusProcessing AttachmentStatus = "processing" + AttachmentStatusReady AttachmentStatus = "ready" + AttachmentStatusFailed AttachmentStatus = "failed" ) type Attachment struct { @@ -58,4 +61,10 @@ type Attachment struct { ErrorCode *string CreatedAt time.Time UpdatedAt time.Time + + Attempts int // авторитетный счётчик попыток (в БД) + NextAttemptAt *time.Time // «не раньше»; nil = можно брать сразу + LockedUntil *time.Time // lease; nil = не арендовано + PipelineVersion int // guard в CompleteJob; в БД default 1 + StagingKey string // S3-ключ оригинала до 202; "" в lab4-пути } -- 2.54.0 From 18afe2563e014f24a86497fa38cc02b2b31459c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Fri, 6 Nov 2026 21:27:05 +0300 Subject: [PATCH 03/46] feat(db): lab5 migration 0005 add queued/processing statuses and job columns on attachments --- backend/migrations/0005_jobs.sql | 77 ++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 backend/migrations/0005_jobs.sql diff --git a/backend/migrations/0005_jobs.sql b/backend/migrations/0005_jobs.sql new file mode 100644 index 00000000..07913480 --- /dev/null +++ b/backend/migrations/0005_jobs.sql @@ -0,0 +1,77 @@ +-- +goose Up +-- +goose StatementBegin + + +ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_status_chk; +ALTER TABLE attachments ADD CONSTRAINT attachments_status_chk + CHECK (status IN ('pending', 'queued', 'processing', 'ready', 'failed')); + +ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_error_code_consistency; +ALTER TABLE attachments ADD CONSTRAINT attachments_error_code_consistency + CHECK ( + (status IN ('pending', 'queued', 'processing', 'ready') AND error_code IS NULL) OR + (status = 'failed' AND error_code IS NOT NULL) + ); + +ALTER TABLE attachments ADD COLUMN attempts INT NOT NULL DEFAULT 0; +ALTER TABLE attachments ADD CONSTRAINT attachments_attempts_nonneg + CHECK (attempts >= 0); + +ALTER TABLE attachments ADD COLUMN next_attempt_at TIMESTAMPTZ; +ALTER TABLE attachments ADD COLUMN locked_until TIMESTAMPTZ; + +ALTER TABLE attachments ADD COLUMN pipeline_version INT NOT NULL DEFAULT 1; +ALTER TABLE attachments ADD CONSTRAINT attachments_pipeline_version_pos + CHECK (pipeline_version >= 1); + +ALTER TABLE attachments ADD COLUMN staging_key TEXT; +ALTER TABLE attachments ADD CONSTRAINT attachments_staging_key_len + CHECK (staging_key IS NULL OR char_length(staging_key) BETWEEN 1 AND 1024); + + +DROP INDEX IF EXISTS attachments_pending_idx; + +CREATE INDEX attachments_active_idx + ON attachments (status, next_attempt_at) + WHERE status IN ('queued', 'processing'); + +CREATE INDEX attachments_locked_idx + ON attachments (locked_until) + WHERE status = 'processing'; + +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin + +DROP INDEX IF EXISTS attachments_locked_idx; +DROP INDEX IF EXISTS attachments_active_idx; + +CREATE INDEX attachments_pending_idx + ON attachments (created_at) + WHERE status = 'pending'; + +ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_staging_key_len; +ALTER TABLE attachments DROP COLUMN IF EXISTS staging_key; + +ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_pipeline_version_pos; +ALTER TABLE attachments DROP COLUMN IF EXISTS pipeline_version; + +ALTER TABLE attachments DROP COLUMN IF EXISTS locked_until; +ALTER TABLE attachments DROP COLUMN IF EXISTS next_attempt_at; + +ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_attempts_nonneg; +ALTER TABLE attachments DROP COLUMN IF EXISTS attempts; + +ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_error_code_consistency; +ALTER TABLE attachments ADD CONSTRAINT attachments_error_code_consistency + CHECK ( + (status IN ('ready', 'pending') AND error_code IS NULL) OR + (status = 'failed' AND error_code IS NOT NULL) + ); + +ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_status_chk; +ALTER TABLE attachments ADD CONSTRAINT attachments_status_chk + CHECK (status IN ('ready', 'pending', 'failed')); + +-- +goose StatementEnd \ No newline at end of file -- 2.54.0 From 7f0fb2b0da50002e297cf5b8b333cd8dbbf5fa4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Fri, 6 Nov 2026 22:43:56 +0300 Subject: [PATCH 04/46] feat(domain,db): lab5 add outbox entry, transaction contract and 0006 migration --- backend/internal/domain/outbox.go | 28 +++++++++++++++++++++++ backend/internal/domain/repository.go | 4 ++++ backend/internal/domain/tx.go | 18 +++++++++++++++ backend/migrations/0006_outbox.sql | 32 +++++++++++++++++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 backend/internal/domain/outbox.go create mode 100644 backend/internal/domain/tx.go create mode 100644 backend/migrations/0006_outbox.sql diff --git a/backend/internal/domain/outbox.go b/backend/internal/domain/outbox.go new file mode 100644 index 00000000..ecaa511b --- /dev/null +++ b/backend/internal/domain/outbox.go @@ -0,0 +1,28 @@ +package domain + +import "time" + +const ( + OutboxAggregateAttachment = "attachment" + OutboxEventProcess = "attachment.process" +) + +// OutboxEntry — намерение опубликовать сообщение, записанное +// в одной транзакции с бизнес-изменением (transactional outbox). +// +// Инварианты: +// - PublishedAt == nil => ещё не опубликовано, publisher обязан дослать; +// - PublishedAt != nil => RabbitMQ принял сообщение, +// а не «мы вызвали Publish». At-least-once: повторная доставка +// возможна и нормальна, гасится идемпотентностью consumer'а. +// +type OutboxEntry struct { + ID string + AggregateType string + AggregateID string + EventType string + Payload []byte + CreatedAt time.Time + PublishedAt *time.Time + Attempts int +} diff --git a/backend/internal/domain/repository.go b/backend/internal/domain/repository.go index b5d6a619..bfa312b9 100644 --- a/backend/internal/domain/repository.go +++ b/backend/internal/domain/repository.go @@ -45,3 +45,7 @@ type AttachmentRepository interface { ExistingKeys(ctx context.Context, keys []string) ([]string, error) DeletePending(ctx context.Context, id string) error } + +type OutboxRepository interface { + Enqueue(ctx context.Context, tx Tx, e *OutboxEntry) error +} diff --git a/backend/internal/domain/tx.go b/backend/internal/domain/tx.go new file mode 100644 index 00000000..e60809d9 --- /dev/null +++ b/backend/internal/domain/tx.go @@ -0,0 +1,18 @@ +package domain + +import "context" + +// Tx — минимальная абстракция транзакции, нужная domain-слою. +// Реализуется адаптером над pgx.Tx в storage/postgres, поэтому +// domain не зависит от драйвера БД. +// +// Зачем: transactional outbox требует, чтобы бизнес-изменение +// (например, INSERT attachments) и INSERT outbox шли в одной +// транзакции. Без передачи транзакции в репозиторий эту гарантию +// не выразить. +// +// Метод один — Exec. Расширять по мере появления реальных +// потребителей, а не на будущее. +type Tx interface { + Exec(ctx context.Context, sql string, args ...any) error +} diff --git a/backend/migrations/0006_outbox.sql b/backend/migrations/0006_outbox.sql new file mode 100644 index 00000000..80a0f1b7 --- /dev/null +++ b/backend/migrations/0006_outbox.sql @@ -0,0 +1,32 @@ +-- +goose Up +-- +goose StatementBegin + +CREATE TABLE outbox ( + id UUID PRIMARY KEY, + aggregate_type TEXT NOT NULL, + aggregate_id UUID NOT NULL, + event_type TEXT NOT NULL, + payload JSONB NOT NULL, + attempts INT NOT NULL DEFAULT 0, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + published_at TIMESTAMPTZ, + + CONSTRAINT outbox_aggregate_type_len + CHECK (char_length(aggregate_type) BETWEEN 1 AND 64), + CONSTRAINT outbox_event_type_len + CHECK (char_length(event_type) BETWEEN 1 AND 64), + CONSTRAINT outbox_attempts_nonneg + CHECK (attempts >= 0) +); + +CREATE INDEX outbox_pending_idx + ON outbox (created_at) + WHERE published_at IS NULL; + +-- +goose StatementEnd + + +-- +goose Down +-- +goose StatementBegin +DROP TABLE IF EXISTS outbox; +-- +goose StatementEnd \ No newline at end of file -- 2.54.0 From 09cf547254525b15a9612d354f29eb8e694c04ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Fri, 6 Nov 2026 22:59:32 +0300 Subject: [PATCH 05/46] feat(domain): lab5 add JobMessage contract --- backend/internal/domain/job.go | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 backend/internal/domain/job.go diff --git a/backend/internal/domain/job.go b/backend/internal/domain/job.go new file mode 100644 index 00000000..8f44e797 --- /dev/null +++ b/backend/internal/domain/job.go @@ -0,0 +1,7 @@ +package domain + +type JobMessage struct { + AttachmentID string `json:"attachment_id"` + PipelineVersion int `json:"pipeline_version"` + Attempt int `json:"attempt"` +} -- 2.54.0 From ab35077afdd345afff8bc676babba67b431718cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Sat, 7 Nov 2026 06:46:21 +0300 Subject: [PATCH 06/46] feat(storage): lab5 implement outbox enqueue and pgx transaction adapter --- backend/internal/storage/postgres/outbox.go | 56 ++++++++++++++++++++ backend/internal/storage/postgres/storage.go | 1 + backend/internal/storage/postgres/tx.go | 35 ++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 backend/internal/storage/postgres/outbox.go create mode 100644 backend/internal/storage/postgres/tx.go diff --git a/backend/internal/storage/postgres/outbox.go b/backend/internal/storage/postgres/outbox.go new file mode 100644 index 00000000..7d666e0e --- /dev/null +++ b/backend/internal/storage/postgres/outbox.go @@ -0,0 +1,56 @@ +package postgres + +import ( + "context" + "time" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5/pgxpool" + + "high-loud-apps-labs/internal/domain" +) + +type OutboxRepo struct { + pool *pgxpool.Pool +} + +func NewOutboxRepo(pool *pgxpool.Pool) *OutboxRepo { + return &OutboxRepo{pool: pool} +} + +func (r *OutboxRepo) Enqueue(ctx context.Context, tx domain.Tx, e *domain.OutboxEntry) error { + if e.AggregateType == "" || e.AggregateID == "" || e.EventType == "" { + return &domain.ValidationError{ + Field: "outbox", + Message: "aggregate_type, aggregate_id и event_type обязательны", + } + } + if len(e.Payload) == 0 { + return &domain.ValidationError{ + Field: "outbox.payload", + Message: "не может быть пустым", + } + } + + if e.ID == "" { + e.ID = uuid.NewString() + } + if e.CreatedAt.IsZero() { + e.CreatedAt = time.Now().UTC() + } + + const q = ` + INSERT INTO outbox + (id, aggregate_type, aggregate_id, event_type, payload, attempts, created_at, published_at) + VALUES ($1::uuid, $2, $3::uuid, $4, $5::jsonb, $6, $7, $8) + ` + if err := tx.Exec(ctx, q, + e.ID, e.AggregateType, e.AggregateID, e.EventType, + string(e.Payload), e.Attempts, e.CreatedAt, e.PublishedAt, + ); err != nil { + return err + } + return nil +} + +var _ domain.OutboxRepository = (*OutboxRepo)(nil) diff --git a/backend/internal/storage/postgres/storage.go b/backend/internal/storage/postgres/storage.go index 07121091..c3c5abbb 100644 --- a/backend/internal/storage/postgres/storage.go +++ b/backend/internal/storage/postgres/storage.go @@ -17,3 +17,4 @@ func (s *Storage) Chats() *ChatRepo { return &ChatRepo{pool: s.pool} func (s *Storage) Messages() *MessageRepo { return &MessageRepo{pool: s.pool} } func (s *Storage) Sessions() *SessionRepo { return &SessionRepo{pool: s.pool} } func (s *Storage) Attachments() *AttachmentRepo { return &AttachmentRepo{pool: s.pool} } +func (s *Storage) Outbox() *OutboxRepo { return &OutboxRepo{pool: s.pool} } diff --git a/backend/internal/storage/postgres/tx.go b/backend/internal/storage/postgres/tx.go new file mode 100644 index 00000000..4d8e771b --- /dev/null +++ b/backend/internal/storage/postgres/tx.go @@ -0,0 +1,35 @@ +package postgres + +import ( + "context" + + "github.com/jackc/pgx/v5" + + "high-loud-apps-labs/internal/domain" +) + +type pgxTx struct { + tx pgx.Tx +} + +func (t *pgxTx) Exec(ctx context.Context, sql string, args ...any) error { + _, err := t.tx.Exec(ctx, sql, args...) + return mapError(err) +} + +// WithTx выполняет fn в одной транзакции: +// - commit, если fn вернул nil; +// - rollback, если fn вернул ошибку. + +func (s *Storage) WithTx(ctx context.Context, fn func(tx domain.Tx) error) error { + tx, err := s.pool.Begin(ctx) + if err != nil { + return mapError(err) + } + defer func() { _ = tx.Rollback(ctx) }() + + if err := fn(&pgxTx{tx: tx}); err != nil { + return err + } + return mapError(tx.Commit(ctx)) +} -- 2.54.0 From 0fddc697f1a515794a38ad69d4433755fdf7eb42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Sat, 7 Nov 2026 07:45:56 +0300 Subject: [PATCH 07/46] feat(storage): lab5 add CreateInTx for transactional outbox support --- backend/internal/domain/repository.go | 1 + .../internal/storage/postgres/attachments.go | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/backend/internal/domain/repository.go b/backend/internal/domain/repository.go index bfa312b9..3343e20b 100644 --- a/backend/internal/domain/repository.go +++ b/backend/internal/domain/repository.go @@ -44,6 +44,7 @@ type AttachmentRepository interface { ListStalePending(ctx context.Context, olderThan time.Time, limit int) ([]Attachment, error) ExistingKeys(ctx context.Context, keys []string) ([]string, error) DeletePending(ctx context.Context, id string) error + CreateInTx(ctx context.Context, tx Tx, a *Attachment) error } type OutboxRepository interface { diff --git a/backend/internal/storage/postgres/attachments.go b/backend/internal/storage/postgres/attachments.go index 915b1f13..98b32ab5 100644 --- a/backend/internal/storage/postgres/attachments.go +++ b/backend/internal/storage/postgres/attachments.go @@ -51,6 +51,23 @@ func (r *AttachmentRepo) Create(ctx context.Context, a *domain.Attachment) error return nil } +func (r *AttachmentRepo) CreateInTx(ctx context.Context, tx domain.Tx, a *domain.Attachment) error { + const q = ` + INSERT INTO attachments + (id, chat_id, owner_id, filename, content_type, size_bytes, sha256, + object_key, status, error_code, + attempts, pipeline_version, staging_key, + created_at, updated_at) + VALUES ($1::uuid, $2::uuid, $3::uuid, $4, $5, $6, $7, $8, $9, NULL, + 0, $10, $11, $12, $12) + ` + return tx.Exec(ctx, q, + a.ID, a.ChatID, a.OwnerID, a.Filename, a.ContentType, a.SizeBytes, + a.SHA256, a.ObjectKey, string(a.Status), + a.PipelineVersion, a.StagingKey, a.CreatedAt, + ) +} + func (r *AttachmentRepo) GetByID(ctx context.Context, id string) (*domain.Attachment, error) { const q = ` SELECT id, chat_id, owner_id, filename, content_type, size_bytes, sha256, -- 2.54.0 From 6eaf2a89b0eabafb128e7d2782f3e1f4b7784e2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Sat, 7 Nov 2026 09:54:11 +0300 Subject: [PATCH 08/46] feat(service): lab5 switch upload to queued and write to transactional outbox --- backend/cmd/server/main.go | 2 + backend/internal/domain/entity.go | 4 +- backend/internal/service/attachment.go | 98 +++++++++++++------------- 3 files changed, 53 insertions(+), 51 deletions(-) diff --git a/backend/cmd/server/main.go b/backend/cmd/server/main.go index c0c74af0..abadff16 100644 --- a/backend/cmd/server/main.go +++ b/backend/cmd/server/main.go @@ -89,6 +89,8 @@ func main() { store.Attachments(), store.Chats(), objectStorage, + store.Outbox(), + store, cfg.MaxUploadBytes, cfg.MaxImagePixels, ) diff --git a/backend/internal/domain/entity.go b/backend/internal/domain/entity.go index 4f93e873..400e02c2 100644 --- a/backend/internal/domain/entity.go +++ b/backend/internal/domain/entity.go @@ -40,8 +40,6 @@ type Session struct { type AttachmentStatus string const ( - // Удалим в Этапе 2 вместе с переходом upload на 202/queued. - AttachmentStatusPending AttachmentStatus = "pending" AttachmentStatusQueued AttachmentStatus = "queued" AttachmentStatusProcessing AttachmentStatus = "processing" AttachmentStatusReady AttachmentStatus = "ready" @@ -63,7 +61,7 @@ type Attachment struct { UpdatedAt time.Time Attempts int // авторитетный счётчик попыток (в БД) - NextAttemptAt *time.Time // «не раньше»; nil = можно брать сразу + NextAttemptAt *time.Time // "не раньше"; nil = можно брать сразу LockedUntil *time.Time // lease; nil = не арендовано PipelineVersion int // guard в CompleteJob; в БД default 1 StagingKey string // S3-ключ оригинала до 202; "" в lab4-пути diff --git a/backend/internal/service/attachment.go b/backend/internal/service/attachment.go index a9b1273d..5a7ff696 100644 --- a/backend/internal/service/attachment.go +++ b/backend/internal/service/attachment.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "crypto/sha256" + "encoding/json" "image" _ "image/jpeg" _ "image/png" @@ -19,36 +20,42 @@ import ( "high-loud-apps-labs/internal/domain" ) +type txRunner interface { + WithTx(ctx context.Context, fn func(tx domain.Tx) error) error +} + type AttachmentService struct { attachments domain.AttachmentRepository chats domain.ChatRepository storage domain.ObjectStorage + outbox domain.OutboxRepository + tx txRunner maxBytes int64 maxPixels int64 - decodeSem chan struct{} } func NewAttachmentService( attachments domain.AttachmentRepository, chats domain.ChatRepository, storage domain.ObjectStorage, + outbox domain.OutboxRepository, + tx txRunner, maxBytes, maxPixels int64, ) *AttachmentService { return &AttachmentService{ attachments: attachments, chats: chats, storage: storage, + outbox: outbox, + tx: tx, maxBytes: maxBytes, maxPixels: maxPixels, - // 40M pixels × 4 bytes (RGBA8) = 160 MB per full decode. - // API_MEMORY=256M → только один decode безопасен. - decodeSem: make(chan struct{}, 1), } } -// Upload принимает тело, валидирует PNG/JPEG по содержимому, -// кладёт в pending, загружает в S3, переводит в ready. -// При ошибке Put компенсирует MarkFailed. +// 1. Валидация, sniff Content-Type, проверка пикселей по заголовку. +// 2. Оригинал в S3 staging/{id}. +// 3. Транзакция: INSERT attachments(status=queued) + INSERT outbox. func (s *AttachmentService) Upload( ctx context.Context, ownerID, chatID, filename string, @@ -95,61 +102,56 @@ func (s *AttachmentService) Upload( } } - if err := s.verifyIntegrity(ctx, buf, contentType); err != nil { - return nil, err - } - sha := sha256.Sum256(buf) id := uuid.NewString() - objectKey := "chats/" + chatID + "/" + id + stagingKey := "staging/" + id now := time.Now().UTC() a := &domain.Attachment{ - ID: id, - ChatID: chatID, - OwnerID: ownerID, - Filename: filename, - ContentType: contentType, - SizeBytes: int64(len(buf)), - SHA256: sha[:], - ObjectKey: objectKey, - Status: domain.AttachmentStatusPending, - CreatedAt: now, - UpdatedAt: now, + ID: id, + ChatID: chatID, + OwnerID: ownerID, + Filename: filename, + ContentType: contentType, + SizeBytes: int64(len(buf)), + SHA256: sha[:], + ObjectKey: "chats/" + chatID + "/" + id, + Status: domain.AttachmentStatusQueued, + StagingKey: stagingKey, + PipelineVersion: 1, + CreatedAt: now, + UpdatedAt: now, } - if err := s.attachments.Create(ctx, a); err != nil { + + if err := s.storage.Put(ctx, stagingKey, bytes.NewReader(buf), int64(len(buf)), contentType); err != nil { return nil, err } - if err := s.storage.Put(ctx, objectKey, bytes.NewReader(buf), int64(len(buf)), contentType); err != nil { - // Компенсация: pending → failed. Reconciler не будет трогать failed. - _ = s.attachments.MarkFailed(ctx, a.ID, "upload_failed") + payload, err := json.Marshal(domain.JobMessage{ + AttachmentID: id, + PipelineVersion: 1, + Attempt: 1, + }) + if err != nil { return nil, err } - if err := s.attachments.MarkReady(ctx, a.ID); err != nil { - // S3 has the object, DB still pending. Reconciler очистит по grace period. - return nil, err - } - a.Status = domain.AttachmentStatusReady - return a, nil -} - -func (s *AttachmentService) verifyIntegrity(ctx context.Context, buf []byte, contentType string) error { - select { - case s.decodeSem <- struct{}{}: - case <-ctx.Done(): - return &domain.UnavailableError{Reason: "ожидание декодирования"} - } - defer func() { <-s.decodeSem }() - - if _, _, err := image.Decode(bytes.NewReader(buf)); err != nil { - return &domain.UnsupportedMediaTypeError{ - MediaType: contentType, - Expected: "целостный " + contentType + " (без обрезки/повреждений)", + err = s.tx.WithTx(ctx, func(tx domain.Tx) error { + if err := s.attachments.CreateInTx(ctx, tx, a); err != nil { + return err } + return s.outbox.Enqueue(ctx, tx, &domain.OutboxEntry{ + AggregateType: domain.OutboxAggregateAttachment, + AggregateID: id, + EventType: domain.OutboxEventProcess, + Payload: payload, + }) + }) + if err != nil { + return nil, err } - return nil + + return a, nil } func (s *AttachmentService) Get(ctx context.Context, userID, attachmentID string) (*domain.Attachment, error) { -- 2.54.0 From c34db41342423d08173b3d1cb2450c43ef2291be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Sat, 7 Nov 2026 10:28:45 +0300 Subject: [PATCH 09/46] feat(db): lab5 drop pending status from attachments constraints --- backend/migrations/0007_drop_pending.sql | 42 ++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 backend/migrations/0007_drop_pending.sql diff --git a/backend/migrations/0007_drop_pending.sql b/backend/migrations/0007_drop_pending.sql new file mode 100644 index 00000000..dcc2d570 --- /dev/null +++ b/backend/migrations/0007_drop_pending.sql @@ -0,0 +1,42 @@ +-- +goose Up +-- +goose StatementBegin + +ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_status_chk; +ALTER TABLE attachments ADD CONSTRAINT attachments_status_chk + CHECK (status IN ('queued', 'processing', 'ready', 'failed')); + +ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_error_code_consistency; +ALTER TABLE attachments ADD CONSTRAINT attachments_error_code_consistency + CHECK ( + (status IN ('queued', 'processing', 'ready') AND error_code IS NULL) OR + (status = 'failed' AND error_code IS NOT NULL) + ); + +DROP INDEX IF EXISTS attachments_pending_idx; +CREATE INDEX attachments_active_idx + ON attachments (created_at) + WHERE status IN ('queued', 'processing'); + +-- +goose StatementEnd + + +-- +goose Down +-- +goose StatementBegin + +DROP INDEX IF EXISTS attachments_active_idx; +CREATE INDEX attachments_pending_idx + ON attachments (created_at) + WHERE status = 'pending'; + +ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_error_code_consistency; +ALTER TABLE attachments ADD CONSTRAINT attachments_error_code_consistency + CHECK ( + (status IN ('ready', 'pending') AND error_code IS NULL) OR + (status = 'failed' AND error_code IS NOT NULL) + ); + +ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_status_chk; +ALTER TABLE attachments ADD CONSTRAINT attachments_status_chk + CHECK (status IN ('ready', 'pending', 'failed')); + +-- +goose StatementEnd \ No newline at end of file -- 2.54.0 From f4c6657a10b3cbcc23327de4e7e1403d0061e157 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Sat, 7 Nov 2026 10:52:00 +0300 Subject: [PATCH 10/46] feat(api): lab5 return 202 on upload and allow queued attachments in messages --- backend/internal/httpapi/handler/attachment.go | 2 +- backend/internal/storage/postgres/message.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/internal/httpapi/handler/attachment.go b/backend/internal/httpapi/handler/attachment.go index ad0f8186..e390c6fc 100644 --- a/backend/internal/httpapi/handler/attachment.go +++ b/backend/internal/httpapi/handler/attachment.go @@ -52,7 +52,7 @@ func (h *AttachmentHandler) Upload(c *gin.Context) { respondError(c, err) return } - c.JSON(http.StatusCreated, dto.AttachmentFromDomain(att)) + c.JSON(http.StatusAccepted, dto.AttachmentFromDomain(att)) } func (h *AttachmentHandler) Get(c *gin.Context) { diff --git a/backend/internal/storage/postgres/message.go b/backend/internal/storage/postgres/message.go index 67df6cd0..604c65d3 100644 --- a/backend/internal/storage/postgres/message.go +++ b/backend/internal/storage/postgres/message.go @@ -262,8 +262,8 @@ func (r *MessageRepo) validateAttachments(ctx context.Context, tx pgx.Tx, chatID if !ok || a.chatID != chatID { return &domain.NotFoundError{Entity: "attachment", ID: id} } - if a.status != domain.AttachmentStatusReady { - return &domain.ConflictError{Reason: "вложение " + id + " не готово"} + if a.status == domain.AttachmentStatusFailed { + return &domain.ConflictError{Reason: "вложение " + id + " в состоянии failed"} } } return nil -- 2.54.0 From 228aa88723f18647c6609037cd7ecc3d230152de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Sat, 7 Nov 2026 11:23:46 +0300 Subject: [PATCH 11/46] fix(storage): read lab5 job fields in GetByID --- backend/internal/storage/postgres/attachments.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/backend/internal/storage/postgres/attachments.go b/backend/internal/storage/postgres/attachments.go index 98b32ab5..9fb95fed 100644 --- a/backend/internal/storage/postgres/attachments.go +++ b/backend/internal/storage/postgres/attachments.go @@ -71,7 +71,8 @@ func (r *AttachmentRepo) CreateInTx(ctx context.Context, tx domain.Tx, a *domain func (r *AttachmentRepo) GetByID(ctx context.Context, id string) (*domain.Attachment, error) { const q = ` SELECT id, chat_id, owner_id, filename, content_type, size_bytes, sha256, - object_key, status, error_code, created_at, updated_at + object_key, status, error_code, created_at, updated_at, + attempts, next_attempt_at, locked_until, pipeline_version, staging_key FROM attachments WHERE id = $1::uuid ` @@ -80,7 +81,8 @@ func (r *AttachmentRepo) GetByID(ctx context.Context, id string) (*domain.Attach err := r.pool.QueryRow(ctx, q, id). Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.ContentType, &a.SizeBytes, &a.SHA256, &a.ObjectKey, &status, &a.ErrorCode, - &a.CreatedAt, &a.UpdatedAt) + &a.CreatedAt, &a.UpdatedAt, + &a.Attempts, &a.NextAttemptAt, &a.LockedUntil, &a.PipelineVersion, &a.StagingKey) if err != nil { return nil, notFound(err, "attachment", id) } -- 2.54.0 From bc9d69c6e27453dc0c4b050dfd7af8849a822c9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Sat, 7 Nov 2026 12:34:22 +0300 Subject: [PATCH 12/46] feat(infra): lab5 add rabbitmq service and outbox config --- backend/internal/config/config.go | 16 ++++++++++++++++ compose.yaml | 28 +++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/backend/internal/config/config.go b/backend/internal/config/config.go index 6419db3a..66d02323 100644 --- a/backend/internal/config/config.go +++ b/backend/internal/config/config.go @@ -31,6 +31,9 @@ type Config struct { ReconcilerEnabled bool ReconcilerInterval time.Duration ReconcilerGracePeriod time.Duration + + RabbitMQURL string + OutboxInterval time.Duration } func Load() (*Config, error) { @@ -127,6 +130,16 @@ func Load() (*Config, error) { return nil, fmt.Errorf("RECONCILER_GRACE_SECONDS должен быть положительным") } + rabbitURL, err := readSecretOptional("RABBITMQ_URL_FILE", "RABBITMQ_URL") + if err != nil { + return nil, fmt.Errorf("rabbitmq url: %w", err) + } + + outboxMs, err := readInt("OUTBOX_INTERVAL_MS", 1000) + if err != nil { + return nil, fmt.Errorf("OUTBOX_INTERVAL_MS: %w", err) + } + return &Config{ Host: getenv("APP_HOST", "0.0.0.0"), Port: getenv("APP_PORT", "8080"), @@ -150,6 +163,9 @@ func Load() (*Config, error) { ReconcilerEnabled: reconcilerEnabled, ReconcilerInterval: time.Duration(reconcilerIntervalSec) * time.Second, ReconcilerGracePeriod: time.Duration(reconcilerGraceSec) * time.Second, + + RabbitMQURL: rabbitURL, + OutboxInterval: time.Duration(outboxMs) * time.Millisecond, }, nil } diff --git a/compose.yaml b/compose.yaml index bda9b132..511ea1d5 100644 --- a/compose.yaml +++ b/compose.yaml @@ -64,6 +64,30 @@ services: networks: - internal +rabbitmq: + image: rabbitmq:3.13-management-alpine + restart: unless-stopped + environment: + RABBITMQ_DEFAULT_USER: "messenger" + RABBITMQ_DEFAULT_PASS_FILE: /run/secrets/rabbitmq_password + secrets: + - rabbitmq_password + volumes: + - rabbitmq_data:/var/lib/rabbitmq + ports: + - "15672:15672" + healthcheck: + test: ["CMD", "rabbitmq-diagnostics", "-q", "ping"] + interval: 5s + timeout: 3s + retries: 10 + deploy: + resources: + limits: + memory: 256M + networks: + - internal + minio: image: pgsty/silo:RELEASE.2026-09-16T00-00-00Z restart: unless-stopped @@ -230,11 +254,13 @@ secrets: file: ./secrets/s3_access_key_id s3_secret_access_key: file: ./secrets/s3_secret_access_key - + rabbitmq_password: + file: ./secrets/rabbitmq_password volumes: postgres_data: redis_data: minio_data: + rabbitmq_data: networks: internal: -- 2.54.0 From 70b1b702c7f8f5fa2910db3a36d135d8955ad7b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Sat, 7 Nov 2026 13:56:12 +0300 Subject: [PATCH 13/46] feat(storage): lab5 add outbox claim batch and mark published --- backend/internal/domain/repository.go | 2 + backend/internal/storage/postgres/outbox.go | 44 +++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/backend/internal/domain/repository.go b/backend/internal/domain/repository.go index 3343e20b..7caba8ae 100644 --- a/backend/internal/domain/repository.go +++ b/backend/internal/domain/repository.go @@ -49,4 +49,6 @@ type AttachmentRepository interface { type OutboxRepository interface { Enqueue(ctx context.Context, tx Tx, e *OutboxEntry) error + ClaimBatch(ctx context.Context, limit int) ([]OutboxEntry, error) + MarkPublished(ctx context.Context, ids []string) error } diff --git a/backend/internal/storage/postgres/outbox.go b/backend/internal/storage/postgres/outbox.go index 7d666e0e..af78d77c 100644 --- a/backend/internal/storage/postgres/outbox.go +++ b/backend/internal/storage/postgres/outbox.go @@ -53,4 +53,48 @@ func (r *OutboxRepo) Enqueue(ctx context.Context, tx domain.Tx, e *domain.Outbox return nil } +func (r *OutboxRepo) ClaimBatch(ctx context.Context, limit int) ([]domain.OutboxEntry, error) { + const q = ` + WITH claimed AS ( + SELECT id FROM outbox + WHERE published_at IS NULL + ORDER BY created_at + LIMIT $1 + FOR UPDATE SKIP LOCKED + ) + UPDATE outbox o + SET attempts = attempts + 1 + FROM claimed c + WHERE o.id = c.id + RETURNING o.id, o.aggregate_type, o.aggregate_id, o.event_type, o.payload, o.attempts, o.created_at, o.published_at + ` + rows, err := r.pool.Query(ctx, q, limit) + if err != nil { + return nil, mapError(err) + } + defer rows.Close() + + var entries []domain.OutboxEntry + for rows.Next() { + var e domain.OutboxEntry + if err := rows.Scan( + &e.ID, &e.AggregateType, &e.AggregateID, &e.EventType, + &e.Payload, &e.Attempts, &e.CreatedAt, &e.PublishedAt, + ); err != nil { + return nil, mapError(err) + } + entries = append(entries, e) + } + return entries, mapError(rows.Err()) +} + +func (r *OutboxRepo) MarkPublished(ctx context.Context, ids []string) error { + if len(ids) == 0 { + return nil + } + const q = `UPDATE outbox SET published_at = now() WHERE id = ANY($1::uuid[])` + _, err := r.pool.Exec(ctx, q, ids) + return mapError(err) +} + var _ domain.OutboxRepository = (*OutboxRepo)(nil) -- 2.54.0 From 1443b07ee1ef6f88cc844f47783c6c4f15d98ee9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Sat, 7 Nov 2026 14:56:14 +0300 Subject: [PATCH 14/46] feat(broker): add rabbitmq publisher interface and struct --- backend/go.mod | 1 + backend/go.sum | 2 ++ backend/internal/broker/rabbitmq.go | 19 +++++++++++++++++++ 3 files changed, 22 insertions(+) create mode 100644 backend/internal/broker/rabbitmq.go diff --git a/backend/go.mod b/backend/go.mod index f4390989..b7956f03 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -45,6 +45,7 @@ require ( github.com/philhofer/fwd v1.2.0 // indirect github.com/quic-go/qpack v0.6.0 // indirect github.com/quic-go/quic-go v0.59.0 // indirect + github.com/rabbitmq/amqp091-go v1.16.0 github.com/rs/xid v1.6.0 // indirect github.com/sethvargo/go-retry v0.3.0 // indirect github.com/tinylib/msgp v1.6.4 // indirect diff --git a/backend/go.sum b/backend/go.sum index 898b3e14..ed994afb 100644 --- a/backend/go.sum +++ b/backend/go.sum @@ -91,6 +91,8 @@ github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8= github.com/quic-go/qpack v0.6.0/go.mod h1:lUpLKChi8njB4ty2bFLX2x4gzDqXwUpaO1DP9qMDZII= github.com/quic-go/quic-go v0.59.0 h1:OLJkp1Mlm/aS7dpKgTc6cnpynnD2Xg7C1pwL6vy/SAw= github.com/quic-go/quic-go v0.59.0/go.mod h1:upnsH4Ju1YkqpLXC305eW3yDZ4NfnNbmQRCMWS58IKU= +github.com/rabbitmq/amqp091-go v1.16.0 h1:1O8oQqnj9hLACoo6CVMgn57z2pLWXvgemhh4GsquV+Y= +github.com/rabbitmq/amqp091-go v1.16.0/go.mod h1:Hy4jKW5kQART1u+JkDTF9YYOQUHXqMuhrgxOEeS7G4o= github.com/redis/go-redis/v9 v9.22.0 h1:laDvpYXTJtZLloinw1fA5Kqd6HAEH2XKxOkG/PDq2F0= github.com/redis/go-redis/v9 v9.22.0/go.mod h1:y2g0Wj8rQvuK0ELM+oxSudcLtC09JScs98I/X9gRWY4= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= diff --git a/backend/internal/broker/rabbitmq.go b/backend/internal/broker/rabbitmq.go new file mode 100644 index 00000000..c64537e3 --- /dev/null +++ b/backend/internal/broker/rabbitmq.go @@ -0,0 +1,19 @@ +package broker + +import ( + "context" + + amqp "github.com/rabbitmq/amqp091-go" +) + +type Publisher interface { + Publish(ctx context.Context, routingKey string, body []byte) error + Close() error +} + +type RabbitPublisher struct { + url string + conn *amqp.Connection + ch *amqp.Channel + confirms chan amqp.Confirmation +} -- 2.54.0 From 2934d221d75c4c4ffd035766fcef9a7e70a26bfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Sat, 7 Nov 2026 15:12:22 +0300 Subject: [PATCH 15/46] feat(broker): lab5 setup rabbitmq connection and durable topology --- backend/internal/broker/rabbitmq.go | 39 +++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/backend/internal/broker/rabbitmq.go b/backend/internal/broker/rabbitmq.go index c64537e3..8cf37c24 100644 --- a/backend/internal/broker/rabbitmq.go +++ b/backend/internal/broker/rabbitmq.go @@ -2,6 +2,7 @@ package broker import ( "context" + "fmt" amqp "github.com/rabbitmq/amqp091-go" ) @@ -17,3 +18,41 @@ type RabbitPublisher struct { ch *amqp.Channel confirms chan amqp.Confirmation } + +func NewRabbitPublisher(url string) (*RabbitPublisher, error) { + p := &RabbitPublisher{url: url} + if err := p.connect(); err != nil { + return nil, err + } + return p, nil +} + +func (p *RabbitPublisher) connect() error { + var err error + p.conn, err = amqp.Dial(p.url) + if err != nil { + return fmt.Errorf("подключение к rabbitmq: %w", err) + } + + p.ch, err = p.conn.Channel() + if err != nil { + return fmt.Errorf("открытие канала rabbitmq: %w", err) + } + + err = p.ch.ExchangeDeclare("attachments", "direct", true, false, false, false, nil) + if err != nil { + return fmt.Errorf("объявление exchange: %w", err) + } + + _, err = p.ch.QueueDeclare("attachments.process", true, false, false, false, nil) + if err != nil { + return fmt.Errorf("объявление очереди: %w", err) + } + + err = p.ch.QueueBind("attachments.process", "process", "attachments", false, nil) + if err != nil { + return fmt.Errorf("привязка очереди: %w", err) + } + + return nil +} -- 2.54.0 From 2048203173dba918455a19b7ab8a65a07cf311eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Sat, 7 Nov 2026 15:25:45 +0300 Subject: [PATCH 16/46] feat(broker): lab5 implement publish with publisher confirms --- backend/internal/broker/rabbitmq.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/backend/internal/broker/rabbitmq.go b/backend/internal/broker/rabbitmq.go index 8cf37c24..a9162bed 100644 --- a/backend/internal/broker/rabbitmq.go +++ b/backend/internal/broker/rabbitmq.go @@ -3,6 +3,7 @@ package broker import ( "context" "fmt" + "time" amqp "github.com/rabbitmq/amqp091-go" ) @@ -39,6 +40,11 @@ func (p *RabbitPublisher) connect() error { return fmt.Errorf("открытие канала rabbitmq: %w", err) } + if err := p.ch.Confirm(false); err != nil { + return fmt.Errorf("включение режима confirm: %w", err) + } + p.confirms = p.ch.NotifyPublish(make(chan amqp.Confirmation, 1)) + err = p.ch.ExchangeDeclare("attachments", "direct", true, false, false, false, nil) if err != nil { return fmt.Errorf("объявление exchange: %w", err) @@ -56,3 +62,26 @@ func (p *RabbitPublisher) connect() error { return nil } + +func (p *RabbitPublisher) Publish(ctx context.Context, routingKey string, body []byte) error { + err := p.ch.PublishWithContext(ctx, "attachments", routingKey, false, false, amqp.Publishing{ + ContentType: "application/json", + DeliveryMode: amqp.Persistent, + Body: body, + }) + if err != nil { + return err + } + + select { + case confirm := <-p.confirms: + if !confirm.Ack { + return fmt.Errorf("сообщение отклонено брокером") + } + return nil + case <-time.After(5 * time.Second): + return fmt.Errorf("таймаут ожидания подтверждения от rabbitmq") + case <-ctx.Done(): + return ctx.Err() + } +} -- 2.54.0 From 4413c43eb530ec6276260ecf7a08e713323862fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Sat, 7 Nov 2026 15:43:22 +0300 Subject: [PATCH 17/46] feat(broker): lab5 make publisher thread-safe with reconnect and close --- backend/internal/broker/rabbitmq.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/backend/internal/broker/rabbitmq.go b/backend/internal/broker/rabbitmq.go index a9162bed..dd797526 100644 --- a/backend/internal/broker/rabbitmq.go +++ b/backend/internal/broker/rabbitmq.go @@ -3,6 +3,7 @@ package broker import ( "context" "fmt" + "sync" "time" amqp "github.com/rabbitmq/amqp091-go" @@ -15,6 +16,7 @@ type Publisher interface { type RabbitPublisher struct { url string + mu sync.Mutex conn *amqp.Connection ch *amqp.Channel confirms chan amqp.Confirmation @@ -64,6 +66,15 @@ func (p *RabbitPublisher) connect() error { } func (p *RabbitPublisher) Publish(ctx context.Context, routingKey string, body []byte) error { + p.mu.Lock() + defer p.mu.Unlock() + + if p.conn == nil || p.conn.IsClosed() { + if err := p.connect(); err != nil { + return err + } + } + err := p.ch.PublishWithContext(ctx, "attachments", routingKey, false, false, amqp.Publishing{ ContentType: "application/json", DeliveryMode: amqp.Persistent, @@ -85,3 +96,15 @@ func (p *RabbitPublisher) Publish(ctx context.Context, routingKey string, body [ return ctx.Err() } } + +func (p *RabbitPublisher) Close() error { + p.mu.Lock() + defer p.mu.Unlock() + if p.ch != nil { + _ = p.ch.Close() + } + if p.conn != nil { + return p.conn.Close() + } + return nil +} -- 2.54.0 From a7c7532622a639a717ef8d6374a34179b97fd489 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Sun, 8 Nov 2026 10:19:32 +0300 Subject: [PATCH 18/46] feat(domain): lab5 add attachment variants and job repository contracts --- backend/go.sum | 2 ++ backend/internal/domain/attachment_variant.go | 25 ++++++++++++++++++ backend/internal/domain/repository.go | 11 ++++++++ backend/migrations/0008_variants.sql | 26 +++++++++++++++++++ 4 files changed, 64 insertions(+) create mode 100644 backend/internal/domain/attachment_variant.go create mode 100644 backend/migrations/0008_variants.sql diff --git a/backend/go.sum b/backend/go.sum index ed994afb..335e366d 100644 --- a/backend/go.sum +++ b/backend/go.sum @@ -128,6 +128,8 @@ go.mongodb.org/mongo-driver/v2 v2.5.0 h1:yXUhImUjjAInNcpTcAlPHiT7bIXhshCTL3jVBkF go.mongodb.org/mongo-driver/v2 v2.5.0/go.mod h1:yOI9kBsufol30iFsl1slpdq1I0eHPzybRWdyYUs8K/0= go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE= go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y= go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= diff --git a/backend/internal/domain/attachment_variant.go b/backend/internal/domain/attachment_variant.go new file mode 100644 index 00000000..be322728 --- /dev/null +++ b/backend/internal/domain/attachment_variant.go @@ -0,0 +1,25 @@ +package domain + +import "time" + +type AttachmentVariant struct { + AttachmentID string + Name string + ObjectKey string + ContentType string + SizeBytes int64 + Width int + Height int + SHA256 []byte + CreatedAt time.Time +} + +const ( + VariantThumbnail = "thumbnail" + VariantCrop = "crop" + VariantWebP = "webp" +) + +func AllVariantNames() []string { + return []string{VariantThumbnail, VariantCrop, VariantWebP} +} diff --git a/backend/internal/domain/repository.go b/backend/internal/domain/repository.go index 7caba8ae..6ace771e 100644 --- a/backend/internal/domain/repository.go +++ b/backend/internal/domain/repository.go @@ -52,3 +52,14 @@ type OutboxRepository interface { ClaimBatch(ctx context.Context, limit int) ([]OutboxEntry, error) MarkPublished(ctx context.Context, ids []string) error } + +type AttachmentJobRepository interface { + ClaimForProcessing(ctx context.Context, id string, lease time.Duration) (*Attachment, error) + CompleteJob(ctx context.Context, id string, pipelineVersion int, variants []AttachmentVariant) (bool, error) + FailJob(ctx context.Context, id string, errorCode string) error +} + +type AttachmentVariantRepository interface { + Upsert(ctx context.Context, tx Tx, v *AttachmentVariant) error + ListByAttachment(ctx context.Context, id string) ([]AttachmentVariant, error) +} diff --git a/backend/migrations/0008_variants.sql b/backend/migrations/0008_variants.sql new file mode 100644 index 00000000..054677f6 --- /dev/null +++ b/backend/migrations/0008_variants.sql @@ -0,0 +1,26 @@ +-- +goose Up +-- +goose StatementBegin + +CREATE TABLE attachment_variants ( + attachment_id UUID NOT NULL REFERENCES attachments(id) ON DELETE CASCADE, + name TEXT NOT NULL, + object_key TEXT NOT NULL, + content_type TEXT NOT NULL, + size_bytes BIGINT NOT NULL, + width INT NOT NULL, + height INT NOT NULL, + sha256 BYTEA NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + + CONSTRAINT attachment_variants_pkey PRIMARY KEY (attachment_id, name), + CONSTRAINT attachment_variants_name_chk CHECK (name IN ('thumbnail', 'crop', 'webp')), + CONSTRAINT attachment_variants_size_chk CHECK (size_bytes > 0), + CONSTRAINT attachment_variants_sha256_len CHECK (octet_length(sha256) = 32) +); + +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin +DROP TABLE IF EXISTS attachment_variants; +-- +goose StatementEnd \ No newline at end of file -- 2.54.0 From 9fbc9da6935ce6dbb7ae292e4d2ae2e0e774bfe1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Sun, 8 Nov 2026 11:39:32 +0300 Subject: [PATCH 19/46] feat(storage): lab5 implement job claim, complete and variants upsert --- backend/internal/domain/attachment_variant.go | 4 -- .../storage/postgres/attachment_variants.go | 47 +++++++++++++++ .../internal/storage/postgres/attachments.go | 59 +++++++++++++++++++ backend/internal/storage/postgres/storage.go | 13 ++-- 4 files changed, 113 insertions(+), 10 deletions(-) create mode 100644 backend/internal/storage/postgres/attachment_variants.go diff --git a/backend/internal/domain/attachment_variant.go b/backend/internal/domain/attachment_variant.go index be322728..7e876c91 100644 --- a/backend/internal/domain/attachment_variant.go +++ b/backend/internal/domain/attachment_variant.go @@ -19,7 +19,3 @@ const ( VariantCrop = "crop" VariantWebP = "webp" ) - -func AllVariantNames() []string { - return []string{VariantThumbnail, VariantCrop, VariantWebP} -} diff --git a/backend/internal/storage/postgres/attachment_variants.go b/backend/internal/storage/postgres/attachment_variants.go new file mode 100644 index 00000000..82468eef --- /dev/null +++ b/backend/internal/storage/postgres/attachment_variants.go @@ -0,0 +1,47 @@ +package postgres + +import ( + "context" + "high-loud-apps-labs/internal/domain" + + "github.com/jackc/pgx/v5/pgxpool" +) + +type AttachmentVariantRepo struct { + pool *pgxpool.Pool +} + +func NewAttachmentVariantRepo(pool *pgxpool.Pool) *AttachmentVariantRepo { + return &AttachmentVariantRepo{pool: pool} +} + +func (r *AttachmentVariantRepo) Upsert(ctx context.Context, tx domain.Tx, v *domain.AttachmentVariant) error { + const q = ` + INSERT INTO attachment_variants + (attachment_id, name, object_key, content_type, size_bytes, width, height, sha256, created_at) + VALUES ($1::uuid, $2, $3, $4, $5, $6, $7, $8, $9) + ON CONFLICT (attachment_id, name) DO NOTHING + ` + return tx.Exec(ctx, q, v.AttachmentID, v.Name, v.ObjectKey, v.ContentType, v.SizeBytes, v.Width, v.Height, v.SHA256, v.CreatedAt) +} + +func (r *AttachmentVariantRepo) ListByAttachment(ctx context.Context, id string) ([]domain.AttachmentVariant, error) { + const q = `SELECT attachment_id, name, object_key, content_type, size_bytes, width, height, sha256, created_at FROM attachment_variants WHERE attachment_id = $1::uuid` + rows, err := r.pool.Query(ctx, q, id) + if err != nil { + return nil, mapError(err) + } + defer rows.Close() + + var out []domain.AttachmentVariant + for rows.Next() { + var v domain.AttachmentVariant + if err := rows.Scan(&v.AttachmentID, &v.Name, &v.ObjectKey, &v.ContentType, &v.SizeBytes, &v.Width, &v.Height, &v.SHA256, &v.CreatedAt); err != nil { + return nil, mapError(err) + } + out = append(out, v) + } + return out, mapError(rows.Err()) +} + +var _ domain.AttachmentVariantRepository = (*AttachmentVariantRepo)(nil) diff --git a/backend/internal/storage/postgres/attachments.go b/backend/internal/storage/postgres/attachments.go index 9fb95fed..d6fe3274 100644 --- a/backend/internal/storage/postgres/attachments.go +++ b/backend/internal/storage/postgres/attachments.go @@ -3,6 +3,7 @@ package postgres import ( "context" + "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgxpool" "high-loud-apps-labs/internal/domain" @@ -216,4 +217,62 @@ func (r *AttachmentRepo) ExistingKeys(ctx context.Context, keys []string) ([]str return out, mapError(rows.Err()) } +func (r *AttachmentRepo) ClaimForProcessing(ctx context.Context, id string, lease time.Duration) (*domain.Attachment, error) { + const q = ` + UPDATE attachments + SET status = 'processing', locked_until = now() + $2, updated_at = now() + WHERE id = $1::uuid AND status = 'queued' + RETURNING id, chat_id, staging_key, pipeline_version + ` + var a domain.Attachment + err := r.pool.QueryRow(ctx, q, id, lease).Scan(&a.ID, &a.ChatID, &a.StagingKey, &a.PipelineVersion) + if err != nil { + if err == pgx.ErrNoRows { + return nil, nil // Задача уже взята или выполнена + } + return nil, mapError(err) + } + return &a, nil +} + +func (r *AttachmentRepo) CompleteJob(ctx context.Context, id string, pipelineVersion int, variants []domain.AttachmentVariant) (bool, error) { + tx, err := r.pool.Begin(ctx) + if err != nil { + return false, mapError(err) + } + defer func() { _ = tx.Rollback(ctx) }() + + const q = ` + UPDATE attachments + SET status = 'ready', locked_until = NULL, updated_at = now() + WHERE id = $1::uuid AND pipeline_version = $2 AND status = 'processing' + ` + tag, err := tx.Exec(ctx, q, id, pipelineVersion) + if err != nil { + return false, mapError(err) + } + if tag.RowsAffected() == 0 { + return false, nil + } + + variantRepo := NewAttachmentVariantRepo(r.pool) + for _, v := range variants { + if err := variantRepo.Upsert(ctx, &pgxTx{tx: tx}, &v); err != nil { + return false, err + } + } + + return true, mapError(tx.Commit(ctx)) +} + +func (r *AttachmentRepo) FailJob(ctx context.Context, id string, errorCode string) error { + const q = ` + UPDATE attachments + SET status = 'failed', error_code = $2, locked_until = NULL, updated_at = now() + WHERE id = $1::uuid AND status = 'processing' + ` + _, err := r.pool.Exec(ctx, q, id, errorCode) + return mapError(err) +} + var _ domain.AttachmentRepository = (*AttachmentRepo)(nil) diff --git a/backend/internal/storage/postgres/storage.go b/backend/internal/storage/postgres/storage.go index c3c5abbb..db660c28 100644 --- a/backend/internal/storage/postgres/storage.go +++ b/backend/internal/storage/postgres/storage.go @@ -12,9 +12,10 @@ func New(pool *pgxpool.Pool) *Storage { func (s *Storage) Pool() *pgxpool.Pool { return s.pool } -func (s *Storage) Users() *UserRepo { return &UserRepo{pool: s.pool} } -func (s *Storage) Chats() *ChatRepo { return &ChatRepo{pool: s.pool} } -func (s *Storage) Messages() *MessageRepo { return &MessageRepo{pool: s.pool} } -func (s *Storage) Sessions() *SessionRepo { return &SessionRepo{pool: s.pool} } -func (s *Storage) Attachments() *AttachmentRepo { return &AttachmentRepo{pool: s.pool} } -func (s *Storage) Outbox() *OutboxRepo { return &OutboxRepo{pool: s.pool} } +func (s *Storage) Users() *UserRepo { return &UserRepo{pool: s.pool} } +func (s *Storage) Chats() *ChatRepo { return &ChatRepo{pool: s.pool} } +func (s *Storage) Messages() *MessageRepo { return &MessageRepo{pool: s.pool} } +func (s *Storage) Sessions() *SessionRepo { return &SessionRepo{pool: s.pool} } +func (s *Storage) Attachments() *AttachmentRepo { return &AttachmentRepo{pool: s.pool} } +func (s *Storage) Outbox() *OutboxRepo { return &OutboxRepo{pool: s.pool} } +func (s *Storage) Variants() *AttachmentVariantRepo { return &AttachmentVariantRepo{pool: s.pool} } -- 2.54.0 From d235c82b39522781bede112297a7618244e01a05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Sun, 8 Nov 2026 13:32:56 +0300 Subject: [PATCH 20/46] feat(imaging): lab5 add thumbnail generation logic --- backend/go.mod | 3 ++ backend/go.sum | 5 ++++ backend/internal/imaging/thumbnail.go | 42 +++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 backend/internal/imaging/thumbnail.go diff --git a/backend/go.mod b/backend/go.mod index b7956f03..f4b54651 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -13,12 +13,15 @@ require ( golang.org/x/crypto v0.57.0 ) +require golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 // indirect + require ( github.com/bytedance/gopkg v0.1.3 // indirect github.com/bytedance/sonic v1.15.0 // indirect github.com/bytedance/sonic/loader v0.5.0 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/cloudwego/base64x v0.1.6 // indirect + github.com/disintegration/imaging v1.6.2 github.com/dustin/go-humanize v1.0.1 // indirect github.com/gabriel-vasile/mimetype v1.4.12 // indirect github.com/gin-contrib/sse v1.1.0 // indirect diff --git a/backend/go.sum b/backend/go.sum index 335e366d..6a155a84 100644 --- a/backend/go.sum +++ b/backend/go.sum @@ -15,6 +15,8 @@ github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gE github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c= +github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4= 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/gabriel-vasile/mimetype v1.4.12 h1:e9hWvmLYvtp846tLHam2o++qitpguFiYCKbn0w9jyqw= @@ -140,12 +142,15 @@ golang.org/x/arch v0.22.0 h1:c/Zle32i5ttqRXjdLyyHZESLD/bB90DCU1g9l/0YBDI= golang.org/x/arch v0.22.0/go.mod h1:dNHoOeKiyja7GTvF9NJS1l3Z2yntpQNzgrjh1cU103A= golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M= golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA= +golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 h1:hVwzHzIUGRjiF7EcUjqNxk3NCfkPxbDKRdnNE1Rpg0U= +golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/net v0.60.0 h1:79p50tfZlm0J9YfoDsSi639qSXNGVwEzOPLCxM2FsYU= golang.org/x/net v0.60.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg= golang.org/x/sync v0.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.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.42.0 h1:JbOZXgfeCPU9gacVtYliJqOhD+zhrEqK4LfdpmlUZqI= golang.org/x/text v0.42.0/go.mod h1:ojzP1Z+2QtioaF8DTtO8K5q7JWVVYwZKenzujK0Zd0E= google.golang.org/protobuf v1.36.12 h1:pJOKDDOyeXErUroCihFAd5LQuwXBSpVnKGrj5o/fwxc= diff --git a/backend/internal/imaging/thumbnail.go b/backend/internal/imaging/thumbnail.go new file mode 100644 index 00000000..db08daa6 --- /dev/null +++ b/backend/internal/imaging/thumbnail.go @@ -0,0 +1,42 @@ +package imaging + +import ( + "bytes" + "crypto/sha256" + "fmt" + "image" + _ "image/jpeg" + _ "image/png" + + "high-loud-apps-labs/internal/domain" + + "github.com/disintegration/imaging" +) + +func Thumbnail(attachmentID string, imgData []byte) (*domain.AttachmentVariant, []byte, error) { + img, _, err := image.Decode(bytes.NewReader(imgData)) + if err != nil { + return nil, nil, fmt.Errorf("декодирование изображения: %w", err) + } + + thumb := imaging.Fit(img, 256, 256, imaging.Lanczos) + + var buf bytes.Buffer + if err := imaging.Encode(&buf, thumb, imaging.JPEG, imaging.JPEGQuality(85)); err != nil { + return nil, nil, fmt.Errorf("кодирование jpeg: %w", err) + } + + data := buf.Bytes() + sha := sha256.Sum256(data) + + v := &domain.AttachmentVariant{ + AttachmentID: attachmentID, + Name: domain.VariantThumbnail, + ContentType: "image/jpeg", + SizeBytes: int64(len(data)), + Width: thumb.Bounds().Dx(), + Height: thumb.Bounds().Dy(), + SHA256: sha[:], + } + return v, data, nil +} -- 2.54.0 From 4f3e91e50eed4ea01d46c774fd98e4b7cc7aa5af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Sun, 8 Nov 2026 15:21:12 +0300 Subject: [PATCH 21/46] feat(worker): create pipeline skeleton --- backend/internal/worker/pipeline.go | 31 +++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 backend/internal/worker/pipeline.go diff --git a/backend/internal/worker/pipeline.go b/backend/internal/worker/pipeline.go new file mode 100644 index 00000000..77150f80 --- /dev/null +++ b/backend/internal/worker/pipeline.go @@ -0,0 +1,31 @@ +package worker + +import ( + "context" + "log" + "time" + + "high-loud-apps-labs/internal/domain" +) + +type Pipeline struct { + jobs domain.AttachmentJobRepository + store domain.ObjectStorage + variants domain.AttachmentVariantRepository + lease time.Duration +} + +func NewPipeline(jobs domain.AttachmentJobRepository, store domain.ObjectStorage, variants domain.AttachmentVariantRepository) *Pipeline { + return &Pipeline{ + jobs: jobs, + store: store, + variants: variants, + lease: 5 * time.Minute, + } +} + +func (p *Pipeline) Process(ctx context.Context, attachmentID string) error { + log.Printf("worker: получена задача на обработку %s (заглушка)", attachmentID) + // TODO + return nil +} -- 2.54.0 From 2895b3fbc103e4e5f7d7b118f4a774e895653400 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Sun, 8 Nov 2026 17:02:12 +0300 Subject: [PATCH 22/46] feat(worker): lab5 implement rabbitmq consumer loop in main.go --- backend/cmd/worker/main.go | 101 +++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 backend/cmd/worker/main.go diff --git a/backend/cmd/worker/main.go b/backend/cmd/worker/main.go new file mode 100644 index 00000000..3708be5a --- /dev/null +++ b/backend/cmd/worker/main.go @@ -0,0 +1,101 @@ +package main + +import ( + "context" + "encoding/json" + "log" + "os" + "os/signal" + "syscall" + "time" + + amqp "github.com/rabbitmq/amqp091-go" + + "high-loud-apps-labs/internal/config" + "high-loud-apps-labs/internal/domain" + "high-loud-apps-labs/internal/storage/postgres" + s3store "high-loud-apps-labs/internal/storage/s3" + "high-loud-apps-labs/internal/worker" +) + +func main() { + cfg, err := config.Load() + if err != nil { + log.Fatalf("конфигурация: %v", err) + } + + ctx := context.Background() + pool, err := postgres.Connect(ctx, postgres.Config{URL: cfg.DatabaseURL}) + if err != nil { + log.Fatalf("БД: %v", err) + } + defer pool.Close() + + store := postgres.New(pool) + objectStorage, err := s3store.New(ctx, s3store.Config{ + Endpoint: cfg.S3Endpoint, + Bucket: cfg.S3Bucket, + AccessKey: cfg.S3AccessKey, + SecretKey: cfg.S3SecretKey, + UseSSL: cfg.S3UseSSL, + Region: cfg.S3Region, + }) + if err != nil { + log.Fatalf("S3: %v", err) + } + + pipeline := worker.NewPipeline(store.Attachments(), objectStorage, store.Variants()) + + conn, err := amqp.Dial(cfg.RabbitMQURL) + if err != nil { + log.Fatalf("RabbitMQ: %v", err) + } + defer conn.Close() + + ch, err := conn.Channel() + if err != nil { + log.Fatalf("RabbitMQ channel: %v", err) + } + defer ch.Close() + + // Prefetch: сколько сообщений worker берет разом (защита от перегрузки) + ch.Qos(2, 0, false) + + q, err := ch.QueueDeclare("attachments.process", true, false, false, false, nil) + if err != nil { + log.Fatalf("RabbitMQ queue: %v", err) + } + + // autoAck = false (ручное подтверждение) + msgs, err := ch.Consume(q.Name, "", false, false, false, false, nil) + if err != nil { + log.Fatalf("RabbitMQ consume: %v", err) + } + + log.Println("worker: запущен и слушает очередь") + + quit := make(chan os.Signal, 1) + signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) + + go func() { + for d := range msgs { + var msg domain.JobMessage + if err := json.Unmarshal(d.Body, &msg); err != nil { + log.Printf("worker: битое сообщение: %v", err) + _ = d.Nack(false, false) // в DLQ + continue + } + + if err := pipeline.Process(ctx, msg.AttachmentID); err != nil { + log.Printf("worker: ошибка %s: %v", msg.AttachmentID, err) + _ = d.Nack(false, true) // requeue + } else { + _ = d.Ack(false) + } + } + }() + + <-quit + log.Println("worker: получен сигнал завершения, останавливаемся...") + time.Sleep(1 * time.Second) +} -- 2.54.0 From 693d92f93d7774dc7a5bf8de868ddca69cbfb4ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Sun, 8 Nov 2026 19:23:11 +0300 Subject: [PATCH 23/46] feat(infra): lab5 add worker service to docker compose --- Dockerfile | 7 +++++ compose.yaml | 88 +++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 70 insertions(+), 25 deletions(-) diff --git a/Dockerfile b/Dockerfile index 16229253..a9b29baf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,11 +13,18 @@ RUN CGO_ENABLED=0 GOOS=linux go build \ -o /out/server \ ./cmd/server +RUN CGO_ENABLED=0 GOOS=linux go build \ + -trimpath \ + -ldflags="-s -w" \ + -o /out/worker \ + ./cmd/worker + FROM alpine:3.20 RUN adduser -D -u 1000 app COPY --from=builder /out/server /server +COPY --from=builder /out/worker /worker EXPOSE 8080 diff --git a/compose.yaml b/compose.yaml index 511ea1d5..dea121f8 100644 --- a/compose.yaml +++ b/compose.yaml @@ -64,29 +64,27 @@ services: networks: - internal -rabbitmq: - image: rabbitmq:3.13-management-alpine - restart: unless-stopped - environment: - RABBITMQ_DEFAULT_USER: "messenger" - RABBITMQ_DEFAULT_PASS_FILE: /run/secrets/rabbitmq_password - secrets: - - rabbitmq_password - volumes: - - rabbitmq_data:/var/lib/rabbitmq - ports: - - "15672:15672" - healthcheck: - test: ["CMD", "rabbitmq-diagnostics", "-q", "ping"] - interval: 5s - timeout: 3s - retries: 10 - deploy: - resources: - limits: - memory: 256M - networks: - - internal + rabbitmq: + image: rabbitmq:3.13-management-alpine + restart: unless-stopped + environment: + RABBITMQ_DEFAULT_USER: "messenger" + RABBITMQ_DEFAULT_PASS: "${RABBITMQ_PASSWORD}" + volumes: + - rabbitmq_data:/var/lib/rabbitmq + ports: + - "15672:15672" + healthcheck: + test: ["CMD", "rabbitmq-diagnostics", "-q", "ping"] + interval: 5s + timeout: 3s + retries: 10 + deploy: + resources: + limits: + memory: 256M + networks: + - internal minio: image: pgsty/silo:RELEASE.2026-09-16T00-00-00Z @@ -190,6 +188,8 @@ rabbitmq: RECONCILER_ENABLED: "${RECONCILER_ENABLED:-true}" RECONCILER_INTERVAL_SECONDS: "${RECONCILER_INTERVAL_SECONDS:-300}" RECONCILER_GRACE_SECONDS: "${RECONCILER_GRACE_SECONDS:-3600}" + RABBITMQ_URL: "amqp://messenger:${RABBITMQ_PASSWORD}@rabbitmq:5672/" + OUTBOX_INTERVAL_MS: "1000" secrets: - database_url - cache_url @@ -200,6 +200,8 @@ rabbitmq: condition: service_healthy redis: condition: service_healthy + rabbitmq: + condition: service_healthy migrate: condition: service_completed_successfully minio: @@ -221,6 +223,43 @@ rabbitmq: networks: - internal + worker: + build: + context: . + dockerfile: Dockerfile + image: mtusi-messenger-api:lab3 + command: ["/worker"] + restart: unless-stopped + environment: + DATABASE_URL_FILE: /run/secrets/database_url + S3_ENDPOINT: "http://minio:9000" + S3_BUCKET: "${S3_BUCKET:-messenger-attachments}" + S3_REGION: "${S3_REGION:-us-east-1}" + S3_USE_SSL: "false" + S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id + S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key + RABBITMQ_URL: "amqp://messenger:${RABBITMQ_PASSWORD}@rabbitmq:5672/" + secrets: + - database_url + - s3_access_key_id + - s3_secret_access_key + depends_on: + postgres: + condition: service_healthy + migrate: + condition: service_completed_successfully + minio: + condition: service_healthy + rabbitmq: + condition: service_healthy + deploy: + replicas: 1 + resources: + limits: + memory: 256M + networks: + - internal + lb: image: nginx:1.27.4-alpine restart: unless-stopped @@ -254,8 +293,7 @@ secrets: file: ./secrets/s3_access_key_id s3_secret_access_key: file: ./secrets/s3_secret_access_key - rabbitmq_password: - file: ./secrets/rabbitmq_password + volumes: postgres_data: redis_data: -- 2.54.0 From 4c979dcc7148a1ac95763543b62c5193a205ff70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Sun, 8 Nov 2026 21:34:04 +0300 Subject: [PATCH 24/46] feat(worker): lab5 implement full pipeline with thumbnail generation --- backend/internal/worker/pipeline.go | 67 +++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 3 deletions(-) diff --git a/backend/internal/worker/pipeline.go b/backend/internal/worker/pipeline.go index 77150f80..a13a4f2b 100644 --- a/backend/internal/worker/pipeline.go +++ b/backend/internal/worker/pipeline.go @@ -1,11 +1,15 @@ package worker import ( + "bytes" "context" + "fmt" + "io" "log" "time" "high-loud-apps-labs/internal/domain" + "high-loud-apps-labs/internal/imaging" ) type Pipeline struct { @@ -20,12 +24,69 @@ func NewPipeline(jobs domain.AttachmentJobRepository, store domain.ObjectStorage jobs: jobs, store: store, variants: variants, - lease: 5 * time.Minute, + lease: 5 * time.Minute, // Lease на 5 минут, чтобы избежать дублей } } func (p *Pipeline) Process(ctx context.Context, attachmentID string) error { - log.Printf("worker: получена задача на обработку %s (заглушка)", attachmentID) - // TODO + log.Printf("worker: начало обработки %s", attachmentID) + + // 1. Забираем задачу (защита от дублей). Если уже взята или готова — nil. + a, err := p.jobs.ClaimForProcessing(ctx, attachmentID, p.lease) + if err != nil { + return fmt.Errorf("захват задачи: %w", err) + } + if a == nil { + log.Printf("worker: задача %s уже взята или выполнена", attachmentID) + return nil // no-op + } + + // 2. Скачиваем оригинал из staging (куда его положил API) + rc, _, _, err := p.store.Get(ctx, a.StagingKey) + if err != nil { + _ = p.jobs.FailJob(ctx, a.ID, "storage_error") + return fmt.Errorf("скачивание staging: %w", err) + } + defer rc.Close() + + buf, err := io.ReadAll(rc) + if err != nil { + _ = p.jobs.FailJob(ctx, a.ID, "storage_error") + return fmt.Errorf("чтение staging: %w", err) + } + + // 3. Генерируем thumbnail + thumbMeta, thumbData, err := imaging.Thumbnail(a.ID, buf) + if err != nil { + _ = p.jobs.FailJob(ctx, a.ID, "decode_failed") + return fmt.Errorf("генерация thumbnail: %w", err) + } + + // 4. Сохраняем оригинал в финальный prefix (chats/...) + // Внимание: a.ObjectKey уже содержит "chats/{chat_id}/{id}", заданный в API + if err := p.store.Put(ctx, a.ObjectKey, bytes.NewReader(buf), int64(len(buf)), "image/jpeg"); err != nil { + _ = p.jobs.FailJob(ctx, a.ID, "storage_error") + return fmt.Errorf("загрузка оригинала: %w", err) + } + + // 5. Сохраняем thumbnail в S3 + thumbKey := fmt.Sprintf("%s/thumbnail", a.ObjectKey) + if err := p.store.Put(ctx, thumbKey, bytes.NewReader(thumbData), thumbMeta.SizeBytes, thumbMeta.ContentType); err != nil { + _ = p.jobs.FailJob(ctx, a.ID, "storage_error") + return fmt.Errorf("загрузка thumbnail: %w", err) + } + thumbMeta.ObjectKey = thumbKey + + // 6. Фиксируем результат в БД (переводим в ready) + ok, err := p.jobs.CompleteJob(ctx, a.ID, a.PipelineVersion, []domain.AttachmentVariant{*thumbMeta}) + if err != nil { + return fmt.Errorf("фиксация в БД: %w", err) + } + if !ok { + log.Printf("worker: версия устарела или статус изменился, пропускаем %s", a.ID) + return nil + } + + log.Printf("worker: успешно завершено %s", a.ID) return nil } -- 2.54.0 From af9e94080e612c26996d9e95cc4ab307b1d3a039 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Sun, 8 Nov 2026 22:12:14 +0300 Subject: [PATCH 25/46] fix(infra): lab5 fix docker entrypoints, migration index and worker deps --- Dockerfile | 2 +- backend/migrations/0007_drop_pending.sql | 1 + compose.yaml | 4 +++- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index a9b29baf..326f4051 100644 --- a/Dockerfile +++ b/Dockerfile @@ -33,4 +33,4 @@ USER app HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \ CMD wget -q -O - http://127.0.0.1:8080/health/live >/dev/null || exit 1 -ENTRYPOINT ["/server"] \ No newline at end of file +CMD ["/server"] \ No newline at end of file diff --git a/backend/migrations/0007_drop_pending.sql b/backend/migrations/0007_drop_pending.sql index dcc2d570..de112c91 100644 --- a/backend/migrations/0007_drop_pending.sql +++ b/backend/migrations/0007_drop_pending.sql @@ -13,6 +13,7 @@ ALTER TABLE attachments ADD CONSTRAINT attachments_error_code_consistency ); DROP INDEX IF EXISTS attachments_pending_idx; +DROP INDEX IF EXISTS attachments_active_idx; CREATE INDEX attachments_active_idx ON attachments (created_at) WHERE status IN ('queued', 'processing'); diff --git a/compose.yaml b/compose.yaml index dea121f8..f9517356 100644 --- a/compose.yaml +++ b/compose.yaml @@ -143,7 +143,7 @@ services: dockerfile: Dockerfile image: mtusi-messenger-api:lab3 restart: "no" - command: ["migrate"] + command: ["/server", "migrate"] environment: DATABASE_URL_FILE: /run/secrets/database_url secrets: @@ -250,6 +250,8 @@ services: condition: service_completed_successfully minio: condition: service_healthy + mc-init: + condition: service_completed_successfully rabbitmq: condition: service_healthy deploy: -- 2.54.0 From 872406773ff005baef76667326bdefc61815ff30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Mon, 9 Nov 2026 09:12:48 +0300 Subject: [PATCH 26/46] feat(outbox): lab5 run outbox background publisher in api server --- backend/cmd/server/main.go | 15 ++++++ backend/internal/outbox/publisher.go | 72 ++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 backend/internal/outbox/publisher.go diff --git a/backend/cmd/server/main.go b/backend/cmd/server/main.go index abadff16..66ab20a1 100644 --- a/backend/cmd/server/main.go +++ b/backend/cmd/server/main.go @@ -13,11 +13,13 @@ import ( "syscall" "time" + "high-loud-apps-labs/internal/broker" "high-loud-apps-labs/internal/config" "high-loud-apps-labs/internal/domain" "high-loud-apps-labs/internal/httpapi" "high-loud-apps-labs/internal/httpapi/handler" "high-loud-apps-labs/internal/httpapi/middleware" + "high-loud-apps-labs/internal/outbox" "high-loud-apps-labs/internal/service" "high-loud-apps-labs/internal/storage/postgres" redisstore "high-loud-apps-labs/internal/storage/redis" @@ -55,6 +57,17 @@ func main() { store := postgres.New(pool) + rabbitPub, err := broker.NewRabbitPublisher(cfg.RabbitMQURL) + if err != nil { + log.Fatalf("rabbitmq: %v", err) + } + defer rabbitPub.Close() + + outboxPub := outbox.NewPublisher(store.Outbox(), rabbitPub, 50) + outboxCtx, outboxCancel := context.WithCancel(context.Background()) + defer outboxCancel() + go outboxPub.Run(outboxCtx, cfg.OutboxInterval) + sessionRepo, cachePing, closeSession := newSessionRepo(cfg, store) defer closeSession() @@ -145,6 +158,8 @@ func main() { log.Printf("Получен сигнал завершения; readiness выключен, drain %s", cfg.ShutdownDrain) ready.Store(false) + + outboxCancel() reconcilerCancel() if cfg.ShutdownDrain > 0 { diff --git a/backend/internal/outbox/publisher.go b/backend/internal/outbox/publisher.go new file mode 100644 index 00000000..2eb5ef29 --- /dev/null +++ b/backend/internal/outbox/publisher.go @@ -0,0 +1,72 @@ +package outbox + +import ( + "context" + "log" + "time" + + "high-loud-apps-labs/internal/broker" + "high-loud-apps-labs/internal/domain" +) + +type Publisher struct { + repo domain.OutboxRepository + broker broker.Publisher + batch int +} + +func NewPublisher(repo domain.OutboxRepository, b broker.Publisher, batch int) *Publisher { + if batch <= 0 { + batch = 50 + } + return &Publisher{repo: repo, broker: b, batch: batch} +} + +func (p *Publisher) Run(ctx context.Context, interval time.Duration) { + if interval <= 0 { + interval = 1 * time.Second + } + ticker := time.NewTicker(interval) + defer ticker.Stop() + + log.Printf("outbox: publisher запущен (interval=%s)", interval) + + for { + select { + case <-ctx.Done(): + log.Println("outbox: publisher остановлен") + return + case <-ticker.C: + p.tick(ctx) + } + } +} + +func (p *Publisher) tick(ctx context.Context) { + entries, err := p.repo.ClaimBatch(ctx, p.batch) + if err != nil { + log.Printf("outbox: ошибка ClaimBatch: %v", err) + return + } + if len(entries) == 0 { + return + } + + var successIDs []string + for _, e := range entries { + err := p.broker.Publish(ctx, "process", e.Payload) + if err != nil { + log.Printf("outbox: ошибка публикации %s: %v", e.ID, err) + break // Прерываем пачку + } + successIDs = append(successIDs, e.ID) + } + + if len(successIDs) > 0 { + if err := p.repo.MarkPublished(ctx, successIDs); err != nil { + log.Printf("outbox: ошибка MarkPublished: %v", err) + } else { + log.Printf("outbox: успешно опубликовано %d сообщений", len(successIDs)) + } + } +} -- 2.54.0 From 22c876f63c8f59bab69a5867ef8495c1795b599e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Mon, 9 Nov 2026 12:39:59 +0300 Subject: [PATCH 27/46] feat(worker): lab5 implement thumbnail generation and complete job logic --- backend/internal/imaging/thumbnail.go | 2 + .../internal/storage/postgres/attachments.go | 40 ++++++++++++++----- backend/internal/worker/pipeline.go | 3 +- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/backend/internal/imaging/thumbnail.go b/backend/internal/imaging/thumbnail.go index db08daa6..a36028f6 100644 --- a/backend/internal/imaging/thumbnail.go +++ b/backend/internal/imaging/thumbnail.go @@ -9,6 +9,7 @@ import ( _ "image/png" "high-loud-apps-labs/internal/domain" + "time" "github.com/disintegration/imaging" ) @@ -37,6 +38,7 @@ func Thumbnail(attachmentID string, imgData []byte) (*domain.AttachmentVariant, Width: thumb.Bounds().Dx(), Height: thumb.Bounds().Dy(), SHA256: sha[:], + CreatedAt: time.Now().UTC(), } return v, data, nil } diff --git a/backend/internal/storage/postgres/attachments.go b/backend/internal/storage/postgres/attachments.go index d6fe3274..42ec3b22 100644 --- a/backend/internal/storage/postgres/attachments.go +++ b/backend/internal/storage/postgres/attachments.go @@ -2,16 +2,15 @@ package postgres import ( "context" + "log" + "time" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgxpool" "high-loud-apps-labs/internal/domain" - "time" ) -// pending -> ready (успешный upload) -// pending -> failed (отказ upload или reconciler) type AttachmentRepo struct { pool *pgxpool.Pool } @@ -88,10 +87,31 @@ func (r *AttachmentRepo) GetByID(ctx context.Context, id string) (*domain.Attach return nil, notFound(err, "attachment", id) } a.Status = domain.AttachmentStatus(status) + + rows, err := r.pool.Query(ctx, ` + SELECT name, object_key, content_type, size_bytes, width, height, sha256, created_at + FROM attachment_variants + WHERE attachment_id = $1::uuid + ORDER BY name + `, id) + if err != nil { + log.Printf("GetByID: ошибка запроса variants: %v", err) + } else { + defer rows.Close() + for rows.Next() { + var v domain.AttachmentVariant + v.AttachmentID = id + if err := rows.Scan(&v.Name, &v.ObjectKey, &v.ContentType, &v.SizeBytes, &v.Width, &v.Height, &v.SHA256, &v.CreatedAt); err != nil { + log.Printf("GetByID: ошибка Scan variant: %v", err) + } else { + a.Variants = append(a.Variants, v) + } + } + } + return a, nil } -// MarkReady переводит pending -> ready. Идемпотентна. func (r *AttachmentRepo) MarkReady(ctx context.Context, id string) error { const q = ` UPDATE attachments @@ -114,7 +134,7 @@ func (r *AttachmentRepo) MarkReady(ctx context.Context, id string) error { } switch domain.AttachmentStatus(status) { case domain.AttachmentStatusReady: - return nil // idempotent no-op + return nil case domain.AttachmentStatusFailed: return &domain.ConflictError{Reason: "нельзя перевести failed-вложение в ready"} default: @@ -122,7 +142,6 @@ func (r *AttachmentRepo) MarkReady(ctx context.Context, id string) error { } } -// MarkFailed переводит pending -> failed. Идемпотентна. func (r *AttachmentRepo) MarkFailed(ctx context.Context, id, errorCode string) error { if errorCode == "" { return &domain.ValidationError{Field: "error_code", Message: "не может быть пустым"} @@ -222,13 +241,13 @@ func (r *AttachmentRepo) ClaimForProcessing(ctx context.Context, id string, leas UPDATE attachments SET status = 'processing', locked_until = now() + $2, updated_at = now() WHERE id = $1::uuid AND status = 'queued' - RETURNING id, chat_id, staging_key, pipeline_version + RETURNING id, chat_id, staging_key, pipeline_version, object_key, content_type ` var a domain.Attachment - err := r.pool.QueryRow(ctx, q, id, lease).Scan(&a.ID, &a.ChatID, &a.StagingKey, &a.PipelineVersion) + err := r.pool.QueryRow(ctx, q, id, lease).Scan(&a.ID, &a.ChatID, &a.StagingKey, &a.PipelineVersion, &a.ObjectKey, &a.ContentType) if err != nil { if err == pgx.ErrNoRows { - return nil, nil // Задача уже взята или выполнена + return nil, nil } return nil, mapError(err) } @@ -258,7 +277,10 @@ func (r *AttachmentRepo) CompleteJob(ctx context.Context, id string, pipelineVer variantRepo := NewAttachmentVariantRepo(r.pool) for _, v := range variants { if err := variantRepo.Upsert(ctx, &pgxTx{tx: tx}, &v); err != nil { + log.Printf("CompleteJob: ошибка Upsert %s: %v", v.Name, err) return false, err + } else { + log.Printf("CompleteJob: успешно добавлен вариант %s", v.Name) } } diff --git a/backend/internal/worker/pipeline.go b/backend/internal/worker/pipeline.go index a13a4f2b..3dc5b137 100644 --- a/backend/internal/worker/pipeline.go +++ b/backend/internal/worker/pipeline.go @@ -63,8 +63,7 @@ func (p *Pipeline) Process(ctx context.Context, attachmentID string) error { } // 4. Сохраняем оригинал в финальный prefix (chats/...) - // Внимание: a.ObjectKey уже содержит "chats/{chat_id}/{id}", заданный в API - if err := p.store.Put(ctx, a.ObjectKey, bytes.NewReader(buf), int64(len(buf)), "image/jpeg"); err != nil { + if err := p.store.Put(ctx, a.ObjectKey, bytes.NewReader(buf), int64(len(buf)), a.ContentType); err != nil { _ = p.jobs.FailJob(ctx, a.ID, "storage_error") return fmt.Errorf("загрузка оригинала: %w", err) } -- 2.54.0 From bb5f2fb23732a7e23101ab6c6cf3c3b03380f02e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Mon, 9 Nov 2026 14:51:17 +0300 Subject: [PATCH 28/46] feat(api): lab5 serve attachment variants in json and content endpoints --- backend/internal/domain/entity.go | 2 ++ backend/internal/httpapi/dto/attachment.go | 12 +++++++++++- backend/internal/httpapi/handler/attachment.go | 6 +----- backend/internal/service/attachment.go | 15 +++++++++++++-- 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/backend/internal/domain/entity.go b/backend/internal/domain/entity.go index 400e02c2..08f30960 100644 --- a/backend/internal/domain/entity.go +++ b/backend/internal/domain/entity.go @@ -65,4 +65,6 @@ type Attachment struct { LockedUntil *time.Time // lease; nil = не арендовано PipelineVersion int // guard в CompleteJob; в БД default 1 StagingKey string // S3-ключ оригинала до 202; "" в lab4-пути + + Variants []AttachmentVariant } diff --git a/backend/internal/httpapi/dto/attachment.go b/backend/internal/httpapi/dto/attachment.go index b608954a..0ce882b1 100644 --- a/backend/internal/httpapi/dto/attachment.go +++ b/backend/internal/httpapi/dto/attachment.go @@ -31,7 +31,17 @@ type AttachmentResponse struct { } func AttachmentFromDomain(a *domain.Attachment) AttachmentResponse { - variants := make([]VariantResponse, 0) + variants := make([]VariantResponse, len(a.Variants)) + for i, v := range a.Variants { + variants[i] = VariantResponse{ + Name: v.Name, + ContentType: v.ContentType, + SizeBytes: v.SizeBytes, + Width: v.Width, + Height: v.Height, + SHA256: hex.EncodeToString(v.SHA256), + } + } errCode := a.ErrorCode return AttachmentResponse{ ID: a.ID, diff --git a/backend/internal/httpapi/handler/attachment.go b/backend/internal/httpapi/handler/attachment.go index e390c6fc..24e9574f 100644 --- a/backend/internal/httpapi/handler/attachment.go +++ b/backend/internal/httpapi/handler/attachment.go @@ -82,12 +82,8 @@ func (h *AttachmentHandler) Content(c *gin.Context) { } variant := c.DefaultQuery("variant", "original") - if variant != "original" { - respondError(c, &domain.NotFoundError{Entity: "variant", ID: variant}) - return - } - rc, size, contentType, err := h.svc.Download(c.Request.Context(), me.ID, attachmentID) + rc, size, contentType, err := h.svc.Download(c.Request.Context(), me.ID, attachmentID, variant) if err != nil { respondError(c, err) return diff --git a/backend/internal/service/attachment.go b/backend/internal/service/attachment.go index 5a7ff696..358ed47a 100644 --- a/backend/internal/service/attachment.go +++ b/backend/internal/service/attachment.go @@ -167,7 +167,7 @@ func (s *AttachmentService) Get(ctx context.Context, userID, attachmentID string func (s *AttachmentService) Download( ctx context.Context, - userID, attachmentID string, + userID, attachmentID, variantName string, ) (io.ReadCloser, int64, string, error) { a, err := s.Get(ctx, userID, attachmentID) if err != nil { @@ -176,7 +176,18 @@ func (s *AttachmentService) Download( if a.Status != domain.AttachmentStatusReady { return nil, 0, "", &domain.ConflictError{Reason: "вложение не готово"} } - return s.storage.Get(ctx, a.ObjectKey) + + if variantName == "original" || variantName == "" { + return s.storage.Get(ctx, a.ObjectKey) + } + + for _, v := range a.Variants { + if v.Name == variantName { + return s.storage.Get(ctx, v.ObjectKey) + } + } + + return nil, 0, "", &domain.NotFoundError{Entity: "variant", ID: variantName} } func (s *AttachmentService) ensureMember(ctx context.Context, userID, chatID string) error { -- 2.54.0 From d0153a88b68aa86ded5a930063a857fb5bc957ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Mon, 9 Nov 2026 15:50:12 +0300 Subject: [PATCH 29/46] feat(imaging): lab5 add crop 128x128 generation --- backend/internal/imaging/crop.go | 44 ++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 backend/internal/imaging/crop.go diff --git a/backend/internal/imaging/crop.go b/backend/internal/imaging/crop.go new file mode 100644 index 00000000..49991efd --- /dev/null +++ b/backend/internal/imaging/crop.go @@ -0,0 +1,44 @@ +package imaging + +import ( + "bytes" + "crypto/sha256" + "fmt" + "image" + _ "image/jpeg" + _ "image/png" + "time" + + "high-loud-apps-labs/internal/domain" + + "github.com/disintegration/imaging" +) + +func Crop(attachmentID string, imgData []byte) (*domain.AttachmentVariant, []byte, error) { + img, _, err := image.Decode(bytes.NewReader(imgData)) + if err != nil { + return nil, nil, fmt.Errorf("декодирование изображения: %w", err) + } + + cropImg := imaging.Fill(img, 128, 128, imaging.Center, imaging.Lanczos) + + var buf bytes.Buffer + if err := imaging.Encode(&buf, cropImg, imaging.JPEG, imaging.JPEGQuality(85)); err != nil { + return nil, nil, fmt.Errorf("кодирование jpeg: %w", err) + } + + data := buf.Bytes() + sha := sha256.Sum256(data) + + v := &domain.AttachmentVariant{ + AttachmentID: attachmentID, + Name: domain.VariantCrop, + ContentType: "image/jpeg", + SizeBytes: int64(len(data)), + Width: cropImg.Bounds().Dx(), + Height: cropImg.Bounds().Dy(), + SHA256: sha[:], + CreatedAt: time.Now().UTC(), + } + return v, data, nil +} -- 2.54.0 From b8d6e0efda9b6ba55aa30a36801ed2f46d047304 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Mon, 9 Nov 2026 16:19:33 +0300 Subject: [PATCH 30/46] feat(imaging): lab5 add webp optimization and strip metadata --- backend/go.mod | 6 ++++- backend/go.sum | 4 +++ backend/internal/imaging/webp.go | 42 ++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 backend/internal/imaging/webp.go diff --git a/backend/go.mod b/backend/go.mod index f4b54651..1fa34b47 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -13,7 +13,10 @@ require ( golang.org/x/crypto v0.57.0 ) -require golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 // indirect +require ( + github.com/ebitengine/purego v0.10.1 // indirect + golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 // indirect +) require ( github.com/bytedance/gopkg v0.1.3 // indirect @@ -24,6 +27,7 @@ require ( github.com/disintegration/imaging v1.6.2 github.com/dustin/go-humanize v1.0.1 // indirect github.com/gabriel-vasile/mimetype v1.4.12 // indirect + github.com/gen2brain/webp v0.6.4 github.com/gin-contrib/sse v1.1.0 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect diff --git a/backend/go.sum b/backend/go.sum index 6a155a84..73ba6e94 100644 --- a/backend/go.sum +++ b/backend/go.sum @@ -19,8 +19,12 @@ github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1 github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= +github.com/ebitengine/purego v0.10.1 h1:dewVBCBT2GaMu1SrNTYxQhgQBethzfhiwvZiLGP/qyY= +github.com/ebitengine/purego v0.10.1/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ= github.com/gabriel-vasile/mimetype v1.4.12 h1:e9hWvmLYvtp846tLHam2o++qitpguFiYCKbn0w9jyqw= github.com/gabriel-vasile/mimetype v1.4.12/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s= +github.com/gen2brain/webp v0.6.4 h1:SUDdmxADOAiPQ+5ylNmuHhuYf2dOi0KgKZHL5vpVCNU= +github.com/gen2brain/webp v0.6.4/go.mod h1:iGWMaCSw7t3I/Cv9llzEKmpnR36S8lS8VL/ZVjxU0JE= github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w= github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM= github.com/gin-gonic/gin v1.12.0 h1:b3YAbrZtnf8N//yjKeU2+MQsh2mY5htkZidOM7O0wG8= diff --git a/backend/internal/imaging/webp.go b/backend/internal/imaging/webp.go new file mode 100644 index 00000000..724743dc --- /dev/null +++ b/backend/internal/imaging/webp.go @@ -0,0 +1,42 @@ +package imaging + +import ( + "bytes" + "crypto/sha256" + "fmt" + "image" + _ "image/jpeg" + _ "image/png" + "time" + + "high-loud-apps-labs/internal/domain" + + "github.com/gen2brain/webp" +) + +func WebP(attachmentID string, imgData []byte) (*domain.AttachmentVariant, []byte, error) { + img, _, err := image.Decode(bytes.NewReader(imgData)) + if err != nil { + return nil, nil, fmt.Errorf("декодирование изображения: %w", err) + } + + var buf bytes.Buffer + if err := webp.Encode(&buf, img, webp.Options{Quality: 80}); err != nil { + return nil, nil, fmt.Errorf("кодирование webp: %w", err) + } + + data := buf.Bytes() + sha := sha256.Sum256(data) + + v := &domain.AttachmentVariant{ + AttachmentID: attachmentID, + Name: domain.VariantWebP, + ContentType: "image/webp", + SizeBytes: int64(len(data)), + Width: img.Bounds().Dx(), + Height: img.Bounds().Dy(), + SHA256: sha[:], + CreatedAt: time.Now().UTC(), + } + return v, data, nil +} -- 2.54.0 From 9b28579031aa556988c28eb6ee3de6ceb03039ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Mon, 9 Nov 2026 17:28:59 +0300 Subject: [PATCH 31/46] feat(imaging): lab5 add optimized webp generation and update enums --- backend/internal/domain/attachment_variant.go | 2 +- backend/internal/imaging/{webp.go => optimized.go} | 13 ++++++++----- backend/migrations/0008_variants.sql | 2 +- 3 files changed, 10 insertions(+), 7 deletions(-) rename backend/internal/imaging/{webp.go => optimized.go} (63%) diff --git a/backend/internal/domain/attachment_variant.go b/backend/internal/domain/attachment_variant.go index 7e876c91..1ec512dd 100644 --- a/backend/internal/domain/attachment_variant.go +++ b/backend/internal/domain/attachment_variant.go @@ -17,5 +17,5 @@ type AttachmentVariant struct { const ( VariantThumbnail = "thumbnail" VariantCrop = "crop" - VariantWebP = "webp" + VariantOptimized = "optimized" ) diff --git a/backend/internal/imaging/webp.go b/backend/internal/imaging/optimized.go similarity index 63% rename from backend/internal/imaging/webp.go rename to backend/internal/imaging/optimized.go index 724743dc..5876428c 100644 --- a/backend/internal/imaging/webp.go +++ b/backend/internal/imaging/optimized.go @@ -11,17 +11,20 @@ import ( "high-loud-apps-labs/internal/domain" + "github.com/disintegration/imaging" "github.com/gen2brain/webp" ) -func WebP(attachmentID string, imgData []byte) (*domain.AttachmentVariant, []byte, error) { +func Optimized(attachmentID string, imgData []byte) (*domain.AttachmentVariant, []byte, error) { img, _, err := image.Decode(bytes.NewReader(imgData)) if err != nil { return nil, nil, fmt.Errorf("декодирование изображения: %w", err) } + optimizedImg := imaging.Fit(img, 1280, 1280, imaging.Lanczos) + var buf bytes.Buffer - if err := webp.Encode(&buf, img, webp.Options{Quality: 80}); err != nil { + if err := webp.Encode(&buf, optimizedImg, webp.Options{Quality: 80}); err != nil { return nil, nil, fmt.Errorf("кодирование webp: %w", err) } @@ -30,11 +33,11 @@ func WebP(attachmentID string, imgData []byte) (*domain.AttachmentVariant, []byt v := &domain.AttachmentVariant{ AttachmentID: attachmentID, - Name: domain.VariantWebP, + Name: domain.VariantOptimized, ContentType: "image/webp", SizeBytes: int64(len(data)), - Width: img.Bounds().Dx(), - Height: img.Bounds().Dy(), + Width: optimizedImg.Bounds().Dx(), + Height: optimizedImg.Bounds().Dy(), SHA256: sha[:], CreatedAt: time.Now().UTC(), } diff --git a/backend/migrations/0008_variants.sql b/backend/migrations/0008_variants.sql index 054677f6..8382941b 100644 --- a/backend/migrations/0008_variants.sql +++ b/backend/migrations/0008_variants.sql @@ -13,7 +13,7 @@ CREATE TABLE attachment_variants ( created_at TIMESTAMPTZ NOT NULL DEFAULT now(), CONSTRAINT attachment_variants_pkey PRIMARY KEY (attachment_id, name), - CONSTRAINT attachment_variants_name_chk CHECK (name IN ('thumbnail', 'crop', 'webp')), + CONSTRAINT attachment_variants_name_chk CHECK (name IN ('thumbnail', 'crop', 'optimized')), CONSTRAINT attachment_variants_size_chk CHECK (size_bytes > 0), CONSTRAINT attachment_variants_sha256_len CHECK (octet_length(sha256) = 32) ); -- 2.54.0 From 295371c326ae2feae0afd6b1800992de318aff01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Mon, 9 Nov 2026 19:12:21 +0300 Subject: [PATCH 32/46] feat(worker): lab5 generate all variants and enforce execution timeout --- backend/internal/worker/pipeline.go | 66 +++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 17 deletions(-) diff --git a/backend/internal/worker/pipeline.go b/backend/internal/worker/pipeline.go index 3dc5b137..ea7e011d 100644 --- a/backend/internal/worker/pipeline.go +++ b/backend/internal/worker/pipeline.go @@ -24,15 +24,18 @@ func NewPipeline(jobs domain.AttachmentJobRepository, store domain.ObjectStorage jobs: jobs, store: store, variants: variants, - lease: 5 * time.Minute, // Lease на 5 минут, чтобы избежать дублей + lease: 5 * time.Minute, } } func (p *Pipeline) Process(ctx context.Context, attachmentID string) error { log.Printf("worker: начало обработки %s", attachmentID) + jobCtx, cancel := context.WithTimeout(ctx, 30*time.Second) + defer cancel() + // 1. Забираем задачу (защита от дублей). Если уже взята или готова — nil. - a, err := p.jobs.ClaimForProcessing(ctx, attachmentID, p.lease) + a, err := p.jobs.ClaimForProcessing(jobCtx, attachmentID, p.lease) if err != nil { return fmt.Errorf("захват задачи: %w", err) } @@ -41,43 +44,72 @@ func (p *Pipeline) Process(ctx context.Context, attachmentID string) error { return nil // no-op } - // 2. Скачиваем оригинал из staging (куда его положил API) - rc, _, _, err := p.store.Get(ctx, a.StagingKey) + // 2. Скачиваем оригинал из staging + rc, _, _, err := p.store.Get(jobCtx, a.StagingKey) if err != nil { - _ = p.jobs.FailJob(ctx, a.ID, "storage_error") + _ = p.jobs.FailJob(context.Background(), a.ID, "storage_error") return fmt.Errorf("скачивание staging: %w", err) } defer rc.Close() buf, err := io.ReadAll(rc) if err != nil { - _ = p.jobs.FailJob(ctx, a.ID, "storage_error") + _ = p.jobs.FailJob(context.Background(), a.ID, "storage_error") return fmt.Errorf("чтение staging: %w", err) } - // 3. Генерируем thumbnail + // 3. Генерируем все варианты (Thumbnail, Crop, Optimized) + log.Printf("worker: генерация вариантов для %s", a.ID) thumbMeta, thumbData, err := imaging.Thumbnail(a.ID, buf) if err != nil { - _ = p.jobs.FailJob(ctx, a.ID, "decode_failed") + _ = p.jobs.FailJob(context.Background(), a.ID, "decode_failed") return fmt.Errorf("генерация thumbnail: %w", err) } + cropMeta, cropData, err := imaging.Crop(a.ID, buf) + if err != nil { + _ = p.jobs.FailJob(context.Background(), a.ID, "decode_failed") + return fmt.Errorf("генерация crop: %w", err) + } + + webpMeta, webpData, err := imaging.Optimized(a.ID, buf) + if err != nil { + _ = p.jobs.FailJob(context.Background(), a.ID, "decode_failed") + return fmt.Errorf("генерация webp: %w", err) + } + // 4. Сохраняем оригинал в финальный prefix (chats/...) - if err := p.store.Put(ctx, a.ObjectKey, bytes.NewReader(buf), int64(len(buf)), a.ContentType); err != nil { - _ = p.jobs.FailJob(ctx, a.ID, "storage_error") + if err := p.store.Put(jobCtx, a.ObjectKey, bytes.NewReader(buf), int64(len(buf)), a.ContentType); err != nil { + _ = p.jobs.FailJob(context.Background(), a.ID, "storage_error") return fmt.Errorf("загрузка оригинала: %w", err) } - // 5. Сохраняем thumbnail в S3 - thumbKey := fmt.Sprintf("%s/thumbnail", a.ObjectKey) - if err := p.store.Put(ctx, thumbKey, bytes.NewReader(thumbData), thumbMeta.SizeBytes, thumbMeta.ContentType); err != nil { - _ = p.jobs.FailJob(ctx, a.ID, "storage_error") - return fmt.Errorf("загрузка thumbnail: %w", err) + // 5. Загружаем все сгенерированные варианты в S3 + variants := []domain.AttachmentVariant{*thumbMeta, *cropMeta, *webpMeta} + + for i := range variants { + v := &variants[i] + key := fmt.Sprintf("%s/%s", a.ObjectKey, v.Name) + + var data []byte + switch v.Name { + case domain.VariantThumbnail: + data = thumbData + case domain.VariantCrop: + data = cropData + case domain.VariantOptimized: + data = webpData + } + + if err := p.store.Put(jobCtx, key, bytes.NewReader(data), v.SizeBytes, v.ContentType); err != nil { + _ = p.jobs.FailJob(context.Background(), a.ID, "storage_error") + return fmt.Errorf("загрузка варианта %s: %w", v.Name, err) + } + v.ObjectKey = key } - thumbMeta.ObjectKey = thumbKey // 6. Фиксируем результат в БД (переводим в ready) - ok, err := p.jobs.CompleteJob(ctx, a.ID, a.PipelineVersion, []domain.AttachmentVariant{*thumbMeta}) + ok, err := p.jobs.CompleteJob(jobCtx, a.ID, a.PipelineVersion, variants) if err != nil { return fmt.Errorf("фиксация в БД: %w", err) } -- 2.54.0 From b022f8f7fc5073369a8e4ad322ee1eb8968282ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Tue, 10 Nov 2026 11:02:57 +0300 Subject: [PATCH 33/46] feat(db): lab5 add failed_jobs table and repository --- backend/internal/domain/failed_jobs.go | 17 ++++++++ backend/internal/domain/repository.go | 6 +++ .../internal/storage/postgres/failed_jobs.go | 42 +++++++++++++++++++ backend/internal/storage/postgres/storage.go | 1 + backend/migrations/0009_failed_jobs.sql | 25 +++++++++++ 5 files changed, 91 insertions(+) create mode 100644 backend/internal/domain/failed_jobs.go create mode 100644 backend/internal/storage/postgres/failed_jobs.go create mode 100644 backend/migrations/0009_failed_jobs.sql diff --git a/backend/internal/domain/failed_jobs.go b/backend/internal/domain/failed_jobs.go new file mode 100644 index 00000000..b3f177c9 --- /dev/null +++ b/backend/internal/domain/failed_jobs.go @@ -0,0 +1,17 @@ +package domain + +import "time" + +// FailedJob — реестр окончательно проваленных задач (DLQ-сторона). +// Заполняется worker'ом после исчерпания retry или при permanent-ошибке. +// Читается для операторского просмотра и для явного replay (Этап 5). +type FailedJob struct { + ID string + AttachmentID string + ReasonCode string // из jobcodes.go + Attempts int + LastErrorAt time.Time + ResolvedAt *time.Time // nil = не разобрана + Resolution string // "replayed", "manual_fix", ... + CreatedAt time.Time +} diff --git a/backend/internal/domain/repository.go b/backend/internal/domain/repository.go index 6ace771e..6fa4c789 100644 --- a/backend/internal/domain/repository.go +++ b/backend/internal/domain/repository.go @@ -57,9 +57,15 @@ type AttachmentJobRepository interface { ClaimForProcessing(ctx context.Context, id string, lease time.Duration) (*Attachment, error) CompleteJob(ctx context.Context, id string, pipelineVersion int, variants []AttachmentVariant) (bool, error) FailJob(ctx context.Context, id string, errorCode string) error + ScheduleRetry(ctx context.Context, id string, nextAttemptAt time.Time, errorCode string) error } type AttachmentVariantRepository interface { Upsert(ctx context.Context, tx Tx, v *AttachmentVariant) error ListByAttachment(ctx context.Context, id string) ([]AttachmentVariant, error) } + +type FailedJobRepository interface { + Record(ctx context.Context, f *FailedJob) error + Resolve(ctx context.Context, attachmentID string, resolution string) error +} diff --git a/backend/internal/storage/postgres/failed_jobs.go b/backend/internal/storage/postgres/failed_jobs.go new file mode 100644 index 00000000..9ddcbc4f --- /dev/null +++ b/backend/internal/storage/postgres/failed_jobs.go @@ -0,0 +1,42 @@ +package postgres + +import ( + "context" + "time" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5/pgxpool" + + "high-loud-apps-labs/internal/domain" +) + +type FailedJobRepo struct { + pool *pgxpool.Pool +} + +func NewFailedJobRepo(pool *pgxpool.Pool) *FailedJobRepo { + return &FailedJobRepo{pool: pool} +} + +func (r *FailedJobRepo) Record(ctx context.Context, f *domain.FailedJob) error { + if f.ID == "" { + f.ID = uuid.NewString() + } + if f.LastErrorAt.IsZero() { + f.LastErrorAt = time.Now().UTC() + } + const q = ` + INSERT INTO failed_jobs (id, attachment_id, reason_code, attempts, last_error_at, created_at) + VALUES ($1::uuid, $2::uuid, $3, $4, $5, $5) + ` + _, err := r.pool.Exec(ctx, q, f.ID, f.AttachmentID, f.ReasonCode, f.Attempts, f.LastErrorAt) + return mapError(err) +} + +func (r *FailedJobRepo) Resolve(ctx context.Context, attachmentID string, resolution string) error { + const q = `UPDATE failed_jobs SET resolved_at = now(), resolution = $2 WHERE attachment_id = $1::uuid AND resolved_at IS NULL` + _, err := r.pool.Exec(ctx, q, attachmentID, resolution) + return mapError(err) +} + +var _ domain.FailedJobRepository = (*FailedJobRepo)(nil) diff --git a/backend/internal/storage/postgres/storage.go b/backend/internal/storage/postgres/storage.go index db660c28..2a6110d0 100644 --- a/backend/internal/storage/postgres/storage.go +++ b/backend/internal/storage/postgres/storage.go @@ -19,3 +19,4 @@ func (s *Storage) Sessions() *SessionRepo { return &SessionRepo{pool: func (s *Storage) Attachments() *AttachmentRepo { return &AttachmentRepo{pool: s.pool} } func (s *Storage) Outbox() *OutboxRepo { return &OutboxRepo{pool: s.pool} } func (s *Storage) Variants() *AttachmentVariantRepo { return &AttachmentVariantRepo{pool: s.pool} } +func (s *Storage) FailedJobs() *FailedJobRepo { return &FailedJobRepo{pool: s.pool} } diff --git a/backend/migrations/0009_failed_jobs.sql b/backend/migrations/0009_failed_jobs.sql new file mode 100644 index 00000000..21ae264f --- /dev/null +++ b/backend/migrations/0009_failed_jobs.sql @@ -0,0 +1,25 @@ +-- +goose Up +-- +goose StatementBegin + +CREATE TABLE failed_jobs ( + id UUID PRIMARY KEY, + attachment_id UUID NOT NULL REFERENCES attachments(id) ON DELETE CASCADE, + reason_code TEXT NOT NULL, + attempts INT NOT NULL, + last_error_at TIMESTAMPTZ NOT NULL DEFAULT now(), + resolved_at TIMESTAMPTZ, + resolution TEXT, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + + CONSTRAINT failed_jobs_reason_len CHECK (char_length(reason_code) BETWEEN 1 AND 64), + CONSTRAINT failed_jobs_attempts_pos CHECK (attempts > 0) +); + +CREATE INDEX failed_jobs_unresolved_idx ON failed_jobs (created_at) WHERE resolved_at IS NULL; + +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin +DROP TABLE IF EXISTS failed_jobs; +-- +goose StatementEnd \ No newline at end of file -- 2.54.0 From d27a779c201a35ef2233a2d78d6af3d0928f19e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Tue, 10 Nov 2026 12:00:48 +0300 Subject: [PATCH 34/46] feat(storage): lab5 implement ScheduleRetry for transient errors --- backend/internal/storage/postgres/attachments.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/backend/internal/storage/postgres/attachments.go b/backend/internal/storage/postgres/attachments.go index 42ec3b22..1a3981cd 100644 --- a/backend/internal/storage/postgres/attachments.go +++ b/backend/internal/storage/postgres/attachments.go @@ -297,4 +297,14 @@ func (r *AttachmentRepo) FailJob(ctx context.Context, id string, errorCode strin return mapError(err) } +func (r *AttachmentRepo) ScheduleRetry(ctx context.Context, id string, nextAttemptAt time.Time, errorCode string) error { + const q = ` + UPDATE attachments + SET status = 'queued', error_code = NULL, next_attempt_at = $2, locked_until = NULL, updated_at = now() + WHERE id = $1::uuid AND status = 'processing' + ` + _, err := r.pool.Exec(ctx, q, id, nextAttemptAt) + return mapError(err) +} + var _ domain.AttachmentRepository = (*AttachmentRepo)(nil) -- 2.54.0 From 8fb191fc78e60193d1fe1890674a353bdb0cde28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Tue, 10 Nov 2026 13:43:12 +0300 Subject: [PATCH 35/46] feat(storage): lab5 implement delayed outbox and attempts tracking --- backend/internal/domain/repository.go | 1 + .../internal/storage/postgres/attachments.go | 6 +++--- backend/internal/storage/postgres/outbox.go | 18 +++++++++++++++++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/backend/internal/domain/repository.go b/backend/internal/domain/repository.go index 6fa4c789..2e39cefd 100644 --- a/backend/internal/domain/repository.go +++ b/backend/internal/domain/repository.go @@ -51,6 +51,7 @@ type OutboxRepository interface { Enqueue(ctx context.Context, tx Tx, e *OutboxEntry) error ClaimBatch(ctx context.Context, limit int) ([]OutboxEntry, error) MarkPublished(ctx context.Context, ids []string) error + EnqueueDirect(ctx context.Context, e *OutboxEntry) error } type AttachmentJobRepository interface { diff --git a/backend/internal/storage/postgres/attachments.go b/backend/internal/storage/postgres/attachments.go index 1a3981cd..4b78dd8c 100644 --- a/backend/internal/storage/postgres/attachments.go +++ b/backend/internal/storage/postgres/attachments.go @@ -239,12 +239,12 @@ func (r *AttachmentRepo) ExistingKeys(ctx context.Context, keys []string) ([]str func (r *AttachmentRepo) ClaimForProcessing(ctx context.Context, id string, lease time.Duration) (*domain.Attachment, error) { const q = ` UPDATE attachments - SET status = 'processing', locked_until = now() + $2, updated_at = now() + SET status = 'processing', locked_until = now() + $2, updated_at = now(), attempts = attempts + 1 -- <--- ИНКРЕМЕНТ WHERE id = $1::uuid AND status = 'queued' - RETURNING id, chat_id, staging_key, pipeline_version, object_key, content_type + RETURNING id, chat_id, staging_key, pipeline_version, object_key, content_type, attempts ` var a domain.Attachment - err := r.pool.QueryRow(ctx, q, id, lease).Scan(&a.ID, &a.ChatID, &a.StagingKey, &a.PipelineVersion, &a.ObjectKey, &a.ContentType) + err := r.pool.QueryRow(ctx, q, id, lease).Scan(&a.ID, &a.ChatID, &a.StagingKey, &a.PipelineVersion, &a.ObjectKey, &a.ContentType, &a.Attempts) if err != nil { if err == pgx.ErrNoRows { return nil, nil diff --git a/backend/internal/storage/postgres/outbox.go b/backend/internal/storage/postgres/outbox.go index af78d77c..414ba0b0 100644 --- a/backend/internal/storage/postgres/outbox.go +++ b/backend/internal/storage/postgres/outbox.go @@ -57,7 +57,7 @@ func (r *OutboxRepo) ClaimBatch(ctx context.Context, limit int) ([]domain.Outbox const q = ` WITH claimed AS ( SELECT id FROM outbox - WHERE published_at IS NULL + WHERE published_at IS NULL AND created_at <= now() ORDER BY created_at LIMIT $1 FOR UPDATE SKIP LOCKED @@ -88,6 +88,22 @@ func (r *OutboxRepo) ClaimBatch(ctx context.Context, limit int) ([]domain.Outbox return entries, mapError(rows.Err()) } +func (r *OutboxRepo) EnqueueDirect(ctx context.Context, e *domain.OutboxEntry) error { + if e.ID == "" { + e.ID = uuid.NewString() + } + const q = ` + INSERT INTO outbox + (id, aggregate_type, aggregate_id, event_type, payload, attempts, created_at, published_at) + VALUES ($1::uuid, $2, $3::uuid, $4, $5::jsonb, $6, $7, $8) + ` + _, err := r.pool.Exec(ctx, q, + e.ID, e.AggregateType, e.AggregateID, e.EventType, + string(e.Payload), e.Attempts, e.CreatedAt, e.PublishedAt, + ) + return mapError(err) +} + func (r *OutboxRepo) MarkPublished(ctx context.Context, ids []string) error { if len(ids) == 0 { return nil -- 2.54.0 From 996e305ea4b2ee37a9a5b06791e4a41a6404d4fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Tue, 10 Nov 2026 15:28:20 +0300 Subject: [PATCH 36/46] feat(worker): lab5 add exponential backoff retries and permanent error classification --- backend/cmd/worker/main.go | 2 +- backend/internal/domain/jobcodes.go | 10 +++ backend/internal/worker/pipeline.go | 104 +++++++++++++++++++++------- 3 files changed, 91 insertions(+), 25 deletions(-) create mode 100644 backend/internal/domain/jobcodes.go diff --git a/backend/cmd/worker/main.go b/backend/cmd/worker/main.go index 3708be5a..3870ce81 100644 --- a/backend/cmd/worker/main.go +++ b/backend/cmd/worker/main.go @@ -58,7 +58,7 @@ func main() { } defer ch.Close() - // Prefetch: сколько сообщений worker берет разом (защита от перегрузки) + // Сколько сообщений worker берет разом (защита от перегрузки) ch.Qos(2, 0, false) q, err := ch.QueueDeclare("attachments.process", true, false, false, false, nil) diff --git a/backend/internal/domain/jobcodes.go b/backend/internal/domain/jobcodes.go new file mode 100644 index 00000000..5078d61e --- /dev/null +++ b/backend/internal/domain/jobcodes.go @@ -0,0 +1,10 @@ +package domain + +const ( + JobErrorDecodeFailed = "decode_failed" + JobErrorTooLarge = "too_large" + JobErrorUnsupportedFormat = "unsupported_format" + JobErrorStorage = "storage_error" + JobErrorInternal = "internal_error" + JobErrorRetryExhausted = "retry_exhausted" +) diff --git a/backend/internal/worker/pipeline.go b/backend/internal/worker/pipeline.go index ea7e011d..fbc98d8e 100644 --- a/backend/internal/worker/pipeline.go +++ b/backend/internal/worker/pipeline.go @@ -3,9 +3,11 @@ package worker import ( "bytes" "context" + "encoding/json" "fmt" "io" "log" + "math/rand" "time" "high-loud-apps-labs/internal/domain" @@ -13,18 +15,30 @@ import ( ) type Pipeline struct { - jobs domain.AttachmentJobRepository - store domain.ObjectStorage - variants domain.AttachmentVariantRepository - lease time.Duration + jobs domain.AttachmentJobRepository + store domain.ObjectStorage + variants domain.AttachmentVariantRepository + failedJobs domain.FailedJobRepository + outbox domain.OutboxRepository + lease time.Duration + maxRetries int } -func NewPipeline(jobs domain.AttachmentJobRepository, store domain.ObjectStorage, variants domain.AttachmentVariantRepository) *Pipeline { +func NewPipeline( + jobs domain.AttachmentJobRepository, + store domain.ObjectStorage, + variants domain.AttachmentVariantRepository, + failedJobs domain.FailedJobRepository, + outbox domain.OutboxRepository, +) *Pipeline { return &Pipeline{ - jobs: jobs, - store: store, - variants: variants, - lease: 5 * time.Minute, + jobs: jobs, + store: store, + variants: variants, + failedJobs: failedJobs, + outbox: outbox, + lease: 5 * time.Minute, + maxRetries: 5, } } @@ -47,41 +61,35 @@ func (p *Pipeline) Process(ctx context.Context, attachmentID string) error { // 2. Скачиваем оригинал из staging rc, _, _, err := p.store.Get(jobCtx, a.StagingKey) if err != nil { - _ = p.jobs.FailJob(context.Background(), a.ID, "storage_error") - return fmt.Errorf("скачивание staging: %w", err) + return p.handleFailure(a, fmt.Errorf("скачивание staging: %w", err), false) } defer rc.Close() buf, err := io.ReadAll(rc) if err != nil { - _ = p.jobs.FailJob(context.Background(), a.ID, "storage_error") - return fmt.Errorf("чтение staging: %w", err) + return p.handleFailure(a, fmt.Errorf("чтение staging: %w", err), false) } // 3. Генерируем все варианты (Thumbnail, Crop, Optimized) log.Printf("worker: генерация вариантов для %s", a.ID) thumbMeta, thumbData, err := imaging.Thumbnail(a.ID, buf) if err != nil { - _ = p.jobs.FailJob(context.Background(), a.ID, "decode_failed") - return fmt.Errorf("генерация thumbnail: %w", err) + return p.handleFailure(a, fmt.Errorf("генерация thumbnail: %w", err), true) } cropMeta, cropData, err := imaging.Crop(a.ID, buf) if err != nil { - _ = p.jobs.FailJob(context.Background(), a.ID, "decode_failed") - return fmt.Errorf("генерация crop: %w", err) + return p.handleFailure(a, fmt.Errorf("генерация crop: %w", err), true) } webpMeta, webpData, err := imaging.Optimized(a.ID, buf) if err != nil { - _ = p.jobs.FailJob(context.Background(), a.ID, "decode_failed") - return fmt.Errorf("генерация webp: %w", err) + return p.handleFailure(a, fmt.Errorf("генерация webp: %w", err), true) } // 4. Сохраняем оригинал в финальный prefix (chats/...) if err := p.store.Put(jobCtx, a.ObjectKey, bytes.NewReader(buf), int64(len(buf)), a.ContentType); err != nil { - _ = p.jobs.FailJob(context.Background(), a.ID, "storage_error") - return fmt.Errorf("загрузка оригинала: %w", err) + return p.handleFailure(a, fmt.Errorf("загрузка оригинала: %w", err), false) } // 5. Загружаем все сгенерированные варианты в S3 @@ -102,8 +110,7 @@ func (p *Pipeline) Process(ctx context.Context, attachmentID string) error { } if err := p.store.Put(jobCtx, key, bytes.NewReader(data), v.SizeBytes, v.ContentType); err != nil { - _ = p.jobs.FailJob(context.Background(), a.ID, "storage_error") - return fmt.Errorf("загрузка варианта %s: %w", v.Name, err) + return p.handleFailure(a, fmt.Errorf("загрузка варианта %s: %w", v.Name, err), false) } v.ObjectKey = key } @@ -111,7 +118,7 @@ func (p *Pipeline) Process(ctx context.Context, attachmentID string) error { // 6. Фиксируем результат в БД (переводим в ready) ok, err := p.jobs.CompleteJob(jobCtx, a.ID, a.PipelineVersion, variants) if err != nil { - return fmt.Errorf("фиксация в БД: %w", err) + return p.handleFailure(a, fmt.Errorf("фиксация в БД: %w", err), false) } if !ok { log.Printf("worker: версия устарела или статус изменился, пропускаем %s", a.ID) @@ -121,3 +128,52 @@ func (p *Pipeline) Process(ctx context.Context, attachmentID string) error { log.Printf("worker: успешно завершено %s", a.ID) return nil } + +func (p *Pipeline) handleFailure(a *domain.Attachment, err error, isPermanent bool) error { + // Если исчерпаны попытки или ошибка постоянная (например, битый файл) + if isPermanent || a.Attempts >= p.maxRetries { + reason := domain.JobErrorInternal + if a.Attempts >= p.maxRetries { + reason = domain.JobErrorRetryExhausted + } else if isPermanent { + reason = domain.JobErrorDecodeFailed + } + + log.Printf("worker: финальная ошибка для %s (попытка %d): %v", a.ID, a.Attempts, err) + + // Используем context.Background(), чтобы запись в БД прошла даже при истекшем jobCtx + _ = p.jobs.FailJob(context.Background(), a.ID, reason) + _ = p.failedJobs.Record(context.Background(), &domain.FailedJob{ + AttachmentID: a.ID, + ReasonCode: reason, + Attempts: a.Attempts, + }) + return nil + } + + // Временная ошибка - делаем Retry с backoff + log.Printf("worker: временная ошибка для %s, попытка %d: %v", a.ID, a.Attempts, err) + + // Экспоненциальный backoff: 2^attempts секунд + случайный jitter (0-1000 мс) + backoff := time.Duration(1< Date: Tue, 10 Nov 2026 15:29:26 +0300 Subject: [PATCH 37/46] hotfix(worker): lab5 add failedjobs and outbox to main --- backend/cmd/worker/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/cmd/worker/main.go b/backend/cmd/worker/main.go index 3870ce81..26ffa067 100644 --- a/backend/cmd/worker/main.go +++ b/backend/cmd/worker/main.go @@ -44,7 +44,7 @@ func main() { log.Fatalf("S3: %v", err) } - pipeline := worker.NewPipeline(store.Attachments(), objectStorage, store.Variants()) + pipeline := worker.NewPipeline(store.Attachments(), objectStorage, store.Variants(), store.FailedJobs(), store.Outbox()) conn, err := amqp.Dial(cfg.RabbitMQURL) if err != nil { -- 2.54.0 From f748c09e1013f15db08194f60dcf579aea7b879f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Tue, 10 Nov 2026 19:08:15 +0300 Subject: [PATCH 38/46] feat(worker): lab5 add retries, DLQ registry and manual replay endpoint --- .../domain/{failed_jobs.go => failed_job.go} | 0 backend/internal/domain/repository.go | 2 + .../internal/httpapi/handler/attachment.go | 17 +++++++ backend/internal/httpapi/router.go | 1 + backend/internal/service/attachment.go | 46 +++++++++++++++++++ .../internal/storage/postgres/attachments.go | 9 ++++ .../internal/storage/postgres/failed_jobs.go | 9 ++++ 7 files changed, 84 insertions(+) rename backend/internal/domain/{failed_jobs.go => failed_job.go} (100%) diff --git a/backend/internal/domain/failed_jobs.go b/backend/internal/domain/failed_job.go similarity index 100% rename from backend/internal/domain/failed_jobs.go rename to backend/internal/domain/failed_job.go diff --git a/backend/internal/domain/repository.go b/backend/internal/domain/repository.go index 2e39cefd..dc07f3a1 100644 --- a/backend/internal/domain/repository.go +++ b/backend/internal/domain/repository.go @@ -45,6 +45,7 @@ type AttachmentRepository interface { ExistingKeys(ctx context.Context, keys []string) ([]string, error) DeletePending(ctx context.Context, id string) error CreateInTx(ctx context.Context, tx Tx, a *Attachment) error + ResetForReplay(ctx context.Context, tx Tx, id string) error } type OutboxRepository interface { @@ -69,4 +70,5 @@ type AttachmentVariantRepository interface { type FailedJobRepository interface { Record(ctx context.Context, f *FailedJob) error Resolve(ctx context.Context, attachmentID string, resolution string) error + ResolveInTx(ctx context.Context, tx Tx, id string) error } diff --git a/backend/internal/httpapi/handler/attachment.go b/backend/internal/httpapi/handler/attachment.go index 24e9574f..3032c47b 100644 --- a/backend/internal/httpapi/handler/attachment.go +++ b/backend/internal/httpapi/handler/attachment.go @@ -92,3 +92,20 @@ func (h *AttachmentHandler) Content(c *gin.Context) { c.DataFromReader(http.StatusOK, size, contentType, rc, nil) } + +func (h *AttachmentHandler) Replay(c *gin.Context) { + me := middleware.CurrentUser(c) + + attachmentID, err := parseUUIDPathParam(c, "attachment_id") + if err != nil { + respondError(c, err) + return + } + + if err := h.svc.Replay(c.Request.Context(), me.ID, attachmentID); err != nil { + respondError(c, err) + return + } + + c.Status(http.StatusAccepted) +} diff --git a/backend/internal/httpapi/router.go b/backend/internal/httpapi/router.go index a614a017..e381f2b4 100644 --- a/backend/internal/httpapi/router.go +++ b/backend/internal/httpapi/router.go @@ -50,6 +50,7 @@ func NewRouter(h Handlers, auth gin.HandlerFunc) *gin.Engine { protected.POST("/chats/:chat_id/attachments", h.Attachment.Upload) protected.GET("/attachments/:attachment_id", h.Attachment.Get) protected.GET("/attachments/:attachment_id/content", h.Attachment.Content) + protected.POST("/attachments/:attachment_id/replay", h.Attachment.Replay) } } diff --git a/backend/internal/service/attachment.go b/backend/internal/service/attachment.go index 358ed47a..b0bd3a57 100644 --- a/backend/internal/service/attachment.go +++ b/backend/internal/service/attachment.go @@ -29,6 +29,7 @@ type AttachmentService struct { chats domain.ChatRepository storage domain.ObjectStorage outbox domain.OutboxRepository + failedJobs domain.FailedJobRepository tx txRunner maxBytes int64 maxPixels int64 @@ -39,6 +40,7 @@ func NewAttachmentService( chats domain.ChatRepository, storage domain.ObjectStorage, outbox domain.OutboxRepository, + failedJobs domain.FailedJobRepository, tx txRunner, maxBytes, maxPixels int64, ) *AttachmentService { @@ -47,6 +49,7 @@ func NewAttachmentService( chats: chats, storage: storage, outbox: outbox, + failedJobs: failedJobs, tx: tx, maxBytes: maxBytes, maxPixels: maxPixels, @@ -190,6 +193,49 @@ func (s *AttachmentService) Download( return nil, 0, "", &domain.NotFoundError{Entity: "variant", ID: variantName} } +// Replay позволяет оператору перезапустить упавшую задачу. +func (s *AttachmentService) Replay(ctx context.Context, userID, attachmentID string) error { + a, err := s.Get(ctx, userID, attachmentID) + if err != nil { + return err + } + + if a.Status != domain.AttachmentStatusFailed { + return &domain.ConflictError{Reason: "перезапустить можно только failed-вложение"} + } + + // Формируем новое сообщение + payload, err := json.Marshal(domain.JobMessage{ + AttachmentID: a.ID, + PipelineVersion: a.PipelineVersion, + Attempt: 1, // Сбрасываем счетчик попыток + }) + if err != nil { + return err + } + + err = s.tx.WithTx(ctx, func(tx domain.Tx) error { + if err := s.attachments.ResetForReplay(ctx, tx, a.ID); err != nil { + return err + } + // Помечаем в реестре упавших задач (внутри транзакции) + if err := s.failedJobs.ResolveInTx(ctx, tx, a.ID); err != nil { + return err + } + return s.outbox.Enqueue(ctx, tx, &domain.OutboxEntry{ + AggregateType: domain.OutboxAggregateAttachment, + AggregateID: a.ID, + EventType: domain.OutboxEventProcess, + Payload: payload, + }) + }) + if err != nil { + return err + } + + return nil +} + func (s *AttachmentService) ensureMember(ctx context.Context, userID, chatID string) error { c, err := s.chats.GetByID(ctx, chatID) if err != nil { diff --git a/backend/internal/storage/postgres/attachments.go b/backend/internal/storage/postgres/attachments.go index 4b78dd8c..3289216c 100644 --- a/backend/internal/storage/postgres/attachments.go +++ b/backend/internal/storage/postgres/attachments.go @@ -307,4 +307,13 @@ func (r *AttachmentRepo) ScheduleRetry(ctx context.Context, id string, nextAttem return mapError(err) } +func (r *AttachmentRepo) ResetForReplay(ctx context.Context, tx domain.Tx, id string) error { + const q = ` + UPDATE attachments + SET status = 'queued', attempts = 0, error_code = NULL, locked_until = NULL, next_attempt_at = NULL, updated_at = now() + WHERE id = $1::uuid AND status = 'failed' + ` + return tx.Exec(ctx, q, id) +} + var _ domain.AttachmentRepository = (*AttachmentRepo)(nil) diff --git a/backend/internal/storage/postgres/failed_jobs.go b/backend/internal/storage/postgres/failed_jobs.go index 9ddcbc4f..7006d3f6 100644 --- a/backend/internal/storage/postgres/failed_jobs.go +++ b/backend/internal/storage/postgres/failed_jobs.go @@ -39,4 +39,13 @@ func (r *FailedJobRepo) Resolve(ctx context.Context, attachmentID string, resolu return mapError(err) } +func (r *FailedJobRepo) ResolveInTx(ctx context.Context, tx domain.Tx, id string) error { + const q = ` + UPDATE failed_jobs + SET resolved_at = now(), resolution = 'manual_replay' + WHERE attachment_id = $1::uuid AND resolved_at IS NULL + ` + return tx.Exec(ctx, q, id) +} + var _ domain.FailedJobRepository = (*FailedJobRepo)(nil) -- 2.54.0 From d40b339906c0f5232ceac01dcd77d6592b151096 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Wed, 11 Nov 2026 12:40:53 +0300 Subject: [PATCH 39/46] feat(reconciler): lab5 recover expired leases and cleanup staging --- backend/cmd/server/main.go | 2 + backend/internal/domain/repository.go | 7 +- backend/internal/service/reconciler.go | 80 +++++++++--- .../internal/storage/postgres/attachments.go | 115 +++++++++++------- 4 files changed, 141 insertions(+), 63 deletions(-) diff --git a/backend/cmd/server/main.go b/backend/cmd/server/main.go index 66ab20a1..ffba9c0d 100644 --- a/backend/cmd/server/main.go +++ b/backend/cmd/server/main.go @@ -103,6 +103,7 @@ func main() { store.Chats(), objectStorage, store.Outbox(), + store.FailedJobs(), store, cfg.MaxUploadBytes, cfg.MaxImagePixels, @@ -139,6 +140,7 @@ func main() { reconciler := service.NewReconciler( store.Attachments(), objectStorage, + store.Outbox(), pool, cfg.ReconcilerGracePeriod, ) diff --git a/backend/internal/domain/repository.go b/backend/internal/domain/repository.go index dc07f3a1..66d5142b 100644 --- a/backend/internal/domain/repository.go +++ b/backend/internal/domain/repository.go @@ -41,11 +41,14 @@ type AttachmentRepository interface { GetByID(ctx context.Context, id string) (*Attachment, error) MarkReady(ctx context.Context, id string) error MarkFailed(ctx context.Context, id string, errorCode string) error - ListStalePending(ctx context.Context, olderThan time.Time, limit int) ([]Attachment, error) ExistingKeys(ctx context.Context, keys []string) ([]string, error) - DeletePending(ctx context.Context, id string) error CreateInTx(ctx context.Context, tx Tx, a *Attachment) error ResetForReplay(ctx context.Context, tx Tx, id string) error + + ListExpiredLeases(ctx context.Context, limit int) ([]Attachment, error) + ResetLease(ctx context.Context, id string) error + ListStagingToCleanup(ctx context.Context, limit int) ([]Attachment, error) + ClearStagingKey(ctx context.Context, id string) error } type OutboxRepository interface { diff --git a/backend/internal/service/reconciler.go b/backend/internal/service/reconciler.go index 325b95d8..a1c69c0f 100644 --- a/backend/internal/service/reconciler.go +++ b/backend/internal/service/reconciler.go @@ -2,6 +2,7 @@ package service import ( "context" + "encoding/json" "log" "time" @@ -15,6 +16,7 @@ const advisoryLockKey int64 = 0x4D5455534904 type Reconciler struct { attachments domain.AttachmentRepository storage domain.ObjectStorage + outbox domain.OutboxRepository pool *pgxpool.Pool gracePeriod time.Duration batchSize int @@ -23,6 +25,7 @@ type Reconciler struct { func NewReconciler( attachments domain.AttachmentRepository, storage domain.ObjectStorage, + outbox domain.OutboxRepository, pool *pgxpool.Pool, gracePeriod time.Duration, ) *Reconciler { @@ -32,6 +35,7 @@ func NewReconciler( return &Reconciler{ attachments: attachments, storage: storage, + outbox: outbox, pool: pool, gracePeriod: gracePeriod, batchSize: 100, @@ -45,14 +49,13 @@ func (r *Reconciler) Run(ctx context.Context, interval time.Duration) { ticker := time.NewTicker(interval) defer ticker.Stop() - log.Printf("reconciler: started, interval=%s grace=%s", interval, r.gracePeriod) - + log.Printf("reconciler: запущен (interval=%s, grace=%s)", interval, r.gracePeriod) r.tick(ctx) for { select { case <-ctx.Done(): - log.Println("reconciler: stopped") + log.Println("reconciler: остановлен") return case <-ticker.C: r.tick(ctx) @@ -63,7 +66,7 @@ func (r *Reconciler) Run(ctx context.Context, interval time.Duration) { func (r *Reconciler) tick(ctx context.Context) { acquired, release, err := r.acquireLock(ctx) if err != nil { - log.Printf("reconciler: acquire lock: %v", err) + log.Printf("reconciler: ошибка получения блокировки: %v", err) return } if !acquired { @@ -71,11 +74,12 @@ func (r *Reconciler) tick(ctx context.Context) { } defer release() - pendingDeleted := r.sweepPending(ctx) + leasesReset := r.sweepExpiredLeases(ctx) + stagingDeleted := r.sweepStaging(ctx) orphansDeleted := r.sweepOrphans(ctx) - if pendingDeleted > 0 || orphansDeleted > 0 { - log.Printf("reconciler: sweep done pending=%d orphans=%d", pendingDeleted, orphansDeleted) + if leasesReset > 0 || stagingDeleted > 0 || orphansDeleted > 0 { + log.Printf("reconciler: очистка завершена (восстановлено_задач=%d, удалено_staging=%d, удалено_сирот=%d)", leasesReset, stagingDeleted, orphansDeleted) } } @@ -99,29 +103,64 @@ func (r *Reconciler) acquireLock(ctx context.Context) (bool, func(), error) { unlockCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() if _, err := conn.Exec(unlockCtx, `SELECT pg_advisory_unlock($1)`, advisoryLockKey); err != nil { - log.Printf("reconciler: unlock: %v", err) + log.Printf("reconciler: ошибка снятия блокировки: %v", err) } conn.Release() } return true, release, nil } -func (r *Reconciler) sweepPending(ctx context.Context) int { - cutoff := time.Now().UTC().Add(-r.gracePeriod) - stale, err := r.attachments.ListStalePending(ctx, cutoff, r.batchSize) +func (r *Reconciler) sweepExpiredLeases(ctx context.Context) int { + expired, err := r.attachments.ListExpiredLeases(ctx, r.batchSize) if err != nil { - log.Printf("reconciler: list stale pending: %v", err) + log.Printf("reconciler: ошибка получения истёкших аренд: %v", err) + return 0 + } + + recovered := 0 + for _, a := range expired { + if err := r.attachments.ResetLease(ctx, a.ID); err != nil { + log.Printf("reconciler: ошибка сброса аренды для %s: %v", a.ID, err) + continue + } + + payload, _ := json.Marshal(domain.JobMessage{ + AttachmentID: a.ID, + PipelineVersion: a.PipelineVersion, + Attempt: a.Attempts + 1, + }) + err := r.outbox.EnqueueDirect(ctx, &domain.OutboxEntry{ + AggregateType: domain.OutboxAggregateAttachment, + AggregateID: a.ID, + EventType: domain.OutboxEventProcess, + Payload: payload, + CreatedAt: time.Now().UTC(), + }) + if err != nil { + log.Printf("reconciler: ошибка постановки в очередь восстановленной задачи %s: %v", a.ID, err) + continue + } + recovered++ + } + return recovered +} + +// Чистим staging в S3 для готовых задач +func (r *Reconciler) sweepStaging(ctx context.Context) int { + toCleanup, err := r.attachments.ListStagingToCleanup(ctx, r.batchSize) + if err != nil { + log.Printf("reconciler: ошибка получения списка staging для очистки: %v", err) return 0 } deleted := 0 - for _, a := range stale { - if err := r.storage.Delete(ctx, a.ObjectKey); err != nil { - log.Printf("reconciler: delete object %s: %v", a.ObjectKey, err) + for _, a := range toCleanup { + if err := r.storage.Delete(ctx, a.StagingKey); err != nil { + log.Printf("reconciler: ошибка удаления объекта staging %s: %v", a.StagingKey, err) continue } - if err := r.attachments.DeletePending(ctx, a.ID); err != nil { - log.Printf("reconciler: delete pending row %s: %v", a.ID, err) + if err := r.attachments.ClearStagingKey(ctx, a.ID); err != nil { + log.Printf("reconciler: ошибка очистки ключа staging %s: %v", a.ID, err) continue } deleted++ @@ -129,10 +168,11 @@ func (r *Reconciler) sweepPending(ctx context.Context) int { return deleted } +// Удаляем старые сиротские объекты (без записи в БД) func (r *Reconciler) sweepOrphans(ctx context.Context) int { objects, err := r.storage.List(ctx, "chats/") if err != nil { - log.Printf("reconciler: list objects: %v", err) + log.Printf("reconciler: ошибка получения списка объектов: %v", err) return 0 } if len(objects) == 0 { @@ -152,7 +192,7 @@ func (r *Reconciler) sweepOrphans(ctx context.Context) int { existing, err := r.attachments.ExistingKeys(ctx, keys) if err != nil { - log.Printf("reconciler: existing keys: %v", err) + log.Printf("reconciler: ошибка получения существующих ключей: %v", err) return 0 } @@ -167,7 +207,7 @@ func (r *Reconciler) sweepOrphans(ctx context.Context) int { continue } if err := r.storage.Delete(ctx, k); err != nil { - log.Printf("reconciler: delete orphan %s: %v", k, err) + log.Printf("reconciler: ошибка удаления сиротского объекта %s: %v", k, err) continue } deleted++ diff --git a/backend/internal/storage/postgres/attachments.go b/backend/internal/storage/postgres/attachments.go index 3289216c..9c778024 100644 --- a/backend/internal/storage/postgres/attachments.go +++ b/backend/internal/storage/postgres/attachments.go @@ -173,47 +173,6 @@ func (r *AttachmentRepo) MarkFailed(ctx context.Context, id, errorCode string) e } } -func (r *AttachmentRepo) ListStalePending(ctx context.Context, olderThan time.Time, limit int) ([]domain.Attachment, error) { - if limit <= 0 { - return []domain.Attachment{}, nil - } - const q = ` - SELECT id, chat_id, owner_id, filename, content_type, size_bytes, sha256, - object_key, status, error_code, created_at, updated_at - FROM attachments - WHERE status = 'pending' AND created_at < $1 - ORDER BY created_at - LIMIT $2 - ` - rows, err := r.pool.Query(ctx, q, olderThan, limit) - if err != nil { - return nil, mapError(err) - } - defer rows.Close() - - out := make([]domain.Attachment, 0, limit) - for rows.Next() { - var a domain.Attachment - var status string - if err := rows.Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, - &a.ContentType, &a.SizeBytes, &a.SHA256, &a.ObjectKey, - &status, &a.ErrorCode, &a.CreatedAt, &a.UpdatedAt); err != nil { - return nil, mapError(err) - } - a.Status = domain.AttachmentStatus(status) - out = append(out, a) - } - return out, mapError(rows.Err()) -} - -func (r *AttachmentRepo) DeletePending(ctx context.Context, id string) error { - const q = `DELETE FROM attachments WHERE id = $1::uuid AND status = 'pending'` - if _, err := r.pool.Exec(ctx, q, id); err != nil { - return mapError(err) - } - return nil -} - func (r *AttachmentRepo) ExistingKeys(ctx context.Context, keys []string) ([]string, error) { if len(keys) == 0 { return []string{}, nil @@ -316,4 +275,78 @@ func (r *AttachmentRepo) ResetForReplay(ctx context.Context, tx domain.Tx, id st return tx.Exec(ctx, q, id) } +// для сверщика. +func (r *AttachmentRepo) ListExpiredLeases(ctx context.Context, limit int) ([]domain.Attachment, error) { + const q = ` + SELECT id, chat_id, owner_id, filename, content_type, size_bytes, sha256, + object_key, status, error_code, created_at, updated_at, + attempts, next_attempt_at, locked_until, pipeline_version, staging_key + FROM attachments + WHERE status = 'processing' AND locked_until < now() + ORDER BY locked_until + LIMIT $1 + ` + rows, err := r.pool.Query(ctx, q, limit) + if err != nil { + return nil, mapError(err) + } + defer rows.Close() + + var out []domain.Attachment + for rows.Next() { + var a domain.Attachment + var status string + err := rows.Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.ContentType, + &a.SizeBytes, &a.SHA256, &a.ObjectKey, &status, &a.ErrorCode, + &a.CreatedAt, &a.UpdatedAt, &a.Attempts, &a.NextAttemptAt, &a.LockedUntil, + &a.PipelineVersion, &a.StagingKey) + if err != nil { + return nil, mapError(err) + } + a.Status = domain.AttachmentStatus(status) + out = append(out, a) + } + return out, mapError(rows.Err()) +} + +func (r *AttachmentRepo) ResetLease(ctx context.Context, id string) error { + const q = ` + UPDATE attachments + SET status = 'queued', locked_until = NULL, updated_at = now() + WHERE id = $1::uuid AND status = 'processing' AND locked_until < now() + ` + _, err := r.pool.Exec(ctx, q, id) + return mapError(err) +} + +func (r *AttachmentRepo) ListStagingToCleanup(ctx context.Context, limit int) ([]domain.Attachment, error) { + const q = ` + SELECT id, staging_key + FROM attachments + WHERE status = 'ready' AND staging_key IS NOT NULL + LIMIT $1 + ` + rows, err := r.pool.Query(ctx, q, limit) + if err != nil { + return nil, mapError(err) + } + defer rows.Close() + + var out []domain.Attachment + for rows.Next() { + var a domain.Attachment + if err := rows.Scan(&a.ID, &a.StagingKey); err != nil { + return nil, mapError(err) + } + out = append(out, a) + } + return out, mapError(rows.Err()) +} + +func (r *AttachmentRepo) ClearStagingKey(ctx context.Context, id string) error { + const q = `UPDATE attachments SET staging_key = NULL WHERE id = $1::uuid` + _, err := r.pool.Exec(ctx, q, id) + return mapError(err) +} + var _ domain.AttachmentRepository = (*AttachmentRepo)(nil) -- 2.54.0 From 20fdd31c128a101c31e19c3b8c8c5a54fb7cb30b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Fri, 13 Nov 2026 09:12:45 +0300 Subject: [PATCH 40/46] fix(worker): lab5 enforce pipeline version check on claim --- backend/cmd/worker/main.go | 4 ++-- backend/internal/domain/repository.go | 2 +- backend/internal/storage/postgres/attachments.go | 8 ++++---- backend/internal/worker/pipeline.go | 10 +++++----- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/backend/cmd/worker/main.go b/backend/cmd/worker/main.go index 26ffa067..b4fc91cf 100644 --- a/backend/cmd/worker/main.go +++ b/backend/cmd/worker/main.go @@ -86,9 +86,9 @@ func main() { continue } - if err := pipeline.Process(ctx, msg.AttachmentID); err != nil { + if err := pipeline.Process(ctx, msg.AttachmentID, msg.PipelineVersion); err != nil { log.Printf("worker: ошибка %s: %v", msg.AttachmentID, err) - _ = d.Nack(false, true) // requeue + _ = d.Nack(false, true) } else { _ = d.Ack(false) } diff --git a/backend/internal/domain/repository.go b/backend/internal/domain/repository.go index 66d5142b..5ca20143 100644 --- a/backend/internal/domain/repository.go +++ b/backend/internal/domain/repository.go @@ -59,7 +59,7 @@ type OutboxRepository interface { } type AttachmentJobRepository interface { - ClaimForProcessing(ctx context.Context, id string, lease time.Duration) (*Attachment, error) + ClaimForProcessing(ctx context.Context, id string, version int, lease time.Duration) (*Attachment, error) CompleteJob(ctx context.Context, id string, pipelineVersion int, variants []AttachmentVariant) (bool, error) FailJob(ctx context.Context, id string, errorCode string) error ScheduleRetry(ctx context.Context, id string, nextAttemptAt time.Time, errorCode string) error diff --git a/backend/internal/storage/postgres/attachments.go b/backend/internal/storage/postgres/attachments.go index 9c778024..56ebc65f 100644 --- a/backend/internal/storage/postgres/attachments.go +++ b/backend/internal/storage/postgres/attachments.go @@ -195,15 +195,15 @@ func (r *AttachmentRepo) ExistingKeys(ctx context.Context, keys []string) ([]str return out, mapError(rows.Err()) } -func (r *AttachmentRepo) ClaimForProcessing(ctx context.Context, id string, lease time.Duration) (*domain.Attachment, error) { +func (r *AttachmentRepo) ClaimForProcessing(ctx context.Context, id string, version int, lease time.Duration) (*domain.Attachment, error) { const q = ` UPDATE attachments - SET status = 'processing', locked_until = now() + $2, updated_at = now(), attempts = attempts + 1 -- <--- ИНКРЕМЕНТ - WHERE id = $1::uuid AND status = 'queued' + SET status = 'processing', locked_until = now() + $2, updated_at = now(), attempts = attempts + 1 + WHERE id = $1::uuid AND status = 'queued' AND pipeline_version = $3 RETURNING id, chat_id, staging_key, pipeline_version, object_key, content_type, attempts ` var a domain.Attachment - err := r.pool.QueryRow(ctx, q, id, lease).Scan(&a.ID, &a.ChatID, &a.StagingKey, &a.PipelineVersion, &a.ObjectKey, &a.ContentType, &a.Attempts) + err := r.pool.QueryRow(ctx, q, id, lease, version).Scan(&a.ID, &a.ChatID, &a.StagingKey, &a.PipelineVersion, &a.ObjectKey, &a.ContentType, &a.Attempts) if err != nil { if err == pgx.ErrNoRows { return nil, nil diff --git a/backend/internal/worker/pipeline.go b/backend/internal/worker/pipeline.go index fbc98d8e..cb848238 100644 --- a/backend/internal/worker/pipeline.go +++ b/backend/internal/worker/pipeline.go @@ -42,19 +42,19 @@ func NewPipeline( } } -func (p *Pipeline) Process(ctx context.Context, attachmentID string) error { - log.Printf("worker: начало обработки %s", attachmentID) +func (p *Pipeline) Process(ctx context.Context, attachmentID string, pipelineVersion int) error { + log.Printf("worker: начало обработки %s (версия пайплайна %d)", attachmentID, pipelineVersion) jobCtx, cancel := context.WithTimeout(ctx, 30*time.Second) defer cancel() - // 1. Забираем задачу (защита от дублей). Если уже взята или готова — nil. - a, err := p.jobs.ClaimForProcessing(jobCtx, attachmentID, p.lease) + // 1. Забираем задачу с проверкой версии + a, err := p.jobs.ClaimForProcessing(jobCtx, attachmentID, pipelineVersion, p.lease) if err != nil { return fmt.Errorf("захват задачи: %w", err) } if a == nil { - log.Printf("worker: задача %s уже взята или выполнена", attachmentID) + log.Printf("worker: задача %s уже взята, выполнена или версия устарела", attachmentID) return nil // no-op } -- 2.54.0 From 19ddbfd16a743e36423d1418652ced374ff2b9ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Fri, 13 Nov 2026 11:59:57 +0300 Subject: [PATCH 41/46] test(lab5): add fault injection script and expose ports for testing --- compose.yaml | 3 + student-tests/fault_injection.py | 96 ++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 student-tests/fault_injection.py diff --git a/compose.yaml b/compose.yaml index f9517356..4fd74bd4 100644 --- a/compose.yaml +++ b/compose.yaml @@ -26,6 +26,8 @@ services: timeout: 3s retries: 10 start_period: 5s + ports: + - "5432:5432" deploy: resources: limits: @@ -74,6 +76,7 @@ services: - rabbitmq_data:/var/lib/rabbitmq ports: - "15672:15672" + - "5672:5672" healthcheck: test: ["CMD", "rabbitmq-diagnostics", "-q", "ping"] interval: 5s diff --git a/student-tests/fault_injection.py b/student-tests/fault_injection.py new file mode 100644 index 00000000..ef6bdfa8 --- /dev/null +++ b/student-tests/fault_injection.py @@ -0,0 +1,96 @@ +#!/usr/bin/env python3 +import sys +import time +import json +import pika +import psycopg2 +from pathlib import Path + +RABBITMQ_URL = "amqp://messenger:sdsdjhkjshdjkhsdkjhkjsdh@localhost:5672/" +DB_URL = "postgresql://messenger:1de200d38ea7d870f772acfbf1887572@localhost:5432/messenger" + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tests')) +from client import Client, arguments, check, run + +def get_db(): + return psycopg2.connect(DB_URL) + +def publish_duplicate(attachment_id: str, pipeline_version: int): + connection = pika.BlockingConnection(pika.URLParameters(RABBITMQ_URL)) + channel = connection.channel() + + payload = json.dumps({ + "attachment_id": attachment_id, + "pipeline_version": pipeline_version, + "attempt": 1 + }) + + channel.basic_publish( + exchange='attachments', + routing_key='process', + body=payload.encode(), + properties=pika.BasicProperties(delivery_mode=2) + ) + connection.close() + +def main(): + args = arguments('Fault Injection (Track 2)') + c = Client(args.base_url, args.ca_file) + + print("=== ТЕСТ 1: Идемпотентность при дублировании сообщений ===") + alice, bob, eve, chat = c.bootstrap() + + att, content = c.attachment(alice, chat) + att_id = att['id'] + print(f"Загружено вложение: {att_id}") + + ready_att = c.wait_ready(bob, att) + print("Первая обработка успешна. Статус: ready.") + + with get_db() as conn: + with conn.cursor() as cur: + cur.execute("SELECT COUNT(*) FROM attachment_variants WHERE attachment_id = %s", (att_id,)) + count_before = cur.fetchone()[0] + + print(f"Вариантов в БД до дубля: {count_before}") + check(count_before == 3, "Ожидалось 3 варианта (thumbnail, crop, optimized)") + + print("Публикуем дубликат сообщения в RabbitMQ...") + publish_duplicate(att_id, 1) + time.sleep(2) + + with get_db() as conn: + with conn.cursor() as cur: + cur.execute("SELECT COUNT(*) FROM attachment_variants WHERE attachment_id = %s", (att_id,)) + count_after = cur.fetchone()[0] + + check(count_after == count_before, "Дубликат сообщения создал лишние варианты (НАРУШЕНИЕ ИДЕМПОТЕНТНОСТИ)") + print("PASS: Дубликат проигнорирован, лишних вариантов не создано.") + + print("\n=== ТЕСТ 2: Защита от устаревшей версии (Versioned Pipeline) ===") + att2, _ = c.attachment(alice, chat) + att2_id = att2['id'] + print(f"Загружено вложение 2: {att2_id}") + + with get_db() as conn: + with conn.cursor() as cur: + cur.execute("UPDATE attachments SET pipeline_version = 2, status = 'queued' WHERE id = %s", (att2_id,)) + conn.commit() + + print("Мы вручную поменяли версию на 2.") + print("Публикуем сообщение со СТАРОЙ версией (1)...") + publish_duplicate(att2_id, 1) + time.sleep(2) + + with get_db() as conn: + with conn.cursor() as cur: + cur.execute("SELECT status FROM attachments WHERE id = %s", (att2_id,)) + status = cur.fetchone()[0] + + check(status == 'queued', f"Старый воркер перезаписал статус! Текущий статус: {status}") + print("PASS: Задача со старой версией проигнорирована. Статус остался 'queued'.") + print("\nВСЕ ТЕСТЫ FAULT INJECTION ПРОЙДЕНЫ!") + +if __name__ == '__main__': + run(main) \ No newline at end of file -- 2.54.0 From de686d1956c4f2e47184eb4b8958ef52b0943e04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Fri, 13 Nov 2026 13:12:32 +0300 Subject: [PATCH 42/46] docs(lab5): add initial report structure, setup guide and architecture diagram --- REPORT.md | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 119 insertions(+), 7 deletions(-) diff --git a/REPORT.md b/REPORT.md index e98f75e9..34981256 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,17 +1,129 @@ -# Отчёт — лабораторная 5 + Отчёт — лабораторная 5 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Автор, группа: Голиков А.П., БВТ2303 +- Целевая оценка; выбранный трек 5, если нужен: Оценка 5, Трек 2 +- Commit/PR: будет добавлено после публикации. +- Использованные источники, библиотеки и помощь ИИ: Документация RabbitMQ (Publisher Confirms, DLX), документация Go (pgx, Gin), библиотека `disintegration/imaging` для ресайза, `gen2brain/webp` для WebP. При написании кода использовалась помощь ИИ для генерации boilerplate-кода и архитектурных консультаций. ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +**Требования (Prerequisites):** +- Docker и Docker Compose (рекомендуется Docker Desktop). +- Python 3.9+ (для запуска тестов). +- OpenSSL (для генерации секретов, встроен в Git Bash). +- ОС: Windows / Linux / macOS. + +**Быстрый старт (Bash / Git Bash):** +Для запуска стенда выполните следующие команды в корне проекта: +```sh +# 1. Создаем .env из шаблона +cp .env.example .env + +# 2. Генерируем секреты и автоматически обновляем .env +chmod +x setup_secrets.sh +./setup_secrets.sh + +# 3. Собираем и запускаем весь стенд +docker compose up -d --build + +# 4. Ожидаем, пока все сервисы пройдут healthcheck + Проверить статус: docker compose ps +``` + +Запуск тестов (Windows / PowerShell): +Для корректной работы Python на Windows с кириллицей в JSON-контрактах необходимо указать кодировку UTF-8: +```powershell + +$env:PYTHONUTF8 = "1" +make test +# или +python -X utf8 tests/smoke.py +``` +Безопасная остановка и удаление данных: +```sh + +# Остановить контейнеры +docker compose stop + +# Полностью удалить контейнеры и тома с учебными данными +docker compose down -v +``` ## Архитектура и решения +``` +[ Клиент ] + │ POST /attachments (bytes) + ▼ +[ API (реплики) ] ───(1) быстрый sniff & DecodeConfig───> [ Проверка лимитов ] + │ + ├──(2) PUT объект в S3 (prefix: staging/{id}) + │ + └──(3) BEGIN TX + ├── INSERT INTO attachments (status='queued') + └── INSERT INTO outbox (payload=JobMessage) + COMMIT TX + │ + ▼ HTTP 202 Accepted (статус queued) + │ +[ Outbox Publisher (горутина) ] ──(4) SELECT ... FOR UPDATE SKIP LOCKED──> [ PostgreSQL ] + │ + ├──(5) Publish в RabbitMQ с Publisher Confirm + └──(6) UPDATE outbox SET published_at = now() + │ + ▼ +[ RabbitMQ ] (Очередь attachments.process, durable) + │ + ▼ +[ Worker ] ──(7) ClaimForProcessing (status='processing', аренда 5 мин) + │ + ├──(8) GET staging/{id} из S3 + ├──(9) Генерация вариантов: Thumbnail, Crop, Optimized(WebP) + ├──(10) PUT оригинал и варианты в S3 (prefix: chats/{chat_id}/{id}) + ├──(11) BEGIN TX + │ ├── UPDATE attachments SET status='ready' + │ └── INSERT INTO attachment_variants + │ COMMIT TX + └──(12) ACK сообщения в RabbitMQ +``` +Источник истины и транзакционные границы + + Источник истины: PostgreSQL. Только база данных является авторитетным хранилищем состояний (queued, processing, ready, failed) и метаданных. + + Транзакционные границы: Для преодоления проблемы «двойной записи» (Dual-Write) между базой данных и брокером сообщений используется паттерн Transactional Outbox. Запись INSERT attachments и INSERT outbox происходит в рамках одной ACID-транзакции PostgreSQL. Это гарантирует, что если клиент получил 202 Accepted, задача сохранена на диске и не будет потеряна, даже если RabbitMQ временно недоступен (Publisher дослать её при восстановлении). + + Идемпотентность: Брокер гарантирует доставку at-least-once (минимум один раз). Защита от дублей реализована на уровне метода ClaimForProcessing (атомарный UPDATE ... WHERE status='queued'), а также через защиту pipeline_version в методе CompleteJob. + +Что перенесено из lab4 и что изменено + + Сохранено: Базовая структура БД (пользователи, чаты, сообщения), аутентификация (Bearer/Basic), идемпотентность создания сообщений, интерфейсы репозиториев. + + Изменено: + + Отказ от синхронного ресайза: Метод Upload больше не делает полный image.Decode (который потреблял до 160 МБ RAM), теперь выполняется только быстрый image.DecodeConfig по заголовку. + + Код ответа: 201 Created заменен на 202 Accepted, так как обработка стала асинхронной. + + Staging: Оригинал при загрузке теперь сохраняется во временный префикс S3 (staging/), а не сразу в конечный (chats/). + + Состояния: Удален статус pending, добавлены queued и processing. + + Инфраструктура: Добавлены сервисы RabbitMQ и Worker. Появился фоновый процесс Outbox Publisher (внутри API) и Reconciler (сборка мусора и спасение зависших задач). + + Обработка: Добавлена генерация crop (128x128) и optimized (WebP до 1280x1280) с удалением метаданных. + +Конфигурация и переменные (.env.example) + +Переменные окружения разделены на логические блоки и передаются в контейнеры через compose.yaml: + + API: APP_PORT, SESSION_TTL_SECONDS, API_REPLICAS (масштабирование). + + Лимиты файлов: MAX_UPLOAD_BYTES (10 МБ), MAX_IMAGE_PIXELS (40 млн). + + Outbox & Worker: OUTBOX_INTERVAL_MS (частота опроса таблицы outbox), WORKER_CONCURRENCY (количество параллельных обработчиков), JOB_MAX_ATTEMPTS (лимит попыток), JOB_TIMEOUT_SECONDS (лимит времени на одну картинку). + + Инфраструктура: Строки подключения (DATABASE_URL, CACHE_URL, RABBITMQ_URL) и настройки S3/MinIO. Все пароли вынесены в Docker Secrets (генерация через setup_secrets.sh). + -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab4, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. ## Доказательства критериев -- 2.54.0 From e1bea0bc3e9e6301312e6eb21dedceb8f3bd54e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Fri, 13 Nov 2026 13:15:15 +0300 Subject: [PATCH 43/46] chore(lab5): add secret maker --- .env.example | 25 +++++++++++++++++-------- setup_secrets.sh | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 setup_secrets.sh diff --git a/.env.example b/.env.example index 119384b5..5c21c595 100644 --- a/.env.example +++ b/.env.example @@ -1,25 +1,34 @@ # Copy to .env for your LOCAL synthetic test environment; never commit .env. -# These are logical interface suggestions; map actual framework names in REPORT.md. +# Run ./setup_secrets.sh before starting to generate required secrets. APP_HOST=0.0.0.0 APP_PORT=8080 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 CACHE_URL_FILE=/run/secrets/cache_url API_REPLICAS=2 IDEMPOTENCY_TTL_SECONDS=86400 -S3_ENDPOINT=http://s3:9000 + +# S3 / MinIO Settings +S3_ENDPOINT=http://minio:9000 S3_REGION=us-east-1 S3_BUCKET=messenger-attachments S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_access_key_id S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret_access_key +MINIO_ROOT_USER_FILE=/run/secrets/minio_root_user +MINIO_ROOT_PASSWORD_FILE=/run/secrets/minio_root_password + +# Upload Limits MAX_UPLOAD_BYTES=10485760 MAX_IMAGE_PIXELS=40000000 -BROKER_URL_FILE=/run/secrets/broker_url + +# RabbitMQ Settings +RABBITMQ_PASSWORD=secret_rabbit +RABBITMQ_URL=amqp://messenger:secret_rabbit@rabbitmq:5672/ + +# Outbox & Worker Settings +OUTBOX_INTERVAL_MS=1000 WORKER_CONCURRENCY=2 JOB_MAX_ATTEMPTS=5 -JOB_TIMEOUT_SECONDS=60 -STAGING_RETENTION_SECONDS=86400 +JOB_TIMEOUT_SECONDS=60 \ No newline at end of file diff --git a/setup_secrets.sh b/setup_secrets.sh new file mode 100644 index 00000000..84b0c2e6 --- /dev/null +++ b/setup_secrets.sh @@ -0,0 +1,38 @@ +#!/bin/bash +set -eu + +echo "Генерация локальных секретов в папке secrets/..." +mkdir -p secrets + +# Генерируем случайные пароли +PG_PW="$(openssl rand -hex 16)" +MINIO_PW="$(openssl rand -hex 16)" +RABBIT_PW="$(openssl rand -hex 16)" +S3_KEY="$(openssl rand -hex 10)" +S3_SECRET="$(openssl rand -hex 20)" + +# Записываем в файлы без переносов строк +printf "%s" "$PG_PW" > secrets/postgres_password +printf "%s" "postgres://messenger:$PG_PW@postgres:5432/messenger?sslmode=disable" > secrets/database_url +printf "%s" "redis://redis:6379/0" > secrets/cache_url + +printf "%s" "admin" > secrets/minio_root_user +printf "%s" "$MINIO_PW" > secrets/minio_root_password +printf "%s" "$S3_KEY" > secrets/s3_access_key_id +printf "%s" "$S3_SECRET" > secrets/s3_secret_access_key + +printf "%s" "$RABBIT_PW" > secrets/rabbitmq_password + +# Выставляем права только для владельца (в Linux/Mac, в Windows игнорируется) +chmod 600 secrets/* 2>/dev/null || true + +# Обновляем .env, чтобы подставить правильный пароль для RabbitMQ +if [ -f .env ]; then + # Заменяем пароль в .env на сгенерированный + sed -i "s/^RABBITMQ_PASSWORD=.*/RABBITMQ_PASSWORD=$RABBIT_PW/" .env + sed -i "s#^RABBITMQ_URL=.*#RABBITMQ_URL=amqp://messenger:$RABBIT_PW@rabbitmq:5672/#" .env +else + echo "ВНИМАНИЕ: файл .env не найден. Сначала выполните 'cp .env.example .env', затем запустите этот скрипт снова." +fi + +echo "Секреты успешно сгенерированы!" \ No newline at end of file -- 2.54.0 From 2c6c8e8a0b2c83e1f28aa32fbcd708170ef03faa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Fri, 13 Nov 2026 19:32:10 +0300 Subject: [PATCH 44/46] test(lab5): add chaos scripts, fault generation tool and evidence logs --- evidence/api_logs.txt | 314 +++++++++++++++++++++++++++++++++++ evidence/failed_jobs.txt | Bin 0 -> 626 bytes evidence/worker_logs.txt | 76 +++++++++ make_failed_job.py | 42 +++++ scripts/chaos/kill-worker.sh | 5 + scripts/chaos/pause-s3.sh | 7 + scripts/restart.bat | 3 + scripts/restart.sh | 31 ++-- scripts/stop-one.bat | 2 + 9 files changed, 470 insertions(+), 10 deletions(-) create mode 100644 evidence/api_logs.txt create mode 100644 evidence/failed_jobs.txt create mode 100644 evidence/worker_logs.txt create mode 100644 make_failed_job.py create mode 100644 scripts/chaos/kill-worker.sh create mode 100644 scripts/chaos/pause-s3.sh create mode 100644 scripts/restart.bat create mode 100644 scripts/stop-one.bat diff --git a/evidence/api_logs.txt b/evidence/api_logs.txt new file mode 100644 index 00000000..4310afab --- /dev/null +++ b/evidence/api_logs.txt @@ -0,0 +1,314 @@ +api-2 | 2026/11/13 18:50:52 outbox: publisher запущен (interval=1s) +api-1 | 2026/11/13 18:50:52 outbox: publisher запущен (interval=1s) +api-1 | 2026/11/13 18:50:52 reconciler: запущен (interval=5m0s, grace=1h0m0s) +api-1 | 2026/11/13 18:50:52 Запускаем на 0.0.0.0:8080 (SESSION_STORE=redis, RECONCILER_ENABLED=true) +api-1 | 2026/11/13 18:50:52 reconciler: очистка завершена (восстановлено_задач=0, удалено_staging=3, удалено_сирот=0) +api-1 | [GIN] 2026/11/13 - 18:50:57 | 200 | 1.33ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:51:02 | 200 | 1.18ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:51:05 | 200 | 1.2ms | 172.20.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:51:06 | 201 | 499.08ms | 172.20.0.1 | POST "/api/v1/auth/sessions" +api-1 | [GIN] 2026/11/13 - 18:51:07 | 201 | 403.94ms | 172.20.0.1 | POST "/api/v1/auth/sessions" +api-1 | [GIN] 2026/11/13 - 18:51:07 | 200 | 1.32ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:51:08 | 201 | 212.15ms | 172.20.0.1 | POST "/api/v1/auth/sessions" +api-1 | [GIN] 2026/11/13 - 18:51:08 | 201 | 5.93ms | 172.20.0.1 | POST "/api/v1/chats/cd232c5a-e91a-4a17-b89a-80d57b1b4067/messages" +api-1 | [GIN] 2026/11/13 - 18:51:08 | 200 | 2.58ms | 172.20.0.1 | GET "/api/v1/attachments/f328767c-3436-4016-bdcd-c5fea4ecb85c" +api-2 | 2026/11/13 18:50:52 reconciler: запущен (interval=5m0s, grace=1h0m0s) +api-1 | [GIN] 2026/11/13 - 18:51:09 | 200 | 2.08ms | 172.20.0.1 | GET "/api/v1/attachments/f328767c-3436-4016-bdcd-c5fea4ecb85c" +api-1 | 2026/11/13 18:51:09 Получен сигнал завершения; readiness выключен, drain 3s +api-1 | 2026/11/13 18:51:09 reconciler: остановлен +api-2 | 2026/11/13 18:50:52 Запускаем на 0.0.0.0:8080 (SESSION_STORE=redis, RECONCILER_ENABLED=true) +api-2 | [GIN] 2026/11/13 - 18:50:57 | 200 | 1.74ms | 127.0.0.1 | GET "/health/ready" +api-1 | 2026/11/13 18:51:09 outbox: publisher остановлен +api-1 | 2026/11/13 18:51:12 Drain завершён, останавливаем HTTP-сервер... +api-2 | [GIN] 2026/11/13 - 18:51:02 | 200 | 1.12ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:51:05 | 200 | 29.757µs | 172.20.0.1 | GET "/health/live" +api-2 | [GIN] 2026/11/13 - 18:51:06 | 201 | 433.68ms | 172.20.0.1 | POST "/api/v1/users" +api-2 | [GIN] 2026/11/13 - 18:51:07 | 201 | 480.7ms | 172.20.0.1 | POST "/api/v1/users" +api-2 | [GIN] 2026/11/13 - 18:51:07 | 200 | 6.63ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:51:08 | 201 | 281.45ms | 172.20.0.1 | POST "/api/v1/users" +api-1 | 2026/11/13 18:51:12 Сервер остановлен +api-1 | 2026/11/13 18:51:12 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-1 | 2026/11/13 18:51:13 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-1 | 2026/11/13 18:51:13 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-1 | 2026/11/13 18:51:14 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-2 | [GIN] 2026/11/13 - 18:51:08 | 201 | 5.07ms | 172.20.0.1 | POST "/api/v1/chats" +api-2 | [GIN] 2026/11/13 - 18:51:08 | 202 | 8.53ms | 172.20.0.1 | POST "/api/v1/chats/cd232c5a-e91a-4a17-b89a-80d57b1b4067/attachments?filename=sample.png" +api-2 | [GIN] 2026/11/13 - 18:51:08 | 200 | 2.75ms | 172.20.0.1 | GET "/api/v1/attachments/f328767c-3436-4016-bdcd-c5fea4ecb85c" +api-2 | 2026/11/13 18:51:08 outbox: успешно опубликовано 1 сообщений +api-1 | 2026/11/13 18:51:15 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-2 | 2026/11/13 18:51:09 Получен сигнал завершения; readiness выключен, drain 3s +api-2 | 2026/11/13 18:51:09 reconciler: остановлен +api-1 | 2026/11/13 18:51:16 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-1 | 2026/11/13 18:51:20 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-2 | 2026/11/13 18:51:09 outbox: publisher остановлен +api-2 | 2026/11/13 18:51:12 Drain завершён, останавливаем HTTP-сервер... +api-1 | 2026/11/13 18:51:27 outbox: publisher запущен (interval=1s) +api-2 | 2026/11/13 18:51:12 Сервер остановлен +api-2 | 2026/11/13 18:51:12 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-2 | 2026/11/13 18:51:13 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-2 | 2026/11/13 18:51:13 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-2 | 2026/11/13 18:51:14 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-1 | 2026/11/13 18:51:27 reconciler: запущен (interval=5m0s, grace=1h0m0s) +api-1 | 2026/11/13 18:51:27 Запускаем на 0.0.0.0:8080 (SESSION_STORE=redis, RECONCILER_ENABLED=true) +api-1 | [GIN] 2026/11/13 - 18:51:30 | 200 | 1.39ms | 172.20.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:51:30 | 200 | 6.73ms | 172.20.0.1 | GET "/api/v1/chats/cd232c5a-e91a-4a17-b89a-80d57b1b4067/messages?limit=100" +api-2 | 2026/11/13 18:51:15 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-2 | 2026/11/13 18:51:16 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-2 | 2026/11/13 18:51:20 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-1 | [GIN] 2026/11/13 - 18:51:32 | 200 | 2.2ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:51:37 | 200 | 1.25ms | 127.0.0.1 | GET "/health/ready" +api-2 | 2026/11/13 18:51:27 outbox: publisher запущен (interval=1s) +api-2 | 2026/11/13 18:51:27 reconciler: запущен (interval=5m0s, grace=1h0m0s) +api-1 | [GIN] 2026/11/13 - 18:51:42 | 200 | 977.159µs | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:51:47 | 200 | 1.13ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:51:52 | 200 | 1.52ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:51:57 | 200 | 1.29ms | 127.0.0.1 | GET "/health/ready" +api-2 | 2026/11/13 18:51:27 Запускаем на 0.0.0.0:8080 (SESSION_STORE=redis, RECONCILER_ENABLED=true) +api-1 | [GIN] 2026/11/13 - 18:52:02 | 200 | 1.37ms | 127.0.0.1 | GET "/health/ready" +api-2 | 2026/11/13 18:51:27 reconciler: очистка завершена (восстановлено_задач=0, удалено_staging=2, удалено_сирот=0) +api-1 | [GIN] 2026/11/13 - 18:52:07 | 200 | 1.42ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:52:12 | 200 | 1.25ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:52:17 | 200 | 938.321µs | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:52:22 | 200 | 1.4ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:51:30 | 200 | 2.15ms | 172.20.0.1 | GET "/api/v1/users/me" +api-2 | [GIN] 2026/11/13 - 18:51:30 | 503 | 2.28ms | 172.20.0.1 | GET "/api/v1/attachments/f328767c-3436-4016-bdcd-c5fea4ecb85c" +api-1 | [GIN] 2026/11/13 - 18:52:27 | 200 | 1.15ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:52:33 | 200 | 1.47ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:52:38 | 200 | 1.21ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:52:43 | 200 | 1.73ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:51:32 | 200 | 1.2ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:51:37 | 200 | 1.25ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:52:48 | 200 | 1.72ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:52:53 | 200 | 1.54ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:52:58 | 200 | 1.27ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:51:42 | 200 | 1.37ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:51:47 | 200 | 1.18ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:51:52 | 200 | 1.57ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:51:57 | 200 | 1.02ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:53:03 | 200 | 1.09ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:53:08 | 200 | 1.25ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:53:14 | 200 | 1.57ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:53:19 | 200 | 1.52ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:52:02 | 200 | 1.41ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:52:07 | 200 | 1.45ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:53:24 | 200 | 1.59ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:53:29 | 200 | 1.61ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:52:12 | 200 | 1.31ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:52:17 | 200 | 1.18ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:53:34 | 200 | 1.18ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:53:39 | 200 | 1.21ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:52:22 | 200 | 1.25ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:52:27 | 200 | 1.15ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:53:44 | 200 | 1.54ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:53:49 | 200 | 1.52ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:52:33 | 200 | 2.27ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:52:38 | 200 | 1.09ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:52:43 | 200 | 1.53ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:52:48 | 200 | 1.32ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:52:53 | 200 | 1.32ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:52:58 | 200 | 933.949µs | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:53:03 | 200 | 1.12ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:53:08 | 200 | 1.35ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:53:14 | 200 | 1.42ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:53:54 | 200 | 1.14ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:53:57 | 200 | 38.724µs | 172.20.0.1 | GET "/health/live" +api-1 | [GIN] 2026/11/13 - 18:53:57 | 201 | 345.16ms | 172.20.0.1 | POST "/api/v1/users" +api-2 | [GIN] 2026/11/13 - 18:53:19 | 200 | 1.61ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:53:24 | 200 | 999.43µs | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:53:29 | 200 | 2.7ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:53:34 | 200 | 1.39ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:53:58 | 201 | 428.24ms | 172.20.0.1 | POST "/api/v1/users" +api-1 | [GIN] 2026/11/13 - 18:53:59 | 201 | 220.38ms | 172.20.0.1 | POST "/api/v1/users" +api-1 | [GIN] 2026/11/13 - 18:53:59 | 201 | 4.54ms | 172.20.0.1 | POST "/api/v1/chats" +api-1 | [GIN] 2026/11/13 - 18:53:59 | 202 | 8.73ms | 172.20.0.1 | POST "/api/v1/chats/7f8e2c9a-bd1a-48b3-8f23-c13151922baf/attachments?filename=sample.png" +api-2 | [GIN] 2026/11/13 - 18:53:39 | 200 | 1.19ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:53:59 | 200 | 1.28ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:53:59 | 200 | 2.77ms | 172.20.0.1 | GET "/api/v1/attachments/3e8c4363-1eb9-46d1-9b44-613f5f946446" +api-1 | 2026/11/13 18:54:00 Получен сигнал завершения; readiness выключен, drain 3s +api-2 | [GIN] 2026/11/13 - 18:53:44 | 200 | 1.16ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:53:49 | 200 | 1.31ms | 127.0.0.1 | GET "/health/ready" +api-1 | 2026/11/13 18:54:00 reconciler: остановлен +api-1 | 2026/11/13 18:54:00 outbox: publisher остановлен +api-2 | [GIN] 2026/11/13 - 18:53:54 | 200 | 807.794µs | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:53:57 | 200 | 1.03ms | 172.20.0.1 | GET "/health/ready" +api-1 | 2026/11/13 18:54:03 Drain завершён, останавливаем HTTP-сервер... +api-1 | 2026/11/13 18:54:03 Сервер остановлен +api-2 | [GIN] 2026/11/13 - 18:53:58 | 201 | 442.76ms | 172.20.0.1 | POST "/api/v1/auth/sessions" +api-2 | [GIN] 2026/11/13 - 18:53:59 | 201 | 365.57ms | 172.20.0.1 | POST "/api/v1/auth/sessions" +api-1 | 2026/11/13 18:54:03 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-1 | 2026/11/13 18:54:04 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-1 | 2026/11/13 18:54:04 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-1 | 2026/11/13 18:54:05 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-1 | 2026/11/13 18:54:06 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-1 | 2026/11/13 18:54:08 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-1 | 2026/11/13 18:54:11 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-2 | [GIN] 2026/11/13 - 18:53:59 | 201 | 176.63ms | 172.20.0.1 | POST "/api/v1/auth/sessions" +api-2 | [GIN] 2026/11/13 - 18:53:59 | 201 | 6.06ms | 172.20.0.1 | POST "/api/v1/chats/7f8e2c9a-bd1a-48b3-8f23-c13151922baf/messages" +api-1 | 2026/11/13 18:54:18 outbox: publisher запущен (interval=1s) +api-1 | 2026/11/13 18:54:18 reconciler: запущен (interval=5m0s, grace=1h0m0s) +api-2 | [GIN] 2026/11/13 - 18:53:59 | 200 | 3.1ms | 172.20.0.1 | GET "/api/v1/attachments/3e8c4363-1eb9-46d1-9b44-613f5f946446" +api-2 | [GIN] 2026/11/13 - 18:53:59 | 200 | 1.18ms | 127.0.0.1 | GET "/health/ready" +api-1 | 2026/11/13 18:54:18 Запускаем на 0.0.0.0:8080 (SESSION_STORE=redis, RECONCILER_ENABLED=true) +api-1 | [GIN] 2026/11/13 - 18:54:22 | 200 | 1.09ms | 172.20.0.1 | GET "/health/ready" +api-2 | 2026/11/13 18:54:00 outbox: успешно опубликовано 1 сообщений +api-2 | [GIN] 2026/11/13 - 18:54:00 | 200 | 1.98ms | 172.20.0.1 | GET "/api/v1/attachments/3e8c4363-1eb9-46d1-9b44-613f5f946446" +api-1 | [GIN] 2026/11/13 - 18:54:22 | 201 | 405.68ms | 172.20.0.1 | POST "/api/v1/auth/sessions" +api-1 | [GIN] 2026/11/13 - 18:54:23 | 200 | 1.28ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:54:23 | 201 | 425.5ms | 172.20.0.1 | POST "/api/v1/auth/sessions" +api-2 | 2026/11/13 18:54:00 Получен сигнал завершения; readiness выключен, drain 3s +api-2 | 2026/11/13 18:54:00 outbox: publisher остановлен +api-1 | [GIN] 2026/11/13 - 18:54:24 | 201 | 225.03ms | 172.20.0.1 | POST "/api/v1/auth/sessions" +api-2 | 2026/11/13 18:54:00 reconciler: остановлен +api-2 | 2026/11/13 18:54:03 Drain завершён, останавливаем HTTP-сервер... +api-1 | [GIN] 2026/11/13 - 18:54:24 | 201 | 6.66ms | 172.20.0.1 | POST "/api/v1/chats/058f30a2-7854-4909-89f2-18a7956b52ee/messages" +api-1 | [GIN] 2026/11/13 - 18:54:24 | 200 | 3.9ms | 172.20.0.1 | GET "/api/v1/attachments/5c05619e-bce6-4ccb-b180-83b80f90dee3" +api-2 | 2026/11/13 18:54:03 Сервер остановлен +api-2 | 2026/11/13 18:54:03 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-1 | [GIN] 2026/11/13 - 18:54:25 | 200 | 1.72ms | 172.20.0.1 | GET "/api/v1/attachments/5c05619e-bce6-4ccb-b180-83b80f90dee3" +api-1 | 2026/11/13 18:54:25 Получен сигнал завершения; readiness выключен, drain 3s +api-2 | 2026/11/13 18:54:04 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-2 | 2026/11/13 18:54:04 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-1 | 2026/11/13 18:54:25 reconciler: остановлен +api-1 | 2026/11/13 18:54:25 outbox: publisher остановлен +api-2 | 2026/11/13 18:54:05 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-2 | 2026/11/13 18:54:06 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-2 | 2026/11/13 18:54:08 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-2 | 2026/11/13 18:54:11 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-1 | [GIN] 2026/11/13 - 18:54:28 | 503 | 124.888µs | 127.0.0.1 | GET "/health/ready" +api-1 | 2026/11/13 18:54:28 Drain завершён, останавливаем HTTP-сервер... +api-2 | 2026/11/13 18:54:18 outbox: publisher запущен (interval=1s) +api-2 | 2026/11/13 18:54:18 reconciler: запущен (interval=5m0s, grace=1h0m0s) +api-1 | 2026/11/13 18:54:28 Сервер остановлен +api-1 | 2026/11/13 18:54:29 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-1 | 2026/11/13 18:54:29 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-1 | 2026/11/13 18:54:29 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-2 | 2026/11/13 18:54:18 Запускаем на 0.0.0.0:8080 (SESSION_STORE=redis, RECONCILER_ENABLED=true) +api-2 | 2026/11/13 18:54:18 reconciler: очистка завершена (восстановлено_задач=0, удалено_staging=1, удалено_сирот=0) +api-2 | [GIN] 2026/11/13 - 18:54:22 | 200 | 69.894µs | 172.20.0.1 | GET "/health/live" +api-2 | [GIN] 2026/11/13 - 18:54:22 | 201 | 398.03ms | 172.20.0.1 | POST "/api/v1/users" +api-1 | 2026/11/13 18:54:30 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-1 | 2026/11/13 18:54:31 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-1 | 2026/11/13 18:54:33 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-1 | 2026/11/13 18:54:36 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-2 | [GIN] 2026/11/13 - 18:54:23 | 200 | 9.18ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:54:23 | 201 | 470.47ms | 172.20.0.1 | POST "/api/v1/users" +api-2 | [GIN] 2026/11/13 - 18:54:24 | 201 | 261.07ms | 172.20.0.1 | POST "/api/v1/users" +api-1 | 2026/11/13 18:54:43 outbox: publisher запущен (interval=1s) +api-1 | 2026/11/13 18:54:43 reconciler: запущен (interval=5m0s, grace=1h0m0s) +api-2 | [GIN] 2026/11/13 - 18:54:24 | 201 | 4.53ms | 172.20.0.1 | POST "/api/v1/chats" +api-2 | [GIN] 2026/11/13 - 18:54:24 | 202 | 9.35ms | 172.20.0.1 | POST "/api/v1/chats/058f30a2-7854-4909-89f2-18a7956b52ee/attachments?filename=sample.png" +api-1 | 2026/11/13 18:54:43 Запускаем на 0.0.0.0:8080 (SESSION_STORE=redis, RECONCILER_ENABLED=true) +api-1 | 2026/11/13 18:54:43 reconciler: очистка завершена (восстановлено_задач=0, удалено_staging=1, удалено_сирот=0) +api-2 | [GIN] 2026/11/13 - 18:54:24 | 200 | 3.95ms | 172.20.0.1 | GET "/api/v1/attachments/5c05619e-bce6-4ccb-b180-83b80f90dee3" +api-2 | 2026/11/13 18:54:25 outbox: успешно опубликовано 1 сообщений +api-1 | [GIN] 2026/11/13 - 18:54:45 | 200 | 1.91ms | 172.20.0.1 | GET "/api/v1/users/me" +api-1 | [GIN] 2026/11/13 - 18:54:45 | 503 | 1.92ms | 172.20.0.1 | GET "/api/v1/attachments/5c05619e-bce6-4ccb-b180-83b80f90dee3" +api-2 | [GIN] 2026/11/13 - 18:54:25 | 200 | 2.19ms | 172.20.0.1 | GET "/api/v1/attachments/5c05619e-bce6-4ccb-b180-83b80f90dee3" +api-2 | 2026/11/13 18:54:25 Получен сигнал завершения; readiness выключен, drain 3s +api-1 | [GIN] 2026/11/13 - 18:54:48 | 200 | 1.89ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:54:53 | 200 | 1.22ms | 127.0.0.1 | GET "/health/ready" +api-2 | 2026/11/13 18:54:25 reconciler: остановлен +api-2 | 2026/11/13 18:54:25 outbox: publisher остановлен +api-1 | [GIN] 2026/11/13 - 18:54:59 | 200 | 1.18ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:55:04 | 200 | 1.26ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:54:28 | 503 | 265.498µs | 127.0.0.1 | GET "/health/ready" +api-2 | 2026/11/13 18:54:28 Drain завершён, останавливаем HTTP-сервер... +api-1 | [GIN] 2026/11/13 - 18:55:09 | 200 | 1.52ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:55:14 | 200 | 1.03ms | 127.0.0.1 | GET "/health/ready" +api-2 | 2026/11/13 18:54:28 Сервер остановлен +api-2 | 2026/11/13 18:54:29 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-1 | [GIN] 2026/11/13 - 18:55:19 | 200 | 1.14ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:55:24 | 200 | 1.26ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:55:29 | 200 | 1.3ms | 127.0.0.1 | GET "/health/ready" +api-2 | 2026/11/13 18:54:29 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-2 | 2026/11/13 18:54:30 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-2 | 2026/11/13 18:54:30 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-1 | [GIN] 2026/11/13 - 18:55:34 | 200 | 1.52ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:55:39 | 200 | 1.26ms | 127.0.0.1 | GET "/health/ready" +api-2 | 2026/11/13 18:54:31 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-2 | 2026/11/13 18:54:33 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-1 | [GIN] 2026/11/13 - 18:55:44 | 200 | 1.21ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:55:49 | 200 | 1.23ms | 127.0.0.1 | GET "/health/ready" +api-2 | 2026/11/13 18:54:36 rabbitmq: подключение к rabbitmq: dial tcp 172.20.0.3:5672: connect: connection refused +api-2 | 2026/11/13 18:54:43 outbox: publisher запущен (interval=1s) +api-1 | [GIN] 2026/11/13 - 18:55:54 | 200 | 1.16ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:56:00 | 200 | 1.23ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:56:03 | 200 | 1.48ms | 172.20.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:56:04 | 201 | 475.58ms | 172.20.0.1 | POST "/api/v1/auth/sessions" +api-2 | 2026/11/13 18:54:43 reconciler: запущен (interval=5m0s, grace=1h0m0s) +api-2 | 2026/11/13 18:54:43 Запускаем на 0.0.0.0:8080 (SESSION_STORE=redis, RECONCILER_ENABLED=true) +api-2 | [GIN] 2026/11/13 - 18:54:45 | 200 | 1.22ms | 172.20.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:54:45 | 200 | 5.33ms | 172.20.0.1 | GET "/api/v1/chats/058f30a2-7854-4909-89f2-18a7956b52ee/messages?limit=100" +api-1 | [GIN] 2026/11/13 - 18:56:05 | 201 | 349.47ms | 172.20.0.1 | POST "/api/v1/auth/sessions" +api-1 | [GIN] 2026/11/13 - 18:56:05 | 200 | 6.69ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:56:05 | 201 | 360.85ms | 172.20.0.1 | POST "/api/v1/auth/sessions" +api-1 | [GIN] 2026/11/13 - 18:56:05 | 409 | 241.83ms | 172.20.0.1 | POST "/api/v1/users" +api-2 | [GIN] 2026/11/13 - 18:54:48 | 200 | 1.28ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:54:53 | 200 | 1.37ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:56:05 | 404 | 2.37ms | 172.20.0.1 | GET "/api/v1/chats/49bcf3d8-4398-4f73-b2bf-850295e339b1" +api-1 | [GIN] 2026/11/13 - 18:56:05 | 200 | 1.69ms | 172.20.0.1 | GET "/api/v1/chats?limit=100" +api-1 | [GIN] 2026/11/13 - 18:56:06 | 201 | 4.82ms | 172.20.0.1 | POST "/api/v1/chats/49bcf3d8-4398-4f73-b2bf-850295e339b1/messages" +api-1 | [GIN] 2026/11/13 - 18:56:06 | 200 | 3.02ms | 172.20.0.1 | GET "/api/v1/chats/49bcf3d8-4398-4f73-b2bf-850295e339b1/messages?limit=1" +api-2 | [GIN] 2026/11/13 - 18:54:59 | 200 | 1.48ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:55:04 | 200 | 1.61ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:56:06 | 400 | 522.712µs | 172.20.0.1 | POST "/api/v1/chats/49bcf3d8-4398-4f73-b2bf-850295e339b1/messages" +api-1 | [GIN] 2026/11/13 - 18:56:06 | 400 | 495.985µs | 172.20.0.1 | GET "/api/v1/chats/49bcf3d8-4398-4f73-b2bf-850295e339b1/messages?limit=0" +api-2 | [GIN] 2026/11/13 - 18:55:09 | 200 | 1.22ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:55:14 | 200 | 1.21ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:56:06 | 401 | 37.872µs | 172.20.0.1 | GET "/api/v1/users/me" +api-1 | [GIN] 2026/11/13 - 18:56:06 | 200 | 950.545µs | 172.20.0.1 | GET "/api/v1/users/me" +api-2 | [GIN] 2026/11/13 - 18:55:19 | 200 | 1.29ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:55:24 | 200 | 1.14ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:56:06 | 409 | 2.27ms | 172.20.0.1 | POST "/api/v1/chats/49bcf3d8-4398-4f73-b2bf-850295e339b1/messages" +api-1 | [GIN] 2026/11/13 - 18:56:06 | 202 | 9.19ms | 172.20.0.1 | POST "/api/v1/chats/49bcf3d8-4398-4f73-b2bf-850295e339b1/attachments?filename=sample.png" +api-1 | [GIN] 2026/11/13 - 18:56:06 | 200 | 2.98ms | 172.20.0.1 | GET "/api/v1/attachments/1b706a21-dfa3-4f64-89b9-2815da9bbcb7" +api-2 | [GIN] 2026/11/13 - 18:55:29 | 200 | 1.32ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:55:34 | 200 | 1.33ms | 127.0.0.1 | GET "/health/ready" +api-1 | 2026/11/13 18:56:06 outbox: успешно опубликовано 1 сообщений +api-1 | [GIN] 2026/11/13 - 18:56:06 | 200 | 5.25ms | 172.20.0.1 | GET "/api/v1/attachments/1b706a21-dfa3-4f64-89b9-2815da9bbcb7/content" +api-2 | [GIN] 2026/11/13 - 18:55:39 | 200 | 1.29ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:55:45 | 200 | 1.33ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:56:06 | 404 | 1.78ms | 172.20.0.1 | GET "/api/v1/attachments/1b706a21-dfa3-4f64-89b9-2815da9bbcb7/content" +api-1 | [GIN] 2026/11/13 - 18:56:06 | 204 | 1.11ms | 172.20.0.1 | DELETE "/api/v1/auth/sessions/current" +api-2 | [GIN] 2026/11/13 - 18:55:50 | 200 | 1.25ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:55:55 | 200 | 1.13ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:56:06 | 401 | 271.021µs | 172.20.0.1 | DELETE "/api/v1/auth/sessions/current" +api-1 | [GIN] 2026/11/13 - 18:56:07 | 200 | 1.28ms | 172.20.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:56:00 | 200 | 1.62ms | 127.0.0.1 | GET "/health/ready" +api-1 | [GIN] 2026/11/13 - 18:56:07 | 201 | 239.96ms | 172.20.0.1 | POST "/api/v1/auth/sessions" +api-2 | [GIN] 2026/11/13 - 18:56:03 | 200 | 33.363µs | 172.20.0.1 | GET "/health/live" +api-2 | [GIN] 2026/11/13 - 18:56:03 | 201 | 456.43ms | 172.20.0.1 | POST "/api/v1/users" +api-2 | [GIN] 2026/11/13 - 18:56:04 | 201 | 336.05ms | 172.20.0.1 | POST "/api/v1/users" +api-1 | [GIN] 2026/11/13 - 18:56:08 | 201 | 164.57ms | 172.20.0.1 | POST "/api/v1/auth/sessions" +api-1 | [GIN] 2026/11/13 - 18:56:08 | 201 | 160.65ms | 172.20.0.1 | POST "/api/v1/auth/sessions" +api-1 | [GIN] 2026/11/13 - 18:56:08 | 202 | 6.65ms | 172.20.0.1 | POST "/api/v1/chats/e151aa0f-c644-444a-81ab-81a0ada46e37/attachments?filename=sample.png" +api-1 | [GIN] 2026/11/13 - 18:56:08 | 200 | 1.91ms | 172.20.0.1 | GET "/api/v1/attachments/1f9520fe-a094-4a7b-ae7c-700423ce068a" +api-1 | [GIN] 2026/11/13 - 18:56:10 | 200 | 1.17ms | 127.0.0.1 | GET "/health/ready" +api-1 | 2026/11/13 18:56:11 outbox: успешно опубликовано 1 сообщений +api-2 | [GIN] 2026/11/13 - 18:56:05 | 201 | 140.53ms | 172.20.0.1 | POST "/api/v1/users" +api-2 | [GIN] 2026/11/13 - 18:56:05 | 200 | 1.18ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:56:05 | 201 | 4.81ms | 172.20.0.1 | POST "/api/v1/chats" +api-2 | [GIN] 2026/11/13 - 18:56:05 | 401 | 120.747µs | 172.20.0.1 | GET "/api/v1/users/me" +api-2 | [GIN] 2026/11/13 - 18:56:05 | 200 | 534.087µs | 172.20.0.1 | GET "/api/v1/users/me" +api-2 | [GIN] 2026/11/13 - 18:56:05 | 200 | 1.4ms | 172.20.0.1 | GET "/api/v1/chats?limit=100" +api-2 | [GIN] 2026/11/13 - 18:56:06 | 201 | 5.8ms | 172.20.0.1 | POST "/api/v1/chats/49bcf3d8-4398-4f73-b2bf-850295e339b1/messages" +api-2 | [GIN] 2026/11/13 - 18:56:06 | 200 | 3.12ms | 172.20.0.1 | GET "/api/v1/chats/49bcf3d8-4398-4f73-b2bf-850295e339b1/messages?limit=1&cursor=bWVzc2FnZXM6NDliY2YzZDgtNDM5OC00ZjczLWIyYmYtODUwMjk1ZTMzOWIxfDIwMjYtMTEtMTNUMTg6NTY6MDUuOTk5NTk0WnxlNGE5MmMyMi1kYTBiLTRlMjMtODBjNS00NTQ5MTkzNTFlOGU" +api-2 | [GIN] 2026/11/13 - 18:56:06 | 404 | 1.06ms | 172.20.0.1 | POST "/api/v1/chats/49bcf3d8-4398-4f73-b2bf-850295e339b1/messages" +api-2 | [GIN] 2026/11/13 - 18:56:06 | 401 | 162.74ms | 172.20.0.1 | POST "/api/v1/auth/sessions" +api-2 | [GIN] 2026/11/13 - 18:56:06 | 401 | 44.184µs | 172.20.0.1 | GET "/api/v1/users/me" +api-2 | [GIN] 2026/11/13 - 18:56:06 | 201 | 2.59ms | 172.20.0.1 | POST "/api/v1/chats/49bcf3d8-4398-4f73-b2bf-850295e339b1/messages" +api-2 | [GIN] 2026/11/13 - 18:56:06 | 400 | 514.328µs | 172.20.0.1 | POST "/api/v1/chats/49bcf3d8-4398-4f73-b2bf-850295e339b1/messages" +api-2 | [GIN] 2026/11/13 - 18:56:06 | 201 | 5.15ms | 172.20.0.1 | POST "/api/v1/chats/49bcf3d8-4398-4f73-b2bf-850295e339b1/messages" +api-2 | [GIN] 2026/11/13 - 18:56:06 | 200 | 3.3ms | 172.20.0.1 | GET "/api/v1/attachments/1b706a21-dfa3-4f64-89b9-2815da9bbcb7" +api-2 | [GIN] 2026/11/13 - 18:56:06 | 404 | 2.02ms | 172.20.0.1 | GET "/api/v1/attachments/1b706a21-dfa3-4f64-89b9-2815da9bbcb7" +api-2 | [GIN] 2026/11/13 - 18:56:06 | 200 | 3.62ms | 172.20.0.1 | GET "/api/v1/attachments/1b706a21-dfa3-4f64-89b9-2815da9bbcb7/content?variant=thumbnail" +api-2 | [GIN] 2026/11/13 - 18:56:06 | 401 | 309.977µs | 172.20.0.1 | GET "/api/v1/users/me" +api-2 | [GIN] 2026/11/13 - 18:56:07 | 200 | 16.861µs | 172.20.0.1 | GET "/health/live" +api-2 | [GIN] 2026/11/13 - 18:56:07 | 201 | 218.77ms | 172.20.0.1 | POST "/api/v1/users" +api-2 | [GIN] 2026/11/13 - 18:56:07 | 201 | 139.91ms | 172.20.0.1 | POST "/api/v1/users" +api-2 | [GIN] 2026/11/13 - 18:56:08 | 201 | 217.51ms | 172.20.0.1 | POST "/api/v1/users" +api-2 | [GIN] 2026/11/13 - 18:56:08 | 201 | 3ms | 172.20.0.1 | POST "/api/v1/chats" +api-2 | [GIN] 2026/11/13 - 18:56:08 | 200 | 1.41ms | 172.20.0.1 | GET "/api/v1/attachments/1f9520fe-a094-4a7b-ae7c-700423ce068a" +api-2 | 2026/11/13 18:56:08 outbox: успешно опубликовано 1 сообщений +api-2 | [GIN] 2026/11/13 - 18:56:10 | 200 | 1.19ms | 127.0.0.1 | GET "/health/ready" +api-2 | [GIN] 2026/11/13 - 18:56:10 | 202 | 7.76ms | 172.20.0.1 | POST "/api/v1/chats/e151aa0f-c644-444a-81ab-81a0ada46e37/attachments?filename=sample.png" diff --git a/evidence/failed_jobs.txt b/evidence/failed_jobs.txt new file mode 100644 index 0000000000000000000000000000000000000000..1c06603072f78325e210f908ce9fba2a43248841 GIT binary patch literal 626 zcmb`E&1%Cy420(#=sWn>QVg||_~&WN+Uw9lS{J({mppsgPrEkc;1Vch4;f3E(PZ>} zO|<@;zIaZZUiGeCAAB`>lmBJjR4$=6GVL^JfYZPq;Pjzm{ixfc`HA8*vy5YmqfQ_X z&*l<6XruHW-}4=<_5OM_`Z9-!Dz(bA#d!;MY{|7#4Nm#Zsn(#1G;;o5ssoh{Mjm=w zy=c$eZcfAUT5@Zcb>w~wxhxsq-YlD0(TT%F8PrPj3wG^sRe;S&3yMxku&37H%Co~f cW^D4zcle2${_f-v#Y7`FJ#*Qoa7lCc14}MW761SM literal 0 HcmV?d00001 diff --git a/evidence/worker_logs.txt b/evidence/worker_logs.txt new file mode 100644 index 00000000..bb1aa844 --- /dev/null +++ b/evidence/worker_logs.txt @@ -0,0 +1,76 @@ +worker-1 | 2026/11/13 18:50:52 worker: запущен и слушает очередь +worker-1 | 2026/11/13 18:50:52 worker: начало обработки 84fcd359-05ce-42b1-a6bb-6e9a9ead961f (версия пайплайна 1) +worker-1 | 2026/11/13 18:50:52 worker: генерация вариантов для 84fcd359-05ce-42b1-a6bb-6e9a9ead961f +worker-1 | 2026/11/13 18:50:52 CompleteJob: успешно добавлен вариант thumbnail +worker-1 | 2026/11/13 18:50:52 CompleteJob: успешно добавлен вариант crop +worker-1 | 2026/11/13 18:50:52 CompleteJob: успешно добавлен вариант optimized +worker-1 | 2026/11/13 18:50:52 worker: успешно завершено 84fcd359-05ce-42b1-a6bb-6e9a9ead961f +worker-1 | 2026/11/13 18:50:52 worker: начало обработки 31ab08f9-193f-4d5c-b8a0-ed01ddf16f08 (версия пайплайна 1) +worker-1 | 2026/11/13 18:50:52 worker: генерация вариантов для 31ab08f9-193f-4d5c-b8a0-ed01ddf16f08 +worker-1 | 2026/11/13 18:50:52 CompleteJob: успешно добавлен вариант thumbnail +worker-1 | 2026/11/13 18:50:52 CompleteJob: успешно добавлен вариант crop +worker-1 | 2026/11/13 18:50:52 CompleteJob: успешно добавлен вариант optimized +worker-1 | 2026/11/13 18:50:52 worker: успешно завершено 31ab08f9-193f-4d5c-b8a0-ed01ddf16f08 +worker-1 | 2026/11/13 18:50:52 worker: начало обработки ceb7a516-0e3b-4f47-a6a9-71bb199b5c91 (версия пайплайна 1) +worker-1 | 2026/11/13 18:50:52 worker: генерация вариантов для ceb7a516-0e3b-4f47-a6a9-71bb199b5c91 +worker-1 | 2026/11/13 18:50:52 CompleteJob: успешно добавлен вариант thumbnail +worker-1 | 2026/11/13 18:50:52 CompleteJob: успешно добавлен вариант crop +worker-1 | 2026/11/13 18:50:52 CompleteJob: успешно добавлен вариант optimized +worker-1 | 2026/11/13 18:50:52 worker: успешно завершено ceb7a516-0e3b-4f47-a6a9-71bb199b5c91 +worker-1 | 2026/11/13 18:50:52 worker: начало обработки 9c78ae63-7ca6-48b2-bb44-1754732da29c (версия пайплайна 1) +worker-1 | 2026/11/13 18:50:52 worker: генерация вариантов для 9c78ae63-7ca6-48b2-bb44-1754732da29c +worker-1 | 2026/11/13 18:50:52 CompleteJob: успешно добавлен вариант thumbnail +worker-1 | 2026/11/13 18:50:52 CompleteJob: успешно добавлен вариант crop +worker-1 | 2026/11/13 18:50:52 CompleteJob: успешно добавлен вариант optimized +worker-1 | 2026/11/13 18:50:52 worker: успешно завершено 9c78ae63-7ca6-48b2-bb44-1754732da29c +worker-1 | 2026/11/13 18:51:08 worker: начало обработки f328767c-3436-4016-bdcd-c5fea4ecb85c (версия пайплайна 1) +worker-1 | 2026/11/13 18:51:08 worker: генерация вариантов для f328767c-3436-4016-bdcd-c5fea4ecb85c +worker-1 | 2026/11/13 18:51:08 CompleteJob: успешно добавлен вариант thumbnail +worker-1 | 2026/11/13 18:51:08 CompleteJob: успешно добавлен вариант crop +worker-1 | 2026/11/13 18:51:08 CompleteJob: успешно добавлен вариант optimized +worker-1 | 2026/11/13 18:51:08 worker: успешно завершено f328767c-3436-4016-bdcd-c5fea4ecb85c +worker-1 | 2026/11/13 18:51:09 worker: получен сигнал завершения, останавливаемся... +worker-1 | 2026/11/13 18:51:13 БД: ping БД: failed to connect to `user=messenger database=messenger`: hostname resolving error: lookup postgres on 127.0.0.11:53: no such host +worker-1 | 2026/11/13 18:51:13 worker: ошибка соединения, перезапуск через 5 секунд: dial tcp 172.20.0.3:5672: connect: connection refused +worker-1 | 2026/11/13 18:51:18 worker: ошибка соединения, перезапуск через 5 секунд: dial tcp 172.20.0.3:5672: connect: connection refused +worker-1 | 2026/11/13 18:51:23 worker: запущен и слушает очередь +worker-1 | 2026/11/13 18:54:00 worker: начало обработки 3e8c4363-1eb9-46d1-9b44-613f5f946446 (версия пайплайна 1) +worker-1 | 2026/11/13 18:54:00 worker: генерация вариантов для 3e8c4363-1eb9-46d1-9b44-613f5f946446 +worker-1 | 2026/11/13 18:54:00 CompleteJob: успешно добавлен вариант thumbnail +worker-1 | 2026/11/13 18:54:00 CompleteJob: успешно добавлен вариант crop +worker-1 | 2026/11/13 18:54:00 CompleteJob: успешно добавлен вариант optimized +worker-1 | 2026/11/13 18:54:00 worker: успешно завершено 3e8c4363-1eb9-46d1-9b44-613f5f946446 +worker-1 | 2026/11/13 18:54:00 worker: получен сигнал завершения, останавливаемся... +worker-1 | 2026/11/13 18:54:04 БД: ping БД: failed to connect to `user=messenger database=messenger`: hostname resolving error: lookup postgres on 127.0.0.11:53: no such host +worker-1 | 2026/11/13 18:54:04 worker: ошибка соединения, перезапуск через 5 секунд: dial tcp 172.20.0.3:5672: connect: connection refused +worker-1 | 2026/11/13 18:54:09 worker: ошибка соединения, перезапуск через 5 секунд: dial tcp 172.20.0.3:5672: connect: connection refused +worker-1 | 2026/11/13 18:54:14 worker: запущен и слушает очередь +worker-1 | 2026/11/13 18:54:25 worker: начало обработки 5c05619e-bce6-4ccb-b180-83b80f90dee3 (версия пайплайна 1) +worker-1 | 2026/11/13 18:54:25 worker: генерация вариантов для 5c05619e-bce6-4ccb-b180-83b80f90dee3 +worker-1 | 2026/11/13 18:54:25 CompleteJob: успешно добавлен вариант thumbnail +worker-1 | 2026/11/13 18:54:25 CompleteJob: успешно добавлен вариант crop +worker-1 | 2026/11/13 18:54:25 CompleteJob: успешно добавлен вариант optimized +worker-1 | 2026/11/13 18:54:25 worker: успешно завершено 5c05619e-bce6-4ccb-b180-83b80f90dee3 +worker-1 | 2026/11/13 18:54:25 worker: получен сигнал завершения, останавливаемся... +worker-1 | 2026/11/13 18:54:29 БД: ping БД: failed to connect to `user=messenger database=messenger`: hostname resolving error: lookup postgres on 127.0.0.11:53: no such host +worker-1 | 2026/11/13 18:54:29 worker: ошибка соединения, перезапуск через 5 секунд: dial tcp 172.20.0.3:5672: connect: connection refused +worker-1 | 2026/11/13 18:54:34 worker: ошибка соединения, перезапуск через 5 секунд: dial tcp 172.20.0.3:5672: connect: connection refused +worker-1 | 2026/11/13 18:54:39 worker: запущен и слушает очередь +worker-1 | 2026/11/13 18:56:06 worker: начало обработки 1b706a21-dfa3-4f64-89b9-2815da9bbcb7 (версия пайплайна 1) +worker-1 | 2026/11/13 18:56:06 worker: генерация вариантов для 1b706a21-dfa3-4f64-89b9-2815da9bbcb7 +worker-1 | 2026/11/13 18:56:06 CompleteJob: успешно добавлен вариант thumbnail +worker-1 | 2026/11/13 18:56:06 CompleteJob: успешно добавлен вариант crop +worker-1 | 2026/11/13 18:56:06 CompleteJob: успешно добавлен вариант optimized +worker-1 | 2026/11/13 18:56:06 worker: успешно завершено 1b706a21-dfa3-4f64-89b9-2815da9bbcb7 +worker-1 | 2026/11/13 18:56:08 worker: начало обработки 1f9520fe-a094-4a7b-ae7c-700423ce068a (версия пайплайна 1) +worker-1 | 2026/11/13 18:56:08 worker: генерация вариантов для 1f9520fe-a094-4a7b-ae7c-700423ce068a +worker-1 | 2026/11/13 18:56:08 CompleteJob: успешно добавлен вариант thumbnail +worker-1 | 2026/11/13 18:56:08 CompleteJob: успешно добавлен вариант crop +worker-1 | 2026/11/13 18:56:08 CompleteJob: успешно добавлен вариант optimized +worker-1 | 2026/11/13 18:56:08 worker: успешно завершено 1f9520fe-a094-4a7b-ae7c-700423ce068a +worker-1 | 2026/11/13 18:56:08 worker: начало обработки 1f9520fe-a094-4a7b-ae7c-700423ce068a (версия пайплайна 1) +worker-1 | 2026/11/13 18:56:08 worker: задача 1f9520fe-a094-4a7b-ae7c-700423ce068a уже взята, выполнена или версия устарела +worker-1 | 2026/11/13 18:56:11 worker: начало обработки d3a3f248-7053-4368-a42a-63f2168288bb (версия пайплайна 1) +worker-1 | 2026/11/13 18:56:11 worker: задача d3a3f248-7053-4368-a42a-63f2168288bb уже взята, выполнена или версия устарела +worker-1 | 2026/11/13 18:56:11 worker: начало обработки d3a3f248-7053-4368-a42a-63f2168288bb (версия пайплайна 1) +worker-1 | 2026/11/13 18:56:11 worker: задача d3a3f248-7053-4368-a42a-63f2168288bb уже взята, выполнена или версия устарела diff --git a/make_failed_job.py b/make_failed_job.py new file mode 100644 index 00000000..1cf14f99 --- /dev/null +++ b/make_failed_job.py @@ -0,0 +1,42 @@ +import sys +import time +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parent / 'tests')) +from client import Client, arguments + +def main(): + args = arguments('Генерация упавшей задачи для failed_jobs') + c = Client(args.base_url, args.ca_file) + + print("Создаем тестовых пользователей и чат...") + alice, bob, eve, chat = c.bootstrap() + + print("Генерируем битый PNG (берем начало от настоящей картинки)...") + sample = Path("tests/fixtures/sample.png").read_bytes() + broken_png = sample[:100] + + print("Загружаем битый файл через API...") + # Добавили schema='Attachment', чтобы клиент распарсил JSON + r = c.call( + 'POST', + f"/api/v1/chats/{chat['id']}/attachments?filename=broken.png", + 202, + actor=alice, + raw=broken_png, + headers={'Content-Type': 'image/png'}, + schema='Attachment' + ) + + att_id = r['json']['id'] + print(f"Файл принят! ID: {att_id}, статус: {r['json']['status']}") + print("Ждем 5 секунд, пока Worker попытается его обработать и упадет...") + + time.sleep(5) + + # Проверяем статус + att = c.call('GET', f'/api/v1/attachments/{att_id}', actor=alice, schema='Attachment')['json'] + print(f"Итоговый статус: {att['status']}, ошибка: {att['error_code']}") + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/scripts/chaos/kill-worker.sh b/scripts/chaos/kill-worker.sh new file mode 100644 index 00000000..30e797a5 --- /dev/null +++ b/scripts/chaos/kill-worker.sh @@ -0,0 +1,5 @@ +#!/bin/sh +set -eu +echo "Убиваем воркер (SIGKILL) для симуляции падения..." +docker compose kill -s SIGKILL worker +echo "Воркер убит. Задача должна зависнуть в статусе 'processing', пока Reconciler не вернет её в очередь." \ No newline at end of file diff --git a/scripts/chaos/pause-s3.sh b/scripts/chaos/pause-s3.sh new file mode 100644 index 00000000..3fbf2e0a --- /dev/null +++ b/scripts/chaos/pause-s3.sh @@ -0,0 +1,7 @@ +#!/bin/sh +set -eu +echo "Ставим MinIO (S3) на паузу на 15 секунд для симуляции временной ошибки..." +docker compose pause minio +sleep 15 +echo "Возобновляем работу MinIO..." +docker compose unpause minio \ No newline at end of file diff --git a/scripts/restart.bat b/scripts/restart.bat new file mode 100644 index 00000000..9a8cfa70 --- /dev/null +++ b/scripts/restart.bat @@ -0,0 +1,3 @@ +@echo off +bash "%~dp0restart.sh" +exit /b %errorlevel% \ No newline at end of file diff --git a/scripts/restart.sh b/scripts/restart.sh index e3f366d9..ea42a179 100644 --- a/scripts/restart.sh +++ b/scripts/restart.sh @@ -1,14 +1,25 @@ #!/bin/sh set -eu - -# Перезапускает API и все хранилища состояния (PostgreSQL, Redis, MinIO), -# сохраняя тома и данные. Используется в `make test-persistence`. -# -# - `docker compose restart` останавливает и запускает существующие контейнеры; -# - именованные тома не затрагиваются (без `down`, без `-v`, без `rm`); -# - `migrate`/`mc-init` не перезапускаются: миграции уже применены, -# bucket уже создан, и то и другое сохраняется благодаря томам и хранилищам состояния. - cd "$(dirname "$0")/.." -docker compose restart postgres redis minio api \ No newline at end of file +# Перезапускаем state stores, API и Worker +docker compose restart postgres redis minio rabbitmq api worker + +i=0 +while [ "$i" -lt 90 ]; do + # Проверяем статус всех нужных сервисов + unhealthy=$(docker compose ps --format '{{.Service}} {{.Health}}' \ + | awk '$1 ~ /^(postgres|redis|minio|rabbitmq|api-[0-9]+)$/ && $2 != "healthy" {print $1}') + + if [ -z "$unhealthy" ]; then + # Даем базе и пулам соединений 5 секунд стабилизироваться + sleep 5 + exit 0 + fi + i=$((i+1)) + sleep 1 +done + +echo "основные сервисы не прошли проверку здоровья за 90 секунд: $unhealthy" >&2 +docker compose ps >&2 +exit 1 \ No newline at end of file diff --git a/scripts/stop-one.bat b/scripts/stop-one.bat new file mode 100644 index 00000000..19d25806 --- /dev/null +++ b/scripts/stop-one.bat @@ -0,0 +1,2 @@ +@echo off +bash "%~dp0stop-one.sh" \ No newline at end of file -- 2.54.0 From 873c67d0d214fe507d1bf66addbc3f0fa1c21ab8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Fri, 13 Nov 2026 19:55:32 +0300 Subject: [PATCH 45/46] feat(worker): lab5 add automatic reconnect loop for rabbitmq --- backend/cmd/worker/main.go | 42 ++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/backend/cmd/worker/main.go b/backend/cmd/worker/main.go index b4fc91cf..6990e8f1 100644 --- a/backend/cmd/worker/main.go +++ b/backend/cmd/worker/main.go @@ -46,30 +46,40 @@ func main() { pipeline := worker.NewPipeline(store.Attachments(), objectStorage, store.Variants(), store.FailedJobs(), store.Outbox()) + // Цикл переподключения к RabbitMQ + for { + err := runWorker(ctx, cfg, pipeline) + if err == nil { + break // Нормальное завершение по сигналу + } + log.Printf("worker: ошибка соединения, перезапуск через 5 секунд: %v", err) + time.Sleep(5 * time.Second) + } +} + +func runWorker(ctx context.Context, cfg *config.Config, pipeline *worker.Pipeline) error { conn, err := amqp.Dial(cfg.RabbitMQURL) if err != nil { - log.Fatalf("RabbitMQ: %v", err) + return err } defer conn.Close() ch, err := conn.Channel() if err != nil { - log.Fatalf("RabbitMQ channel: %v", err) + return err } defer ch.Close() - // Сколько сообщений worker берет разом (защита от перегрузки) ch.Qos(2, 0, false) q, err := ch.QueueDeclare("attachments.process", true, false, false, false, nil) if err != nil { - log.Fatalf("RabbitMQ queue: %v", err) + return err } - // autoAck = false (ручное подтверждение) msgs, err := ch.Consume(q.Name, "", false, false, false, false, nil) if err != nil { - log.Fatalf("RabbitMQ consume: %v", err) + return err } log.Println("worker: запущен и слушает очередь") @@ -77,15 +87,21 @@ func main() { quit := make(chan os.Signal, 1) signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) - go func() { - for d := range msgs { + for { + select { + case <-quit: + log.Println("worker: получен сигнал завершения, останавливаемся...") + return nil + case d, ok := <-msgs: + if !ok { + return context.Canceled // Канал закрыт (соединение разорвано) + } var msg domain.JobMessage if err := json.Unmarshal(d.Body, &msg); err != nil { log.Printf("worker: битое сообщение: %v", err) - _ = d.Nack(false, false) // в DLQ + _ = d.Nack(false, false) continue } - if err := pipeline.Process(ctx, msg.AttachmentID, msg.PipelineVersion); err != nil { log.Printf("worker: ошибка %s: %v", msg.AttachmentID, err) _ = d.Nack(false, true) @@ -93,9 +109,5 @@ func main() { _ = d.Ack(false) } } - }() - - <-quit - log.Println("worker: получен сигнал завершения, останавливаемся...") - time.Sleep(1 * time.Second) + } } -- 2.54.0 From 5175ccb676130f0c7c402588dbf09a5a6353f227 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Fri, 13 Nov 2026 22:14:09 +0300 Subject: [PATCH 46/46] docs(lab5):final --- .github/pull_request_template.md | 68 ++++++++++++++++++----- REPORT.md | 92 ++++++++++++++++---------------- 2 files changed, 101 insertions(+), 59 deletions(-) diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 7d1ff4a4..b6f76833 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -1,18 +1,58 @@ ## Лабораторная 5 -Автор / группа: TODO -Целевая оценка (3/4/5): TODO -Трек 5, если заявлен: TODO +Автор / группа: Голиков А.П., БВТ2303 +Целевая оценка (3/4/5): 5 +Трек 5, если заявлен: Трек 2 (Доказанная идемпотентность pipeline) -Что реализовано и что перенесено: TODO -Команды запуска из чистого clone: TODO -Результаты проверок и ссылка на REPORT.md: TODO -Ограничения / известные ошибки: TODO -Источники и использование ИИ: TODO +**Что реализовано и что перенесено:** +* **Перенесено из Lab 4:** Базовая структура пользователей, чатов, сообщений, аутентификация (Bearer/Basic), идемпотентность сообщений, загрузка вложений (изменен HTTP-код на 202 Accepted). +* **Реализовано в Lab 5:** + * **Архитектура:** Выделен отдельный Worker, внедрен RabbitMQ. API больше не делает тяжелый ресайз, а только валидирует заголовок. + * **Transactional Outbox:** Запись в `attachments` и `outbox` происходит атомарно в одной транзакции. Publisher (горутина в API) надежно перекладывает задачи в RabbitMQ с Publisher Confirms. + * **Обработка:** Worker генерирует Thumbnail (256x160), Crop (128x128) и Optimized WebP (до 1280x1280, без EXIF) с лимитами по времени и памяти. + * **Отказоустойчивость (Level 4):** Экспоненциальный Retry с jitter для временных ошибок. DLQ (`failed_jobs`) для постоянных ошибок. Ручка `POST /replay` для восстановления. Reconciler чистит Staging и спасает "зависшие" задачи (Lease Recovery). + * **Идемпотентность (Level 5):** Защита от дублей на уровне БД (`ClaimForProcessing`) и защита от гонки версий пайплайна (`pipeline_version`). -- [ ] Весь обязательный минимум текущей и предыдущих работ сохранён с объявленными переходами. -- [ ] Контракт и публичные тесты не ослаблены. -- [ ] make check и make test выполнены; приложен реальный вывод. -- [ ] Сценарии отказов/безопасности своего уровня воспроизведены. -- [ ] Все секреты и приватные ключи находятся вне Git; отчёт не содержит tokens. -- [ ] README задания сохранён, REPORT.md содержит инструкцию именно моего решения. +**Команды запуска из чистого clone:** +```sh +cp .env.example .env +chmod +x setup_secrets.sh +./setup_secrets.sh +docker compose up -d --build +# Ожидаем healthcheck: docker compose ps +$env:PYTHONUTF8 = "1" # Для Windows +python -X utf8 tests/smoke.py +``` +Результаты проверок и ссылка на REPORT.md: + + Публичный смоук tests/smoke.py пройден (лог в evidence/smoke.log). + + Скрипт student-tests/fault_injection.py показывает 2 PASS (дубликаты и старая версия), доказывая Трек 2 (лог в evidence/fault_injection.log). + + Все логи и доказательства (включая восстановление Worker'а после рестарта RabbitMQ) собраны в папке evidence/. + + Подробное описание, схема и результаты экспериментов в REPORT.md. + +Ограничения / известные ошибки: + + На ОС Windows скрипт make test-persistence может падать на этапе проверки API из-за специфики перезапуска Docker Desktop сетей (API временно отдает 503). Логи восстановления Worker'а и API при этом подтверждают успешное переподключение. + + Файлы staging хранятся в том же S3-бакете, что и финальные файлы (разделение по префиксам). В production лучше использовать отдельный бакет с Lifecycle Policy. + + Трассировка OpenTelemetry не реализована (хотя ID для этого в сообщениях передаются). + +Источники и использование ИИ: + + Документация RabbitMQ (Publisher Confirms, DLX), Go (pgx, Gin). + + Библиотеки: disintegration/imaging, gen2brain/webp. + + ИИ использовался для генерации boilerplate-кода, написания bash-скриптов для Chaos Engineering и структурирования отчета. + +Чек-лист (проверено): +- [x] Весь обязательный минимум текущей и предыдущих работ сохранён с объявленными переходами. +- [x] Контракт и публичные тесты не ослаблены. +- [x] make check и make test выполнены; приложен реальный вывод. +- [x] Сценарии отказов/безопасности своего уровня воспроизведены. +- [x] Все секреты и приватные ключи находятся вне Git; отчёт не содержит tokens. +- [x] README задания сохранён, REPORT.md содержит инструкцию именно моего решения. \ No newline at end of file diff --git a/REPORT.md b/REPORT.md index 34981256..0adb6c48 100644 --- a/REPORT.md +++ b/REPORT.md @@ -2,7 +2,7 @@ - Автор, группа: Голиков А.П., БВТ2303 - Целевая оценка; выбранный трек 5, если нужен: Оценка 5, Трек 2 -- Commit/PR: будет добавлено после публикации. +- Commit/PR: ... - Использованные источники, библиотеки и помощь ИИ: Документация RabbitMQ (Publisher Confirms, DLX), документация Go (pgx, Gin), библиотека `disintegration/imaging` для ресайза, `gen2brain/webp` для WebP. При написании кода использовалась помощь ИИ для генерации boilerplate-кода и архитектурных консультаций. ## Запуск из чистого clone @@ -33,7 +33,6 @@ docker compose up -d --build Запуск тестов (Windows / PowerShell): Для корректной работы Python на Windows с кириллицей в JSON-контрактах необходимо указать кодировку UTF-8: ```powershell - $env:PYTHONUTF8 = "1" make test # или @@ -41,7 +40,6 @@ python -X utf8 tests/smoke.py ``` Безопасная остановка и удаление данных: ```sh - # Остановить контейнеры docker compose stop @@ -85,65 +83,69 @@ docker compose down -v │ COMMIT TX └──(12) ACK сообщения в RabbitMQ ``` -Источник истины и транзакционные границы - Источник истины: PostgreSQL. Только база данных является авторитетным хранилищем состояний (queued, processing, ready, failed) и метаданных. +**Источник истины и транзакционные границы** +* **Источник истины:** PostgreSQL. Только база данных является авторитетным хранилищем состояний (`queued`, `processing`, `ready`, `failed`) и метаданных. +* **Транзакционные границы:** Для преодоления проблемы «двойной записи» (Dual-Write) между базой данных и брокером сообщений используется паттерн **Transactional Outbox**. Запись `INSERT attachments` и `INSERT outbox` происходит в рамках одной ACID-транзакции PostgreSQL. Это гарантирует, что если клиент получил `202 Accepted`, задача сохранена на диске и не будет потеряна, даже если RabbitMQ временно недоступен (Publisher дослать её при восстановлении). +* **Идемпотентность:** Брокер гарантирует доставку *at-least-once* (минимум один раз). Защита от дублей реализована на уровне метода `ClaimForProcessing` (атомарный `UPDATE ... WHERE status='queued'`), а также через защиту `pipeline_version` в методе `CompleteJob`. - Транзакционные границы: Для преодоления проблемы «двойной записи» (Dual-Write) между базой данных и брокером сообщений используется паттерн Transactional Outbox. Запись INSERT attachments и INSERT outbox происходит в рамках одной ACID-транзакции PostgreSQL. Это гарантирует, что если клиент получил 202 Accepted, задача сохранена на диске и не будет потеряна, даже если RabbitMQ временно недоступен (Publisher дослать её при восстановлении). +**Логика обработки ошибок и Retry (Level 4)** +В Worker реализована классификация ошибок. Постоянные ошибки (например, `decode_failed` при битом файле) сразу переводят задачу в `failed`. Временные ошибки (например, `storage_error` при недоступности S3) ретраятся. Используется экспоненциальный backoff: задержка рассчитывается как `2^attempts` секунд + случайный jitter (до 1 сек). Для отложенных ретраев используется Outbox. После исчерпания лимита попыток (`JOB_MAX_ATTEMPTS=5`) задача попадает в реестр `failed_jobs` (DLQ). Для восстановления предусмотрена ручка `POST /attachments/{id}/replay`. - Идемпотентность: Брокер гарантирует доставку at-least-once (минимум один раз). Защита от дублей реализована на уровне метода ClaimForProcessing (атомарный UPDATE ... WHERE status='queued'), а также через защиту pipeline_version в методе CompleteJob. +**Лимиты и защита** +Worker ограничен по памяти (лимит Docker 256 МБ), а обработка каждого изображения ограничена по времени (30 секунд через `context.WithTimeout`). Лимиты по размеру (10 МБ) и пикселям (40 млн) проверяются на уровне API. WebP генерируется с качеством 80 и удалением EXIF, JPEG (thumbnail и crop) — с качеством 85. -Что перенесено из lab4 и что изменено - - Сохранено: Базовая структура БД (пользователи, чаты, сообщения), аутентификация (Bearer/Basic), идемпотентность создания сообщений, интерфейсы репозиториев. - - Изменено: - - Отказ от синхронного ресайза: Метод Upload больше не делает полный image.Decode (который потреблял до 160 МБ RAM), теперь выполняется только быстрый image.DecodeConfig по заголовку. - - Код ответа: 201 Created заменен на 202 Accepted, так как обработка стала асинхронной. - - Staging: Оригинал при загрузке теперь сохраняется во временный префикс S3 (staging/), а не сразу в конечный (chats/). - - Состояния: Удален статус pending, добавлены queued и processing. - - Инфраструктура: Добавлены сервисы RabbitMQ и Worker. Появился фоновый процесс Outbox Publisher (внутри API) и Reconciler (сборка мусора и спасение зависших задач). - - Обработка: Добавлена генерация crop (128x128) и optimized (WebP до 1280x1280) с удалением метаданных. - -Конфигурация и переменные (.env.example) - -Переменные окружения разделены на логические блоки и передаются в контейнеры через compose.yaml: - - API: APP_PORT, SESSION_TTL_SECONDS, API_REPLICAS (масштабирование). - - Лимиты файлов: MAX_UPLOAD_BYTES (10 МБ), MAX_IMAGE_PIXELS (40 млн). - - Outbox & Worker: OUTBOX_INTERVAL_MS (частота опроса таблицы outbox), WORKER_CONCURRENCY (количество параллельных обработчиков), JOB_MAX_ATTEMPTS (лимит попыток), JOB_TIMEOUT_SECONDS (лимит времени на одну картинку). - - Инфраструктура: Строки подключения (DATABASE_URL, CACHE_URL, RABBITMQ_URL) и настройки S3/MinIO. Все пароли вынесены в Docker Secrets (генерация через setup_secrets.sh). +**Что перенесено из lab4 и что изменено** +* **Сохранено:** Базовая структура БД (пользователи, чаты, сообщения), аутентификация (Bearer/Basic), идемпотентность создания сообщений, интерфейсы репозиториев. +* **Изменено:** + * *Отказ от синхронного ресайза:* Метод `Upload` больше не делает полный `image.Decode` (который потреблял до 160 МБ RAM), теперь выполняется только быстрый `image.DecodeConfig` по заголовку. + * *Код ответа:* `201 Created` заменен на `202 Accepted`, так как обработка стала асинхронной. + * *Staging:* Оригинал при загрузке теперь сохраняется во временный префикс S3 (`staging/`), а не сразу в конечный (`chats/`). + * *Состояния:* Удален статус `pending`, добавлены `queued` и `processing`. + * *Инфраструктура:* Добавлены сервисы RabbitMQ и Worker. Появился фоновый процесс Outbox Publisher (внутри API) и Reconciler (сборка мусора и спасение зависших задач). + * *Обработка:* Добавлена генерация `crop` (128x128) и `optimized` (WebP до 1280x1280) с удалением метаданных. +**Конфигурация и переменные (.env.example)** +Переменные окружения разделены на логические блоки и передаются в контейнеры через `compose.yaml`: +* **API:** `APP_PORT`, `SESSION_TTL_SECONDS`, `API_REPLICAS` (масштабирование). +* **Лимиты файлов:** `MAX_UPLOAD_BYTES` (10 МБ), `MAX_IMAGE_PIXELS` (40 млн). +* **Outbox & Worker:** `OUTBOX_INTERVAL_MS` (частота опроса таблицы outbox), `WORKER_CONCURRENCY` (количество параллельных обработчиков), `JOB_MAX_ATTEMPTS` (лимит попыток), `JOB_TIMEOUT_SECONDS` (лимит времени на одну картинку). +* **Инфраструктура:** Строки подключения (`DATABASE_URL`, `CACHE_URL`, `RABBITMQ_URL`) и настройки S3/MinIO. Все пароли вынесены в Docker Secrets (генерация через `setup_secrets.sh`). ## Доказательства критериев | Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | | --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | - -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +| Публичный smoke (Level 3) | `python -X utf8 tests/smoke.py` | `PASS: lab5 public API scenario` | `evidence/smoke.log` | +| Outbox + Async Worker (Level 3) | Загрузка файла через API | API отвечает 202, файл получает `ready` после Worker'а | `evidence/worker_logs.txt` | +| Переподключение Worker (Level 4) | `restart.sh` (рестарт БД/RabbitMQ) | Worker теряет связь, пишет ошибку, переподключается и продолжает работу | `evidence/worker_logs.txt` (строки 43-45) | +| Идемпотентность (Level 5) | `python -X utf8 student-tests/fault_injection.py` | `PASS` для дублей и устаревших версий. Лог воркера: "уже взята" | `evidence/fault_injection.log`, `evidence/worker_logs.txt` (строка 80) | +| Reconciler (Level 4) | Логи фонового процесса API | `reconciler: очистка завершена (удалено_staging=3...)` | `evidence/api_logs.txt` (строка 5) | +| DLQ / Failed jobs (Level 4) | Загрузка битого файла | Запись в `failed_jobs` создана | `evidence/failed_jobs.txt` | ## Эксперименты и отказы -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +**Track 2: Доказанная идемпотентность pipeline** + +- **Гипотеза:** Использование `pipeline_version` и транзакционного захвата задачи (`ClaimForProcessing`) гарантирует, что даже при at-least-once доставке сообщений от RabbitMQ, конечное состояние в БД будет корректным (effectively-once). +- **Методика:** Скрипт `fault_injection.py` отправляет дубликаты сообщений напрямую в RabbitMQ. Также скрипт меняет `pipeline_version` в БД и отправляет сообщение со старой версией. Дополнительно проверено восстановление после падения Worker'а (временное отключение БД/RabbitMQ через `restart.sh`) и обработка постоянных ошибок (битый файл). +- **Результаты:** + 1. При дублировании сообщений количество вариантов (thumbnail, crop, optimized) остается равным 3. В логах Worker'а видно, что дубль игнорируется (`задача уже взята...`). + 2. Задача со старой версией пайплайна игнорируется (статус остается `queued`). + 3. Worker успешно переподключается к брокеру и БД после рестарта, не теряя принятые задачи. +- **Ограничения:** Мы не достигаем exactly-once на уровне брокера (at-least-once — гарантия RabbitMQ), но достигаем exactly-once эффекта (идемпотентности) на уровне записи в БД. Это доказывает, что архитектура спроектирована правильно. | Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | | --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +| Дубль сообщения (RabbitMQ) | Игнорирование дубля, 3 варианта в БД | Игнорирование, 3 варианта | 0 сек / потерь нет | +| Устаревшая версия (pipeline) | Игнорирование сообщения, статус `queued` | Игнорирование, `queued` | 0 сек / потерь нет | +| Рестарт БД и RabbitMQ (`restart.sh`) | API и Worker переподключаются | API временно отдает 503, Worker перезапускает коннект и продолжает работу | ~5 сек / потерь нет | +| Неисправимый файл (битый PNG) | Задача падает в `failed`, запись в `failed_jobs` | Запись в `failed_jobs` создана | - / потерь нет | ## Ограничения и следующие шаги -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +- **Какие отказы выдерживаются:** Система выдерживает недоступность S3 (ретраи), недоступность RabbitMQ (задачи копятся в Outbox), падение Worker'а (аренда истекает, Reconciler спасает задачу). +- **Учебные упрощения:** Файлы staging хранятся в том же бакете MinIO, что и финальные (разделение по префиксам). Для production лучше использовать lifecycle policy. +- **Что не реализовано:** В полной мере не реализована распределенная трассировка (OpenTelemetry), однако в сообщениях есть все необходимые ID для нее. +- **Следующие шаги:** Добавление Grafana для мониторинга глубины очередей и health-метрик. \ No newline at end of file -- 2.54.0