From 713f196e96022685991a02f085f328c595623307 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 23 Apr 2020 15:25:11 -0400 Subject: [PATCH 1/4] Convert some fed handler methods to async. --- synapse/handlers/federation.py | 41 +++++++++++++++------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/synapse/handlers/federation.py b/synapse/handlers/federation.py index c7aa7acf3b61..8348761ec426 100644 --- a/synapse/handlers/federation.py +++ b/synapse/handlers/federation.py @@ -1694,16 +1694,15 @@ async def on_send_leave_request(self, origin, pdu): return None - @defer.inlineCallbacks - def get_state_for_pdu(self, room_id, event_id): + async def get_state_for_pdu(self, room_id, event_id): """Returns the state at the event. i.e. not including said event. """ - event = yield self.store.get_event( + event = await self.store.get_event( event_id, allow_none=False, check_room_id=room_id ) - state_groups = yield self.state_store.get_state_groups(room_id, [event_id]) + state_groups = await self.state_store.get_state_groups(room_id, [event_id]) if state_groups: _, state = list(iteritems(state_groups)).pop() @@ -1714,7 +1713,7 @@ def get_state_for_pdu(self, room_id, event_id): if "replaces_state" in event.unsigned: prev_id = event.unsigned["replaces_state"] if prev_id != event.event_id: - prev_event = yield self.store.get_event(prev_id) + prev_event = await self.store.get_event(prev_id) results[(event.type, event.state_key)] = prev_event else: del results[(event.type, event.state_key)] @@ -1724,15 +1723,14 @@ def get_state_for_pdu(self, room_id, event_id): else: return [] - @defer.inlineCallbacks - def get_state_ids_for_pdu(self, room_id, event_id): + async def get_state_ids_for_pdu(self, room_id, event_id): """Returns the state at the event. i.e. not including said event. """ - event = yield self.store.get_event( + event = await self.store.get_event( event_id, allow_none=False, check_room_id=room_id ) - state_groups = yield self.state_store.get_state_groups_ids(room_id, [event_id]) + state_groups = await self.state_store.get_state_groups_ids(room_id, [event_id]) if state_groups: _, state = list(state_groups.items()).pop() @@ -1751,49 +1749,46 @@ def get_state_ids_for_pdu(self, room_id, event_id): else: return [] - @defer.inlineCallbacks @log_function - def on_backfill_request(self, origin, room_id, pdu_list, limit): - in_room = yield self.auth.check_host_in_room(room_id, origin) + async def on_backfill_request(self, origin, room_id, pdu_list, limit): + in_room = await self.auth.check_host_in_room(room_id, origin) if not in_room: raise AuthError(403, "Host not in room.") # Synapse asks for 100 events per backfill request. Do not allow more. limit = min(limit, 100) - events = yield self.store.get_backfill_events(room_id, pdu_list, limit) + events = await self.store.get_backfill_events(room_id, pdu_list, limit) - events = yield filter_events_for_server(self.storage, origin, events) + events = await filter_events_for_server(self.storage, origin, events) return events - @defer.inlineCallbacks @log_function - def get_persisted_pdu(self, origin, event_id): + async def get_persisted_pdu(self, origin: str, event_id: str) -> Optional[EventBase]: """Get an event from the database for the given server. Args: - origin [str]: hostname of server which is requesting the event; we + origin: hostname of server which is requesting the event; we will check that the server is allowed to see it. - event_id [str]: id of the event being requested + event_id: id of the event being requested Returns: - Deferred[EventBase|None]: None if we know nothing about the event; - otherwise the (possibly-redacted) event. + None if we know nothing about the event; otherwise the (possibly-redacted) event. Raises: AuthError if the server is not currently in the room """ - event = yield self.store.get_event( + event = await self.store.get_event( event_id, allow_none=True, allow_rejected=True ) if event: - in_room = yield self.auth.check_host_in_room(event.room_id, origin) + in_room = await self.auth.check_host_in_room(event.room_id, origin) if not in_room: raise AuthError(403, "Host not in room.") - events = yield filter_events_for_server(self.storage, origin, [event]) + events = await filter_events_for_server(self.storage, origin, [event]) event = events[0] return event else: From cf1bc294112ba432d6d1a9e5a3f9bc2d78e4863c Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 23 Apr 2020 15:44:01 -0400 Subject: [PATCH 2/4] Add newsfragement. --- changelog.d/7338.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/7338.misc diff --git a/changelog.d/7338.misc b/changelog.d/7338.misc new file mode 100644 index 000000000000..7cafd074cad1 --- /dev/null +++ b/changelog.d/7338.misc @@ -0,0 +1 @@ +Convert some federation handler code to async/await. From 78a80835744a1f314f80764f71eef2f1106f00e6 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 23 Apr 2020 15:48:25 -0400 Subject: [PATCH 3/4] lint --- synapse/handlers/federation.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/synapse/handlers/federation.py b/synapse/handlers/federation.py index 8348761ec426..5107ff9a5d19 100644 --- a/synapse/handlers/federation.py +++ b/synapse/handlers/federation.py @@ -1765,7 +1765,9 @@ async def on_backfill_request(self, origin, room_id, pdu_list, limit): return events @log_function - async def get_persisted_pdu(self, origin: str, event_id: str) -> Optional[EventBase]: + async def get_persisted_pdu( + self, origin: str, event_id: str + ) -> Optional[EventBase]: """Get an event from the database for the given server. Args: From d7a0bdcca56defe08c48e92bea65b62a5d18821a Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Fri, 24 Apr 2020 13:33:52 -0400 Subject: [PATCH 4/4] Add more typing information. --- synapse/handlers/federation.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/synapse/handlers/federation.py b/synapse/handlers/federation.py index 5107ff9a5d19..41b96c0a7331 100644 --- a/synapse/handlers/federation.py +++ b/synapse/handlers/federation.py @@ -343,7 +343,7 @@ async def on_receive_pdu(self, origin, pdu, sent_to_us_directly=False) -> None: ours = await self.state_store.get_state_groups_ids(room_id, seen) # state_maps is a list of mappings from (type, state_key) to event_id - state_maps = list(ours.values()) # type: list[StateMap[str]] + state_maps = list(ours.values()) # type: List[StateMap[str]] # we don't need this any more, let's delete it. del ours @@ -1694,7 +1694,7 @@ async def on_send_leave_request(self, origin, pdu): return None - async def get_state_for_pdu(self, room_id, event_id): + async def get_state_for_pdu(self, room_id: str, event_id: str) -> List[EventBase]: """Returns the state at the event. i.e. not including said event. """ @@ -1723,7 +1723,7 @@ async def get_state_for_pdu(self, room_id, event_id): else: return [] - async def get_state_ids_for_pdu(self, room_id, event_id): + async def get_state_ids_for_pdu(self, room_id: str, event_id: str) -> List[str]: """Returns the state at the event. i.e. not including said event. """ event = await self.store.get_event( @@ -1750,7 +1750,9 @@ async def get_state_ids_for_pdu(self, room_id, event_id): return [] @log_function - async def on_backfill_request(self, origin, room_id, pdu_list, limit): + async def on_backfill_request( + self, origin: str, room_id: str, pdu_list: List[str], limit: int + ) -> List[EventBase]: in_room = await self.auth.check_host_in_room(room_id, origin) if not in_room: raise AuthError(403, "Host not in room.") @@ -2394,7 +2396,7 @@ async def _update_context_for_auth_events( """ # exclude the state key of the new event from the current_state in the context. if event.is_state(): - event_key = (event.type, event.state_key) + event_key = (event.type, event.state_key) # type: Optional[Tuple[str, str]] else: event_key = None state_updates = {