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

Convert room list handler to async/await. #7912

Merged
merged 2 commits into from
Jul 21, 2020
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/7912.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Convert `RoomListHandler` to async/await.
10 changes: 2 additions & 8 deletions synapse/federation/transport/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import re
from typing import Optional, Tuple, Type

from twisted.internet.defer import maybeDeferred

import synapse
from synapse.api.errors import Codes, FederationDeniedError, SynapseError
from synapse.api.room_versions import RoomVersions
Expand Down Expand Up @@ -795,12 +793,8 @@ async def on_GET(self, origin, content, query):
# zero is a special value which corresponds to no limit.
limit = None

data = await maybeDeferred(
self.handler.get_local_public_room_list,
limit,
since_token,
network_tuple=network_tuple,
from_federation=True,
data = await self.handler.get_local_public_room_list(
limit, since_token, network_tuple=network_tuple, from_federation=True
)
return 200, data

Expand Down
62 changes: 29 additions & 33 deletions synapse/handlers/room_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@
import msgpack
from unpaddedbase64 import decode_base64, encode_base64

from twisted.internet import defer

from synapse.api.constants import EventTypes, JoinRules
from synapse.api.errors import Codes, HttpResponseException
from synapse.types import ThirdPartyInstanceID
from synapse.util.caches.descriptors import cachedInlineCallbacks
from synapse.util.caches.descriptors import cached
from synapse.util.caches.response_cache import ResponseCache

from ._base import BaseHandler
Expand All @@ -47,7 +45,7 @@ def __init__(self, hs):
hs, "remote_room_list", timeout_ms=30 * 1000
)

def get_local_public_room_list(
async def get_local_public_room_list(
self,
limit=None,
since_token=None,
Expand All @@ -72,7 +70,7 @@ def get_local_public_room_list(
API
"""
if not self.enable_room_list_search:
return defer.succeed({"chunk": [], "total_room_count_estimate": 0})
return {"chunk": [], "total_room_count_estimate": 0}

logger.info(
"Getting public room list: limit=%r, since=%r, search=%r, network=%r",
Expand All @@ -87,7 +85,7 @@ def get_local_public_room_list(
# appservice specific lists.
logger.info("Bypassing cache as search request.")

return self._get_public_room_list(
return await self._get_public_room_list(
limit,
since_token,
search_filter,
Expand All @@ -96,7 +94,7 @@ def get_local_public_room_list(
)

key = (limit, since_token, network_tuple)
return self.response_cache.wrap(
return await self.response_cache.wrap(
key,
self._get_public_room_list,
limit,
Expand All @@ -105,8 +103,7 @@ def get_local_public_room_list(
from_federation=from_federation,
)

@defer.inlineCallbacks
def _get_public_room_list(
async def _get_public_room_list(
self,
limit: Optional[int] = None,
since_token: Optional[str] = None,
Expand Down Expand Up @@ -145,7 +142,7 @@ def _get_public_room_list(
# we request one more than wanted to see if there are more pages to come
probing_limit = limit + 1 if limit is not None else None

results = yield self.store.get_largest_public_rooms(
results = await self.store.get_largest_public_rooms(
network_tuple,
search_filter,
probing_limit,
Expand Down Expand Up @@ -221,52 +218,52 @@ def build_room_entry(room):

response["chunk"] = results

response["total_room_count_estimate"] = yield self.store.count_public_rooms(
response["total_room_count_estimate"] = await self.store.count_public_rooms(
network_tuple, ignore_non_federatable=from_federation
)

return response

@cachedInlineCallbacks(num_args=1, cache_context=True)
def generate_room_entry(
@cached(num_args=1, cache_context=True)
async def generate_room_entry(
self,
room_id,
num_joined_users,
room_id: str,
num_joined_users: int,
cache_context,
with_alias=True,
allow_private=False,
):
with_alias: bool = True,
allow_private: bool = False,
) -> Optional[dict]:
"""Returns the entry for a room

Args:
room_id (str): The room's ID.
num_joined_users (int): Number of users in the room.
room_id: The room's ID.
num_joined_users: Number of users in the room.
cache_context: Information for cached responses.
with_alias (bool): Whether to return the room's aliases in the result.
allow_private (bool): Whether invite-only rooms should be shown.
with_alias: Whether to return the room's aliases in the result.
allow_private: Whether invite-only rooms should be shown.

Returns:
Deferred[dict|None]: Returns a room entry as a dictionary, or None if this
Returns a room entry as a dictionary, or None if this
room was determined not to be shown publicly.
"""
result = {"room_id": room_id, "num_joined_members": num_joined_users}

if with_alias:
aliases = yield self.store.get_aliases_for_room(
aliases = await self.store.get_aliases_for_room(
room_id, on_invalidate=cache_context.invalidate
)
if aliases:
result["aliases"] = aliases

current_state_ids = yield self.store.get_current_state_ids(
current_state_ids = await self.store.get_current_state_ids(
room_id, on_invalidate=cache_context.invalidate
)

if not current_state_ids:
# We're not in the room, so may as well bail out here.
return result

event_map = yield self.store.get_events(
event_map = await self.store.get_events(
[
event_id
for key, event_id in current_state_ids.items()
Expand Down Expand Up @@ -336,8 +333,7 @@ def generate_room_entry(

return result

@defer.inlineCallbacks
def get_remote_public_room_list(
async def get_remote_public_room_list(
self,
server_name,
limit=None,
Expand All @@ -356,7 +352,7 @@ def get_remote_public_room_list(
# to a locally-filtered search if we must.

try:
res = yield self._get_remote_list_cached(
res = await self._get_remote_list_cached(
server_name,
limit=limit,
since_token=since_token,
Expand All @@ -381,7 +377,7 @@ def get_remote_public_room_list(
limit = None
since_token = None

res = yield self._get_remote_list_cached(
res = await self._get_remote_list_cached(
server_name,
limit=limit,
since_token=since_token,
Expand All @@ -400,7 +396,7 @@ def get_remote_public_room_list(

return res

def _get_remote_list_cached(
async def _get_remote_list_cached(
self,
server_name,
limit=None,
Expand All @@ -412,7 +408,7 @@ def _get_remote_list_cached(
repl_layer = self.hs.get_federation_client()
if search_filter:
# We can't cache when asking for search
return repl_layer.get_public_rooms(
return await repl_layer.get_public_rooms(
server_name,
limit=limit,
since_token=since_token,
Expand All @@ -428,7 +424,7 @@ def _get_remote_list_cached(
include_all_networks,
third_party_instance_id,
)
return self.remote_response_cache.wrap(
return await self.remote_response_cache.wrap(
key,
repl_layer.get_public_rooms,
server_name,
Expand Down