Files
mtusi2026lab3/src/messenger/messaging/entrypoints/ioc.py
T
ArtyomandClaude Opus 5.5 7bf28fc6ed Make message posts idempotent with Idempotency-Key
POST /api/v1/chats/{chat_id}/messages now requires the header, checked
at the HTTP boundary (1-128 of [A-Za-z0-9._:-], else 400). The handler
keeps the lab 2 order under the chat row lock: membership, then text,
then the receipt for (chat, sender, key) younger than
IDEMPOTENCY_TTL_SECONDS. A receipt with the same fingerprint replays
the stored message, a different one is 409 conflict; without a
receipt one statement inserts the message, the key row and the chat
mark, so both commit or neither does. Concurrent retries wait on the
same row lock and then read the committed receipt. The fingerprint is
SHA-256 of the canonical JSON of the parsed body, so layout does not
matter, and only 201 answers are ever stored.

A lock timeout now counts as a dependency failure and answers 503
instead of 500, so a retry with the same key is safe.

Own tests cover replay, a different layout of the same JSON, conflict,
missing and malformed keys, rejected attempts that leave the key free,
scope by user and chat, revalidation after logout and for outsiders,
expiry, and eight concurrent retries through two app instances
sharing one PostgreSQL. Every earlier message post sends a fresh key.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-28 11:36:15 +03:00

70 lines
2.7 KiB
Python

from datetime import timedelta
from dishka import Provider, Scope, provide
from messenger.messaging.application.bus import MessagingCommandBus
from messenger.messaging.application.commands import CreateChat, PostMessage
from messenger.messaging.application.handlers import CreateChatHandler, PostMessageHandler
from messenger.messaging.application.ports import ChatDirectory, CursorCodec, MessagingUnitOfWork
from messenger.messaging.application.queries import GetChat, ListChats, ReadHistory
from messenger.messaging.config import MessagingSettings
from messenger.messaging.infrastructure.cursor import SignedCursorCodec
from messenger.messaging.infrastructure.postgres import (
PostgresChatDirectory,
PostgresMessagingUnitOfWork,
)
from messenger.shared.application.commands import Command
from messenger.shared.application.messagebus import CommandHandler
from messenger.shared.application.ports import Clock, EventPublisher, IdGenerator
from messenger.shared.infrastructure.postgres import Database
class MessagingProvider(Provider):
def __init__(self, settings: MessagingSettings) -> None:
super().__init__()
self._settings = settings
@provide(scope=Scope.APP)
def settings(self) -> MessagingSettings:
return self._settings
@provide(scope=Scope.APP)
def directory(self, database: Database) -> ChatDirectory:
return PostgresChatDirectory(database)
@provide(scope=Scope.APP)
def cursors(self, settings: MessagingSettings) -> CursorCodec:
return SignedCursorCodec(settings.cursor_secret.get_secret_value().encode())
@provide(scope=Scope.REQUEST)
def unit_of_work(self, database: Database) -> MessagingUnitOfWork:
return PostgresMessagingUnitOfWork(database)
@provide(scope=Scope.REQUEST)
def chat(self, directory: ChatDirectory) -> GetChat:
return GetChat(directory)
@provide(scope=Scope.REQUEST)
def chat_list(self, directory: ChatDirectory, cursors: CursorCodec) -> ListChats:
return ListChats(directory, cursors)
@provide(scope=Scope.REQUEST)
def history(self, directory: ChatDirectory, cursors: CursorCodec) -> ReadHistory:
return ReadHistory(directory, cursors)
@provide(scope=Scope.REQUEST)
def command_bus(
self,
uow: MessagingUnitOfWork,
publisher: EventPublisher,
clock: Clock,
ids: IdGenerator,
settings: MessagingSettings,
) -> MessagingCommandBus:
ttl = timedelta(seconds=settings.idempotency_ttl_seconds)
handlers: dict[type[Command], CommandHandler] = {
CreateChat: CreateChatHandler(uow, clock, ids),
PostMessage: PostMessageHandler(uow, clock, ids, ttl),
}
return MessagingCommandBus(handlers, uow, publisher)