mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2026-11-01 01:24:45 +03:00
feat: update codebase to use httpx2 and enhance docstrings for clarity
This commit is contained in:
@@ -17,13 +17,17 @@ from app.schemas.chat import ChatMessage
|
||||
|
||||
@attrs.define(slots=True)
|
||||
class ChatSession:
|
||||
"""Conversation history associated with one websocket connection."""
|
||||
|
||||
id: UUID = attrs.field(factory=uuid4)
|
||||
messages: list[ChatMessage] = attrs.field(factory=list)
|
||||
|
||||
def add(self, message: ChatMessage) -> None:
|
||||
"""Append a message to this conversation."""
|
||||
self.messages.append(message)
|
||||
|
||||
def history(self) -> list[ChatMessage]:
|
||||
"""Return a copy of the conversation history."""
|
||||
return list(self.messages)
|
||||
|
||||
|
||||
@@ -35,23 +39,28 @@ class ChatSessionManager:
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
"""Create an empty, lock-protected session registry."""
|
||||
self._sessions: dict[UUID, ChatSession] = {}
|
||||
self._lock = asyncio.Lock()
|
||||
|
||||
async def create(self) -> ChatSession:
|
||||
"""Create and register a new conversation."""
|
||||
session = ChatSession()
|
||||
async with self._lock:
|
||||
self._sessions[session.id] = session
|
||||
return session
|
||||
|
||||
async def get(self, session_id: UUID) -> ChatSession | None:
|
||||
"""Return the matching session, if it is active."""
|
||||
async with self._lock:
|
||||
return self._sessions.get(session_id)
|
||||
|
||||
async def remove(self, session_id: UUID) -> None:
|
||||
"""Remove a session from the active registry."""
|
||||
async with self._lock:
|
||||
self._sessions.pop(session_id, None)
|
||||
|
||||
async def count(self) -> int:
|
||||
"""Return the number of active sessions."""
|
||||
async with self._lock:
|
||||
return len(self._sessions)
|
||||
|
||||
Reference in New Issue
Block a user