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

Commit fefe994

Browse files
authored
Convert presence handler helpers to async/await. (#7939)
1 parent 83434df commit fefe994

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

changelog.d/7939.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Convert presence handler helpers to async/await.

synapse/federation/sender/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,9 @@ def _process_presence_inner(self, states: List[UserPresenceState]):
453453
"""Given a list of states populate self.pending_presence_by_dest and
454454
poke to send a new transaction to each destination
455455
"""
456-
hosts_and_states = yield get_interested_remotes(self.store, states, self.state)
456+
hosts_and_states = yield defer.ensureDeferred(
457+
get_interested_remotes(self.store, states, self.state)
458+
)
457459

458460
for destinations, states in hosts_and_states:
459461
for destination in destinations:

synapse/handlers/presence.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@
3030
from prometheus_client import Counter
3131
from typing_extensions import ContextManager
3232

33-
from twisted.internet import defer
34-
3533
import synapse.metrics
3634
from synapse.api.constants import EventTypes, Membership, PresenceState
3735
from synapse.api.errors import SynapseError
3836
from synapse.logging.context import run_in_background
3937
from synapse.logging.utils import log_function
4038
from synapse.metrics import LaterGauge
4139
from synapse.metrics.background_process_metrics import run_as_background_process
40+
from synapse.state import StateHandler
41+
from synapse.storage.data_stores.main import DataStore
4242
from synapse.storage.presence import UserPresenceState
4343
from synapse.types import JsonDict, UserID, get_domain_from_id
4444
from synapse.util.async_helpers import Linearizer
@@ -895,16 +895,9 @@ async def _handle_state_delta(self, deltas):
895895

896896
await self._on_user_joined_room(room_id, state_key)
897897

898-
async def _on_user_joined_room(self, room_id, user_id):
898+
async def _on_user_joined_room(self, room_id: str, user_id: str) -> None:
899899
"""Called when we detect a user joining the room via the current state
900900
delta stream.
901-
902-
Args:
903-
room_id (str)
904-
user_id (str)
905-
906-
Returns:
907-
Deferred
908901
"""
909902

910903
if self.is_mine_id(user_id):
@@ -1296,22 +1289,24 @@ def handle_update(prev_state, new_state, is_mine, wheel_timer, now):
12961289
return new_state, persist_and_notify, federation_ping
12971290

12981291

1299-
@defer.inlineCallbacks
1300-
def get_interested_parties(store, states):
1292+
async def get_interested_parties(
1293+
store: DataStore, states: List[UserPresenceState]
1294+
) -> Tuple[Dict[str, List[UserPresenceState]], Dict[str, List[UserPresenceState]]]:
13011295
"""Given a list of states return which entities (rooms, users)
13021296
are interested in the given states.
13031297
13041298
Args:
1305-
states (list(UserPresenceState))
1299+
store
1300+
states
13061301
13071302
Returns:
1308-
2-tuple: `(room_ids_to_states, users_to_states)`,
1303+
A 2-tuple of `(room_ids_to_states, users_to_states)`,
13091304
with each item being a dict of `entity_name` -> `[UserPresenceState]`
13101305
"""
13111306
room_ids_to_states = {} # type: Dict[str, List[UserPresenceState]]
13121307
users_to_states = {} # type: Dict[str, List[UserPresenceState]]
13131308
for state in states:
1314-
room_ids = yield store.get_rooms_for_user(state.user_id)
1309+
room_ids = await store.get_rooms_for_user(state.user_id)
13151310
for room_id in room_ids:
13161311
room_ids_to_states.setdefault(room_id, []).append(state)
13171312

@@ -1321,31 +1316,33 @@ def get_interested_parties(store, states):
13211316
return room_ids_to_states, users_to_states
13221317

13231318

1324-
@defer.inlineCallbacks
1325-
def get_interested_remotes(store, states, state_handler):
1319+
async def get_interested_remotes(
1320+
store: DataStore, states: List[UserPresenceState], state_handler: StateHandler
1321+
) -> List[Tuple[List[str], List[UserPresenceState]]]:
13261322
"""Given a list of presence states figure out which remote servers
13271323
should be sent which.
13281324
13291325
All the presence states should be for local users only.
13301326
13311327
Args:
1332-
store (DataStore)
1333-
states (list(UserPresenceState))
1328+
store
1329+
states
1330+
state_handler
13341331
13351332
Returns:
1336-
Deferred list of ([destinations], [UserPresenceState]), where for
1337-
each row the list of UserPresenceState should be sent to each
1333+
A list of 2-tuples of destinations and states, where for
1334+
each tuple the list of UserPresenceState should be sent to each
13381335
destination
13391336
"""
13401337
hosts_and_states = []
13411338

13421339
# First we look up the rooms each user is in (as well as any explicit
13431340
# subscriptions), then for each distinct room we look up the remote
13441341
# hosts in those rooms.
1345-
room_ids_to_states, users_to_states = yield get_interested_parties(store, states)
1342+
room_ids_to_states, users_to_states = await get_interested_parties(store, states)
13461343

13471344
for room_id, states in room_ids_to_states.items():
1348-
hosts = yield state_handler.get_current_hosts_in_room(room_id)
1345+
hosts = await state_handler.get_current_hosts_in_room(room_id)
13491346
hosts_and_states.append((hosts, states))
13501347

13511348
for user_id, states in users_to_states.items():

0 commit comments

Comments
 (0)