forked from mtusi2026/mtusi2026lab2
fix: session cache validation, statement_timeout, TTL minimum
This commit is contained in:
+1
-1
@@ -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"
|
||||
|
||||
+12
-12
@@ -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)."
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
|
||||
@@ -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:
|
||||
|
||||
+11
-2
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user