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

Convert runWithConnection to async. #8121

Merged
merged 2 commits into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions changelog.d/8121.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Convert various parts of the codebase to async/await.
2 changes: 1 addition & 1 deletion synapse/metrics/background_process_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def run_as_background_process(desc: str, func, *args, **kwargs):
It returns a Deferred which completes when the function completes, but it doesn't
follow the synapse logcontext rules, which makes it appropriate for passing to
clock.looping_call and friends (or for firing-and-forgetting in the middle of a
normal synapse inlineCallbacks function).
normal synapse async function).

Args:
desc: a description for this background process type
Expand Down
27 changes: 13 additions & 14 deletions synapse/storage/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,14 +516,16 @@ def runInteraction(self, desc: str, func: Callable, *args: Any, **kwargs: Any):
logger.warning("Starting db txn '%s' from sentinel context", desc)

try:
result = yield self.runWithConnection(
self.new_transaction,
desc,
after_callbacks,
exception_callbacks,
func,
*args,
**kwargs
result = yield defer.ensureDeferred(
self.runWithConnection(
self.new_transaction,
desc,
after_callbacks,
exception_callbacks,
func,
*args,
**kwargs
)
)

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

return result

@defer.inlineCallbacks
def runWithConnection(self, func: Callable, *args: Any, **kwargs: Any):
async def runWithConnection(self, func: Callable, *args: Any, **kwargs: Any) -> Any:
"""Wraps the .runWithConnection() method on the underlying db_pool.

Arguments:
Expand All @@ -547,7 +548,7 @@ def runWithConnection(self, func: Callable, *args: Any, **kwargs: Any):
kwargs: named args to pass to `func`

Returns:
Deferred: The result of func
The result of func
"""
parent_context = current_context() # type: Optional[LoggingContextOrSentinel]
if not parent_context:
Expand All @@ -570,12 +571,10 @@ def inner_func(conn, *args, **kwargs):

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

result = yield make_deferred_yieldable(
return await make_deferred_yieldable(
self._db_pool.runWithConnection(inner_func, *args, **kwargs)
)

return result

@staticmethod
def cursor_to_dict(cursor):
"""Converts a SQL cursor into an list of dicts.
Expand Down