Skip to content

Commit

Permalink
adds request lock
Browse files Browse the repository at this point in the history
  • Loading branch information
David Erb committed Jun 10, 2023
1 parent 9c3cc31 commit 00006c7
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions src/xchembku_lib/datafaces/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async def activate_coro(self):

# Use a lock around all transaction-based requests.
# TODO: Remove aiohttp transaction lock and instead use connection pool.
self.__transaction_lock = asyncio.Lock()
self.__request_lock = asyncio.Lock()

# Get the local implementation started.
await self.__actual_dataface.start()
Expand All @@ -111,10 +111,9 @@ async def direct_shutdown(self):
await self.__actual_dataface.disconnect()

except Exception as exception:
raise RuntimeError(
logger.warning(
callsign(
self,
explain(exception, "disconnecting local xchembku_dataface"),
self, explain(exception, "disconnecting actual xchembku_dataface")
)
)

Expand All @@ -132,29 +131,30 @@ async def __do_actually(self, function, args, kwargs):
# Get the function which the caller wants executed.
function = getattr(self.__actual_dataface, function)

# Caller wants the function wrapped in a transaction?
if "as_transaction" in kwargs:
as_transaction = kwargs["as_transaction"]
# Take the keyword out of the kwargs because the functions don't have it.
kwargs.pop("as_transaction")
else:
as_transaction = False
# Lock out all other requests from running at the same time.
async with self.__request_lock:

# Caller wants the function wrapped in a transaction?
if "as_transaction" in kwargs:
as_transaction = kwargs["as_transaction"]
# Take the keyword out of the kwargs because the functions don't have it.
kwargs.pop("as_transaction")
else:
as_transaction = False

if as_transaction:
# Make sure we have an actual connection.
await self.__actual_dataface.establish_database_connection()
if as_transaction:
# Make sure we have an actual connection.
await self.__actual_dataface.establish_database_connection()

# Lock out all other requests from running their own transaction.
async with self.__transaction_lock:
try:
await self.__actual_dataface.begin()
response = await function(*args, **kwargs)
await self.__actual_dataface.commit()
except Exception:
await self.__actual_dataface.rollback()
raise
else:
response = await function(*args, **kwargs)
else:
response = await function(*args, **kwargs)

return response

Expand Down

0 comments on commit 00006c7

Please sign in to comment.