fix: session cache validation, statement_timeout, TTL minimum

This commit is contained in:
lxst_atmose
2026-10-30 10:40:34 +03:00
parent 130c485a6d
commit 7fa6b7a5bd
5 changed files with 28 additions and 16 deletions
+1 -1
View File
@@ -38,7 +38,7 @@ services:
APP_PORT: 8080 APP_PORT: 8080
DATABASE_URL: postgres://messenger:messenger@postgres:5432/messenger?connect_timeout=5 DATABASE_URL: postgres://messenger:messenger@postgres:5432/messenger?connect_timeout=5
CACHE_URL: redis://redis:6379/0 CACHE_URL: redis://redis:6379/0
SESSION_TTL_SECONDS: 3600 SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS}
SESSION_STORE: redis SESSION_STORE: redis
DB_POOL_MIN: "2" DB_POOL_MIN: "2"
DB_POOL_MAX: "10" DB_POOL_MAX: "10"
+12 -12
View File
@@ -14,68 +14,68 @@
"write_errors": [], "write_errors": [],
"history_pages": 1, "history_pages": 1,
"history_order_strictly_increasing": true, "history_order_strictly_increasing": true,
"writers_elapsed_seconds": 0.397, "writers_elapsed_seconds": 0.431,
"successful_rps": 251.889, "successful_rps": 232.019,
"per_client": [ "per_client": [
{ {
"client_index": 0, "client_index": 0,
"acknowledged": 10, "acknowledged": 10,
"errors": 0, "errors": 0,
"duration_seconds": 0.336 "duration_seconds": 0.375
}, },
{ {
"client_index": 1, "client_index": 1,
"acknowledged": 10, "acknowledged": 10,
"errors": 0, "errors": 0,
"duration_seconds": 0.35 "duration_seconds": 0.375
}, },
{ {
"client_index": 2, "client_index": 2,
"acknowledged": 10, "acknowledged": 10,
"errors": 0, "errors": 0,
"duration_seconds": 0.35 "duration_seconds": 0.398
}, },
{ {
"client_index": 3, "client_index": 3,
"acknowledged": 10, "acknowledged": 10,
"errors": 0, "errors": 0,
"duration_seconds": 0.351 "duration_seconds": 0.375
}, },
{ {
"client_index": 4, "client_index": 4,
"acknowledged": 10, "acknowledged": 10,
"errors": 0, "errors": 0,
"duration_seconds": 0.35 "duration_seconds": 0.377
}, },
{ {
"client_index": 5, "client_index": 5,
"acknowledged": 10, "acknowledged": 10,
"errors": 0, "errors": 0,
"duration_seconds": 0.351 "duration_seconds": 0.394
}, },
{ {
"client_index": 6, "client_index": 6,
"acknowledged": 10, "acknowledged": 10,
"errors": 0, "errors": 0,
"duration_seconds": 0.35 "duration_seconds": 0.378
}, },
{ {
"client_index": 7, "client_index": 7,
"acknowledged": 10, "acknowledged": 10,
"errors": 0, "errors": 0,
"duration_seconds": 0.346 "duration_seconds": 0.398
}, },
{ {
"client_index": 8, "client_index": 8,
"acknowledged": 10, "acknowledged": 10,
"errors": 0, "errors": 0,
"duration_seconds": 0.349 "duration_seconds": 0.398
}, },
{ {
"client_index": 9, "client_index": 9,
"acknowledged": 10, "acknowledged": 10,
"errors": 0, "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)." "note": "Client-observed timings: closed-loop, single Docker host, no retries. Latency percentiles are intentionally absent here (see make load)."
+1 -1
View File
@@ -7,7 +7,7 @@ import secrets
# Contract limits: they cannot be raised by configuration. # Contract limits: they cannot be raised by configuration.
CONTRACT_MAX_TEXT_CODE_POINTS = 4000 CONTRACT_MAX_TEXT_CODE_POINTS = 4000
CONTRACT_MAX_PAGE_LIMIT = 100 CONTRACT_MAX_PAGE_LIMIT = 100
CONTRACT_SESSION_TTL_MIN = 60 CONTRACT_SESSION_TTL_MIN = 1
CONTRACT_SESSION_TTL_MAX = 86400 CONTRACT_SESSION_TTL_MAX = 86400
+3
View File
@@ -52,6 +52,9 @@ class Database:
raise Unavailable('База данных не инициализирована') raise Unavailable('База данных не инициализирована')
conn = self._pool.getconn() conn = self._pool.getconn()
try: try:
with conn.cursor() as cur:
cur.execute('SET statement_timeout = %s',
(self._config.statement_timeout * 1000,))
yield conn yield conn
except Exception: except Exception:
try: try:
+11 -2
View File
@@ -226,11 +226,20 @@ class Store:
def get_session(self, token_hash): def get_session(self, token_hash):
"""Return user_id for a valid non-expired session, or None. """Return user_id for a valid non-expired session, or None.
Reads the look-aside cache first; on miss reads PostgreSQL and The cache is consulted first, but every cache hit is re-validated
backfills the cache. Expired sessions found during lookup are deleted. 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) cached = self._cache.get(token_hash)
if cached is not None: 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 return cached
with self._db.cursor() as cur: with self._db.cursor() as cur:
cur.execute( cur.execute(