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

Commit 4ae35a3

Browse files
committed
Merge commit 'c978f6c45' into anoa/dinsic_release_1_21_x
* commit 'c978f6c45': Convert federation client to async/await. (#7975)
2 parents 6d02e38 + c978f6c commit 4ae35a3

File tree

18 files changed

+209
-221
lines changed

18 files changed

+209
-221
lines changed

changelog.d/7975.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Convert various parts of the codebase to async/await.

contrib/cmdclient/console.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -609,13 +609,15 @@ def do_stream(self, line):
609609

610610
@defer.inlineCallbacks
611611
def _do_event_stream(self, timeout):
612-
res = yield self.http_client.get_json(
613-
self._url() + "/events",
614-
{
615-
"access_token": self._tok(),
616-
"timeout": str(timeout),
617-
"from": self.event_stream_token,
618-
},
612+
res = yield defer.ensureDeferred(
613+
self.http_client.get_json(
614+
self._url() + "/events",
615+
{
616+
"access_token": self._tok(),
617+
"timeout": str(timeout),
618+
"from": self.event_stream_token,
619+
},
620+
)
619621
)
620622
print(json.dumps(res, indent=4))
621623

synapse/crypto/keyring.py

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -632,18 +632,20 @@ def get_server_verify_key_v2_indirect(self, keys_to_fetch, key_server):
632632
)
633633

634634
try:
635-
query_response = yield self.client.post_json(
636-
destination=perspective_name,
637-
path="/_matrix/key/v2/query",
638-
data={
639-
"server_keys": {
640-
server_name: {
641-
key_id: {"minimum_valid_until_ts": min_valid_ts}
642-
for key_id, min_valid_ts in server_keys.items()
635+
query_response = yield defer.ensureDeferred(
636+
self.client.post_json(
637+
destination=perspective_name,
638+
path="/_matrix/key/v2/query",
639+
data={
640+
"server_keys": {
641+
server_name: {
642+
key_id: {"minimum_valid_until_ts": min_valid_ts}
643+
for key_id, min_valid_ts in server_keys.items()
644+
}
645+
for server_name, server_keys in keys_to_fetch.items()
643646
}
644-
for server_name, server_keys in keys_to_fetch.items()
645-
}
646-
},
647+
},
648+
)
647649
)
648650
except (NotRetryingDestination, RequestSendFailed) as e:
649651
# these both have str() representations which we can't really improve upon
@@ -792,23 +794,25 @@ def get_server_verify_key_v2_direct(self, server_name, key_ids):
792794

793795
time_now_ms = self.clock.time_msec()
794796
try:
795-
response = yield self.client.get_json(
796-
destination=server_name,
797-
path="/_matrix/key/v2/server/"
798-
+ urllib.parse.quote(requested_key_id),
799-
ignore_backoff=True,
800-
# we only give the remote server 10s to respond. It should be an
801-
# easy request to handle, so if it doesn't reply within 10s, it's
802-
# probably not going to.
803-
#
804-
# Furthermore, when we are acting as a notary server, we cannot
805-
# wait all day for all of the origin servers, as the requesting
806-
# server will otherwise time out before we can respond.
807-
#
808-
# (Note that get_json may make 4 attempts, so this can still take
809-
# almost 45 seconds to fetch the headers, plus up to another 60s to
810-
# read the response).
811-
timeout=10000,
797+
response = yield defer.ensureDeferred(
798+
self.client.get_json(
799+
destination=server_name,
800+
path="/_matrix/key/v2/server/"
801+
+ urllib.parse.quote(requested_key_id),
802+
ignore_backoff=True,
803+
# we only give the remote server 10s to respond. It should be an
804+
# easy request to handle, so if it doesn't reply within 10s, it's
805+
# probably not going to.
806+
#
807+
# Furthermore, when we are acting as a notary server, we cannot
808+
# wait all day for all of the origin servers, as the requesting
809+
# server will otherwise time out before we can respond.
810+
#
811+
# (Note that get_json may make 4 attempts, so this can still take
812+
# almost 45 seconds to fetch the headers, plus up to another 60s to
813+
# read the response).
814+
timeout=10000,
815+
)
812816
)
813817
except (NotRetryingDestination, RequestSendFailed) as e:
814818
# these both have str() representations which we can't really improve

synapse/federation/federation_client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def make_query(
135135
and try the request anyway.
136136
137137
Returns:
138-
a Deferred which will eventually yield a JSON object from the
138+
a Awaitable which will eventually yield a JSON object from the
139139
response
140140
"""
141141
sent_queries_counter.labels(query_type).inc()
@@ -157,7 +157,7 @@ def query_client_keys(self, destination, content, timeout):
157157
content (dict): The query content.
158158
159159
Returns:
160-
a Deferred which will eventually yield a JSON object from the
160+
an Awaitable which will eventually yield a JSON object from the
161161
response
162162
"""
163163
sent_queries_counter.labels("client_device_keys").inc()
@@ -180,7 +180,7 @@ def claim_client_keys(self, destination, content, timeout):
180180
content (dict): The query content.
181181
182182
Returns:
183-
a Deferred which will eventually yield a JSON object from the
183+
an Awaitable which will eventually yield a JSON object from the
184184
response
185185
"""
186186
sent_queries_counter.labels("client_one_time_keys").inc()
@@ -900,7 +900,7 @@ def get_public_rooms(
900900
party instance
901901
902902
Returns:
903-
Deferred[Dict[str, Any]]: The response from the remote server, or None if
903+
Awaitable[Dict[str, Any]]: The response from the remote server, or None if
904904
`remote_server` is the same as the local server_name
905905
906906
Raises:

synapse/federation/sender/__init__.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,7 @@ def _send_pdu(self, pdu: EventBase, destinations: Iterable[str]) -> None:
288288
for destination in destinations:
289289
self._get_per_destination_queue(destination).send_pdu(pdu, order)
290290

291-
@defer.inlineCallbacks
292-
def send_read_receipt(self, receipt: ReadReceipt):
291+
async def send_read_receipt(self, receipt: ReadReceipt) -> None:
293292
"""Send a RR to any other servers in the room
294293
295294
Args:
@@ -330,9 +329,7 @@ def send_read_receipt(self, receipt: ReadReceipt):
330329
room_id = receipt.room_id
331330

332331
# Work out which remote servers should be poked and poke them.
333-
domains = yield defer.ensureDeferred(
334-
self.state.get_current_hosts_in_room(room_id)
335-
)
332+
domains = await self.state.get_current_hosts_in_room(room_id)
336333
domains = [
337334
d
338335
for d in domains
@@ -387,8 +384,7 @@ def _flush_rrs_for_room(self, room_id: str) -> None:
387384
queue.flush_read_receipts_for_room(room_id)
388385

389386
@preserve_fn # the caller should not yield on this
390-
@defer.inlineCallbacks
391-
def send_presence(self, states: List[UserPresenceState]):
387+
async def send_presence(self, states: List[UserPresenceState]):
392388
"""Send the new presence states to the appropriate destinations.
393389
394390
This actually queues up the presence states ready for sending and
@@ -423,7 +419,7 @@ def send_presence(self, states: List[UserPresenceState]):
423419
if not states_map:
424420
break
425421

426-
yield self._process_presence_inner(list(states_map.values()))
422+
await self._process_presence_inner(list(states_map.values()))
427423
except Exception:
428424
logger.exception("Error sending presence states to servers")
429425
finally:
@@ -450,14 +446,11 @@ def send_presence_to_destinations(
450446
self._get_per_destination_queue(destination).send_presence(states)
451447

452448
@measure_func("txnqueue._process_presence")
453-
@defer.inlineCallbacks
454-
def _process_presence_inner(self, states: List[UserPresenceState]):
449+
async def _process_presence_inner(self, states: List[UserPresenceState]):
455450
"""Given a list of states populate self.pending_presence_by_dest and
456451
poke to send a new transaction to each destination
457452
"""
458-
hosts_and_states = yield defer.ensureDeferred(
459-
get_interested_remotes(self.store, states, self.state)
460-
)
453+
hosts_and_states = await get_interested_remotes(self.store, states, self.state)
461454

462455
for destinations, states in hosts_and_states:
463456
for destination in destinations:

0 commit comments

Comments
 (0)