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

Commit 4a739c7

Browse files
authored
Convert simple_update* and simple_select* to async (#8173)
1 parent a466b67 commit 4a739c7

19 files changed

+164
-133
lines changed

changelog.d/8173.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.

synapse/handlers/room.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
create_requester,
5252
)
5353
from synapse.util import stringutils
54-
from synapse.util.async_helpers import Linearizer, maybe_awaitable
54+
from synapse.util.async_helpers import Linearizer
5555
from synapse.util.caches.response_cache import ResponseCache
5656
from synapse.visibility import filter_events_for_client
5757

@@ -1329,9 +1329,7 @@ async def shutdown_room(
13291329
ratelimit=False,
13301330
)
13311331

1332-
aliases_for_room = await maybe_awaitable(
1333-
self.store.get_aliases_for_room(room_id)
1334-
)
1332+
aliases_for_room = await self.store.get_aliases_for_room(room_id)
13351333

13361334
await self.store.update_aliases_for_room(
13371335
room_id, new_room_id, requester_user_id

synapse/storage/database.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,13 +1132,13 @@ def simple_select_onecol_txn(
11321132

11331133
return [r[0] for r in txn]
11341134

1135-
def simple_select_onecol(
1135+
async def simple_select_onecol(
11361136
self,
11371137
table: str,
11381138
keyvalues: Optional[Dict[str, Any]],
11391139
retcol: str,
11401140
desc: str = "simple_select_onecol",
1141-
) -> defer.Deferred:
1141+
) -> List[Any]:
11421142
"""Executes a SELECT query on the named table, which returns a list
11431143
comprising of the values of the named column from the selected rows.
11441144
@@ -1148,19 +1148,19 @@ def simple_select_onecol(
11481148
retcol: column whos value we wish to retrieve.
11491149
11501150
Returns:
1151-
Deferred: Results in a list
1151+
Results in a list
11521152
"""
1153-
return self.runInteraction(
1153+
return await self.runInteraction(
11541154
desc, self.simple_select_onecol_txn, table, keyvalues, retcol
11551155
)
11561156

1157-
def simple_select_list(
1157+
async def simple_select_list(
11581158
self,
11591159
table: str,
11601160
keyvalues: Optional[Dict[str, Any]],
11611161
retcols: Iterable[str],
11621162
desc: str = "simple_select_list",
1163-
) -> defer.Deferred:
1163+
) -> List[Dict[str, Any]]:
11641164
"""Executes a SELECT query on the named table, which may return zero or
11651165
more rows, returning the result as a list of dicts.
11661166
@@ -1170,10 +1170,11 @@ def simple_select_list(
11701170
column names and values to select the rows with, or None to not
11711171
apply a WHERE clause.
11721172
retcols: the names of the columns to return
1173+
11731174
Returns:
1174-
defer.Deferred: resolves to list[dict[str, Any]]
1175+
A list of dictionaries.
11751176
"""
1176-
return self.runInteraction(
1177+
return await self.runInteraction(
11771178
desc, self.simple_select_list_txn, table, keyvalues, retcols
11781179
)
11791180

@@ -1299,14 +1300,14 @@ def simple_select_many_txn(
12991300
txn.execute(sql, values)
13001301
return cls.cursor_to_dict(txn)
13011302

1302-
def simple_update(
1303+
async def simple_update(
13031304
self,
13041305
table: str,
13051306
keyvalues: Dict[str, Any],
13061307
updatevalues: Dict[str, Any],
13071308
desc: str,
1308-
) -> defer.Deferred:
1309-
return self.runInteraction(
1309+
) -> int:
1310+
return await self.runInteraction(
13101311
desc, self.simple_update_txn, table, keyvalues, updatevalues
13111312
)
13121313

@@ -1332,13 +1333,13 @@ def simple_update_txn(
13321333

13331334
return txn.rowcount
13341335

1335-
def simple_update_one(
1336+
async def simple_update_one(
13361337
self,
13371338
table: str,
13381339
keyvalues: Dict[str, Any],
13391340
updatevalues: Dict[str, Any],
13401341
desc: str = "simple_update_one",
1341-
) -> defer.Deferred:
1342+
) -> None:
13421343
"""Executes an UPDATE query on the named table, setting new values for
13431344
columns in a row matching the key values.
13441345
@@ -1347,7 +1348,7 @@ def simple_update_one(
13471348
keyvalues: dict of column names and values to select the row with
13481349
updatevalues: dict giving column names and values to update
13491350
"""
1350-
return self.runInteraction(
1351+
await self.runInteraction(
13511352
desc, self.simple_update_one_txn, table, keyvalues, updatevalues
13521353
)
13531354

synapse/storage/databases/main/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import calendar
1919
import logging
2020
import time
21+
from typing import Any, Dict, List
2122

2223
from synapse.api.constants import PresenceState
2324
from synapse.config.homeserver import HomeServerConfig
@@ -476,14 +477,13 @@ def _generate_user_daily_visits(txn):
476477
"generate_user_daily_visits", _generate_user_daily_visits
477478
)
478479

479-
def get_users(self):
480+
async def get_users(self) -> List[Dict[str, Any]]:
480481
"""Function to retrieve a list of users in users table.
481482
482-
Args:
483483
Returns:
484-
defer.Deferred: resolves to list[dict[str, Any]]
484+
A list of dictionaries representing users.
485485
"""
486-
return self.db_pool.simple_select_list(
486+
return await self.db_pool.simple_select_list(
487487
table="users",
488488
keyvalues={},
489489
retcols=[

synapse/storage/databases/main/directory.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# limitations under the License.
1515

1616
from collections import namedtuple
17-
from typing import Iterable, Optional
17+
from typing import Iterable, List, Optional
1818

1919
from synapse.api.errors import SynapseError
2020
from synapse.storage._base import SQLBaseStore
@@ -68,8 +68,8 @@ async def get_room_alias_creator(self, room_alias: str) -> str:
6868
)
6969

7070
@cached(max_entries=5000)
71-
def get_aliases_for_room(self, room_id):
72-
return self.db_pool.simple_select_onecol(
71+
async def get_aliases_for_room(self, room_id: str) -> List[str]:
72+
return await self.db_pool.simple_select_onecol(
7373
"room_aliases",
7474
{"room_id": room_id},
7575
"room_alias",

synapse/storage/databases/main/e2e_room_keys.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
from typing import Optional
18+
1719
from synapse.api.errors import StoreError
1820
from synapse.logging.opentracing import log_kv, trace
1921
from synapse.storage._base import SQLBaseStore, db_to_json
@@ -368,18 +370,22 @@ def _create_e2e_room_keys_version_txn(txn):
368370
)
369371

370372
@trace
371-
def update_e2e_room_keys_version(
372-
self, user_id, version, info=None, version_etag=None
373-
):
373+
async def update_e2e_room_keys_version(
374+
self,
375+
user_id: str,
376+
version: str,
377+
info: Optional[dict] = None,
378+
version_etag: Optional[int] = None,
379+
) -> None:
374380
"""Update a given backup version
375381
376382
Args:
377-
user_id(str): the user whose backup version we're updating
378-
version(str): the version ID of the backup version we're updating
379-
info (dict): the new backup version info to store. If None, then
380-
the backup version info is not updated
381-
version_etag (Optional[int]): etag of the keys in the backup. If
382-
None, then the etag is not updated
383+
user_id: the user whose backup version we're updating
384+
version: the version ID of the backup version we're updating
385+
info: the new backup version info to store. If None, then the backup
386+
version info is not updated.
387+
version_etag: etag of the keys in the backup. If None, then the etag
388+
is not updated.
383389
"""
384390
updatevalues = {}
385391

@@ -389,7 +395,7 @@ def update_e2e_room_keys_version(
389395
updatevalues["etag"] = version_etag
390396

391397
if updatevalues:
392-
return self.db_pool.simple_update(
398+
await self.db_pool.simple_update(
393399
table="e2e_room_keys_versions",
394400
keyvalues={"user_id": user_id, "version": version},
395401
updatevalues=updatevalues,

synapse/storage/databases/main/event_federation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,8 @@ def _get_rooms_with_many_extremities_txn(txn):
368368
)
369369

370370
@cached(max_entries=5000, iterable=True)
371-
def get_latest_event_ids_in_room(self, room_id):
372-
return self.db_pool.simple_select_onecol(
371+
async def get_latest_event_ids_in_room(self, room_id: str) -> List[str]:
372+
return await self.db_pool.simple_select_onecol(
373373
table="event_forward_extremities",
374374
keyvalues={"room_id": room_id},
375375
retcol="event_id",

synapse/storage/databases/main/group_server.py

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,26 @@ async def get_group(self, group_id: str) -> Optional[Dict[str, Any]]:
4444
desc="get_group",
4545
)
4646

47-
def get_users_in_group(self, group_id, include_private=False):
47+
async def get_users_in_group(
48+
self, group_id: str, include_private: bool = False
49+
) -> List[Dict[str, Any]]:
4850
# TODO: Pagination
4951

5052
keyvalues = {"group_id": group_id}
5153
if not include_private:
5254
keyvalues["is_public"] = True
5355

54-
return self.db_pool.simple_select_list(
56+
return await self.db_pool.simple_select_list(
5557
table="group_users",
5658
keyvalues=keyvalues,
5759
retcols=("user_id", "is_public", "is_admin"),
5860
desc="get_users_in_group",
5961
)
6062

61-
def get_invited_users_in_group(self, group_id):
63+
async def get_invited_users_in_group(self, group_id: str) -> List[str]:
6264
# TODO: Pagination
6365

64-
return self.db_pool.simple_select_onecol(
66+
return await self.db_pool.simple_select_onecol(
6567
table="group_invites",
6668
keyvalues={"group_id": group_id},
6769
retcol="user_id",
@@ -265,15 +267,14 @@ async def get_group_role(self, group_id, role_id):
265267

266268
return role
267269

268-
def get_local_groups_for_room(self, room_id):
270+
async def get_local_groups_for_room(self, room_id: str) -> List[str]:
269271
"""Get all of the local group that contain a given room
270272
Args:
271-
room_id (str): The ID of a room
273+
room_id: The ID of a room
272274
Returns:
273-
Deferred[list[str]]: A twisted.Deferred containing a list of group ids
274-
containing this room
275+
A list of group ids containing this room
275276
"""
276-
return self.db_pool.simple_select_onecol(
277+
return await self.db_pool.simple_select_onecol(
277278
table="group_rooms",
278279
keyvalues={"room_id": room_id},
279280
retcol="group_id",
@@ -422,10 +423,10 @@ def _get_users_membership_in_group_txn(txn):
422423
"get_users_membership_info_in_group", _get_users_membership_in_group_txn
423424
)
424425

425-
def get_publicised_groups_for_user(self, user_id):
426+
async def get_publicised_groups_for_user(self, user_id: str) -> List[str]:
426427
"""Get all groups a user is publicising
427428
"""
428-
return self.db_pool.simple_select_onecol(
429+
return await self.db_pool.simple_select_onecol(
429430
table="local_group_membership",
430431
keyvalues={"user_id": user_id, "membership": "join", "is_publicised": True},
431432
retcol="group_id",
@@ -466,8 +467,8 @@ async def get_remote_attestation(self, group_id, user_id):
466467

467468
return None
468469

469-
def get_joined_groups(self, user_id):
470-
return self.db_pool.simple_select_onecol(
470+
async def get_joined_groups(self, user_id: str) -> List[str]:
471+
return await self.db_pool.simple_select_onecol(
471472
table="local_group_membership",
472473
keyvalues={"user_id": user_id, "membership": "join"},
473474
retcol="group_id",
@@ -585,14 +586,14 @@ def _get_all_groups_changes_txn(txn):
585586

586587

587588
class GroupServerStore(GroupServerWorkerStore):
588-
def set_group_join_policy(self, group_id, join_policy):
589+
async def set_group_join_policy(self, group_id: str, join_policy: str) -> None:
589590
"""Set the join policy of a group.
590591
591592
join_policy can be one of:
592593
* "invite"
593594
* "open"
594595
"""
595-
return self.db_pool.simple_update_one(
596+
await self.db_pool.simple_update_one(
596597
table="groups",
597598
keyvalues={"group_id": group_id},
598599
updatevalues={"join_policy": join_policy},
@@ -1050,8 +1051,10 @@ def add_room_to_group(self, group_id, room_id, is_public):
10501051
desc="add_room_to_group",
10511052
)
10521053

1053-
def update_room_in_group_visibility(self, group_id, room_id, is_public):
1054-
return self.db_pool.simple_update(
1054+
async def update_room_in_group_visibility(
1055+
self, group_id: str, room_id: str, is_public: bool
1056+
) -> int:
1057+
return await self.db_pool.simple_update(
10551058
table="group_rooms",
10561059
keyvalues={"group_id": group_id, "room_id": room_id},
10571060
updatevalues={"is_public": is_public},
@@ -1076,10 +1079,12 @@ def _remove_room_from_group_txn(txn):
10761079
"remove_room_from_group", _remove_room_from_group_txn
10771080
)
10781081

1079-
def update_group_publicity(self, group_id, user_id, publicise):
1082+
async def update_group_publicity(
1083+
self, group_id: str, user_id: str, publicise: bool
1084+
) -> None:
10801085
"""Update whether the user is publicising their membership of the group
10811086
"""
1082-
return self.db_pool.simple_update_one(
1087+
await self.db_pool.simple_update_one(
10831088
table="local_group_membership",
10841089
keyvalues={"group_id": group_id, "user_id": user_id},
10851090
updatevalues={"is_publicised": publicise},
@@ -1218,20 +1223,24 @@ async def update_group_profile(self, group_id, profile):
12181223
desc="update_group_profile",
12191224
)
12201225

1221-
def update_attestation_renewal(self, group_id, user_id, attestation):
1226+
async def update_attestation_renewal(
1227+
self, group_id: str, user_id: str, attestation: dict
1228+
) -> None:
12221229
"""Update an attestation that we have renewed
12231230
"""
1224-
return self.db_pool.simple_update_one(
1231+
await self.db_pool.simple_update_one(
12251232
table="group_attestations_renewals",
12261233
keyvalues={"group_id": group_id, "user_id": user_id},
12271234
updatevalues={"valid_until_ms": attestation["valid_until_ms"]},
12281235
desc="update_attestation_renewal",
12291236
)
12301237

1231-
def update_remote_attestion(self, group_id, user_id, attestation):
1238+
async def update_remote_attestion(
1239+
self, group_id: str, user_id: str, attestation: dict
1240+
) -> None:
12321241
"""Update an attestation that a remote has renewed
12331242
"""
1234-
return self.db_pool.simple_update_one(
1243+
await self.db_pool.simple_update_one(
12351244
table="group_attestations_remote",
12361245
keyvalues={"group_id": group_id, "user_id": user_id},
12371246
updatevalues={

0 commit comments

Comments
 (0)