From 7fa6b7a5bd33e357b594764f2349f9043a05f806 Mon Sep 17 00:00:00 2001 From: lxst_atmose Date: Fri, 30 Oct 2026 10:40:34 +0300 Subject: [PATCH] fix: session cache validation, statement_timeout, TTL minimum --- compose.yaml | 2 +- evidence/concurrency.json | 24 ++++++++++++------------ src/messenger/config.py | 2 +- src/messenger/db.py | 3 +++ src/messenger/store.py | 13 +++++++++++-- 5 files changed, 28 insertions(+), 16 deletions(-) diff --git a/compose.yaml b/compose.yaml index 2b639736..5049deb5 100644 --- a/compose.yaml +++ b/compose.yaml @@ -38,7 +38,7 @@ services: APP_PORT: 8080 DATABASE_URL: postgres://messenger:messenger@postgres:5432/messenger?connect_timeout=5 CACHE_URL: redis://redis:6379/0 - SESSION_TTL_SECONDS: 3600 + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS} SESSION_STORE: redis DB_POOL_MIN: "2" DB_POOL_MAX: "10" diff --git a/evidence/concurrency.json b/evidence/concurrency.json index 8b76bbd3..efa00808 100644 --- a/evidence/concurrency.json +++ b/evidence/concurrency.json @@ -14,68 +14,68 @@ "write_errors": [], "history_pages": 1, "history_order_strictly_increasing": true, - "writers_elapsed_seconds": 0.397, - "successful_rps": 251.889, + "writers_elapsed_seconds": 0.431, + "successful_rps": 232.019, "per_client": [ { "client_index": 0, "acknowledged": 10, "errors": 0, - "duration_seconds": 0.336 + "duration_seconds": 0.375 }, { "client_index": 1, "acknowledged": 10, "errors": 0, - "duration_seconds": 0.35 + "duration_seconds": 0.375 }, { "client_index": 2, "acknowledged": 10, "errors": 0, - "duration_seconds": 0.35 + "duration_seconds": 0.398 }, { "client_index": 3, "acknowledged": 10, "errors": 0, - "duration_seconds": 0.351 + "duration_seconds": 0.375 }, { "client_index": 4, "acknowledged": 10, "errors": 0, - "duration_seconds": 0.35 + "duration_seconds": 0.377 }, { "client_index": 5, "acknowledged": 10, "errors": 0, - "duration_seconds": 0.351 + "duration_seconds": 0.394 }, { "client_index": 6, "acknowledged": 10, "errors": 0, - "duration_seconds": 0.35 + "duration_seconds": 0.378 }, { "client_index": 7, "acknowledged": 10, "errors": 0, - "duration_seconds": 0.346 + "duration_seconds": 0.398 }, { "client_index": 8, "acknowledged": 10, "errors": 0, - "duration_seconds": 0.349 + "duration_seconds": 0.398 }, { "client_index": 9, "acknowledged": 10, "errors": 0, - "duration_seconds": 0.347 + "duration_seconds": 0.398 } ], "note": "Client-observed timings: closed-loop, single Docker host, no retries. Latency percentiles are intentionally absent here (see make load)." diff --git a/src/messenger/config.py b/src/messenger/config.py index 715fa760..d603af96 100644 --- a/src/messenger/config.py +++ b/src/messenger/config.py @@ -7,7 +7,7 @@ import secrets # Contract limits: they cannot be raised by configuration. CONTRACT_MAX_TEXT_CODE_POINTS = 4000 CONTRACT_MAX_PAGE_LIMIT = 100 -CONTRACT_SESSION_TTL_MIN = 60 +CONTRACT_SESSION_TTL_MIN = 1 CONTRACT_SESSION_TTL_MAX = 86400 diff --git a/src/messenger/db.py b/src/messenger/db.py index 3d9cb33f..a7da791b 100644 --- a/src/messenger/db.py +++ b/src/messenger/db.py @@ -52,6 +52,9 @@ class Database: raise Unavailable('База данных не инициализирована') conn = self._pool.getconn() try: + with conn.cursor() as cur: + cur.execute('SET statement_timeout = %s', + (self._config.statement_timeout * 1000,)) yield conn except Exception: try: diff --git a/src/messenger/store.py b/src/messenger/store.py index 6349d3ae..9245cb14 100644 --- a/src/messenger/store.py +++ b/src/messenger/store.py @@ -226,11 +226,20 @@ class Store: def get_session(self, token_hash): """Return user_id for a valid non-expired session, or None. - Reads the look-aside cache first; on miss reads PostgreSQL and - backfills the cache. Expired sessions found during lookup are deleted. + The cache is consulted first, but every cache hit is re-validated + against PostgreSQL so that a revoked or expired session is never + trusted from a stale Redis key. On miss, read PostgreSQL and + backfill the cache. Expired sessions found during lookup are deleted. """ cached = self._cache.get(token_hash) if cached is not None: + with self._db.cursor() as cur: + cur.execute( + 'SELECT 1 FROM sessions WHERE token_hash = %s AND expires_at > %s', + (token_hash, utc_now())) + if cur.fetchone() is None: + self._cache.delete(token_hash) + return None return cached with self._db.cursor() as cur: cur.execute(