Files
mtusi2026lab4/app/cache.py
Ilya a97c35fc12 выполнение ЛР4 на 4
- S3: SeaweedFS 4.51 (MinIO-образы недоступны), приватный bucket,
  отдельные ограниченные ключи API, служебные порты закрыты;
  идемпотентный s3-init с проверкой приватности и прогревом записи
- вложения: потоковая загрузка PNG/JPEG, лимиты 10 MiB / 40 Мпикс,
  тип по содержимому, проверка filename, скачивание только участникам
- согласованность PG/S3: pending -> PUT -> ready, компенсация, sweeper
  с grace и dry-run; attachment_ids в сообщениях, FK по чату
- back-pressure на S3 PUT после найденного OOM SeaweedFS
- тесты student-tests, evidence, REPORT.md, DEMO.md
2026-10-28 18:06:07 +03:00

126 lines
4.5 KiB
Python

from __future__ import annotations
import asyncio
import json
import logging
import time
from typing import Dict, Optional, Set, Union
import redis.asyncio as redis
from redis.exceptions import RedisError
from app import config
logger = logging.getLogger("messenger.cache")
REVOKED = "revoked"
class SessionCache:
def __init__(self, url: Optional[str]):
self._client: Optional[redis.Redis] = None
self._down_until = 0.0
self._pending_revokes: Dict[bytes, float] = {}
self._retry_tasks: Set[asyncio.Task] = set()
if url:
pool = redis.BlockingConnectionPool.from_url(
url,
max_connections=config.CACHE_POOL_MAX,
timeout=config.CACHE_TIMEOUT,
socket_timeout=config.CACHE_TIMEOUT,
socket_connect_timeout=config.CACHE_TIMEOUT,
decode_responses=True,
)
self._client = redis.Redis(connection_pool=pool)
@property
def enabled(self) -> bool:
return self._client is not None
def _usable(self) -> bool:
return self._client is not None and time.monotonic() >= self._down_until
def _failed(self, op: str, exc: Exception) -> None:
self._down_until = time.monotonic() + config.CACHE_RETRY_AFTER
logger.warning("cache %s failed (%s); bypassing cache for %.0fs", op, type(exc).__name__, config.CACHE_RETRY_AFTER)
@staticmethod
def _key(token_hash: bytes) -> str:
return "session:" + token_hash.hex()
def _revoke_pending(self, token_hash: bytes) -> bool:
deadline = self._pending_revokes.get(token_hash)
if deadline is None:
return False
if time.monotonic() >= deadline:
self._pending_revokes.pop(token_hash, None)
return False
return True
async def get(self, token_hash: bytes) -> Union[dict, str, None]:
if not self._usable() or self._revoke_pending(token_hash):
return None
try:
raw = await self._client.get(self._key(token_hash))
except (RedisError, OSError) as exc:
self._failed("get", exc)
return None
if raw is None or raw == REVOKED:
return raw
try:
return json.loads(raw)
except ValueError:
return None
async def put(self, token_hash: bytes, entry: dict, ttl_seconds: int) -> None:
ttl = min(ttl_seconds, config.SESSION_CACHE_MAX_SECONDS)
if ttl <= 0 or not self._usable():
return
try:
await self._client.set(self._key(token_hash), json.dumps(entry), ex=ttl, nx=True)
except (RedisError, OSError) as exc:
self._failed("set", exc)
async def _write_tombstone(self, token_hash: bytes, ttl_seconds: int) -> bool:
try:
await self._client.set(self._key(token_hash), REVOKED, ex=max(1, min(ttl_seconds, config.SESSION_CACHE_MAX_SECONDS)))
return True
except (RedisError, OSError) as exc:
self._failed("revoke", exc)
return False
async def revoke(self, token_hash: bytes, ttl_seconds: int) -> bool:
if self._client is None:
return True
if await self._write_tombstone(token_hash, ttl_seconds):
return True
deadline = time.monotonic() + config.SESSION_CACHE_MAX_SECONDS + 1
self._pending_revokes[token_hash] = deadline
task = asyncio.create_task(self._retry_revoke(token_hash, ttl_seconds, deadline))
self._retry_tasks.add(task)
task.add_done_callback(self._retry_tasks.discard)
return False
async def _retry_revoke(self, token_hash: bytes, ttl_seconds: int, deadline: float) -> None:
while time.monotonic() < deadline:
await asyncio.sleep(1)
remaining = int(deadline - time.monotonic())
if remaining > 0 and await self._write_tombstone(token_hash, min(ttl_seconds, remaining)):
logger.info("revocation tombstone written after cache recovery")
break
self._pending_revokes.pop(token_hash, None)
async def ping(self) -> bool:
if self._client is None:
return False
try:
return bool(await self._client.ping())
except (RedisError, OSError):
return False
async def close(self) -> None:
for task in list(self._retry_tasks):
task.cancel()
if self._client is not None:
await self._client.aclose()