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

Commit

Permalink
Merge commit '912e02491' into anoa/dinsic_release_1_21_x
Browse files Browse the repository at this point in the history
* commit '912e02491':
  Convert runInteraction to async/await (#8156)
  • Loading branch information
anoadragon453 committed Oct 20, 2020
2 parents d7fd0de + 912e024 commit 2df215d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
1 change: 1 addition & 0 deletions changelog.d/8156.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Convert various parts of the codebase to async/await.
29 changes: 14 additions & 15 deletions synapse/storage/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
Optional,
Tuple,
TypeVar,
cast,
overload,
)

from prometheus_client import Histogram
from typing_extensions import Literal

from twisted.enterprise import adbapi
from twisted.internet import defer

from synapse.api.errors import StoreError
from synapse.config.database import DatabaseConnectionConfig
Expand Down Expand Up @@ -507,8 +507,9 @@ def new_transaction(
self._txn_perf_counters.update(desc, duration)
sql_txn_timer.labels(desc).observe(duration)

@defer.inlineCallbacks
def runInteraction(self, desc: str, func: Callable, *args: Any, **kwargs: Any):
async def runInteraction(
self, desc: str, func: "Callable[..., R]", *args: Any, **kwargs: Any
) -> R:
"""Starts a transaction on the database and runs a given function
Arguments:
Expand All @@ -521,7 +522,7 @@ def runInteraction(self, desc: str, func: Callable, *args: Any, **kwargs: Any):
kwargs: named args to pass to `func`
Returns:
Deferred: The result of func
The result of func
"""
after_callbacks = [] # type: List[_CallbackListEntry]
exception_callbacks = [] # type: List[_CallbackListEntry]
Expand All @@ -530,16 +531,14 @@ def runInteraction(self, desc: str, func: Callable, *args: Any, **kwargs: Any):
logger.warning("Starting db txn '%s' from sentinel context", desc)

try:
result = yield defer.ensureDeferred(
self.runWithConnection(
self.new_transaction,
desc,
after_callbacks,
exception_callbacks,
func,
*args,
**kwargs
)
result = await 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 @@ -549,7 +548,7 @@ def runInteraction(self, desc: str, func: Callable, *args: Any, **kwargs: Any):
after_callback(*after_args, **after_kwargs)
raise

return result
return cast(R, result)

async def runWithConnection(
self, func: "Callable[..., R]", *args: Any, **kwargs: Any
Expand Down

0 comments on commit 2df215d

Please sign in to comment.