forked from mtusi2026/mtusi2026lab5
feat(attachments): accept uploads through staging and transactional outbox
This commit is contained in:
+2
-11
@@ -99,16 +99,7 @@ func Run() error {
|
||||
backgroundWG.Go(func() {
|
||||
runIdempotencyCleanup(backgroundCtx, log, queries, cfg.Server.IdempotencyTTL, idempotencyCleanupInterval)
|
||||
})
|
||||
backgroundWG.Go(func() {
|
||||
runAttachmentCleanup(
|
||||
backgroundCtx,
|
||||
log,
|
||||
queries,
|
||||
s3Client,
|
||||
attachmentOrphanGracePeriod,
|
||||
attachmentCleanupInterval,
|
||||
)
|
||||
})
|
||||
|
||||
defer func() {
|
||||
stopBackground()
|
||||
backgroundWG.Wait()
|
||||
@@ -118,7 +109,7 @@ func Run() error {
|
||||
userService := service.NewUserService(queries)
|
||||
chatService := service.NewChatService(pool, queries)
|
||||
messageService := service.NewMessageService(pool, queries)
|
||||
attachmentService := service.NewAttachmentService(queries, s3Client, cfg.S3.MaxUploadBytes, cfg.S3.MaxImagePixels)
|
||||
attachmentService := service.NewAttachmentService(pool, queries, s3Client, cfg.S3.MaxUploadBytes, cfg.S3.MaxImagePixels)
|
||||
|
||||
router := NewRouter(
|
||||
log,
|
||||
|
||||
@@ -4,18 +4,11 @@ import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"mtusi2026-messenger/internal/db"
|
||||
)
|
||||
|
||||
const (
|
||||
sessionCleanupInterval = time.Hour
|
||||
idempotencyCleanupInterval = time.Hour
|
||||
|
||||
attachmentCleanupInterval = 10 * time.Minute
|
||||
attachmentOrphanGracePeriod = time.Hour
|
||||
)
|
||||
|
||||
type sessionCleaner interface {
|
||||
@@ -26,15 +19,6 @@ type idempotencyCleaner interface {
|
||||
DeleteExpiredIdempotencyKeys(ctx context.Context, cutoff time.Time) (int64, error)
|
||||
}
|
||||
|
||||
type staleUploadLister interface {
|
||||
ListStaleAttachmentUploads(ctx context.Context, cutoff time.Time) ([]db.ListStaleAttachmentUploadsRow, error)
|
||||
DeleteQueuedAttachment(ctx context.Context, id uuid.UUID) (int64, error)
|
||||
}
|
||||
|
||||
type objectDeleter interface {
|
||||
DeleteAdmin(ctx context.Context, key string) error
|
||||
}
|
||||
|
||||
func runSessionCleanup(ctx context.Context, log *slog.Logger, q sessionCleaner, interval time.Duration) {
|
||||
ticker := time.NewTicker(interval)
|
||||
defer ticker.Stop()
|
||||
@@ -86,64 +70,3 @@ func runIdempotencyCleanup(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func runAttachmentCleanup(
|
||||
ctx context.Context,
|
||||
log *slog.Logger,
|
||||
q staleUploadLister,
|
||||
store objectDeleter,
|
||||
grace time.Duration,
|
||||
interval time.Duration,
|
||||
) {
|
||||
ticker := time.NewTicker(interval)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
cleanupStaleUploads(ctx, log, q, store, grace)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func cleanupStaleUploads(
|
||||
ctx context.Context,
|
||||
log *slog.Logger,
|
||||
q staleUploadLister,
|
||||
store objectDeleter,
|
||||
grace time.Duration,
|
||||
) {
|
||||
cutoff := time.Now().Add(-grace)
|
||||
|
||||
rows, err := q.ListStaleAttachmentUploads(ctx, cutoff)
|
||||
if err != nil {
|
||||
if ctx.Err() == nil {
|
||||
log.Error("list stale attachment uploads", "error", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
deleted := 0
|
||||
|
||||
for _, row := range rows {
|
||||
removed, err := q.DeleteQueuedAttachment(ctx, row.ID)
|
||||
if err != nil {
|
||||
log.Error("delete stale upload row", "id", row.ID, "error", err)
|
||||
continue
|
||||
}
|
||||
if removed == 0 {
|
||||
continue
|
||||
}
|
||||
if err := store.DeleteAdmin(ctx, row.S3Key); err != nil {
|
||||
log.Error("delete stale upload object", "key", row.S3Key, "error", err)
|
||||
continue
|
||||
}
|
||||
deleted++
|
||||
}
|
||||
|
||||
if deleted > 0 {
|
||||
log.Info("stale attachment uploads deleted", "count", deleted)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user