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

Commit d89692e

Browse files
authored
Convert runWithConnection to async. (#8121)
1 parent d294f0e commit d89692e

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

changelog.d/8121.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/metrics/background_process_metrics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def run_as_background_process(desc: str, func, *args, **kwargs):
175175
It returns a Deferred which completes when the function completes, but it doesn't
176176
follow the synapse logcontext rules, which makes it appropriate for passing to
177177
clock.looping_call and friends (or for firing-and-forgetting in the middle of a
178-
normal synapse inlineCallbacks function).
178+
normal synapse async function).
179179
180180
Args:
181181
desc: a description for this background process type

synapse/storage/database.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -516,14 +516,16 @@ def runInteraction(self, desc: str, func: Callable, *args: Any, **kwargs: Any):
516516
logger.warning("Starting db txn '%s' from sentinel context", desc)
517517

518518
try:
519-
result = yield self.runWithConnection(
520-
self.new_transaction,
521-
desc,
522-
after_callbacks,
523-
exception_callbacks,
524-
func,
525-
*args,
526-
**kwargs
519+
result = yield defer.ensureDeferred(
520+
self.runWithConnection(
521+
self.new_transaction,
522+
desc,
523+
after_callbacks,
524+
exception_callbacks,
525+
func,
526+
*args,
527+
**kwargs
528+
)
527529
)
528530

529531
for after_callback, after_args, after_kwargs in after_callbacks:
@@ -535,8 +537,7 @@ def runInteraction(self, desc: str, func: Callable, *args: Any, **kwargs: Any):
535537

536538
return result
537539

538-
@defer.inlineCallbacks
539-
def runWithConnection(self, func: Callable, *args: Any, **kwargs: Any):
540+
async def runWithConnection(self, func: Callable, *args: Any, **kwargs: Any) -> Any:
540541
"""Wraps the .runWithConnection() method on the underlying db_pool.
541542
542543
Arguments:
@@ -547,7 +548,7 @@ def runWithConnection(self, func: Callable, *args: Any, **kwargs: Any):
547548
kwargs: named args to pass to `func`
548549
549550
Returns:
550-
Deferred: The result of func
551+
The result of func
551552
"""
552553
parent_context = current_context() # type: Optional[LoggingContextOrSentinel]
553554
if not parent_context:
@@ -570,12 +571,10 @@ def inner_func(conn, *args, **kwargs):
570571

571572
return func(conn, *args, **kwargs)
572573

573-
result = yield make_deferred_yieldable(
574+
return await make_deferred_yieldable(
574575
self._db_pool.runWithConnection(inner_func, *args, **kwargs)
575576
)
576577

577-
return result
578-
579578
@staticmethod
580579
def cursor_to_dict(cursor):
581580
"""Converts a SQL cursor into an list of dicts.

0 commit comments

Comments
 (0)