From d13ecf68166b22a73ebde2a17bd960a9b91935f0 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Tue, 1 Dec 2026 00:24:00 +0400 Subject: [PATCH] refac --- backend/open_webui/socket/main.py | 29 +++++++++++++++++--- backend/open_webui/socket/utils.py | 6 ++++ src/lib/components/chat/Overview/View.svelte | 5 ++-- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/backend/open_webui/socket/main.py b/backend/open_webui/socket/main.py index 529dfbbfa0..caab6b0eb0 100644 --- a/backend/open_webui/socket/main.py +++ b/backend/open_webui/socket/main.py @@ -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 diff --git a/backend/open_webui/socket/utils.py b/backend/open_webui/socket/utils.py index 29d0a8bd90..fca156fdf9 100644 --- a/backend/open_webui/socket/utils.py +++ b/backend/open_webui/socket/utils.py @@ -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: diff --git a/src/lib/components/chat/Overview/View.svelte b/src/lib/components/chat/Overview/View.svelte index ef329dd179..ab63000b4d 100644 --- a/src/lib/components/chat/Overview/View.svelte +++ b/src/lib/components/chat/Overview/View.svelte @@ -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 }] }); } };