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

Commit 8857135

Browse files
committed
Merge commit 'a0acdfa9e' into anoa/dinsic_release_1_21_x
* commit 'a0acdfa9e': Converts event_federation and registration databases to async/await (#8061)
2 parents 04f61d9 + a0acdfa commit 8857135

File tree

9 files changed

+168
-227
lines changed

9 files changed

+168
-227
lines changed

changelog.d/8061.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Convert various parts of the codebase to async/await.

synapse/handlers/account_validity.py

+13-11
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
from email.mime.text import MIMEText
2121
from typing import List
2222

23-
from twisted.internet import defer
24-
2523
from synapse.api.errors import StoreError
2624
from synapse.logging.context import make_deferred_yieldable
2725
from synapse.metrics.background_process_metrics import run_as_background_process
@@ -90,12 +88,17 @@ def send_emails():
9088

9189
self.clock.looping_call(send_emails, 30 * 60 * 1000)
9290

93-
# If account_validity is enabled,check every hour to remove expired users from
94-
# the user directory
91+
# Mark users as inactive when they expired. Check once every hour
9592
if self._account_validity.enabled:
96-
self.clock.looping_call(
97-
self._mark_expired_users_as_inactive, 60 * 60 * 1000
98-
)
93+
94+
def mark_expired_users_as_inactive():
95+
# run as a background process to allow async functions to work
96+
return run_as_background_process(
97+
"_mark_expired_users_as_inactive",
98+
self._mark_expired_users_as_inactive,
99+
)
100+
101+
self.clock.looping_call(mark_expired_users_as_inactive, 60 * 60 * 1000)
99102

100103
async def _send_renewal_emails(self):
101104
"""Gets the list of users whose account is expiring in the amount of time
@@ -286,16 +289,15 @@ async def renew_account_for_user(
286289

287290
return expiration_ts
288291

289-
@defer.inlineCallbacks
290-
def _mark_expired_users_as_inactive(self):
292+
async def _mark_expired_users_as_inactive(self):
291293
"""Iterate over active, expired users. Mark them as inactive in order to hide them
292294
from the user directory.
293295
294296
Returns:
295297
Deferred
296298
"""
297299
# Get active, expired users
298-
active_expired_users = yield self.store.get_expired_users()
300+
active_expired_users = await self.store.get_expired_users()
299301

300302
# Mark each as non-active
301-
yield self.profile_handler.set_active(active_expired_users, False, True)
303+
await self.profile_handler.set_active(active_expired_users, False, True)

synapse/push/pusherpool.py

-16
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,6 @@ async def on_new_notifications(self, min_stream_id, max_stream_id):
192192

193193
for u in users_affected:
194194
if u in self.pushers:
195-
# Don't push if the user account has expired
196-
if self._account_validity.enabled:
197-
expired = await self.store.is_account_expired(
198-
u, self.clock.time_msec()
199-
)
200-
if expired:
201-
continue
202-
203195
for p in self.pushers[u].values():
204196
p.on_new_notifications(min_stream_id, max_stream_id)
205197

@@ -220,14 +212,6 @@ async def on_new_receipts(self, min_stream_id, max_stream_id, affected_room_ids)
220212

221213
for u in users_affected:
222214
if u in self.pushers:
223-
# Don't push if the user account has expired
224-
if self._account_validity.enabled:
225-
expired = yield self.store.is_account_expired(
226-
u, self.clock.time_msec()
227-
)
228-
if expired:
229-
continue
230-
231215
for p in self.pushers[u].values():
232216
p.on_new_receipts(min_stream_id, max_stream_id)
233217

synapse/storage/databases/main/event_federation.py

+13-25
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
import itertools
1616
import logging
1717
from queue import Empty, PriorityQueue
18-
from typing import Dict, List, Optional, Set, Tuple
19-
20-
from twisted.internet import defer
18+
from typing import Dict, Iterable, List, Optional, Set, Tuple
2119

2220
from synapse.api.errors import StoreError
2321
from synapse.metrics.background_process_metrics import run_as_background_process
@@ -286,17 +284,13 @@ def get_oldest_events_with_depth_in_room_txn(self, txn, room_id):
286284

287285
return dict(txn)
288286

289-
@defer.inlineCallbacks
290-
def get_max_depth_of(self, event_ids):
287+
async def get_max_depth_of(self, event_ids: List[str]) -> int:
291288
"""Returns the max depth of a set of event IDs
292289
293290
Args:
294-
event_ids (list[str])
295-
296-
Returns
297-
Deferred[int]
291+
event_ids: The event IDs to calculate the max depth of.
298292
"""
299-
rows = yield self.db_pool.simple_select_many_batch(
293+
rows = await self.db_pool.simple_select_many_batch(
300294
table="events",
301295
column="event_id",
302296
iterable=event_ids,
@@ -550,17 +544,16 @@ def _get_backfill_events(self, txn, room_id, event_list, limit):
550544

551545
return event_results
552546

553-
@defer.inlineCallbacks
554-
def get_missing_events(self, room_id, earliest_events, latest_events, limit):
555-
ids = yield self.db_pool.runInteraction(
547+
async def get_missing_events(self, room_id, earliest_events, latest_events, limit):
548+
ids = await self.db_pool.runInteraction(
556549
"get_missing_events",
557550
self._get_missing_events,
558551
room_id,
559552
earliest_events,
560553
latest_events,
561554
limit,
562555
)
563-
events = yield self.get_events_as_list(ids)
556+
events = await self.get_events_as_list(ids)
564557
return events
565558

566559
def _get_missing_events(self, txn, room_id, earliest_events, latest_events, limit):
@@ -595,17 +588,13 @@ def _get_missing_events(self, txn, room_id, earliest_events, latest_events, limi
595588
event_results.reverse()
596589
return event_results
597590

598-
@defer.inlineCallbacks
599-
def get_successor_events(self, event_ids):
591+
async def get_successor_events(self, event_ids: Iterable[str]) -> List[str]:
600592
"""Fetch all events that have the given events as a prev event
601593
602594
Args:
603-
event_ids (iterable[str])
604-
605-
Returns:
606-
Deferred[list[str]]
595+
event_ids: The events to use as the previous events.
607596
"""
608-
rows = yield self.db_pool.simple_select_many_batch(
597+
rows = await self.db_pool.simple_select_many_batch(
609598
table="event_edges",
610599
column="prev_event_id",
611600
iterable=event_ids,
@@ -674,8 +663,7 @@ def _clean_room_for_join_txn(self, txn, room_id):
674663
txn.execute(query, (room_id,))
675664
txn.call_after(self.get_latest_event_ids_in_room.invalidate, (room_id,))
676665

677-
@defer.inlineCallbacks
678-
def _background_delete_non_state_event_auth(self, progress, batch_size):
666+
async def _background_delete_non_state_event_auth(self, progress, batch_size):
679667
def delete_event_auth(txn):
680668
target_min_stream_id = progress.get("target_min_stream_id_inclusive")
681669
max_stream_id = progress.get("max_stream_id_exclusive")
@@ -714,12 +702,12 @@ def delete_event_auth(txn):
714702

715703
return min_stream_id >= target_min_stream_id
716704

717-
result = yield self.db_pool.runInteraction(
705+
result = await self.db_pool.runInteraction(
718706
self.EVENT_AUTH_STATE_ONLY, delete_event_auth
719707
)
720708

721709
if not result:
722-
yield self.db_pool.updates._end_background_update(
710+
await self.db_pool.updates._end_background_update(
723711
self.EVENT_AUTH_STATE_ONLY
724712
)
725713

0 commit comments

Comments
 (0)