Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ SUPABASE_URL=http://127.0.0.1:54321
DATABASE_URL=postgresql+asyncpg://postgres:postgres@127.0.0.1:54322/postgres
ENVIRONMENT=development
CORS_ORIGINS=https://app.example.com,https://admin.example.com
REDIS_URL=redis://127.0.0.1:6379
4 changes: 3 additions & 1 deletion backend/app/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Settings(BaseSettings):
# supabase config
SUPABASE_URL: AnyHttpUrl

REDIS_URL: SecretStr

@property
def list_cors_origins(self) -> list[str]:
origins = [origin.strip() for origin in self.CORS_ORIGINS.split(",")]
Expand All @@ -33,4 +35,4 @@ def list_cors_origins(self) -> list[str]:

@lru_cache
def get_settings():
return Settings()
return Settings() # type:ignore
6 changes: 6 additions & 0 deletions backend/app/core/dep.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging

from fastapi.requests import HTTPConnection
from redis.asyncio import Redis

from app.session.engine import SessionEngine
from app.session.manager import ConnectionManager
Expand All @@ -18,3 +19,8 @@ def get_session_engine(connection: HTTPConnection) -> SessionEngine:

def get_logger() -> logging.Logger:
return logging.getLogger("uvicorn.error")


def get_session_store(connection: HTTPConnection) -> Redis:
"""Dependency injection for Cache pool"""
return connection.app.state.redis
10 changes: 6 additions & 4 deletions backend/app/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from contextlib import asynccontextmanager

import redis.asyncio as redis
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.openapi.utils import get_openapi
Expand All @@ -14,10 +15,6 @@
from app.session.manager import ConnectionManager
from app.session.views import router as session_router

# Startup and shutdown logic for shared variables, such as
# (db session, settings, connection manager, etc)
# You can view more of this on "FastAPI Lifespan"

settings = get_settings()


Expand All @@ -31,8 +28,13 @@ async def lifespan(app: FastAPI):
app.state.session_engine = SessionEngine()
app.state.connection_manager = ConnectionManager()

app.state.redis = redis.from_url(
settings.REDIS_URL.get_secret_value(),
)

yield

await app.state.redis.close()
await engine.dispose()


Expand Down
13 changes: 2 additions & 11 deletions backend/app/session/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,19 @@

from fastapi import WebSocket

from app.session.schemas import ServerSessionMessage, StateSnapshotMessage
from app.session.schemas import ServerSessionMessage

from .models import SessionActor, SessionLiveState
from .models import SessionActor


class ConnectionManager:
def __init__(self):
# Initialize dictionary with room_name and dict with websocket -> delegation
self.active_connections: dict[int, dict[WebSocket, SessionActor]] = {}
self.room_states: dict[int, SessionLiveState] = {}

#
async def connect(self, websocket: WebSocket, session_id: int, actor: SessionActor):

Check warning on line 17 in backend/app/session/manager.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use asynchronous features in this function or remove the `async` keyword.

See more on https://sonarcloud.io/project/issues?id=uspcodelab_webmun&issues=AaBUfSyE5kXlEh9oaxKb&open=AaBUfSyE5kXlEh9oaxKb&pullRequest=105
self.active_connections.setdefault(session_id, {})[websocket] = actor

# Send the initial state through the same typed protocol as later updates.
if session_id in self.room_states:
state = self.room_states[session_id]
await websocket.send_json(
StateSnapshotMessage(state=state).model_dump(mode="json")
)

def disconnect(self, websocket: WebSocket, session_id: int):
self.active_connections[session_id].pop(websocket)

Expand Down
29 changes: 17 additions & 12 deletions backend/app/session/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
from datetime import datetime
from uuid import UUID

from redis.asyncio import Redis
from sqlalchemy.ext.asyncio import AsyncSession

