Carry over the lab 5 solution

Bring the application, own tests, Dockerfile, toolchain, compose stack,
HAProxy, k6, RustFS and RabbitMQ configuration, migrations, hooks,
scenario scripts and the CI job from the lab 5 solution commit 64d5c67,
keep the lab 5 Makefile targets on top of the unchanged skeleton
targets, and append the lab 5 solution variables to the skeleton
.env.example. The package and the application follow the contract
version 1.6.0, and the image, the throwaway test containers and the
broker vhost description are tagged lab6 so they do not collide with a
lab 5 stand on the same host.

This is the starting point of the lab 6 cycle: lint, own tests and the
offline skeleton check pass before any lab 6 change.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Artyom
2026-10-29 03:51:58 +03:00
co-authored by Claude Opus 5.5
parent a0d3947710
commit 5ae3d77152
204 changed files with 18547 additions and 1 deletions
+112
View File
@@ -0,0 +1,112 @@
from collections.abc import AsyncIterator
from datetime import timedelta
from dishka import Provider, Scope, provide
from redis.asyncio import Redis
from redis.asyncio.retry import Retry
from redis.backoff import NoBackoff
from messenger.identity.application.bus import IdentityCommandBus
from messenger.identity.application.commands import CloseSession, OpenSession, RegisterUser
from messenger.identity.application.handlers import (
CloseSessionHandler,
OpenSessionHandler,
RegisterUserHandler,
)
from messenger.identity.application.ports import (
IdentityUnitOfWork,
PasswordHasher,
SessionDirectory,
UserDirectory,
)
from messenger.identity.application.queries import Authenticate, GetCurrentUser, VerifyCredentials
from messenger.identity.config import IdentitySettings
from messenger.identity.infrastructure.argon2 import Argon2PasswordHasher
from messenger.identity.infrastructure.postgres import (
PostgresIdentityUnitOfWork,
PostgresSessionDirectory,
PostgresUserDirectory,
)
from messenger.identity.infrastructure.valkey import CachedSessionDirectory
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 IdentityProvider(Provider):
def __init__(self, settings: IdentitySettings) -> None:
super().__init__()
self._settings = settings
@provide(scope=Scope.APP)
def settings(self) -> IdentitySettings:
return self._settings
@provide(scope=Scope.APP)
def hasher(self, settings: IdentitySettings) -> PasswordHasher:
return Argon2PasswordHasher(settings.password_hash_concurrency)
@provide(scope=Scope.APP)
def users(self, database: Database) -> UserDirectory:
return PostgresUserDirectory(database)
@provide(scope=Scope.APP)
async def sessions(
self, database: Database, settings: IdentitySettings, logger: Logger
) -> AsyncIterator[SessionDirectory]:
origin = PostgresSessionDirectory(database)
if settings.cache_url is None:
yield origin
return
client = Redis.from_url(
settings.cache_url.get_secret_value(),
socket_connect_timeout=settings.cache_timeout_seconds,
socket_timeout=settings.cache_timeout_seconds,
retry=Retry(NoBackoff(), 1),
)
try:
yield CachedSessionDirectory(
origin,
client,
logger,
limit=timedelta(seconds=settings.session_cache_ttl_seconds),
cooldown=settings.cache_cooldown_seconds,
)
finally:
await client.aclose()
@provide(scope=Scope.APP)
def verify(self, users: UserDirectory, hasher: PasswordHasher) -> VerifyCredentials:
return VerifyCredentials(users, hasher)
@provide(scope=Scope.APP)
def authenticate(self, sessions: SessionDirectory, clock: Clock) -> Authenticate:
return Authenticate(sessions, clock)
@provide(scope=Scope.REQUEST)
def unit_of_work(self, database: Database) -> IdentityUnitOfWork:
return PostgresIdentityUnitOfWork(database)
@provide(scope=Scope.REQUEST)
def current_user(self, users: UserDirectory) -> GetCurrentUser:
return GetCurrentUser(users)
@provide(scope=Scope.REQUEST)
def command_bus(
self,
uow: IdentityUnitOfWork,
publisher: EventPublisher,
clock: Clock,
ids: IdGenerator,
hasher: PasswordHasher,
sessions: SessionDirectory,
settings: IdentitySettings,
) -> IdentityCommandBus:
ttl = timedelta(seconds=settings.session_ttl_seconds)
handlers: dict[type[Command], CommandHandler] = {
RegisterUser: RegisterUserHandler(uow, clock, ids, hasher),
OpenSession: OpenSessionHandler(uow, clock, ttl),
CloseSession: CloseSessionHandler(uow, sessions),
}
return IdentityCommandBus(handlers, uow, publisher)