Compensate failed uploads and sweep abandoned intents
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
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
70a0c6065e
commit
0e4eb1738c
@@ -8,12 +8,13 @@ from fastapi import FastAPI
|
||||
|
||||
from messenger.attachments.application.ports import ChatMembership
|
||||
from messenger.attachments.config import AttachmentsSettings
|
||||
from messenger.attachments.domain.events import AttachmentStored
|
||||
from messenger.attachments.domain.events import AttachmentDiscarded, AttachmentStored
|
||||
from messenger.attachments.entrypoints.errors import ERROR_CODES as ATTACHMENT_ERRORS
|
||||
from messenger.attachments.entrypoints.http import UPLOAD_PATH, attachments_router, uploads_router
|
||||
from messenger.attachments.entrypoints.ioc import AttachmentsProvider
|
||||
from messenger.attachments.entrypoints.journal import LOG_FIELDS as ATTACHMENT_FIELDS
|
||||
from messenger.attachments.entrypoints.journal import AttachmentsJournal
|
||||
from messenger.attachments.entrypoints.sweeper import UploadSweeper
|
||||
from messenger.identity.config import IdentitySettings
|
||||
from messenger.identity.domain.events import SessionClosed, SessionOpened, UserRegistered
|
||||
from messenger.identity.entrypoints.authentication import challenge_for, guard
|
||||
@@ -76,6 +77,7 @@ def wire_events(bus: EventBus, logger: Logger) -> None:
|
||||
|
||||
attachments = AttachmentsJournal(logger)
|
||||
bus.subscribe(AttachmentStored, attachments.on_attachment_stored)
|
||||
bus.subscribe(AttachmentDiscarded, attachments.on_attachment_discarded)
|
||||
|
||||
|
||||
def create_app(
|
||||
@@ -102,16 +104,19 @@ def create_app(
|
||||
FastapiProvider(),
|
||||
*overrides,
|
||||
)
|
||||
sweeper = UploadSweeper(container, attachments, logger)
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(_app: FastAPI) -> AsyncIterator[None]:
|
||||
bus = await container.get(EventBus)
|
||||
wire_events(bus, logger)
|
||||
bus.start()
|
||||
sweeper.start()
|
||||
logger.info("service.started")
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
await sweeper.stop()
|
||||
await bus.stop()
|
||||
await container.close()
|
||||
logger.info("service.stopped")
|
||||
@@ -142,4 +147,5 @@ def create_app(
|
||||
app.include_router(uploads_router)
|
||||
app.include_router(attachments_router)
|
||||
setup_dishka(container, app)
|
||||
app.state.sweeper = sweeper
|
||||
return app
|
||||
|
||||
Reference in New Issue
Block a user