attempting proxying using shared manager

This commit is contained in:
2025-12-09 16:26:49 -05:00
parent 7d23bf60cf
commit 3e89f8fae6
2 changed files with 17 additions and 11 deletions

View File

@@ -3,6 +3,7 @@ import queue
import uuid
from typing import Dict, Any
import logging
from multiprocessing import Manager
from flask import Response
@@ -10,6 +11,7 @@ from events.types.event import Event
logger = logging.getLogger(__name__)
manager = Manager()
# Maps user_id → dict of {connection_id: queue}
user_queues: Dict[str, Dict[str, queue.Queue]] = {}
logging.basicConfig(level=logging.INFO)
@@ -18,30 +20,23 @@ logging.basicConfig(level=logging.INFO)
def get_queue(user_id: str, connection_id: str) -> queue.Queue:
"""Get or create a queue for a specific user connection."""
if user_id not in user_queues:
user_queues[user_id] = {}
user_queues[user_id] = manager.dict()
if connection_id not in user_queues[user_id]:
user_queues[user_id][connection_id] = queue.Queue()
user_queues[user_id][connection_id] = manager.Queue()
return user_queues[user_id][connection_id]
def send_to_user(user_id: str, data: Dict[str, Any]):
"""Send data to all connections for a specific user."""
logger.info(f"Sending data to {user_id} user quesues are {user_queues.keys()}")
logger.info(f"Sending data to {user_id}")
if user_id in user_queues:
logger.info(f"Queued {user_id}")
# Format as SSE message once
message = f"data: {json.dumps(data)}\n\n".encode('utf-8')
# Send to all connections for this user
for connection_id, q in user_queues[user_id].items():
try:
logger.info(f"Sending message to {connection_id}")
q.put(message)
q.put(message, block=False)
except queue.Full:
# Skip if queue is full (connection might be dead)
pass
@@ -82,3 +77,9 @@ def sse_response_for_user(user_id: str):
'Connection': 'keep-alive'
}
)
_shared_manager = None
def set_shared_manager(manager):
global _shared_manager
_shared_manager = manager