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] 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 c0c74af..abadff1 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 4f93e87..400e02c 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 a9b1273..5a7ff69 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) {