From 78348ca41cf02edad10cef4adfa9ee0eea8c78cf Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Wed, 6 Sep 2023 14:25:29 +0100 Subject: [PATCH 1/3] Reduce calls to send_presence_to_destinations --- synapse/handlers/presence.py | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/synapse/handlers/presence.py b/synapse/handlers/presence.py index 375c7d0901d5..7c7cda3e95b8 100644 --- a/synapse/handlers/presence.py +++ b/synapse/handlers/presence.py @@ -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: @@ -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") @@ -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. @@ -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) + 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 From 0d37f898d90114be2e5711dec04ec22e27031a03 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Tue, 26 Sep 2023 10:09:56 +0100 Subject: [PATCH 2/3] Newsfile --- changelog.d/16385.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/16385.misc diff --git a/changelog.d/16385.misc b/changelog.d/16385.misc new file mode 100644 index 000000000000..40591e28179f --- /dev/null +++ b/changelog.d/16385.misc @@ -0,0 +1 @@ +Increase performance of sending presence to federated servers. From 1b8a0f77ed7ed8e95cda601c3114f14fd1155b95 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Tue, 26 Sep 2023 10:34:30 +0100 Subject: [PATCH 3/3] Newsfile --- changelog.d/16385.misc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.d/16385.misc b/changelog.d/16385.misc index 40591e28179f..d439a931d69c 100644 --- a/changelog.d/16385.misc +++ b/changelog.d/16385.misc @@ -1 +1 @@ -Increase performance of sending presence to federated servers. +Minor performance improvement when sending presence to federated servers.