@@ -1027,21 +1027,25 @@ def can_get_connection(self) -> bool:
1027
1027
)
1028
1028
1029
1029
async def get_connection (self , command_name , * keys , ** options ):
1030
- """Get a connection from the pool"""
1030
+ """Get a connected connection from the pool"""
1031
+ connection = self .get_available_connection ()
1032
+ try :
1033
+ await self .ensure_connection (connection )
1034
+ except BaseException :
1035
+ await self .release (connection )
1036
+ raise
1037
+
1038
+ return connection
1039
+
1040
+ def get_available_connection (self ):
1041
+ """Get a connection from the pool, without making sure it is connected"""
1031
1042
try :
1032
1043
connection = self ._available_connections .pop ()
1033
1044
except IndexError :
1034
1045
if len (self ._in_use_connections ) >= self .max_connections :
1035
1046
raise ConnectionError ("Too many connections" ) from None
1036
1047
connection = self .make_connection ()
1037
1048
self ._in_use_connections .add (connection )
1038
-
1039
- try :
1040
- await self .ensure_connection (connection )
1041
- except BaseException :
1042
- await self .release (connection )
1043
- raise
1044
-
1045
1049
return connection
1046
1050
1047
1051
def get_encoder (self ):
@@ -1169,10 +1173,19 @@ async def get_connection(self, command_name, *keys, **options):
1169
1173
async with async_timeout (self .timeout ):
1170
1174
async with self ._condition :
1171
1175
await self ._condition .wait_for (self .can_get_connection )
1172
- return await super ().get_connection (command_name , * keys , ** options )
1176
+ connection = super ().get_available_connection ()
1177
+
1173
1178
except asyncio .TimeoutError as err :
1174
1179
raise ConnectionError ("No connection available." ) from err
1175
1180
1181
+ # We now perform the connection check outside of the lock.
1182
+ try :
1183
+ await self .ensure_connection (connection )
1184
+ return connection
1185
+ except BaseException :
1186
+ await self .release (connection )
1187
+ raise
1188
+
1176
1189
async def release (self , connection : AbstractConnection ):
1177
1190
"""Releases the connection back to the pool."""
1178
1191
async with self ._condition :
0 commit comments