Files
Wizard/wizard_server/app/sessions.py
T
marvinandClaude Opus 5.5 87dfcd55b7 Add resource limits, abuse protection and overload handling
Running games always continue; new work is refused when there is no
capacity; abuse is rejected immediately.

- Docker: CPU/memory/pids caps per container (overridable in .env);
  uvicorn message size, queue and concurrency limits
- Server abuse limits: connections total/per IP (close 1013), message
  rate per connection (token bucket, close 1008 on floods), lobby
  creation rate per IP
- Load monitor: event loop lag, CPU of the own cgroup quota and
  throttling, with hysteresis (normal/busy/overloaded, severe lag
  overloads immediately)
- Admission control: create_lobby and start_game refused while
  overloaded or MAX_ACTIVE_GAMES are running; server_status message
  broadcast on changes; /health reports load but stays ok
- AI bridge: timeout sent with requests, in-flight cap, circuit breaker
- AI service: bounded workers and queue ("overloaded"), drops expired
  requests, degrades to the heuristic agent when busy or too slow
- Website: busy notices, Create/Start disabled when not accepting
  games, slow retry when the server is full
- Tests for limiters, load levels, admission control and AI service
  limits; docs updated

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-27 12:51:46 +02:00

99 lines
3.0 KiB
Python

"""Player sessions and their websocket connections.
A ``Session`` identifies a human player across reconnects (e.g. a page
reload) via a secret token. A ``Connection`` wraps one websocket and owns an
outgoing message queue so that messages are sent in order and never
concurrently.
"""
from __future__ import annotations
import asyncio
import contextlib
import logging
import secrets
import time
import uuid
from typing import Any
from starlette.websockets import WebSocket
logger = logging.getLogger(__name__)
class Connection:
def __init__(self, websocket: WebSocket) -> None:
self.websocket = websocket
# Client IP; behind the reverse proxy uvicorn takes it from X-Forwarded-For.
self.ip = websocket.client.host if websocket.client else "unknown"
self._queue: asyncio.Queue[dict[str, Any] | None] = asyncio.Queue()
self._writer: asyncio.Task | None = None
def start(self) -> None:
self._writer = asyncio.create_task(self._write_loop())
def send(self, message: dict[str, Any]) -> None:
self._queue.put_nowait(message)
async def close(self, code: int = 1000) -> None:
self._queue.put_nowait(None)
if self._writer:
with contextlib.suppress(Exception):
await self._writer
with contextlib.suppress(Exception):
await self.websocket.close(code)
async def _write_loop(self) -> None:
while (message := await self._queue.get()) is not None:
try:
await self.websocket.send_json(message)
except Exception: # connection is gone; the reader loop cleans up
logger.debug("Failed to send message", exc_info=True)
return
class Session:
def __init__(self) -> None:
self.player_id = uuid.uuid4().hex[:12]
self.token = secrets.token_urlsafe(24)
self.name: str | None = None
self.lobby_code: str | None = None
self.connection: Connection | None = None
self.last_seen = time.monotonic()
@property
def connected(self) -> bool:
return self.connection is not None
def send(self, message: dict[str, Any]) -> None:
if self.connection:
self.connection.send(message)
class SessionRegistry:
def __init__(self) -> None:
self._by_token: dict[str, Session] = {}
def create(self) -> Session:
session = Session()
self._by_token[session.token] = session
return session
def get(self, token: str | None) -> Session | None:
if not token:
return None
return self._by_token.get(token)
def remove(self, session: Session) -> None:
self._by_token.pop(session.token, None)
def connected(self) -> list[Session]:
return [s for s in self._by_token.values() if s.connected]
def expired(self, ttl: float) -> list[Session]:
now = time.monotonic()
return [s for s in self._by_token.values() if not s.connected and now - s.last_seen > ttl]
def __len__(self) -> int:
return len(self._by_token)