Skip to content

Commit

Permalink
Refactor how asyncio.BlockingConnectionPool gets connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
kristjanvalur committed Oct 11, 2023
1 parent 5391c5f commit 48eb0b4
Showing 1 changed file with 2 additions and 10 deletions.
12 changes: 2 additions & 10 deletions redis/asyncio/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1167,24 +1167,16 @@ def __init__(
self._condition = asyncio.Condition()
self.timeout = timeout

async def get_connection(self, command_name, *keys, **options):
async def get_available_connection(self):
"""Gets a connection from the pool, blocking until one is available"""
try:
async with self._condition:
async with async_timeout(self.timeout):
await self._condition.wait_for(self.can_get_connection)
connection = super().get_available_connection()
return super().get_available_connection()
except asyncio.TimeoutError as err:
raise ConnectionError("No connection available.") from err

# We now perform the connection check outside of the lock.
try:
await self.ensure_connection(connection)
return connection
except BaseException:
await self.release(connection)
raise

async def release(self, connection: AbstractConnection):
"""Releases the connection back to the pool."""
async with self._condition:
Expand Down

0 comments on commit 48eb0b4

Please sign in to comment.