This commit is contained in:
Timothy Jaeryang Baek committed 2026-12-01 00:24:00 +04:00
1 parent a658799f98
commit d13ecf6816
3 files changed
+34 -6

No files matched your search

+25 -4
View File
@@ -237,9 +237,18 @@ YDOC_MANAGER = YdocManager(
)
async def get_session_pool_batches():
"""All session pool entries, in bounded batches for the Redis backing."""
async def get_session_pool_batches(room: str | None = None):
"""All session pool entries, in bounded batches for the Redis backing; a room narrows Redis to its own sessions."""
if WEBSOCKET_MANAGER == 'redis':
if room:
key = f'{REDIS_KEY_PREFIX}:{room}:session_ids'
session_ids = list(await REDIS.smembers(key))
batch = list(zip(session_ids, SESSION_POOL.get_many(*session_ids)))
# Disconnected sessions are dropped here rather than on every disconnect path.
if disconnected := [sid for sid, session in batch if session is None]:
await REDIS.srem(key, *disconnected)
yield batch
return
batches = SESSION_POOL.scan_batches()
while True:
batch = await asyncio.to_thread(next, batches, None)
@@ -264,7 +273,7 @@ async def publish_chat_presence(chat_id: str):
async with lock, asyncio.timeout(WEBSOCKET_REDIS_LOCK_TIMEOUT / 2):
now = time.time()
people = {}
async for batch in get_session_pool_batches():
async for batch in get_session_pool_batches(f'chat:{chat_id}'):
for _, session in batch:
if (
session
@@ -303,7 +312,7 @@ async def publish_note_presence(note_id: str):
async with lock, asyncio.timeout(WEBSOCKET_REDIS_LOCK_TIMEOUT / 2):
now = time.time()
people = {}
async for batch in get_session_pool_batches():
async for batch in get_session_pool_batches(f'note:{note_id}'):
for _, session in batch:
if (
not session
@@ -746,6 +755,8 @@ async def refresh_chat_access(chat_id=None):
**current,
'chat_ids': [id for id in current.get('chat_ids', []) if id != cid],
}
if WEBSOCKET_MANAGER == 'redis':
await REDIS.srem(f'{REDIS_KEY_PREFIX}:chat:{cid}:session_ids', sid)
await sio.emit(
'events', {'chat_id': cid, 'shared': True, 'data': {'type': 'chat:access', 'data': {}}}, to=sid
)
@@ -847,6 +858,8 @@ async def join_note(sid, data):
session = SESSION_POOL.get(sid)
if session:
SESSION_POOL[sid] = {**session, 'note_ids': list(set(session.get('note_ids', [])) | {note.id})}
if WEBSOCKET_MANAGER == 'redis':
await REDIS.sadd(f'{REDIS_KEY_PREFIX}:note:{note.id}:session_ids', sid)
await publish_note_presence(note.id)
@@ -864,6 +877,8 @@ async def leave_note(sid, data):
'note_ids': [id for id in session.get('note_ids', []) if id != note_id],
'note_locations': locations,
}
if WEBSOCKET_MANAGER == 'redis':
await REDIS.srem(f'{REDIS_KEY_PREFIX}:note:{note_id}:session_ids', sid)
await publish_note_presence(note_id)
@@ -971,6 +986,12 @@ async def chat_events(sid, data):
else:
chat_ids.add(chat_id)
SESSION_POOL[sid] = {**session, 'chat_ids': list(chat_ids)}
if WEBSOCKET_MANAGER == 'redis':
key = f'{REDIS_KEY_PREFIX}:chat:{chat_id}:session_ids'
if event_type == 'leave':
await REDIS.srem(key, sid)
else:
await REDIS.sadd(key, sid)
await publish_chat_presence(chat_id)
return True
+6
View File
@@ -136,6 +136,12 @@ class RedisDict:
if cursor == 0:
break
def get_many(self, *keys):
"""Values of fields in one HMGET, None where missing; no keys returns [] (HMGET rejects an empty field list)."""
if not keys:
return []
return [JSONCodec.loads(v) if v is not None else None for v in self.redis.hmget(self.name, keys)]
def delete_many(self, *keys):
"""Delete fields in one HDEL; no keys is a no-op (HDEL rejects an empty field list)."""
if keys:
+3 -2
View File
@@ -65,10 +65,11 @@
$: if (!pinned) focusCurrent();
const focusCurrent = () => {
const focusCurrent = async () => {
focusedId = history?.currentId ?? null;
if (focusedId && !pinned) {
fitView({ nodes: [{ id: focusedId }] });
await tick();
await fitView({ nodes: [{ id: focusedId }] });
}
};