From d512f315fa3b2e66ccb45f3cea9c8ec09c010cd4 Mon Sep 17 00:00:00 2001 From: masha Date: Sun, 18 Oct 2026 01:14:08 +0300 Subject: [PATCH] 3 lab --- .env.example | 13 ------------- app/services.py | 8 ++++++-- compose.yaml | 1 + 3 files changed, 7 insertions(+), 15 deletions(-) delete mode 100644 .env.example diff --git a/.env.example b/.env.example deleted file mode 100644 index d32b9be0..00000000 --- a/.env.example +++ /dev/null @@ -1,13 +0,0 @@ -# Copy to .env for your LOCAL synthetic test environment; never commit .env. -# These are logical interface suggestions; map actual framework names in REPORT.md. -APP_HOST=0.0.0.0 -APP_PORT=8080 -APP_ENV=lab -SESSION_TTL_SECONDS=3600 -# Choose credentials locally. File paths contain no secret values. -DATABASE_URL_FILE=/run/secrets/database_url -# Optional on 3, required use on 4 in lab2; Redis OR Valkey: -SESSION_STORE=postgres -CACHE_URL_FILE=/run/secrets/cache_url -API_REPLICAS=2 -IDEMPOTENCY_TTL_SECONDS=86400 diff --git a/app/services.py b/app/services.py index fec902ac..5a8b7433 100644 --- a/app/services.py +++ b/app/services.py @@ -1,6 +1,7 @@ """Business rules for MTUSI Messenger Lab3.""" from __future__ import annotations +import asyncio import hashlib import json import re @@ -103,11 +104,13 @@ async def register_user( display_name: str, password: str, ) -> dict[str, Any]: + # Argon2 is CPU-bound (~0.5-1 s); off the event loop so /health/ready keeps answering. + password_hash = await asyncio.to_thread(hash_password, password) user = await repositories.create_user( db, username=username, display_name=display_name, - password_hash=hash_password(password), + password_hash=password_hash, ) if user is None: @@ -132,7 +135,8 @@ async def create_user_session( username, password = credentials user = await repositories.get_user_by_username(db, username) - if user is None or not verify_password( + if user is None or not await asyncio.to_thread( + verify_password, user["password_hash"], password, ): diff --git a/compose.yaml b/compose.yaml index 6c722210..98dd4a1b 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,3 +1,4 @@ +#file: noinspection ComposeUnknownValues # Lab3: client -> HAProxy -> API x N -> PostgreSQL (source of truth: data, # sessions, idempotency keys) + Valkey (shared session cache from lab2). # Start: see REPORT.md, "Запуск из чистого clone".