Skip to content

Commit d3983fc

Browse files
committed
Remove redundant async locking from the ConnectionPool class
1 parent a422b16 commit d3983fc

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

redis/asyncio/connection.py

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,6 @@ def __init__(
998998
self.connection_kwargs = connection_kwargs
999999
self.max_connections = max_connections
10001000

1001-
self._lock = asyncio.Lock()
10021001
self._created_connections: int
10031002
self._available_connections: List[AbstractConnection]
10041003
self._in_use_connections: Set[AbstractConnection]
@@ -1012,19 +1011,17 @@ def __repr__(self):
10121011
)
10131012

10141013
def reset(self):
1015-
self._lock = asyncio.Lock()
10161014
self._created_connections = 0
10171015
self._available_connections = []
10181016
self._in_use_connections = set()
10191017

10201018
async def get_connection(self, command_name, *keys, **options):
10211019
"""Get a connection from the pool"""
1022-
async with self._lock:
1023-
try:
1024-
connection = self._available_connections.pop()
1025-
except IndexError:
1026-
connection = self.make_connection()
1027-
self._in_use_connections.add(connection)
1020+
try:
1021+
connection = self._available_connections.pop()
1022+
except IndexError:
1023+
connection = self.make_connection()
1024+
self._in_use_connections.add(connection)
10281025

10291026
try:
10301027
# ensure this connection is connected to Redis
@@ -1067,11 +1064,10 @@ def make_connection(self):
10671064

10681065
async def release(self, connection: AbstractConnection):
10691066
"""Releases the connection back to the pool"""
1070-
async with self._lock:
1071-
# Connections should always be returned to the correct pool,
1072-
# not doing so is an error that will cause an exception here.
1073-
self._in_use_connections.remove(connection)
1074-
self._available_connections.append(connection)
1067+
# Connections should always be returned to the correct pool,
1068+
# not doing so is an error that will cause an exception here.
1069+
self._in_use_connections.remove(connection)
1070+
self._available_connections.append(connection)
10751071

10761072
async def disconnect(self, inuse_connections: bool = True):
10771073
"""
@@ -1081,20 +1077,19 @@ async def disconnect(self, inuse_connections: bool = True):
10811077
current in use, potentially by other tasks. Otherwise only disconnect
10821078
connections that are idle in the pool.
10831079
"""
1084-
async with self._lock:
1085-
if inuse_connections:
1086-
connections: Iterable[AbstractConnection] = chain(
1087-
self._available_connections, self._in_use_connections
1088-
)
1089-
else:
1090-
connections = self._available_connections
1091-
resp = await asyncio.gather(
1092-
*(connection.disconnect() for connection in connections),
1093-
return_exceptions=True,
1080+
if inuse_connections:
1081+
connections: Iterable[AbstractConnection] = chain(
1082+
self._available_connections, self._in_use_connections
10941083
)
1095-
exc = next((r for r in resp if isinstance(r, BaseException)), None)
1096-
if exc:
1097-
raise exc
1084+
else:
1085+
connections = self._available_connections
1086+
resp = await asyncio.gather(
1087+
*(connection.disconnect() for connection in connections),
1088+
return_exceptions=True,
1089+
)
1090+
exc = next((r for r in resp if isinstance(r, BaseException)), None)
1091+
if exc:
1092+
raise exc
10981093

10991094
def set_retry(self, retry: "Retry") -> None:
11001095
for conn in self._available_connections:
@@ -1154,6 +1149,7 @@ def __init__(
11541149
max_connections=max_connections,
11551150
**connection_kwargs,
11561151
)
1152+
self._lock = asyncio.Lock()
11571153

11581154
def reset(self):
11591155
# Create and fill up a queue with ``None`` values.

0 commit comments

Comments
 (0)