feat(service): lab5 switch upload to queued and write to transactional outbox
This commit is contained in:
@@ -89,6 +89,8 @@ func main() {
|
|||||||
store.Attachments(),
|
store.Attachments(),
|
||||||
store.Chats(),
|
store.Chats(),
|
||||||
objectStorage,
|
objectStorage,
|
||||||
|
store.Outbox(),
|
||||||
|
store,
|
||||||
cfg.MaxUploadBytes,
|
cfg.MaxUploadBytes,
|
||||||
cfg.MaxImagePixels,
|
cfg.MaxImagePixels,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -40,8 +40,6 @@ type Session struct {
|
|||||||
type AttachmentStatus string
|
type AttachmentStatus string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// Удалим в Этапе 2 вместе с переходом upload на 202/queued.
|
|
||||||
AttachmentStatusPending AttachmentStatus = "pending"
|
|
||||||
AttachmentStatusQueued AttachmentStatus = "queued"
|
AttachmentStatusQueued AttachmentStatus = "queued"
|
||||||
AttachmentStatusProcessing AttachmentStatus = "processing"
|
AttachmentStatusProcessing AttachmentStatus = "processing"
|
||||||
AttachmentStatusReady AttachmentStatus = "ready"
|
AttachmentStatusReady AttachmentStatus = "ready"
|
||||||
@@ -63,7 +61,7 @@ type Attachment struct {
|
|||||||
UpdatedAt time.Time
|
UpdatedAt time.Time
|
||||||
|
|
||||||
Attempts int // авторитетный счётчик попыток (в БД)
|
Attempts int // авторитетный счётчик попыток (в БД)
|
||||||
NextAttemptAt *time.Time // «не раньше»; nil = можно брать сразу
|
NextAttemptAt *time.Time // "не раньше"; nil = можно брать сразу
|
||||||
LockedUntil *time.Time // lease; nil = не арендовано
|
LockedUntil *time.Time // lease; nil = не арендовано
|
||||||
PipelineVersion int // guard в CompleteJob; в БД default 1
|
PipelineVersion int // guard в CompleteJob; в БД default 1
|
||||||
StagingKey string // S3-ключ оригинала до 202; "" в lab4-пути
|
StagingKey string // S3-ключ оригинала до 202; "" в lab4-пути
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
|
"encoding/json"
|
||||||
"image"
|
"image"
|
||||||
_ "image/jpeg"
|
_ "image/jpeg"
|
||||||
_ "image/png"
|
_ "image/png"
|
||||||
@@ -19,36 +20,42 @@ import (
|
|||||||
"high-loud-apps-labs/internal/domain"
|
"high-loud-apps-labs/internal/domain"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type txRunner interface {
|
||||||
|
WithTx(ctx context.Context, fn func(tx domain.Tx) error) error
|
||||||
|
}
|
||||||
|
|
||||||
type AttachmentService struct {
|
type AttachmentService struct {
|
||||||
attachments domain.AttachmentRepository
|
attachments domain.AttachmentRepository
|
||||||
chats domain.ChatRepository
|
chats domain.ChatRepository
|
||||||
storage domain.ObjectStorage
|
storage domain.ObjectStorage
|
||||||
|
outbox domain.OutboxRepository
|
||||||
|
tx txRunner
|
||||||
maxBytes int64
|
maxBytes int64
|
||||||
maxPixels int64
|
maxPixels int64
|
||||||
decodeSem chan struct{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAttachmentService(
|
func NewAttachmentService(
|
||||||
attachments domain.AttachmentRepository,
|
attachments domain.AttachmentRepository,
|
||||||
chats domain.ChatRepository,
|
chats domain.ChatRepository,
|
||||||
storage domain.ObjectStorage,
|
storage domain.ObjectStorage,
|
||||||
|
outbox domain.OutboxRepository,
|
||||||
|
tx txRunner,
|
||||||
maxBytes, maxPixels int64,
|
maxBytes, maxPixels int64,
|
||||||
) *AttachmentService {
|
) *AttachmentService {
|
||||||
return &AttachmentService{
|
return &AttachmentService{
|
||||||
attachments: attachments,
|
attachments: attachments,
|
||||||
chats: chats,
|
chats: chats,
|
||||||
storage: storage,
|
storage: storage,
|
||||||
|
outbox: outbox,
|
||||||
|
tx: tx,
|
||||||
maxBytes: maxBytes,
|
maxBytes: maxBytes,
|
||||||
maxPixels: maxPixels,
|
maxPixels: maxPixels,
|
||||||
// 40M pixels × 4 bytes (RGBA8) = 160 MB per full decode.
|
|
||||||
// API_MEMORY=256M → только один decode безопасен.
|
|
||||||
decodeSem: make(chan struct{}, 1),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Upload принимает тело, валидирует PNG/JPEG по содержимому,
|
// 1. Валидация, sniff Content-Type, проверка пикселей по заголовку.
|
||||||
// кладёт в pending, загружает в S3, переводит в ready.
|
// 2. Оригинал в S3 staging/{id}.
|
||||||
// При ошибке Put компенсирует MarkFailed.
|
// 3. Транзакция: INSERT attachments(status=queued) + INSERT outbox.
|
||||||
func (s *AttachmentService) Upload(
|
func (s *AttachmentService) Upload(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
ownerID, chatID, filename string,
|
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)
|
sha := sha256.Sum256(buf)
|
||||||
id := uuid.NewString()
|
id := uuid.NewString()
|
||||||
objectKey := "chats/" + chatID + "/" + id
|
stagingKey := "staging/" + id
|
||||||
now := time.Now().UTC()
|
now := time.Now().UTC()
|
||||||
|
|
||||||
a := &domain.Attachment{
|
a := &domain.Attachment{
|
||||||
ID: id,
|
ID: id,
|
||||||
ChatID: chatID,
|
ChatID: chatID,
|
||||||
OwnerID: ownerID,
|
OwnerID: ownerID,
|
||||||
Filename: filename,
|
Filename: filename,
|
||||||
ContentType: contentType,
|
ContentType: contentType,
|
||||||
SizeBytes: int64(len(buf)),
|
SizeBytes: int64(len(buf)),
|
||||||
SHA256: sha[:],
|
SHA256: sha[:],
|
||||||
ObjectKey: objectKey,
|
ObjectKey: "chats/" + chatID + "/" + id,
|
||||||
Status: domain.AttachmentStatusPending,
|
Status: domain.AttachmentStatusQueued,
|
||||||
CreatedAt: now,
|
StagingKey: stagingKey,
|
||||||
UpdatedAt: now,
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.storage.Put(ctx, objectKey, bytes.NewReader(buf), int64(len(buf)), contentType); err != nil {
|
payload, err := json.Marshal(domain.JobMessage{
|
||||||
// Компенсация: pending → failed. Reconciler не будет трогать failed.
|
AttachmentID: id,
|
||||||
_ = s.attachments.MarkFailed(ctx, a.ID, "upload_failed")
|
PipelineVersion: 1,
|
||||||
|
Attempt: 1,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.attachments.MarkReady(ctx, a.ID); err != nil {
|
err = s.tx.WithTx(ctx, func(tx domain.Tx) error {
|
||||||
// S3 has the object, DB still pending. Reconciler очистит по grace period.
|
if err := s.attachments.CreateInTx(ctx, tx, a); err != nil {
|
||||||
return nil, err
|
return 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 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) {
|
func (s *AttachmentService) Get(ctx context.Context, userID, attachmentID string) (*domain.Attachment, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user