Skip to content

Commit b886731

Browse files
committed
Refactor how asyncio.BlockingConnectionPool gets connection.
1 parent 5391c5f commit b886731

File tree

1 file changed

+4
-12
lines changed

1 file changed

+4
-12
lines changed

redis/asyncio/connection.py

+4-12
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,7 @@ def can_get_connection(self) -> bool:
10281028

10291029
async def get_connection(self, command_name, *keys, **options):
10301030
"""Get a connected connection from the pool"""
1031-
connection = self.get_available_connection()
1031+
connection = await self.get_available_connection()
10321032
try:
10331033
await self.ensure_connection(connection)
10341034
except BaseException:
@@ -1037,7 +1037,7 @@ async def get_connection(self, command_name, *keys, **options):
10371037

10381038
return connection
10391039

1040-
def get_available_connection(self):
1040+
async def get_available_connection(self):
10411041
"""Get a connection from the pool, without making sure it is connected"""
10421042
try:
10431043
connection = self._available_connections.pop()
@@ -1167,24 +1167,16 @@ def __init__(
11671167
self._condition = asyncio.Condition()
11681168
self.timeout = timeout
11691169

1170-
async def get_connection(self, command_name, *keys, **options):
1170+
async def get_available_connection(self):
11711171
"""Gets a connection from the pool, blocking until one is available"""
11721172
try:
11731173
async with self._condition:
11741174
async with async_timeout(self.timeout):
11751175
await self._condition.wait_for(self.can_get_connection)
1176-
connection = super().get_available_connection()
1176+
return await super().get_available_connection()
11771177
except asyncio.TimeoutError as err:
11781178
raise ConnectionError("No connection available.") from err
11791179

1180-
# We now perform the connection check outside of the lock.
1181-
try:
1182-
await self.ensure_connection(connection)
1183-
return connection
1184-
except BaseException:
1185-
await self.release(connection)
1186-
raise
1187-
11881180
async def release(self, connection: AbstractConnection):
11891181
"""Releases the connection back to the pool."""
11901182
async with self._condition:

0 commit comments

Comments
 (0)