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

Convert federation client to async/await. #7975

Merged
merged 9 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 9 additions & 7 deletions contrib/cmdclient/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,13 +609,15 @@ def do_stream(self, line):

@defer.inlineCallbacks
def _do_event_stream(self, timeout):
res = yield self.http_client.get_json(
self._url() + "/events",
{
"access_token": self._tok(),
"timeout": str(timeout),
"from": self.event_stream_token,
},
res = yield defer.ensureDeferred(
self.http_client.get_json(
self._url() + "/events",
{
"access_token": self._tok(),
"timeout": str(timeout),
"from": self.event_stream_token,
},
)
)
print(json.dumps(res, indent=4))

Expand Down
36 changes: 19 additions & 17 deletions synapse/crypto/keyring.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,23 +794,25 @@ def get_server_verify_key_v2_direct(self, server_name, key_ids):

time_now_ms = self.clock.time_msec()
try:
response = yield self.client.get_json(
destination=server_name,
path="/_matrix/key/v2/server/"
+ urllib.parse.quote(requested_key_id),
ignore_backoff=True,
# we only give the remote server 10s to respond. It should be an
# easy request to handle, so if it doesn't reply within 10s, it's
# probably not going to.
#
# Furthermore, when we are acting as a notary server, we cannot
# wait all day for all of the origin servers, as the requesting
# server will otherwise time out before we can respond.
#
# (Note that get_json may make 4 attempts, so this can still take
# almost 45 seconds to fetch the headers, plus up to another 60s to
# read the response).
timeout=10000,
response = yield defer.ensureDeferred(
self.client.get_json(
destination=server_name,
path="/_matrix/key/v2/server/"
+ urllib.parse.quote(requested_key_id),
ignore_backoff=True,
# we only give the remote server 10s to respond. It should be an
# easy request to handle, so if it doesn't reply within 10s, it's
# probably not going to.
#
# Furthermore, when we are acting as a notary server, we cannot
# wait all day for all of the origin servers, as the requesting
# server will otherwise time out before we can respond.
#
# (Note that get_json may make 4 attempts, so this can still take
# almost 45 seconds to fetch the headers, plus up to another 60s to
# read the response).
timeout=10000,
)
)
except (NotRetryingDestination, RequestSendFailed) as e:
# these both have str() representations which we can't really improve
Expand Down
18 changes: 8 additions & 10 deletions synapse/http/matrixfederationclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,8 +739,7 @@ async def post_json(
)
return body

@defer.inlineCallbacks
def get_json(
async def get_json(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

various things in TransportLayerClient that call this (backfill, get_event) have docstrings that claim to return Deferreds, which will no longer be true. It'd be nice to clean them up.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm...I thought I caught all those! I'll check again.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a few more of these, good call! 👍 Pushed an additional fix.

self,
destination,
path,
Expand Down Expand Up @@ -772,7 +771,7 @@ def get_json(
response we should try appending a trailing slash to the end of
the request. Workaround for #3622 in Synapse <= v0.99.3.
Returns:
Deferred[dict|list]: Succeeds when we get a 2xx HTTP response. The
dict|list: Succeeds when we get a 2xx HTTP response. The
result will be the decoded JSON body.

Raises:
Expand All @@ -789,7 +788,7 @@ def get_json(
method="GET", destination=destination, path=path, query=args
)

response = yield self._send_request_with_optional_trailing_slash(
response = await self._send_request_with_optional_trailing_slash(
request,
try_trailing_slash_on_400,
backoff_on_404=False,
Expand All @@ -798,14 +797,13 @@ def get_json(
timeout=timeout,
)

body = yield _handle_json_response(
body = await _handle_json_response(
self.reactor, self.default_timeout, request, response
)

return body

@defer.inlineCallbacks
def delete_json(
async def delete_json(
self,
destination,
path,
Expand Down Expand Up @@ -833,7 +831,7 @@ def delete_json(

args (dict): query params
Returns:
Deferred[dict|list]: Succeeds when we get a 2xx HTTP response. The
dict|list: Succeeds when we get a 2xx HTTP response. The
result will be the decoded JSON body.

Raises:
Expand All @@ -850,14 +848,14 @@ def delete_json(
method="DELETE", destination=destination, path=path, query=args
)

response = yield self._send_request(
response = await self._send_request(
request,
long_retries=long_retries,
timeout=timeout,
ignore_backoff=ignore_backoff,
)

body = yield _handle_json_response(
body = await _handle_json_response(
self.reactor, self.default_timeout, request, response
)
return body
Expand Down
2 changes: 1 addition & 1 deletion tests/crypto/test_keyring.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def test_get_keys_from_server(self):
}
signedjson.sign.sign_json(response, SERVER_NAME, testkey)

def get_json(destination, path, **kwargs):
async def get_json(destination, path, **kwargs):
self.assertEqual(destination, SERVER_NAME)
self.assertEqual(path, "/_matrix/key/v2/server/key1")
return response
Expand Down
21 changes: 11 additions & 10 deletions tests/federation/test_complexity.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from synapse.types import UserID

from tests import unittest
from tests.test_utils import make_awaitable


class RoomComplexityTests(unittest.FederatingHomeserverTestCase):
Expand Down Expand Up @@ -78,9 +79,9 @@ def test_join_too_large(self):
fed_transport = self.hs.get_federation_transport_client()

# Mock out some things, because we don't want to test the whole join
fed_transport.client.get_json = Mock(return_value=defer.succeed({"v1": 9999}))
fed_transport.client.get_json = Mock(return_value=make_awaitable({"v1": 9999}))
handler.federation_handler.do_invite_join = Mock(
return_value=defer.succeed(("", 1))
return_value=make_awaitable(("", 1))
)

d = handler._remote_join(
Expand Down Expand Up @@ -109,9 +110,9 @@ def test_join_too_large_admin(self):
fed_transport = self.hs.get_federation_transport_client()

# Mock out some things, because we don't want to test the whole join
fed_transport.client.get_json = Mock(return_value=defer.succeed({"v1": 9999}))
fed_transport.client.get_json = Mock(return_value=make_awaitable({"v1": 9999}))
handler.federation_handler.do_invite_join = Mock(
return_value=defer.succeed(("", 1))
return_value=make_awaitable(("", 1))
)

d = handler._remote_join(
Expand Down Expand Up @@ -147,9 +148,9 @@ def test_join_too_large_once_joined(self):
fed_transport = self.hs.get_federation_transport_client()

# Mock out some things, because we don't want to test the whole join
fed_transport.client.get_json = Mock(return_value=defer.succeed(None))
fed_transport.client.get_json = Mock(return_value=make_awaitable(None))
handler.federation_handler.do_invite_join = Mock(
return_value=defer.succeed(("", 1))
return_value=make_awaitable(("", 1))
)

# Artificially raise the complexity
Expand Down Expand Up @@ -203,9 +204,9 @@ def test_join_too_large_no_admin(self):
fed_transport = self.hs.get_federation_transport_client()

# Mock out some things, because we don't want to test the whole join
fed_transport.client.get_json = Mock(return_value=defer.succeed({"v1": 9999}))
fed_transport.client.get_json = Mock(return_value=make_awaitable({"v1": 9999}))
handler.federation_handler.do_invite_join = Mock(
return_value=defer.succeed(("", 1))
return_value=make_awaitable(("", 1))
)

d = handler._remote_join(
Expand Down Expand Up @@ -233,9 +234,9 @@ def test_join_too_large_admin(self):
fed_transport = self.hs.get_federation_transport_client()

# Mock out some things, because we don't want to test the whole join
fed_transport.client.get_json = Mock(return_value=defer.succeed({"v1": 9999}))
fed_transport.client.get_json = Mock(return_value=make_awaitable({"v1": 9999}))
handler.federation_handler.do_invite_join = Mock(
return_value=defer.succeed(("", 1))
return_value=make_awaitable(("", 1))
)

d = handler._remote_join(
Expand Down
32 changes: 23 additions & 9 deletions tests/http/test_fedclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ def test_client_get(self):
@defer.inlineCallbacks
def do_request():
with LoggingContext("one") as context:
fetch_d = self.cl.get_json("testserv:8008", "foo/bar")
fetch_d = defer.ensureDeferred(
self.cl.get_json("testserv:8008", "foo/bar")
)

# Nothing happened yet
self.assertNoResult(fetch_d)
Expand Down Expand Up @@ -120,15 +122,19 @@ def test_dns_error(self):
"""
If the DNS lookup returns an error, it will bubble up.
"""
d = self.cl.get_json("testserv2:8008", "foo/bar", timeout=10000)
d = defer.ensureDeferred(
self.cl.get_json("testserv2:8008", "foo/bar", timeout=10000)
)
self.pump()

f = self.failureResultOf(d)
self.assertIsInstance(f.value, RequestSendFailed)
self.assertIsInstance(f.value.inner_exception, DNSLookupError)

def test_client_connection_refused(self):
d = self.cl.get_json("testserv:8008", "foo/bar", timeout=10000)
d = defer.ensureDeferred(
self.cl.get_json("testserv:8008", "foo/bar", timeout=10000)
)

self.pump()

Expand All @@ -154,7 +160,9 @@ def test_client_never_connect(self):
If the HTTP request is not connected and is timed out, it'll give a
ConnectingCancelledError or TimeoutError.
"""
d = self.cl.get_json("testserv:8008", "foo/bar", timeout=10000)
d = defer.ensureDeferred(
self.cl.get_json("testserv:8008", "foo/bar", timeout=10000)
)

self.pump()

Expand Down Expand Up @@ -184,7 +192,9 @@ def test_client_connect_no_response(self):
If the HTTP request is connected, but gets no response before being
timed out, it'll give a ResponseNeverReceived.
"""
d = self.cl.get_json("testserv:8008", "foo/bar", timeout=10000)
d = defer.ensureDeferred(
self.cl.get_json("testserv:8008", "foo/bar", timeout=10000)
)

self.pump()

Expand Down Expand Up @@ -226,7 +236,7 @@ def test_client_ip_range_blacklist(self):
# Try making a GET request to a blacklisted IPv4 address
# ------------------------------------------------------
# Make the request
d = cl.get_json("internal:8008", "foo/bar", timeout=10000)
d = defer.ensureDeferred(cl.get_json("internal:8008", "foo/bar", timeout=10000))

# Nothing happened yet
self.assertNoResult(d)
Expand Down Expand Up @@ -346,7 +356,9 @@ def test_client_requires_trailing_slashes(self):
requiring a trailing slash. We need to retry the request with a
trailing slash. Workaround for Synapse <= v0.99.3, explained in #3622.
"""
d = self.cl.get_json("testserv:8008", "foo/bar", try_trailing_slash_on_400=True)
d = defer.ensureDeferred(
self.cl.get_json("testserv:8008", "foo/bar", try_trailing_slash_on_400=True)
)

# Send the request
self.pump()
Expand Down Expand Up @@ -399,7 +411,9 @@ def test_client_does_not_retry_on_400_plus(self):

See test_client_requires_trailing_slashes() for context.
"""
d = self.cl.get_json("testserv:8008", "foo/bar", try_trailing_slash_on_400=True)
d = defer.ensureDeferred(
self.cl.get_json("testserv:8008", "foo/bar", try_trailing_slash_on_400=True)
)

# Send the request
self.pump()
Expand Down Expand Up @@ -461,7 +475,7 @@ def test_client_sends_body(self):

def test_closes_connection(self):
"""Check that the client closes unused HTTP connections"""
d = self.cl.get_json("testserv:8008", "foo/bar")
d = defer.ensureDeferred(self.cl.get_json("testserv:8008", "foo/bar"))

self.pump()

Expand Down
2 changes: 1 addition & 1 deletion tests/rest/key/v2/test_remote_key_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def expect_outgoing_key_request(
Tell the mock http client to expect an outgoing GET request for the given key
"""

def get_json(destination, path, ignore_backoff=False, **kwargs):
async def get_json(destination, path, ignore_backoff=False, **kwargs):
self.assertTrue(ignore_backoff)
self.assertEqual(destination, server_name)
key_id = "%s:%s" % (signing_key.alg, signing_key.version)
Expand Down