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

Reduce calls to send_presence_to_destinations #16385

Merged
merged 3 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/16385.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Minor performance improvement when sending presence to federated servers.
33 changes: 18 additions & 15 deletions synapse/handlers/presence.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,9 +401,9 @@ async def maybe_send_presence_to_interested_destinations(
states,
)

for destination, host_states in hosts_to_states.items():
for destinations, host_states in hosts_to_states:
await self._federation.send_presence_to_destinations(
host_states, [destination]
host_states, destinations
)

async def send_full_presence_to_users(self, user_ids: StrCollection) -> None:
Expand Down Expand Up @@ -1000,9 +1000,9 @@ async def _update_states(
list(to_federation_ping.values()),
)

for destination, states in hosts_to_states.items():
for destinations, states in hosts_to_states:
await self._federation_queue.send_presence_to_destinations(
states, [destination]
states, destinations
)

@wrap_as_background_process("handle_presence_timeouts")
Expand Down Expand Up @@ -2276,7 +2276,7 @@ async def get_interested_remotes(
store: DataStore,
presence_router: PresenceRouter,
states: List[UserPresenceState],
) -> Dict[str, Set[UserPresenceState]]:
) -> List[Tuple[StrCollection, Collection[UserPresenceState]]]:
"""Given a list of presence states figure out which remote servers
should be sent which.

Expand All @@ -2290,23 +2290,26 @@ async def get_interested_remotes(
Returns:
A map from destinations to presence states to send to that destination.
"""
hosts_and_states: Dict[str, Set[UserPresenceState]] = {}
hosts_and_states: List[Tuple[StrCollection, Collection[UserPresenceState]]] = []

# First we look up the rooms each user is in (as well as any explicit
# subscriptions), then for each distinct room we look up the remote
# hosts in those rooms.
room_ids_to_states, users_to_states = await get_interested_parties(
store, presence_router, states
)
for state in states:
room_ids = await store.get_rooms_for_user(state.user_id)
hosts: Set[str] = set()
for room_id in room_ids:
room_hosts = await store.get_current_hosts_in_room(room_id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a bulk version of this query we can use?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe so, but having a batched cached function I think would also reduce memory overhead, maybe 🤷

hosts.update(room_hosts)
hosts_and_states.append((hosts, [state]))

for room_id, states in room_ids_to_states.items():
hosts = await store.get_current_hosts_in_room(room_id)
for host in hosts:
hosts_and_states.setdefault(host, set()).update(states)
# Ask a presence routing module for any additional parties if one
# is loaded.
router_users_to_states = await presence_router.get_users_for_states(states)

for user_id, states in users_to_states.items():
for user_id, user_states in router_users_to_states.items():
host = get_domain_from_id(user_id)
hosts_and_states.setdefault(host, set()).update(states)
hosts_and_states.append(([host], user_states))

return hosts_and_states

Expand Down
Loading