-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathstate.py
More file actions
28 lines (22 loc) · 1.1 KB
/
state.py
File metadata and controls
28 lines (22 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
"""
MoltGrid Shared Mutable State — global state variables shared across modules.
Extracted from main.py to prevent circular imports when routers import state.
"""
import threading
# WebSocket connections: agent_id -> set of WebSocket objects
_ws_connections: dict[str, set] = {}
# SSE connections: agent_id -> set of asyncio.Queue objects (one per subscriber)
_sse_connections: dict[str, set] = {}
# Network WebSocket clients (lobby/broadcast)
_network_ws_clients: list = []
# Global embedding model (loaded lazily on first use)
_embed_model = None
_embed_lock = threading.Lock()
# IP-based auth rate limiting: ip -> [timestamps]
_auth_rate_limits: dict = {}
# Intra-worker thread safety for the auth rate-limit read-modify-write.
# NOTE: this lock does NOT protect against cross-worker bypass -- the 4 Uvicorn
# worker processes each have their own _auth_rate_limits dict, so two requests
# landing on different workers each see their own counter. Cross-worker
# correctness requires a Redis INCR-based limiter and is out of scope for this phase.
_auth_rate_limits_lock = threading.Lock()