lab5: асинхронная очередь, worker и идемпотентный pipeline
API принимает 202 после durable staging и TX intent (job+outbox); отдельные relay и worker публикуют/обрабатывают задания с publisher confirm, lease, retry/backoff, DLQ и replay. Добавлены версии pipeline, варианты thumbnail/crop/optimized, presign в verified staging, status-aware sweeper и admission control.
This commit is contained in:
@@ -71,6 +71,8 @@ func (f *fakeStore) Open(_ context.Context, key string) (io.ReadCloser, objectst
|
||||
return io.NopCloser(bytes.NewReader(object.data)), f.object(key, object), nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) Ping(context.Context) error { return nil }
|
||||
|
||||
func (f *fakeStore) Head(_ context.Context, key string) (objectstore.Object, error) {
|
||||
object, ok := f.objects[key]
|
||||
if !ok {
|
||||
@@ -144,9 +146,11 @@ func pngBytes(t *testing.T, width, height int) []byte {
|
||||
|
||||
func serviceConfig() attachment.Config {
|
||||
return attachment.Config{
|
||||
MaxUploadBytes: 1 << 20,
|
||||
MaxImagePixels: 1_000_000,
|
||||
PresignTTL: 5 * time.Minute,
|
||||
MaxUploadBytes: 1 << 20,
|
||||
MaxImagePixels: 1_000_000,
|
||||
PresignTTL: 5 * time.Minute,
|
||||
PipelineVersion: 1,
|
||||
MaxAttempts: 5,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -162,7 +166,7 @@ func TestValidateFilename(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestUploadStoresOriginal(t *testing.T) {
|
||||
func TestUploadStagesAndQueues(t *testing.T) {
|
||||
repo := mocks.NewMockRepository(t)
|
||||
store := newFakeStore()
|
||||
service := attachment.NewService(repo, store, serviceConfig(), slog.Default())
|
||||
@@ -171,16 +175,25 @@ func TestUploadStoresOriginal(t *testing.T) {
|
||||
ownerID := uuid.New()
|
||||
content := pngBytes(t, 16, 16)
|
||||
sum := sha256.Sum256(content)
|
||||
digest := hex.EncodeToString(sum[:])
|
||||
|
||||
repo.EXPECT().IsChatMember(mock.Anything, chatID, ownerID).Return(true, nil).Once()
|
||||
repo.EXPECT().Create(mock.Anything, mock.MatchedBy(func(draft domain.AttachmentDraft) bool {
|
||||
repo.EXPECT().Admission(mock.Anything, ownerID).Return(0, 0, int64(0), nil).Once()
|
||||
repo.EXPECT().CreateQueued(mock.Anything, mock.MatchedBy(func(draft domain.AttachmentDraft) bool {
|
||||
return draft.ChatID == chatID && draft.OwnerID == ownerID &&
|
||||
draft.ContentType == "image/png" && draft.SizeBytes == int64(len(content)) &&
|
||||
draft.SHA256 == hex.EncodeToString(sum[:]) && strings.HasPrefix(draft.ObjectKey, "attachments/")
|
||||
})).Return(domain.Attachment{Status: domain.AttachmentPending}, nil).Once()
|
||||
repo.EXPECT().MarkReady(mock.Anything, mock.Anything, mock.Anything).
|
||||
Return(domain.Attachment{Status: domain.AttachmentReady, ContentType: "image/png", SizeBytes: int64(len(content))}, nil).
|
||||
Once()
|
||||
draft.SHA256 == digest && draft.Status == domain.AttachmentQueued &&
|
||||
draft.PipelineVersion == 1 &&
|
||||
strings.HasPrefix(draft.ObjectKey, "attachments/") &&
|
||||
strings.HasPrefix(draft.StagingKey, "staging/uploads/")
|
||||
}), mock.MatchedBy(func(job domain.Job) bool {
|
||||
return job.PipelineVersion == 1 && job.Attempt == 1 &&
|
||||
job.MaxAttempts == 5 && job.SourceKey != ""
|
||||
})).Return(domain.Attachment{
|
||||
ID: uuid.New(), ChatID: chatID, OwnerID: ownerID,
|
||||
ContentType: "image/png", SizeBytes: int64(len(content)), SHA256: digest,
|
||||
Status: domain.AttachmentQueued, PipelineVersion: 1,
|
||||
}, nil).Once()
|
||||
|
||||
result, err := service.Upload(t.Context(), attachment.UploadInput{
|
||||
ChatID: chatID,
|
||||
@@ -190,28 +203,28 @@ func TestUploadStoresOriginal(t *testing.T) {
|
||||
Body: bytes.NewReader(content),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, domain.AttachmentReady, result.Status)
|
||||
require.Equal(t, domain.AttachmentQueued, result.Status)
|
||||
require.Len(t, store.objects, 1)
|
||||
for key := range store.objects {
|
||||
require.Contains(t, key, "staging/uploads/")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUploadRejectsTooLargeBody(t *testing.T) {
|
||||
repo := mocks.NewMockRepository(t)
|
||||
service := attachment.NewService(repo, newFakeStore(), attachment.Config{
|
||||
MaxUploadBytes: 8,
|
||||
MaxImagePixels: 1_000_000,
|
||||
PresignTTL: time.Minute,
|
||||
MaxUploadBytes: 8, MaxImagePixels: 1_000_000, PresignTTL: time.Minute,
|
||||
PipelineVersion: 1, MaxAttempts: 5,
|
||||
}, slog.Default())
|
||||
|
||||
repo.EXPECT().IsChatMember(mock.Anything, mock.Anything, mock.Anything).Return(true, nil).Once()
|
||||
repo.EXPECT().Admission(mock.Anything, mock.Anything).Return(0, 0, int64(0), nil).Once()
|
||||
|
||||
_, err := service.Upload(t.Context(), attachment.UploadInput{
|
||||
ChatID: uuid.New(),
|
||||
OwnerID: uuid.New(),
|
||||
Filename: "photo.png",
|
||||
ChatID: uuid.New(), OwnerID: uuid.New(), Filename: "photo.png",
|
||||
DeclaredContentType: "image/png",
|
||||
Body: bytes.NewReader(bytes.Repeat([]byte("x"), 9)),
|
||||
})
|
||||
|
||||
require.ErrorIs(t, err, domain.ErrTooLarge)
|
||||
}
|
||||
|
||||
@@ -220,19 +233,63 @@ func TestUploadRejectsMIMEMismatch(t *testing.T) {
|
||||
service := attachment.NewService(repo, newFakeStore(), serviceConfig(), slog.Default())
|
||||
|
||||
repo.EXPECT().IsChatMember(mock.Anything, mock.Anything, mock.Anything).Return(true, nil).Once()
|
||||
repo.EXPECT().Admission(mock.Anything, mock.Anything).Return(0, 0, int64(0), nil).Once()
|
||||
|
||||
_, err := service.Upload(t.Context(), attachment.UploadInput{
|
||||
ChatID: uuid.New(),
|
||||
OwnerID: uuid.New(),
|
||||
Filename: "photo.jpg",
|
||||
ChatID: uuid.New(), OwnerID: uuid.New(), Filename: "photo.jpg",
|
||||
DeclaredContentType: "image/jpeg",
|
||||
Body: bytes.NewReader(pngBytes(t, 8, 8)),
|
||||
})
|
||||
|
||||
require.ErrorIs(t, err, domain.ErrUnsupportedMedia)
|
||||
}
|
||||
|
||||
func TestGetRejectsNonReadyAttachment(t *testing.T) {
|
||||
func TestUploadAdmissionRejectsFlood(t *testing.T) {
|
||||
repo := mocks.NewMockRepository(t)
|
||||
service := attachment.NewService(repo, newFakeStore(), attachment.Config{
|
||||
MaxUploadBytes: 1 << 20, MaxImagePixels: 1_000_000, PresignTTL: time.Minute,
|
||||
PipelineVersion: 1, MaxAttempts: 5, MaxPendingPerUser: 1,
|
||||
}, slog.Default())
|
||||
|
||||
repo.EXPECT().IsChatMember(mock.Anything, mock.Anything, mock.Anything).Return(true, nil).Once()
|
||||
repo.EXPECT().Admission(mock.Anything, mock.Anything).Return(1, 1, int64(0), nil).Once()
|
||||
|
||||
_, err := service.Upload(t.Context(), attachment.UploadInput{
|
||||
ChatID: uuid.New(), OwnerID: uuid.New(), Filename: "photo.png",
|
||||
DeclaredContentType: "image/png", Body: bytes.NewReader(pngBytes(t, 8, 8)),
|
||||
})
|
||||
require.ErrorIs(t, err, domain.ErrRateLimited)
|
||||
}
|
||||
|
||||
func TestGetReturnsQueuedSnapshotWithoutVariants(t *testing.T) {
|
||||
repo := mocks.NewMockRepository(t)
|
||||
service := attachment.NewService(repo, newFakeStore(), serviceConfig(), slog.Default())
|
||||
|
||||
repo.EXPECT().GetForMember(mock.Anything, mock.Anything, mock.Anything).
|
||||
Return(domain.Attachment{Status: domain.AttachmentQueued, PipelineVersion: 1}, nil).Once()
|
||||
|
||||
got, err := service.Get(t.Context(), uuid.New(), uuid.New())
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, domain.AttachmentQueued, got.Status)
|
||||
require.Empty(t, got.Variants)
|
||||
}
|
||||
|
||||
func TestGetReadyLoadsCurrentVariants(t *testing.T) {
|
||||
repo := mocks.NewMockRepository(t)
|
||||
service := attachment.NewService(repo, newFakeStore(), serviceConfig(), slog.Default())
|
||||
|
||||
attachmentID := uuid.New()
|
||||
variant := domain.Variant{Name: domain.VariantThumbnail, ContentType: "image/jpeg", SizeBytes: 10, Width: 256, Height: 160, SHA256: strings.Repeat("a", 64)}
|
||||
|
||||
repo.EXPECT().GetForMember(mock.Anything, mock.Anything, attachmentID).
|
||||
Return(domain.Attachment{ID: attachmentID, Status: domain.AttachmentReady, PipelineVersion: 2}, nil).Once()
|
||||
repo.EXPECT().ListVariants(mock.Anything, attachmentID, 2).Return([]domain.Variant{variant}, nil).Once()
|
||||
|
||||
got, err := service.Get(t.Context(), uuid.New(), attachmentID)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, []domain.Variant{variant}, got.Variants)
|
||||
}
|
||||
|
||||
func TestGetRejectsInternalPending(t *testing.T) {
|
||||
repo := mocks.NewMockRepository(t)
|
||||
service := attachment.NewService(repo, newFakeStore(), serviceConfig(), slog.Default())
|
||||
|
||||
@@ -243,7 +300,18 @@ func TestGetRejectsNonReadyAttachment(t *testing.T) {
|
||||
require.ErrorIs(t, err, domain.ErrConflict)
|
||||
}
|
||||
|
||||
func TestCompletePublishesPresignedObject(t *testing.T) {
|
||||
func TestOpenVariantRejectsQueued(t *testing.T) {
|
||||
repo := mocks.NewMockRepository(t)
|
||||
service := attachment.NewService(repo, newFakeStore(), serviceConfig(), slog.Default())
|
||||
|
||||
repo.EXPECT().GetForMember(mock.Anything, mock.Anything, mock.Anything).
|
||||
Return(domain.Attachment{Status: domain.AttachmentQueued}, nil).Once()
|
||||
|
||||
_, _, _, err := service.OpenVariant(t.Context(), uuid.New(), uuid.New(), domain.VariantOriginal)
|
||||
require.ErrorIs(t, err, domain.ErrConflict)
|
||||
}
|
||||
|
||||
func TestCompleteQueuesPresignedObject(t *testing.T) {
|
||||
repo := mocks.NewMockRepository(t)
|
||||
store := newFakeStore()
|
||||
service := attachment.NewService(repo, store, serviceConfig(), slog.Default())
|
||||
@@ -257,29 +325,32 @@ func TestCompletePublishesPresignedObject(t *testing.T) {
|
||||
stagingKey := "staging/presign/" + chatID.String() + "/" + id.String()
|
||||
|
||||
store.objects[stagingKey] = storedObject{
|
||||
data: content,
|
||||
contentType: "image/png",
|
||||
metadata: map[string]string{"sha256": digest},
|
||||
lastModified: time.Now().UTC(),
|
||||
data: content, contentType: "image/png",
|
||||
metadata: map[string]string{"sha256": digest}, lastModified: time.Now().UTC(),
|
||||
}
|
||||
|
||||
pending := domain.Attachment{
|
||||
ID: id, ChatID: chatID, OwnerID: ownerID, ObjectKey: stagingKey,
|
||||
ContentType: "image/png", SizeBytes: int64(len(content)), SHA256: digest,
|
||||
Status: domain.AttachmentPending,
|
||||
StagingKey: stagingKey, ContentType: "image/png",
|
||||
SizeBytes: int64(len(content)), SHA256: digest,
|
||||
Status: domain.AttachmentPending, PipelineVersion: 1,
|
||||
}
|
||||
|
||||
repo.EXPECT().GetForOwner(mock.Anything, ownerID, id).Return(pending, nil).Once()
|
||||
repo.EXPECT().MarkReady(mock.Anything, id, mock.MatchedBy(func(key string) bool {
|
||||
return strings.HasPrefix(key, "attachments/")
|
||||
})).Return(domain.Attachment{
|
||||
repo.EXPECT().CompletePresign(
|
||||
mock.Anything, ownerID, id,
|
||||
mock.MatchedBy(func(key string) bool { return strings.HasPrefix(key, "staging/verified/") }),
|
||||
mock.MatchedBy(func(key string) bool { return strings.HasPrefix(key, "attachments/") }),
|
||||
1, 5, mock.Anything,
|
||||
).Return(domain.Attachment{
|
||||
ID: id, ChatID: chatID, OwnerID: ownerID, ContentType: "image/png",
|
||||
SizeBytes: int64(len(content)), SHA256: digest, Status: domain.AttachmentReady,
|
||||
SizeBytes: int64(len(content)), SHA256: digest,
|
||||
Status: domain.AttachmentQueued, PipelineVersion: 1,
|
||||
}, nil).Once()
|
||||
|
||||
completed, err := service.Complete(t.Context(), ownerID, id, digest)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, domain.AttachmentReady, completed.Status)
|
||||
require.Equal(t, domain.AttachmentQueued, completed.Status)
|
||||
require.NotContains(t, store.objects, stagingKey)
|
||||
require.Len(t, store.objects, 1)
|
||||
}
|
||||
@@ -297,16 +368,13 @@ func TestCompleteRejectsChecksumMismatch(t *testing.T) {
|
||||
digest := hex.EncodeToString(sum[:])
|
||||
stagingKey := "staging/presign/" + chatID.String() + "/" + id.String()
|
||||
|
||||
store.objects[stagingKey] = storedObject{
|
||||
data: content,
|
||||
contentType: "image/png",
|
||||
metadata: map[string]string{"sha256": digest},
|
||||
}
|
||||
store.objects[stagingKey] = storedObject{data: content, contentType: "image/png", metadata: map[string]string{"sha256": digest}}
|
||||
|
||||
pending := domain.Attachment{
|
||||
ID: id, ChatID: chatID, OwnerID: ownerID, ObjectKey: stagingKey,
|
||||
ContentType: "image/png", SizeBytes: int64(len(content)) + 1, SHA256: digest,
|
||||
Status: domain.AttachmentPending,
|
||||
StagingKey: stagingKey, ContentType: "image/png",
|
||||
SizeBytes: int64(len(content)) + 1, SHA256: digest,
|
||||
Status: domain.AttachmentPending, PipelineVersion: 1,
|
||||
}
|
||||
|
||||
repo.EXPECT().GetForOwner(mock.Anything, ownerID, id).Return(pending, nil).Once()
|
||||
@@ -330,15 +398,12 @@ func TestCompleteKeepsObjectOnWrongRequestChecksum(t *testing.T) {
|
||||
digest := hex.EncodeToString(sum[:])
|
||||
stagingKey := "staging/presign/" + chatID.String() + "/" + id.String()
|
||||
|
||||
store.objects[stagingKey] = storedObject{
|
||||
data: content,
|
||||
contentType: "image/png",
|
||||
metadata: map[string]string{"sha256": digest},
|
||||
}
|
||||
store.objects[stagingKey] = storedObject{data: content, contentType: "image/png", metadata: map[string]string{"sha256": digest}}
|
||||
pending := domain.Attachment{
|
||||
ID: id, ChatID: chatID, OwnerID: ownerID, ObjectKey: stagingKey,
|
||||
ContentType: "image/png", SizeBytes: int64(len(content)), SHA256: digest,
|
||||
Status: domain.AttachmentPending,
|
||||
StagingKey: stagingKey, ContentType: "image/png",
|
||||
SizeBytes: int64(len(content)), SHA256: digest,
|
||||
Status: domain.AttachmentPending, PipelineVersion: 1,
|
||||
}
|
||||
|
||||
repo.EXPECT().GetForOwner(mock.Anything, ownerID, id).Return(pending, nil).Once()
|
||||
|
||||
Reference in New Issue
Block a user