summaryrefslogtreecommitdiff
path: root/redis/connection.py
diff options
context:
space:
mode:
authorAndy McCurdy <andy@andymccurdy.com>2020-05-14 14:16:14 -0700
committerAndy McCurdy <andy@andymccurdy.com>2020-05-14 14:16:14 -0700
commit32a58406b2769f423170142e21d164c863697ebc (patch)
tree4f59a4a5e119c768440803324d940d7e45d6303e /redis/connection.py
parentfa37b8ba7e69c83d0dcd144b6463621d917b5f13 (diff)
downloadredis-py-32a58406b2769f423170142e21d164c863697ebc.tar.gz
Tune the locking in ConnectionPool.get_connection
The lock does not need to be held while waiting for the socket to establish and validate the TCP connection.
Diffstat (limited to 'redis/connection.py')
-rwxr-xr-xredis/connection.py43
1 files changed, 22 insertions, 21 deletions
diff --git a/redis/connection.py b/redis/connection.py
index 28620c3..9781b8c 100755
--- a/redis/connection.py
+++ b/redis/connection.py
@@ -1098,7 +1098,7 @@ class ConnectionPool(object):
)
def reset(self):
- self._lock = threading.RLock()
+ self._lock = threading.Lock()
self._created_connections = 0
self._available_connections = []
self._in_use_connections = set()
@@ -1177,28 +1177,29 @@ class ConnectionPool(object):
except IndexError:
connection = self.make_connection()
self._in_use_connections.add(connection)
+
+ try:
+ # ensure this connection is connected to Redis
+ connection.connect()
+ # connections that the pool provides should be ready to send
+ # a command. if not, the connection was either returned to the
+ # pool before all data has been read or the socket has been
+ # closed. either way, reconnect and verify everything is good.
try:
- # ensure this connection is connected to Redis
+ if connection.can_read():
+ raise ConnectionError('Connection has data')
+ except ConnectionError:
+ connection.disconnect()
connection.connect()
- # connections that the pool provides should be ready to send
- # a command. if not, the connection was either returned to the
- # pool before all data has been read or the socket has been
- # closed. either way, reconnect and verify everything is good.
- try:
- if connection.can_read():
- raise ConnectionError('Connection has data')
- except ConnectionError:
- connection.disconnect()
- connection.connect()
- if connection.can_read():
- raise ConnectionError('Connection not ready')
- except BaseException:
- # release the connection back to the pool so that we don't
- # leak it
- self.release(connection)
- raise
-
- return connection
+ if connection.can_read():
+ raise ConnectionError('Connection not ready')
+ except BaseException:
+ # release the connection back to the pool so that we don't
+ # leak it
+ self.release(connection)
+ raise
+
+ return connection
def get_encoder(self):
"Return an encoder based on encoding settings"