Skip to content

Commit

Permalink
remove non necessary lock
Browse files Browse the repository at this point in the history
  • Loading branch information
dmanchon committed Dec 15, 2023
1 parent e303675 commit 7ce2b73
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
12 changes: 8 additions & 4 deletions guillotina/db/storages/pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ class watch_lock(metrics.watch_lock):
def __init__(self, lock: asyncio.Lock, operation: str):
super().__init__(lock, PG_LOCK_ACQUIRE_TIME, labels={"type": operation})

class watch_nonlock(metrics.watch_nonlock):
def __init__(self, operation: str):
super().__init__(PG_LOCK_ACQUIRE_TIME, labels={"type": operation})


except ImportError:
watch = metrics.dummy_watch # type: ignore
Expand Down Expand Up @@ -967,7 +971,7 @@ async def store(self, oid, old_serial, writer, serialized, obj, txn):
update = True

conn = await txn.get_connection()
async with watch_lock(txn._lock, "store_object"):
async with watch_nonlock("store_object"):
try:
with watch("store_object"):
result = await conn.fetch(
Expand Down Expand Up @@ -1088,7 +1092,7 @@ async def start_transaction(self, txn, retries=0):
if txn._db_txn is not None:
return

async with watch_lock(txn._lock, "start_txn"):
async with watch_nonlock("start_txn"):
txn._db_txn = await self._async_db_transaction_factory(txn)

try:
Expand Down Expand Up @@ -1142,7 +1146,7 @@ async def get_conflicts(self, txn):
return await conn.fetch(sql, txn._tid)

async def commit(self, transaction):
async with watch_lock(transaction._lock, "commit_txn"):
async with watch_nonlock("commit_txn"):
if transaction._db_txn is not None:
with watch("commit_txn"):
await transaction._db_txn.commit()
Expand All @@ -1151,7 +1155,7 @@ async def commit(self, transaction):
return transaction._tid

async def abort(self, transaction):
async with watch_lock(transaction._lock, "rollback_txn"):
async with watch_nonlock("rollback_txn"):
if transaction._db_txn is not None:
try:
with watch("rollback_txn"):
Expand Down
25 changes: 25 additions & 0 deletions guillotina/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,28 @@ async def __aexit__(self, exc_type, exc, tb):
return

self.lock.release()


class watch_nonlock:
def __init__(
self,
histogram: Optional[Histogram] = None,
labels: Optional[Dict[str, str]] = None,
):
self.histogram = histogram
self.labels = labels or {}

async def __aenter__(self) -> None:
if self.histogram is None:
return

start = time.time()
finished = time.time()
if len(self.labels) > 0:
self.histogram.labels(**self.labels).observe(finished - start)
else:
self.histogram.observe(finished - start)

async def __aexit__(self, exc_type, exc, tb):
if self.histogram is None:
return

0 comments on commit 7ce2b73

Please sign in to comment.