Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit b29ccf9

Browse files
Fizzadarclokep
authored andcommitted
Batch up calls to get_rooms_for_users (#14109)
1 parent 0096413 commit b29ccf9

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

changelog.d/14109.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Break up calls to fetch rooms for many users. Contributed by Nick @ Beeper (@fizzadar).

synapse/storage/databases/main/roommember.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ async def get_rooms_for_user(self, user_id: str) -> FrozenSet[str]:
666666
cached_method_name="get_rooms_for_user",
667667
list_name="user_ids",
668668
)
669-
async def get_rooms_for_users(
669+
async def _get_rooms_for_users(
670670
self, user_ids: Collection[str]
671671
) -> Dict[str, FrozenSet[str]]:
672672
"""A batched version of `get_rooms_for_user`.
@@ -697,6 +697,21 @@ async def get_rooms_for_users(
697697

698698
return {key: frozenset(rooms) for key, rooms in user_rooms.items()}
699699

700+
async def get_rooms_for_users(
701+
self, user_ids: Collection[str]
702+
) -> Dict[str, FrozenSet[str]]:
703+
"""A batched wrapper around `_get_rooms_for_users`, to prevent locking
704+
other calls to `get_rooms_for_user` for large user lists.
705+
"""
706+
all_user_rooms: Dict[str, FrozenSet[str]] = {}
707+
708+
# 250 users is pretty arbitrary but the data can be quite large if users
709+
# are in many rooms.
710+
for user_ids in batch_iter(user_ids, 250):
711+
all_user_rooms.update(await self._get_rooms_for_users(user_ids))
712+
713+
return all_user_rooms
714+
700715
@cached(max_entries=10000)
701716
async def does_pair_of_users_share_a_room(
702717
self, user_id: str, other_user_id: str

0 commit comments

Comments
 (0)