update 1. GTP-6 Astra, оптимизация кода и фикс
This commit is contained in:
@@ -2,6 +2,7 @@ from __future__ import annotations
|
||||
|
||||
import os
|
||||
from contextlib import contextmanager
|
||||
from contextvars import ContextVar
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from threading import RLock
|
||||
@@ -49,6 +50,10 @@ from .models import (
|
||||
)
|
||||
|
||||
|
||||
# Scoped to a request/task and this database object, never shared across threads.
|
||||
_atomic_session: ContextVar[tuple[object, Session] | None] = ContextVar("hockey_atomic_session", default=None)
|
||||
|
||||
|
||||
class HockeyDatabase:
|
||||
"""Remote PostgreSQL database layer with additive schema repair.
|
||||
|
||||
@@ -375,6 +380,12 @@ class HockeyDatabase:
|
||||
|
||||
@contextmanager
|
||||
def session(self) -> Iterator[Session]:
|
||||
current = _atomic_session.get()
|
||||
if current is not None and current[0] is self:
|
||||
current[1].flush()
|
||||
yield current[1]
|
||||
current[1].flush()
|
||||
return
|
||||
session = self.SessionFactory()
|
||||
try:
|
||||
yield session
|
||||
@@ -384,3 +395,13 @@ class HockeyDatabase:
|
||||
raise
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
@contextmanager
|
||||
def atomic_session(self) -> Iterator[Session]:
|
||||
"""Keep the authority check, service writes and receipt in one transaction."""
|
||||
with self.session() as session:
|
||||
token = _atomic_session.set((self, session))
|
||||
try:
|
||||
yield session
|
||||
finally:
|
||||
_atomic_session.reset(token)
|
||||
|
||||
Reference in New Issue
Block a user