import app.session.enums as enums
import app.session.repository as repository
import app.session.schemas as schemas
import app.session.store as store
from app.access.models import CommitteeAssignment
from app.core.exceptions import (
BadRequest,
Expand All @@ -38,11 +40,11 @@ class SessionFetchError(Exception):
pass


def build_actor(
async def build_actor(
user_id: UUID,
manager: ConnectionManager,
session_id: int,
role: enums.SessionRole,
redis: Redis,
delegation_id: int | None = None,
) -> SessionActor:

Expand All @@ -56,7 +58,7 @@ def build_actor(
if role == enums.SessionRole.DELEGATE:
if delegation_id is None:
raise ActorResolutionError("needs delegate id")
state = manager.room_states.get(session_id)
state = await store.get_state(session=redis, session_id=session_id)
if state is None:
raise ActorResolutionError("session not found")

Expand Down Expand Up @@ -109,6 +111,7 @@ async def activate_session(
session: AsyncSession,
manager: ConnectionManager,
committee_session_id: int,
redis: Redis,
):
"""Activate a planned session"""
stored = await get_session_for_activation(session, committee_session_id)
Expand Down Expand Up @@ -145,7 +148,8 @@ async def activate_session(

await session.commit()

manager.room_states[committee_session_id] = live_state
await store.save_state(session=redis, state=live_state)

manager.active_connections.setdefault(committee_session_id, {})


Expand All @@ -170,11 +174,12 @@ async def prepare_session_connect(
manager: ConnectionManager,
committee_session_id: int,
assignment: CommitteeAssignment,
redis: Redis,
) -> SessionActor:
"""Service that prepares for session connect.
Primarily used as a fallback in case the SessionLiveState is not in manager
"""
live_state = manager.room_states.get(committee_session_id)
live_state = await store.get_state(session=redis, session_id=committee_session_id)

if live_state is None:
stored_state = await repository.get_session_info(session, committee_session_id)
Expand All @@ -188,37 +193,37 @@ async def prepare_session_connect(
# fetch state_snapshot and validate to be a SessionLiveState
live_state = SessionLiveState.model_validate(stored_state.state_snapshot)

# put SessionLiveState on ConnectionManager
manager.room_states[committee_session_id] = live_state
# put SessionLiveState on Cache
live_state = await store.save_state(session=redis, state=live_state)
manager.active_connections.setdefault(committee_session_id, {})

actor = build_actor(
actor = await build_actor(
user_id=assignment.user_id,
manager=manager,
session_id=committee_session_id,
role=enums.SessionRole(assignment.role.upper()),
delegation_id=assignment.representation_id,
redis=redis,
)

return actor


async def handle_client_messages(
manager: ConnectionManager,
engine: SessionEngine,
logger: logging.Logger,
session_id: int,
actor: SessionActor,
data: schemas.EventMessage,
redis: Redis,
):
event = data.event
state = manager.room_states[session_id]
state = await store.get_state(session=redis, session_id=session_id)

logger.info(event) # Debugging

try:
result = engine.dispatch(state, event, actor)
manager.room_states[session_id] = result.state
await store.save_state(session=redis, state=state)
return result
except EventRejectedError as exc:
return exc
19 changes: 19 additions & 0 deletions backend/app/session/store.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import redis.asyncio as redis

from app.session.models import SessionLiveState


async def get_state(session: redis.Redis, session_id: int) -> SessionLiveState | None:
res = await session.get(f"webmun:session:{session_id}")
if res is None:
return None

return SessionLiveState.model_validate_json(res)


async def save_state(session: redis.Redis, state: SessionLiveState) -> None:
await session.set(f"webmun:session:{state.session_id}", state.model_dump_json())


async def delete_state(session: redis.Redis, session_id: int) -> None:
await session.delete(f"webmun:session:{session_id}")
32 changes: 25 additions & 7 deletions backend/app/session/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
status,
)
from pydantic import ValidationError
from redis.asyncio import Redis
from sqlalchemy.ext.asyncio import AsyncSession

import app.access.service as access
Expand All @@ -26,8 +27,14 @@
)
from app.core.config import Settings, get_settings
from app.core.database import get_db_session
from app.core.dep import get_connection_manager, get_logger, get_session_engine
from app.core.dep import (
get_connection_manager,
get_logger,
get_session_engine,
get_session_store,
)
from app.core.exceptions import AccessDeniedError
from app.session import store
from app.session.engine import EventRejectedError, SessionEngine
from app.session.enums import EventErrorCode
from app.session.manager import ConnectionManager
Expand All @@ -38,6 +45,7 @@
EventMessage,
EventRejectedMessage,
SessionCreationSchema,
StateSnapshotMessage,
)

router = APIRouter()
Expand Down Expand Up @@ -75,6 +83,7 @@ async def activate_session_endpoint(
db_session: Annotated[AsyncSession, Depends(get_db_session)],
manager: Annotated[ConnectionManager, Depends(get_connection_manager)],
current_user: Annotated[AuthUser, Depends(get_current_user)],
redis: Annotated[Redis, Depends(get_session_store)],
):
"""Endpoint to activate a planned session"""
stored = await service.get_session_for_activation(
Expand All @@ -87,7 +96,10 @@ async def activate_session_endpoint(
required_role="chair",
)
await service.activate_session(
session=db_session, manager=manager, committee_session_id=session_id
session=db_session,
redis=redis,
manager=manager,
committee_session_id=session_id,
)


Expand All @@ -99,13 +111,10 @@ async def websocket_endpoint(
engine: Annotated[SessionEngine, Depends(get_session_engine)],
logger: Annotated[logging.Logger, Depends(get_logger)],
settings: Annotated[Settings, Depends(get_settings)],
redis: Annotated[Redis, Depends(get_session_store)],
):
"""
Endpoint for connecting to a committee session.

Overall flow: accepts websocket -> user sends token -> we verify it ->
if valid, lookup an assigment -> builds actor, connects to manager, and receive The
current session state
"""

await websocket.accept()
Expand All @@ -130,9 +139,18 @@ async def websocket_endpoint(
manager=manager,
committee_session_id=session_id,
assignment=assignment,
redis=redis,
)

# connect, and send state snapshot
state = await store.get_state(session=redis, session_id=session_id)
await manager.connect(websocket, session_id, actor)
await manager.send_message(
session_id=session_id,
websocket=websocket,
message=StateSnapshotMessage(state=state),
)

try:
while True:
data = await websocket.receive_json()
Expand All @@ -148,12 +166,12 @@ async def websocket_endpoint(
continue

result = await service.handle_client_messages(
manager=manager,
engine=engine,
logger=logger,
session_id=session_id,
actor=actor,
data=validated_event,
redis=redis,
)
match result:
# Either broadcast result outcome, or send error message back to socket
Expand Down
21 changes: 21 additions & 0 deletions backend/app/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"DATABASE_URL", "postgresql+asyncpg://postgres:postgres@localhost:5432/webmun_test"
)
os.environ.setdefault("SUPABASE_URL", "https://supabase.example.test")
os.environ.setdefault("REDIS_URL", "redis://localhost:6379/0")


from app.session.engine import SessionEngine
from app.session.enums import (
Expand Down Expand Up @@ -95,6 +97,25 @@ def fake_engine():
return FakeEngine()


class FakeRedis:
def __init__(self) -> None:
self.values: dict[str, str] = {}

async def get(self, key: str) -> str | None:
return self.values.get(key)

async def set(self, key: str, value: str) -> None:
self.values[key] = value

async def delete(self, key: str) -> int:
return int(self.values.pop(key, None) is not None)


@pytest.fixture
def fake_redis() -> FakeRedis:
return FakeRedis()


@pytest.fixture
def connection_manager():
return ConnectionManager()
Loading
Loading