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

Commit 99a623d

Browse files
committed
Check appservice user interest against the local users instead of all users
`get_local_users_in_room` is way more performant since it looks at a single table (`local_current_membership`) and is looking through way less data since it only worries about the local users in the room instead of everyone in the room across the federation. Fix #13942 Related to: - #13605 - #13608 - #13606
1 parent 6f0c3e6 commit 99a623d

File tree

5 files changed

+30
-16
lines changed

5 files changed

+30
-16
lines changed

synapse/appservice/__init__.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,15 +172,11 @@ async def _matches_user_in_member_list(
172172
Returns:
173173
True if this service would like to know about this room.
174174
"""
175-
member_list = await store.get_users_in_room(
176-
room_id, on_invalidate=cache_context.invalidate
175+
app_service_users_in_room = await store.get_app_service_users_in_room(
176+
room_id, self, on_invalidate=cache_context.invalidate
177177
)
178178

179-
# check joined member events
180-
for user_id in member_list:
181-
if self.is_interested_in_user(user_id):
182-
return True
183-
return False
179+
return len(app_service_users_in_room) > 0
184180

185181
def is_interested_in_user(
186182
self,

synapse/handlers/room_member.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,7 @@ async def transfer_room_state_on_room_upgrade(
11501150
logger.info("Transferring room state from %s to %s", old_room_id, room_id)
11511151

11521152
# Find all local users that were in the old room and copy over each user's state
1153-
users = await self.store.get_users_in_room(old_room_id)
1153+
users = await self.store.get_local_users_in_room(old_room_id)
11541154
await self.copy_user_state_on_room_upgrade(old_room_id, room_id, users)
11551155

11561156
# Add new room to the room directory if the old room was there

synapse/handlers/user_directory.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ async def _track_user_joined_room(self, room_id: str, user_id: str) -> None:
392392
if is_public:
393393
await self.store.add_users_in_public_rooms(room_id, (user_id,))
394394
else:
395+
# TODO: get_local_users_in_room here
395396
users_in_room = await self.store.get_users_in_room(room_id)
396397
other_users_in_room = [
397398
other

synapse/storage/databases/main/appservice.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,19 @@ async def get_app_service_users_in_room(
157157
app_service: "ApplicationService",
158158
cache_context: _CacheContext,
159159
) -> List[str]:
160-
users_in_room = await self.get_users_in_room(
160+
"""
161+
Get all users in a room that the appservice controls.
162+
163+
Args:
164+
room_id: The room to check in.
165+
app_service: The application service to check interest/control against
166+
167+
Returns:
168+
List of user IDs that the appservice controls.
169+
"""
170+
# We can use `get_local_users_in_room(...)` here because an application
171+
# service can only act on behalf of users of the server it's on.
172+
users_in_room = await self.get_local_users_in_room(
161173
room_id, on_invalidate=cache_context.invalidate
162174
)
163175
return list(filter(app_service.is_interested_in_user, users_in_room))

synapse/storage/databases/main/roommember.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,18 @@ async def get_users_in_room(self, room_id: str) -> List[str]:
152152
`get_current_hosts_in_room()` and so we can re-use the cache but it's
153153
not horrible to have here either.
154154
155-
Uses `m.room.member`s in the room state at the current forward extremities to
156-
determine which users are in the room.
157-
158-
Will return inaccurate results for rooms with partial state, since the state for
159-
the forward extremities of those rooms will exclude most members. We may also
160-
calculate room state incorrectly for such rooms and believe that a member is or
161-
is not in the room when the opposite is true.
155+
Uses `m.room.member`s in the room state at the current forward
156+
extremities to determine which users are in the room.
157+
158+
Will return inaccurate results for rooms with partial state, since the
159+
state for the forward extremities of those rooms will exclude most
160+
members. We may also calculate room state incorrectly for such rooms and
161+
believe that a member is or is not in the room when the opposite is
162+
true.
163+
164+
Note: If you only care about users in the room local to the homeserver,
165+
use `get_local_users_in_room(...)` instead which will be more
166+
performant.
162167
"""
163168
return await self.db_pool.runInteraction(
164169
"get_users_in_room", self.get_users_in_room_txn, room_id

0 commit comments

Comments
 (0)