File tree Expand file tree Collapse file tree 2 files changed +22
-8
lines changed Expand file tree Collapse file tree 2 files changed +22
-8
lines changed Original file line number Diff line number Diff line change @@ -605,17 +605,24 @@ async def __aenter__(self: _RedisT) -> _RedisT:
605
605
connection pool is only closed (via aclose()) when no context is using
606
606
the client.
607
607
"""
608
- async with self ._usage_lock :
609
- self ._usage_counter += 1
608
+ await self ._increment_usage ()
610
609
try :
611
610
# Initialize the client (i.e. establish connection, etc.)
612
611
return await self .initialize ()
613
612
except Exception :
614
613
# 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 ()
617
615
raise
618
616
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
+
619
626
async def _decrement_usage (self ) -> int :
620
627
"""
621
628
Helper coroutine to decrement the usage counter while holding the lock.
Original file line number Diff line number Diff line change @@ -478,17 +478,24 @@ async def __aenter__(self) -> "RedisCluster":
478
478
connection pool is only closed (via aclose()) when no context is using
479
479
the client.
480
480
"""
481
- async with self ._usage_lock :
482
- self ._usage_counter += 1
481
+ await self ._increment_usage ()
483
482
try :
484
483
# Initialize the client (i.e. establish connection, etc.)
485
484
return await self .initialize ()
486
485
except Exception :
487
486
# 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 ()
490
488
raise
491
489
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
+
492
499
async def _decrement_usage (self ) -> int :
493
500
"""
494
501
Helper coroutine to decrement the usage counter while holding the lock.
You can’t perform that action at this time.
0 commit comments