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

Commit 47ffc7e

Browse files
authored
Reduce calls to send_presence_to_destinations (#16385)
1 parent 2763c49 commit 47ffc7e

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

changelog.d/16385.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Minor performance improvement when sending presence to federated servers.

synapse/handlers/presence.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,9 @@ async def maybe_send_presence_to_interested_destinations(
401401
states,
402402
)
403403

404-
for destination, host_states in hosts_to_states.items():
404+
for destinations, host_states in hosts_to_states:
405405
await self._federation.send_presence_to_destinations(
406-
host_states, [destination]
406+
host_states, destinations
407407
)
408408

409409
async def send_full_presence_to_users(self, user_ids: StrCollection) -> None:
@@ -1000,9 +1000,9 @@ async def _update_states(
10001000
list(to_federation_ping.values()),
10011001
)
10021002

1003-
for destination, states in hosts_to_states.items():
1003+
for destinations, states in hosts_to_states:
10041004
await self._federation_queue.send_presence_to_destinations(
1005-
states, [destination]
1005+
states, destinations
10061006
)
10071007

10081008
@wrap_as_background_process("handle_presence_timeouts")
@@ -2276,7 +2276,7 @@ async def get_interested_remotes(
22762276
store: DataStore,
22772277
presence_router: PresenceRouter,
22782278
states: List[UserPresenceState],
2279-
) -> Dict[str, Set[UserPresenceState]]:
2279+
) -> List[Tuple[StrCollection, Collection[UserPresenceState]]]:
22802280
"""Given a list of presence states figure out which remote servers
22812281
should be sent which.
22822282
@@ -2290,23 +2290,26 @@ async def get_interested_remotes(
22902290
Returns:
22912291
A map from destinations to presence states to send to that destination.
22922292
"""
2293-
hosts_and_states: Dict[str, Set[UserPresenceState]] = {}
2293+
hosts_and_states: List[Tuple[StrCollection, Collection[UserPresenceState]]] = []
22942294

22952295
# First we look up the rooms each user is in (as well as any explicit
22962296
# subscriptions), then for each distinct room we look up the remote
22972297
# hosts in those rooms.
2298-
room_ids_to_states, users_to_states = await get_interested_parties(
2299-
store, presence_router, states
2300-
)
2298+
for state in states:
2299+
room_ids = await store.get_rooms_for_user(state.user_id)
2300+
hosts: Set[str] = set()
2301+
for room_id in room_ids:
2302+
room_hosts = await store.get_current_hosts_in_room(room_id)
2303+
hosts.update(room_hosts)
2304+
hosts_and_states.append((hosts, [state]))
23012305

2302-
for room_id, states in room_ids_to_states.items():
2303-
hosts = await store.get_current_hosts_in_room(room_id)
2304-
for host in hosts:
2305-
hosts_and_states.setdefault(host, set()).update(states)
2306+
# Ask a presence routing module for any additional parties if one
2307+
# is loaded.
2308+
router_users_to_states = await presence_router.get_users_for_states(states)
23062309

2307-
for user_id, states in users_to_states.items():
2310+
for user_id, user_states in router_users_to_states.items():
23082311
host = get_domain_from_id(user_id)
2309-
hosts_and_states.setdefault(host, set()).update(states)
2312+
hosts_and_states.append(([host], user_states))
23102313

23112314
return hosts_and_states
23122315

0 commit comments

Comments
 (0)