Skip to content

Commit

Permalink
Batch up calls to get_rooms_for_users (matrix-org#14109)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fizzadar authored Oct 12, 2022
1 parent 09be8ab commit f9bc542
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.d/14109.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Break up calls to fetch rooms for many users. Contributed by Nick @ Beeper (@fizzadar).
17 changes: 16 additions & 1 deletion synapse/storage/databases/main/roommember.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ async def get_rooms_for_user(self, user_id: str) -> FrozenSet[str]:
cached_method_name="get_rooms_for_user",
list_name="user_ids",
)
async def get_rooms_for_users(
async def _get_rooms_for_users(
self, user_ids: Collection[str]
) -> Dict[str, FrozenSet[str]]:
"""A batched version of `get_rooms_for_user`.
Expand Down Expand Up @@ -697,6 +697,21 @@ async def get_rooms_for_users(

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

async def get_rooms_for_users(
self, user_ids: Collection[str]
) -> Dict[str, FrozenSet[str]]:
"""A batched wrapper around `_get_rooms_for_users`, to prevent locking
other calls to `get_rooms_for_user` for large user lists.
"""
all_user_rooms: Dict[str, FrozenSet[str]] = {}

# 250 users is pretty arbitrary but the data can be quite large if users
# are in many rooms.
for user_ids in batch_iter(user_ids, 250):
all_user_rooms.update(await self._get_rooms_for_users(user_ids))

return all_user_rooms

@cached(max_entries=10000)
async def does_pair_of_users_share_a_room(
self, user_id: str, other_user_id: str
Expand Down

0 comments on commit f9bc542

Please sign in to comment.