Skip to content

Commit fa43e6b

Browse files
committed
Fix PR feedback
1 parent 2c9af66 commit fa43e6b

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

redis/asyncio/client.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -605,17 +605,24 @@ async def __aenter__(self: _RedisT) -> _RedisT:
605605
connection pool is only closed (via aclose()) when no context is using
606606
the client.
607607
"""
608-
async with self._usage_lock:
609-
self._usage_counter += 1
608+
await self._increment_usage()
610609
try:
611610
# Initialize the client (i.e. establish connection, etc.)
612611
return await self.initialize()
613612
except Exception:
614613
# If initialization fails, decrement the counter to keep it in sync
615-
async with self._usage_lock:
616-
self._usage_counter -= 1
614+
await self._decrement_usage()
617615
raise
618616

617+
async def _increment_usage(self) -> int:
618+
"""
619+
Helper coroutine to increment the usage counter while holding the lock.
620+
Returns the new value of the usage counter.
621+
"""
622+
async with self._usage_lock:
623+
self._usage_counter += 1
624+
return self._usage_counter
625+
619626
async def _decrement_usage(self) -> int:
620627
"""
621628
Helper coroutine to decrement the usage counter while holding the lock.

redis/asyncio/cluster.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -478,17 +478,24 @@ async def __aenter__(self) -> "RedisCluster":
478478
connection pool is only closed (via aclose()) when no context is using
479479
the client.
480480
"""
481-
async with self._usage_lock:
482-
self._usage_counter += 1
481+
await self._increment_usage()
483482
try:
484483
# Initialize the client (i.e. establish connection, etc.)
485484
return await self.initialize()
486485
except Exception:
487486
# If initialization fails, decrement the counter to keep it in sync
488-
async with self._usage_lock:
489-
self._usage_counter -= 1
487+
await self._decrement_usage()
490488
raise
491489

490+
async def _increment_usage(self) -> int:
491+
"""
492+
Helper coroutine to increment the usage counter while holding the lock.
493+
Returns the new value of the usage counter.
494+
"""
495+
async with self._usage_lock:
496+
self._usage_counter += 1
497+
return self._usage_counter
498+
492499
async def _decrement_usage(self) -> int:
493500
"""
494501
Helper coroutine to decrement the usage counter while holding the lock.

0 commit comments

Comments
 (0)