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

Commit

Permalink
Convert runWithConnection to async.
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep committed Aug 18, 2020
1 parent f40645e commit f4551ea
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
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.
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

0 comments on commit f4551ea

Please sign in to comment.