Convert room list handler to async/await. (#7912)

This commit is contained in:
Patrick Cloke 2020-07-21 07:51:48 -04:00 committed by GitHub
parent 759481af6d
commit de119063f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 41 deletions

1
changelog.d/7912.misc Normal file
View File

@ -0,0 +1 @@
Convert `RoomListHandler` to async/await.

View File

@ -20,8 +20,6 @@ import logging
import re import re
from typing import Optional, Tuple, Type from typing import Optional, Tuple, Type
from twisted.internet.defer import maybeDeferred
import synapse import synapse
from synapse.api.errors import Codes, FederationDeniedError, SynapseError from synapse.api.errors import Codes, FederationDeniedError, SynapseError
from synapse.api.room_versions import RoomVersions from synapse.api.room_versions import RoomVersions
@ -795,12 +793,8 @@ class PublicRoomList(BaseFederationServlet):
# zero is a special value which corresponds to no limit. # zero is a special value which corresponds to no limit.
limit = None limit = None
data = await maybeDeferred( data = await self.handler.get_local_public_room_list(
self.handler.get_local_public_room_list, limit, since_token, network_tuple=network_tuple, from_federation=True
limit,
since_token,
network_tuple=network_tuple,
from_federation=True,
) )
return 200, data return 200, data

View File

@ -20,12 +20,10 @@ from typing import Any, Dict, Optional
import msgpack import msgpack
from unpaddedbase64 import decode_base64, encode_base64 from unpaddedbase64 import decode_base64, encode_base64
from twisted.internet import defer
from synapse.api.constants import EventTypes, JoinRules from synapse.api.constants import EventTypes, JoinRules
from synapse.api.errors import Codes, HttpResponseException from synapse.api.errors import Codes, HttpResponseException
from synapse.types import ThirdPartyInstanceID 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 synapse.util.caches.response_cache import ResponseCache
from ._base import BaseHandler from ._base import BaseHandler
@ -47,7 +45,7 @@ class RoomListHandler(BaseHandler):
hs, "remote_room_list", timeout_ms=30 * 1000 hs, "remote_room_list", timeout_ms=30 * 1000
) )
def get_local_public_room_list( async def get_local_public_room_list(
self, self,
limit=None, limit=None,
since_token=None, since_token=None,
@ -72,7 +70,7 @@ class RoomListHandler(BaseHandler):
API API
""" """
if not self.enable_room_list_search: 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( logger.info(
"Getting public room list: limit=%r, since=%r, search=%r, network=%r", "Getting public room list: limit=%r, since=%r, search=%r, network=%r",
@ -87,7 +85,7 @@ class RoomListHandler(BaseHandler):
# appservice specific lists. # appservice specific lists.
logger.info("Bypassing cache as search request.") logger.info("Bypassing cache as search request.")
return self._get_public_room_list( return await self._get_public_room_list(
limit, limit,
since_token, since_token,
search_filter, search_filter,
@ -96,7 +94,7 @@ class RoomListHandler(BaseHandler):
) )
key = (limit, since_token, network_tuple) key = (limit, since_token, network_tuple)
return self.response_cache.wrap( return await self.response_cache.wrap(
key, key,
self._get_public_room_list, self._get_public_room_list,
limit, limit,
@ -105,8 +103,7 @@ class RoomListHandler(BaseHandler):
from_federation=from_federation, from_federation=from_federation,
) )
@defer.inlineCallbacks async def _get_public_room_list(
def _get_public_room_list(
self, self,
limit: Optional[int] = None, limit: Optional[int] = None,
since_token: Optional[str] = None, since_token: Optional[str] = None,
@ -145,7 +142,7 @@ class RoomListHandler(BaseHandler):
# we request one more than wanted to see if there are more pages to come # 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 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, network_tuple,
search_filter, search_filter,
probing_limit, probing_limit,
@ -221,44 +218,44 @@ class RoomListHandler(BaseHandler):
response["chunk"] = results 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 network_tuple, ignore_non_federatable=from_federation
) )
return response return response
@cachedInlineCallbacks(num_args=1, cache_context=True) @cached(num_args=1, cache_context=True)
def generate_room_entry( async def generate_room_entry(
self, self,
room_id, room_id: str,
num_joined_users, num_joined_users: int,
cache_context, cache_context,
with_alias=True, with_alias: bool = True,
allow_private=False, allow_private: bool = False,
): ) -> Optional[dict]:
"""Returns the entry for a room """Returns the entry for a room
Args: Args:
room_id (str): The room's ID. room_id: The room's ID.
num_joined_users (int): Number of users in the room. num_joined_users: Number of users in the room.
cache_context: Information for cached responses. cache_context: Information for cached responses.
with_alias (bool): Whether to return the room's aliases in the result. with_alias: Whether to return the room's aliases in the result.
allow_private (bool): Whether invite-only rooms should be shown. allow_private: Whether invite-only rooms should be shown.
Returns: 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. room was determined not to be shown publicly.
""" """
result = {"room_id": room_id, "num_joined_members": num_joined_users} result = {"room_id": room_id, "num_joined_members": num_joined_users}
if with_alias: 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 room_id, on_invalidate=cache_context.invalidate
) )
if aliases: if aliases:
result["aliases"] = 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 room_id, on_invalidate=cache_context.invalidate
) )
@ -266,7 +263,7 @@ class RoomListHandler(BaseHandler):
# We're not in the room, so may as well bail out here. # We're not in the room, so may as well bail out here.
return result return result
event_map = yield self.store.get_events( event_map = await self.store.get_events(
[ [
event_id event_id
for key, event_id in current_state_ids.items() for key, event_id in current_state_ids.items()
@ -336,8 +333,7 @@ class RoomListHandler(BaseHandler):
return result return result
@defer.inlineCallbacks async def get_remote_public_room_list(
def get_remote_public_room_list(
self, self,
server_name, server_name,
limit=None, limit=None,
@ -356,7 +352,7 @@ class RoomListHandler(BaseHandler):
# to a locally-filtered search if we must. # to a locally-filtered search if we must.
try: try:
res = yield self._get_remote_list_cached( res = await self._get_remote_list_cached(
server_name, server_name,
limit=limit, limit=limit,
since_token=since_token, since_token=since_token,
@ -381,7 +377,7 @@ class RoomListHandler(BaseHandler):
limit = None limit = None
since_token = None since_token = None
res = yield self._get_remote_list_cached( res = await self._get_remote_list_cached(
server_name, server_name,
limit=limit, limit=limit,
since_token=since_token, since_token=since_token,
@ -400,7 +396,7 @@ class RoomListHandler(BaseHandler):
return res return res
def _get_remote_list_cached( async def _get_remote_list_cached(
self, self,
server_name, server_name,
limit=None, limit=None,
@ -412,7 +408,7 @@ class RoomListHandler(BaseHandler):
repl_layer = self.hs.get_federation_client() repl_layer = self.hs.get_federation_client()
if search_filter: if search_filter:
# We can't cache when asking for search # We can't cache when asking for search
return repl_layer.get_public_rooms( return await repl_layer.get_public_rooms(
server_name, server_name,
limit=limit, limit=limit,
since_token=since_token, since_token=since_token,
@ -428,7 +424,7 @@ class RoomListHandler(BaseHandler):
include_all_networks, include_all_networks,
third_party_instance_id, third_party_instance_id,
) )
return self.remote_response_cache.wrap( return await self.remote_response_cache.wrap(
key, key,
repl_layer.get_public_rooms, repl_layer.get_public_rooms,
server_name, server_name,