Skip to content
Snippets Groups Projects
Unverified Commit 912e0249 authored by Patrick Cloke's avatar Patrick Cloke Committed by GitHub
Browse files

Convert runInteraction to async/await (#8156)

parent 112266ea
No related branches found
No related tags found
No related merge requests found
Convert various parts of the codebase to async/await.
...@@ -28,6 +28,7 @@ from typing import ( ...@@ -28,6 +28,7 @@ from typing import (
Optional, Optional,
Tuple, Tuple,
TypeVar, TypeVar,
cast,
overload, overload,
) )
...@@ -35,7 +36,6 @@ from prometheus_client import Histogram ...@@ -35,7 +36,6 @@ from prometheus_client import Histogram
from typing_extensions import Literal from typing_extensions import Literal
from twisted.enterprise import adbapi from twisted.enterprise import adbapi
from twisted.internet import defer
from synapse.api.errors import StoreError from synapse.api.errors import StoreError
from synapse.config.database import DatabaseConnectionConfig from synapse.config.database import DatabaseConnectionConfig
...@@ -507,8 +507,9 @@ class DatabasePool(object): ...@@ -507,8 +507,9 @@ class DatabasePool(object):
self._txn_perf_counters.update(desc, duration) self._txn_perf_counters.update(desc, duration)
sql_txn_timer.labels(desc).observe(duration) sql_txn_timer.labels(desc).observe(duration)
@defer.inlineCallbacks async def runInteraction(
def runInteraction(self, desc: str, func: Callable, *args: Any, **kwargs: Any): self, desc: str, func: "Callable[..., R]", *args: Any, **kwargs: Any
) -> R:
"""Starts a transaction on the database and runs a given function """Starts a transaction on the database and runs a given function
Arguments: Arguments:
...@@ -521,7 +522,7 @@ class DatabasePool(object): ...@@ -521,7 +522,7 @@ class DatabasePool(object):
kwargs: named args to pass to `func` kwargs: named args to pass to `func`
Returns: Returns:
Deferred: The result of func The result of func
""" """
after_callbacks = [] # type: List[_CallbackListEntry] after_callbacks = [] # type: List[_CallbackListEntry]
exception_callbacks = [] # type: List[_CallbackListEntry] exception_callbacks = [] # type: List[_CallbackListEntry]
...@@ -530,16 +531,14 @@ class DatabasePool(object): ...@@ -530,16 +531,14 @@ class DatabasePool(object):
logger.warning("Starting db txn '%s' from sentinel context", desc) logger.warning("Starting db txn '%s' from sentinel context", desc)
try: try:
result = yield defer.ensureDeferred( result = await self.runWithConnection(
self.runWithConnection( self.new_transaction,
self.new_transaction, desc,
desc, after_callbacks,
after_callbacks, exception_callbacks,
exception_callbacks, func,
func, *args,
*args, **kwargs
**kwargs
)
) )
for after_callback, after_args, after_kwargs in after_callbacks: for after_callback, after_args, after_kwargs in after_callbacks:
...@@ -549,7 +548,7 @@ class DatabasePool(object): ...@@ -549,7 +548,7 @@ class DatabasePool(object):
after_callback(*after_args, **after_kwargs) after_callback(*after_args, **after_kwargs)
raise raise
return result return cast(R, result)
async def runWithConnection( async def runWithConnection(
self, func: "Callable[..., R]", *args: Any, **kwargs: Any self, func: "Callable[..., R]", *args: Any, **kwargs: Any
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment