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:
@@ -56,11 +56,14 @@ func (h *Handler) DownloadAttachment(
|
||||
ctx context.Context,
|
||||
params oas.DownloadAttachmentParams,
|
||||
) (oas.DownloadAttachmentRes, error) {
|
||||
if variant, ok := params.Variant.Get(); ok && variant != oas.DownloadAttachmentVariantOriginal {
|
||||
return nil, fmt.Errorf("%w: variant %q is not available", domain.ErrNotFound, variant)
|
||||
variant := domain.VariantOriginal
|
||||
if requested, ok := params.Variant.Get(); ok {
|
||||
variant = domain.VariantName(requested)
|
||||
}
|
||||
|
||||
attachment, body, err := h.attachments.Open(ctx, caller(ctx), params.AttachmentID)
|
||||
attachment, metadata, body, err := h.attachments.OpenVariant(
|
||||
ctx, caller(ctx), params.AttachmentID, variant,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -73,7 +76,7 @@ func (h *Handler) DownloadAttachment(
|
||||
requestID := oas.NewOptString(requestIDFromContext(ctx))
|
||||
reader := closeOnEOF{ReadCloser: body}
|
||||
|
||||
switch attachment.ContentType {
|
||||
switch metadata.ContentType {
|
||||
case "image/png":
|
||||
return &oas.DownloadAttachmentOKImagePNGHeaders{
|
||||
XRequestID: requestID,
|
||||
@@ -84,6 +87,11 @@ func (h *Handler) DownloadAttachment(
|
||||
XRequestID: requestID,
|
||||
Response: oas.DownloadAttachmentOKImageJpeg{Data: reader},
|
||||
}, nil
|
||||
case "image/webp":
|
||||
return &oas.DownloadAttachmentOKImageWEBPHeaders{
|
||||
XRequestID: requestID,
|
||||
Response: oas.DownloadAttachmentOKImageWEBP{Data: reader},
|
||||
}, nil
|
||||
default:
|
||||
_ = body.Close()
|
||||
|
||||
@@ -110,11 +118,11 @@ func attachmentResponse(attachment domain.Attachment) oas.Attachment {
|
||||
Filename: attachment.Filename,
|
||||
SizeBytes: int(attachment.SizeBytes),
|
||||
SHA256: attachment.SHA256,
|
||||
Status: oas.AttachmentStatusReady,
|
||||
Variants: []oas.Variant{},
|
||||
Status: attachmentStatus(attachment.Status),
|
||||
ErrorCode: oas.NilString{Null: true},
|
||||
CreatedAt: attachment.CreatedAt,
|
||||
UpdatedAt: attachment.UpdatedAt,
|
||||
Variants: make([]oas.Variant, 0, len(attachment.Variants)),
|
||||
}
|
||||
|
||||
switch attachment.ContentType {
|
||||
@@ -130,9 +138,37 @@ func attachmentResponse(attachment domain.Attachment) oas.Attachment {
|
||||
response.ErrorCode = oas.NilString{Value: *attachment.ErrorCode}
|
||||
}
|
||||
|
||||
for _, variant := range attachment.Variants {
|
||||
response.Variants = append(response.Variants, oas.Variant{
|
||||
Name: oas.VariantName(variant.Name),
|
||||
ContentType: oas.VariantContentType(variant.ContentType),
|
||||
SizeBytes: int(variant.SizeBytes),
|
||||
Width: variant.Width,
|
||||
Height: variant.Height,
|
||||
SHA256: variant.SHA256,
|
||||
})
|
||||
}
|
||||
|
||||
return response
|
||||
}
|
||||
|
||||
func attachmentStatus(status domain.AttachmentStatus) oas.AttachmentStatus {
|
||||
switch status {
|
||||
case domain.AttachmentQueued:
|
||||
return oas.AttachmentStatusQueued
|
||||
case domain.AttachmentProcessing:
|
||||
return oas.AttachmentStatusProcessing
|
||||
case domain.AttachmentReady:
|
||||
return oas.AttachmentStatusReady
|
||||
case domain.AttachmentFailed:
|
||||
return oas.AttachmentStatusFailed
|
||||
default:
|
||||
// Pending is internal and never reaches the transport; map it to the
|
||||
// closest public state rather than inventing a new enum value.
|
||||
return oas.AttachmentStatusQueued
|
||||
}
|
||||
}
|
||||
|
||||
type closeOnEOF struct {
|
||||
io.ReadCloser
|
||||
}
|
||||
|
||||
@@ -60,6 +60,8 @@ func statusForDomainError(err error) int {
|
||||
errors.Is(err, domain.ErrUnknownMember),
|
||||
errors.Is(err, domain.ErrInvalidCursor):
|
||||
return http.StatusBadRequest
|
||||
case errors.Is(err, domain.ErrRateLimited):
|
||||
return http.StatusTooManyRequests
|
||||
case errors.Is(err, domain.ErrTooLarge):
|
||||
return http.StatusRequestEntityTooLarge
|
||||
case errors.Is(err, domain.ErrUnsupportedMedia):
|
||||
|
||||
@@ -176,7 +176,7 @@ func (e *Extensions) complete(w http.ResponseWriter, r *http.Request, userID uui
|
||||
return err
|
||||
}
|
||||
|
||||
return writeJSON(w, http.StatusOK, attachmentResponse(attachment))
|
||||
return writeJSON(w, http.StatusAccepted, attachmentResponse(attachment))
|
||||
}
|
||||
|
||||
func decodeJSON(r *http.Request, target any) error {
|
||||
|
||||
@@ -56,7 +56,11 @@ type SessionService interface {
|
||||
type AttachmentService interface {
|
||||
Upload(ctx context.Context, input attachmentService.UploadInput) (domain.Attachment, error)
|
||||
Get(ctx context.Context, callerID, id uuid.UUID) (domain.Attachment, error)
|
||||
Open(ctx context.Context, callerID, id uuid.UUID) (domain.Attachment, io.ReadCloser, error)
|
||||
OpenVariant(
|
||||
ctx context.Context,
|
||||
callerID, id uuid.UUID,
|
||||
name domain.VariantName,
|
||||
) (domain.Attachment, domain.Variant, io.ReadCloser, error)
|
||||
Presign(ctx context.Context, request domain.PresignRequest) (domain.PresignResult, error)
|
||||
Complete(ctx context.Context, ownerID, id uuid.UUID, sha256 string) (domain.Attachment, error)
|
||||
}
|
||||
|
||||
@@ -357,8 +357,14 @@ func (s *AttachmentStatus) Decode(d *jx.Decoder) error {
|
||||
}
|
||||
// Try to use constant string.
|
||||
switch AttachmentStatus(v) {
|
||||
case AttachmentStatusQueued:
|
||||
*s = AttachmentStatusQueued
|
||||
case AttachmentStatusProcessing:
|
||||
*s = AttachmentStatusProcessing
|
||||
case AttachmentStatusReady:
|
||||
*s = AttachmentStatusReady
|
||||
case AttachmentStatusFailed:
|
||||
*s = AttachmentStatusFailed
|
||||
default:
|
||||
*s = AttachmentStatus(v)
|
||||
}
|
||||
|
||||
@@ -4592,7 +4592,7 @@ func encodeUploadAttachmentResponse(response UploadAttachmentRes, w http.Respons
|
||||
}
|
||||
}
|
||||
}
|
||||
w.WriteHeader(201)
|
||||
w.WriteHeader(202)
|
||||
|
||||
e := new(jx.Encoder)
|
||||
response.Response.Encode(e)
|
||||
|
||||
@@ -230,21 +230,33 @@ func (*AttachmentHeaders) uploadAttachmentRes() {}
|
||||
type AttachmentStatus string
|
||||
|
||||
const (
|
||||
AttachmentStatusReady AttachmentStatus = "ready"
|
||||
AttachmentStatusQueued AttachmentStatus = "queued"
|
||||
AttachmentStatusProcessing AttachmentStatus = "processing"
|
||||
AttachmentStatusReady AttachmentStatus = "ready"
|
||||
AttachmentStatusFailed AttachmentStatus = "failed"
|
||||
)
|
||||
|
||||
// AllValues returns all AttachmentStatus values.
|
||||
func (AttachmentStatus) AllValues() []AttachmentStatus {
|
||||
return []AttachmentStatus{
|
||||
AttachmentStatusQueued,
|
||||
AttachmentStatusProcessing,
|
||||
AttachmentStatusReady,
|
||||
AttachmentStatusFailed,
|
||||
}
|
||||
}
|
||||
|
||||
// MarshalText implements encoding.TextMarshaler.
|
||||
func (s AttachmentStatus) MarshalText() ([]byte, error) {
|
||||
switch s {
|
||||
case AttachmentStatusQueued:
|
||||
return []byte(s), nil
|
||||
case AttachmentStatusProcessing:
|
||||
return []byte(s), nil
|
||||
case AttachmentStatusReady:
|
||||
return []byte(s), nil
|
||||
case AttachmentStatusFailed:
|
||||
return []byte(s), nil
|
||||
default:
|
||||
return nil, errors.Errorf("invalid value: %q", s)
|
||||
}
|
||||
@@ -253,9 +265,18 @@ func (s AttachmentStatus) MarshalText() ([]byte, error) {
|
||||
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||
func (s *AttachmentStatus) UnmarshalText(data []byte) error {
|
||||
switch AttachmentStatus(data) {
|
||||
case AttachmentStatusQueued:
|
||||
*s = AttachmentStatusQueued
|
||||
return nil
|
||||
case AttachmentStatusProcessing:
|
||||
*s = AttachmentStatusProcessing
|
||||
return nil
|
||||
case AttachmentStatusReady:
|
||||
*s = AttachmentStatusReady
|
||||
return nil
|
||||
case AttachmentStatusFailed:
|
||||
*s = AttachmentStatusFailed
|
||||
return nil
|
||||
default:
|
||||
return errors.Errorf("invalid value: %q", data)
|
||||
}
|
||||
@@ -818,13 +839,19 @@ func (*DownloadAttachmentServiceUnavailable) downloadAttachmentRes() {}
|
||||
type DownloadAttachmentVariant string
|
||||
|
||||
const (
|
||||
DownloadAttachmentVariantOriginal DownloadAttachmentVariant = "original"
|
||||
DownloadAttachmentVariantOriginal DownloadAttachmentVariant = "original"
|
||||
DownloadAttachmentVariantThumbnail DownloadAttachmentVariant = "thumbnail"
|
||||
DownloadAttachmentVariantCrop DownloadAttachmentVariant = "crop"
|
||||
DownloadAttachmentVariantOptimized DownloadAttachmentVariant = "optimized"
|
||||
)
|
||||
|
||||
// AllValues returns all DownloadAttachmentVariant values.
|
||||
func (DownloadAttachmentVariant) AllValues() []DownloadAttachmentVariant {
|
||||
return []DownloadAttachmentVariant{
|
||||
DownloadAttachmentVariantOriginal,
|
||||
DownloadAttachmentVariantThumbnail,
|
||||
DownloadAttachmentVariantCrop,
|
||||
DownloadAttachmentVariantOptimized,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -833,6 +860,12 @@ func (s DownloadAttachmentVariant) MarshalText() ([]byte, error) {
|
||||
switch s {
|
||||
case DownloadAttachmentVariantOriginal:
|
||||
return []byte(s), nil
|
||||
case DownloadAttachmentVariantThumbnail:
|
||||
return []byte(s), nil
|
||||
case DownloadAttachmentVariantCrop:
|
||||
return []byte(s), nil
|
||||
case DownloadAttachmentVariantOptimized:
|
||||
return []byte(s), nil
|
||||
default:
|
||||
return nil, errors.Errorf("invalid value: %q", s)
|
||||
}
|
||||
@@ -844,6 +877,15 @@ func (s *DownloadAttachmentVariant) UnmarshalText(data []byte) error {
|
||||
case DownloadAttachmentVariantOriginal:
|
||||
*s = DownloadAttachmentVariantOriginal
|
||||
return nil
|
||||
case DownloadAttachmentVariantThumbnail:
|
||||
*s = DownloadAttachmentVariantThumbnail
|
||||
return nil
|
||||
case DownloadAttachmentVariantCrop:
|
||||
*s = DownloadAttachmentVariantCrop
|
||||
return nil
|
||||
case DownloadAttachmentVariantOptimized:
|
||||
*s = DownloadAttachmentVariantOptimized
|
||||
return nil
|
||||
default:
|
||||
return errors.Errorf("invalid value: %q", data)
|
||||
}
|
||||
|
||||
@@ -234,8 +234,14 @@ func (s *AttachmentHeaders) Validate() error {
|
||||
|
||||
func (s AttachmentStatus) Validate() error {
|
||||
switch s {
|
||||
case "queued":
|
||||
return nil
|
||||
case "processing":
|
||||
return nil
|
||||
case "ready":
|
||||
return nil
|
||||
case "failed":
|
||||
return nil
|
||||
default:
|
||||
return errors.Errorf("invalid value: %v", s)
|
||||
}
|
||||
@@ -1103,6 +1109,12 @@ func (s DownloadAttachmentVariant) Validate() error {
|
||||
switch s {
|
||||
case "original":
|
||||
return nil
|
||||
case "thumbnail":
|
||||
return nil
|
||||
case "crop":
|
||||
return nil
|
||||
case "optimized":
|
||||
return nil
|
||||
default:
|
||||
return errors.Errorf("invalid value: %v", s)
|
||||
}
|
||||
|
||||
@@ -200,54 +200,61 @@ func (_c *MockAttachmentService_Get_Call) RunAndReturn(run func(ctx context.Cont
|
||||
return _c
|
||||
}
|
||||
|
||||
// Open provides a mock function for the type MockAttachmentService
|
||||
func (_mock *MockAttachmentService) Open(ctx context.Context, callerID uuid.UUID, id uuid.UUID) (domain.Attachment, io.ReadCloser, error) {
|
||||
ret := _mock.Called(ctx, callerID, id)
|
||||
// OpenVariant provides a mock function for the type MockAttachmentService
|
||||
func (_mock *MockAttachmentService) OpenVariant(ctx context.Context, callerID uuid.UUID, id uuid.UUID, name domain.VariantName) (domain.Attachment, domain.Variant, io.ReadCloser, error) {
|
||||
ret := _mock.Called(ctx, callerID, id, name)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for Open")
|
||||
panic("no return value specified for OpenVariant")
|
||||
}
|
||||
|
||||
var r0 domain.Attachment
|
||||
var r1 io.ReadCloser
|
||||
var r2 error
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, uuid.UUID, uuid.UUID) (domain.Attachment, io.ReadCloser, error)); ok {
|
||||
return returnFunc(ctx, callerID, id)
|
||||
var r1 domain.Variant
|
||||
var r2 io.ReadCloser
|
||||
var r3 error
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, uuid.UUID, uuid.UUID, domain.VariantName) (domain.Attachment, domain.Variant, io.ReadCloser, error)); ok {
|
||||
return returnFunc(ctx, callerID, id, name)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, uuid.UUID, uuid.UUID) domain.Attachment); ok {
|
||||
r0 = returnFunc(ctx, callerID, id)
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, uuid.UUID, uuid.UUID, domain.VariantName) domain.Attachment); ok {
|
||||
r0 = returnFunc(ctx, callerID, id, name)
|
||||
} else {
|
||||
r0 = ret.Get(0).(domain.Attachment)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(1).(func(context.Context, uuid.UUID, uuid.UUID) io.ReadCloser); ok {
|
||||
r1 = returnFunc(ctx, callerID, id)
|
||||
if returnFunc, ok := ret.Get(1).(func(context.Context, uuid.UUID, uuid.UUID, domain.VariantName) domain.Variant); ok {
|
||||
r1 = returnFunc(ctx, callerID, id, name)
|
||||
} else {
|
||||
if ret.Get(1) != nil {
|
||||
r1 = ret.Get(1).(io.ReadCloser)
|
||||
r1 = ret.Get(1).(domain.Variant)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(2).(func(context.Context, uuid.UUID, uuid.UUID, domain.VariantName) io.ReadCloser); ok {
|
||||
r2 = returnFunc(ctx, callerID, id, name)
|
||||
} else {
|
||||
if ret.Get(2) != nil {
|
||||
r2 = ret.Get(2).(io.ReadCloser)
|
||||
}
|
||||
}
|
||||
if returnFunc, ok := ret.Get(2).(func(context.Context, uuid.UUID, uuid.UUID) error); ok {
|
||||
r2 = returnFunc(ctx, callerID, id)
|
||||
if returnFunc, ok := ret.Get(3).(func(context.Context, uuid.UUID, uuid.UUID, domain.VariantName) error); ok {
|
||||
r3 = returnFunc(ctx, callerID, id, name)
|
||||
} else {
|
||||
r2 = ret.Error(2)
|
||||
r3 = ret.Error(3)
|
||||
}
|
||||
return r0, r1, r2
|
||||
return r0, r1, r2, r3
|
||||
}
|
||||
|
||||
// MockAttachmentService_Open_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Open'
|
||||
type MockAttachmentService_Open_Call struct {
|
||||
// MockAttachmentService_OpenVariant_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'OpenVariant'
|
||||
type MockAttachmentService_OpenVariant_Call struct {
|
||||
*mock.Call
|
||||
}
|
||||
|
||||
// Open is a helper method to define mock.On call
|
||||
// OpenVariant is a helper method to define mock.On call
|
||||
// - ctx context.Context
|
||||
// - callerID uuid.UUID
|
||||
// - id uuid.UUID
|
||||
func (_e *MockAttachmentService_Expecter) Open(ctx any, callerID any, id any) *MockAttachmentService_Open_Call {
|
||||
return &MockAttachmentService_Open_Call{Call: _e.mock.On("Open", ctx, callerID, id)}
|
||||
// - name domain.VariantName
|
||||
func (_e *MockAttachmentService_Expecter) OpenVariant(ctx any, callerID any, id any, name any) *MockAttachmentService_OpenVariant_Call {
|
||||
return &MockAttachmentService_OpenVariant_Call{Call: _e.mock.On("OpenVariant", ctx, callerID, id, name)}
|
||||
}
|
||||
|
||||
func (_c *MockAttachmentService_Open_Call) Run(run func(ctx context.Context, callerID uuid.UUID, id uuid.UUID)) *MockAttachmentService_Open_Call {
|
||||
func (_c *MockAttachmentService_OpenVariant_Call) Run(run func(ctx context.Context, callerID uuid.UUID, id uuid.UUID, name domain.VariantName)) *MockAttachmentService_OpenVariant_Call {
|
||||
_c.Call.Run(func(args mock.Arguments) {
|
||||
var arg0 context.Context
|
||||
if args[0] != nil {
|
||||
@@ -261,21 +268,26 @@ func (_c *MockAttachmentService_Open_Call) Run(run func(ctx context.Context, cal
|
||||
if args[2] != nil {
|
||||
arg2 = args[2].(uuid.UUID)
|
||||
}
|
||||
var arg3 domain.VariantName
|
||||
if args[3] != nil {
|
||||
arg3 = args[3].(domain.VariantName)
|
||||
}
|
||||
run(
|
||||
arg0,
|
||||
arg1,
|
||||
arg2,
|
||||
arg3,
|
||||
)
|
||||
})
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockAttachmentService_Open_Call) Return(attachment domain.Attachment, readCloser io.ReadCloser, err error) *MockAttachmentService_Open_Call {
|
||||
_c.Call.Return(attachment, readCloser, err)
|
||||
func (_c *MockAttachmentService_OpenVariant_Call) Return(attachment domain.Attachment, variant domain.Variant, readCloser io.ReadCloser, err error) *MockAttachmentService_OpenVariant_Call {
|
||||
_c.Call.Return(attachment, variant, readCloser, err)
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockAttachmentService_Open_Call) RunAndReturn(run func(ctx context.Context, callerID uuid.UUID, id uuid.UUID) (domain.Attachment, io.ReadCloser, error)) *MockAttachmentService_Open_Call {
|
||||
func (_c *MockAttachmentService_OpenVariant_Call) RunAndReturn(run func(ctx context.Context, callerID uuid.UUID, id uuid.UUID, name domain.VariantName) (domain.Attachment, domain.Variant, io.ReadCloser, error)) *MockAttachmentService_OpenVariant_Call {
|
||||
_c.Call.Return(run)
|
||||
return _c
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user