When the ready commit fails after the object was written, the upload handler now locks the row, keeps it if it turned out ready, and otherwise deletes the object first and the pending row second before answering 503. A ready update that touches no row means the intent was already swept, so it fails as unavailable and the object goes too; a 201 without a row cannot happen. Every worker runs an UploadSweeper that every 30 s removes pending intents older than UPLOAD_GRACE_SECONDS (900 s, at least 60 s) one row at a time under FOR UPDATE SKIP LOCKED over the partial pending index, object first, and logs an audit event for each discard. Bodies are buffered only inside one of UPLOAD_CONCURRENCY slots per worker and released before the ready commit, and a client that disconnects in the middle of a body becomes a 400 instead of an unhandled 500. The tests run against a throwaway PostgreSQL: a trigger rejects the ready commit and the compensation leaves neither row nor object, a row removed during the write gives 503, a failed write stays until the grace ends, fresh intents and an aged ready attachment in a message survive a sweep, two concurrent sweeps share five intents exactly, the background loop runs by itself, a second upload waits for the slot without reading its body, and aborted and stalled bodies leave nothing. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01V1d3RLHNBBMA6UqBLLJv27
113 lines
4.1 KiB
Python
113 lines
4.1 KiB
Python
from collections.abc import AsyncIterator
|
|
|
|
from dishka import Provider, Scope, provide
|
|
|
|
from messenger.attachments.application.bus import AttachmentsCommandBus
|
|
from messenger.attachments.application.commands import SweepAbandonedUploads, UploadAttachment
|
|
from messenger.attachments.application.handlers import (
|
|
SweepAbandonedUploadsHandler,
|
|
UploadAttachmentHandler,
|
|
UploadSlots,
|
|
)
|
|
from messenger.attachments.application.ports import (
|
|
AttachmentDirectory,
|
|
AttachmentsUnitOfWork,
|
|
ChatMembership,
|
|
ImageInspector,
|
|
ObjectStorage,
|
|
)
|
|
from messenger.attachments.application.queries import GetAttachment, OpenAttachment
|
|
from messenger.attachments.config import AttachmentsSettings
|
|
from messenger.attachments.infrastructure.images import PillowImageInspector
|
|
from messenger.attachments.infrastructure.postgres import (
|
|
PostgresAttachmentDirectory,
|
|
PostgresAttachmentsUnitOfWork,
|
|
)
|
|
from messenger.attachments.infrastructure.s3 import S3ObjectStorage, s3_client
|
|
from messenger.shared.application.commands import Command
|
|
from messenger.shared.application.messagebus import CommandHandler
|
|
from messenger.shared.application.ports import Clock, EventPublisher, IdGenerator, Logger
|
|
from messenger.shared.infrastructure.postgres import Database
|
|
|
|
|
|
class AttachmentsProvider(Provider):
|
|
def __init__(self, settings: AttachmentsSettings) -> None:
|
|
super().__init__()
|
|
self._settings = settings
|
|
|
|
@provide(scope=Scope.APP)
|
|
def settings(self) -> AttachmentsSettings:
|
|
return self._settings
|
|
|
|
@provide(scope=Scope.APP)
|
|
async def storage(self, settings: AttachmentsSettings) -> AsyncIterator[ObjectStorage]:
|
|
async with s3_client(
|
|
endpoint=settings.s3_endpoint,
|
|
region=settings.s3_region,
|
|
access_key_id=settings.s3_access_key_id.get_secret_value(),
|
|
secret_access_key=settings.s3_secret_access_key.get_secret_value(),
|
|
connect_timeout=settings.s3_connect_timeout_seconds,
|
|
read_timeout=settings.s3_read_timeout_seconds,
|
|
max_attempts=settings.s3_max_attempts,
|
|
) as client:
|
|
yield S3ObjectStorage(client, settings.s3_bucket)
|
|
|
|
@provide(scope=Scope.APP)
|
|
def images(self, settings: AttachmentsSettings) -> ImageInspector:
|
|
return PillowImageInspector(settings.max_image_pixels, settings.image_check_concurrency)
|
|
|
|
@provide(scope=Scope.APP)
|
|
def slots(self, settings: AttachmentsSettings) -> UploadSlots:
|
|
return UploadSlots(settings.upload_concurrency)
|
|
|
|
@provide(scope=Scope.APP)
|
|
def directory(self, database: Database) -> AttachmentDirectory:
|
|
return PostgresAttachmentDirectory(database)
|
|
|
|
@provide(scope=Scope.REQUEST)
|
|
def unit_of_work(self, database: Database) -> AttachmentsUnitOfWork:
|
|
return PostgresAttachmentsUnitOfWork(database)
|
|
|
|
@provide(scope=Scope.REQUEST)
|
|
def attachment(
|
|
self, directory: AttachmentDirectory, membership: ChatMembership
|
|
) -> GetAttachment:
|
|
return GetAttachment(directory, membership)
|
|
|
|
@provide(scope=Scope.REQUEST)
|
|
def content(
|
|
self, attachment: GetAttachment, storage: ObjectStorage, logger: Logger
|
|
) -> OpenAttachment:
|
|
return OpenAttachment(attachment, storage, logger)
|
|
|
|
@provide(scope=Scope.REQUEST)
|
|
def command_bus(
|
|
self,
|
|
uow: AttachmentsUnitOfWork,
|
|
membership: ChatMembership,
|
|
images: ImageInspector,
|
|
storage: ObjectStorage,
|
|
publisher: EventPublisher,
|
|
clock: Clock,
|
|
ids: IdGenerator,
|
|
settings: AttachmentsSettings,
|
|
slots: UploadSlots,
|
|
logger: Logger,
|
|
) -> AttachmentsCommandBus:
|
|
upload = UploadAttachmentHandler(
|
|
uow,
|
|
membership,
|
|
images,
|
|
storage,
|
|
clock=clock,
|
|
ids=ids,
|
|
limit=settings.max_upload_bytes,
|
|
slots=slots,
|
|
logger=logger,
|
|
)
|
|
handlers: dict[type[Command], CommandHandler] = {
|
|
UploadAttachment: upload,
|
|
SweepAbandonedUploads: SweepAbandonedUploadsHandler(uow, storage, clock),
|
|
}
|
|
return AttachmentsCommandBus(handlers, uow, publisher)
|