ЛР3: балансировка HAProxy, 2 реплики API, идемпотентность, отказоустойчивость (на 4)
- X-Instance-Id в каждом ответе; обязательный Idempotency-Key, таблица idempotency_keys (миграция 0002, одна транзакция, конкурентные повторы через уникальный индекс) - graceful shutdown: SIGTERM -> ready=503 -> drain 3 с -> закрытие порта - compose: lb (HAProxy) + api x2 без портов + разовый migrate + postgres, лимиты CPU/RAM - хуки stop-one/restart/restore-replicas, scripts/experiments.py (failover, idempotency, rolling) - свои тесты: 44 (13 новых); evidence: smoke, pytest, persistence, failover, эксперименты
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import delete, func, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from models import AuthSession
|
||||
|
||||
|
||||
def create_session(db: Session, token_hash: bytes, user_id: uuid.UUID,
|
||||
created_at: datetime, expires_at: datetime) -> AuthSession:
|
||||
auth_session = AuthSession(token_hash=token_hash, user_id=user_id, created_at=created_at, expires_at=expires_at)
|
||||
db.add(auth_session)
|
||||
db.commit()
|
||||
return auth_session
|
||||
|
||||
|
||||
def get_active_session(db: Session, token_hash: bytes) -> AuthSession | None:
|
||||
query = select(AuthSession).where(
|
||||
AuthSession.token_hash == token_hash,
|
||||
AuthSession.expires_at > func.now(),
|
||||
)
|
||||
return db.scalar(query)
|
||||
|
||||
|
||||
def delete_session(db: Session, token_hash: bytes) -> bool:
|
||||
result = db.execute(delete(AuthSession).where(AuthSession.token_hash == token_hash))
|
||||
db.commit()
|
||||
return result.rowcount > 0
|
||||
Reference in New Issue
Block a user