feat(service): lab5 switch upload to queued and write to transactional outbox

This commit is contained in:
Голиков Maximillian Антон
2026-11-07 09:54:11 +03:00
parent 0fddc697f1
commit 6eaf2a89b0
3 changed files with 53 additions and 51 deletions
+2
View File
@@ -89,6 +89,8 @@ func main() {
store.Attachments(),
store.Chats(),
objectStorage,
store.Outbox(),
store,
cfg.MaxUploadBytes,
cfg.MaxImagePixels,
)
+1 -3
View File
@@ -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-пути
+50 -48
View File
@@ -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) {