Commit efd7f6f1 authored by Vincent Pelletier's avatar Vincent Pelletier

client.pool: Optimise getConnForNode for most likely execution path.

Connection is more often established than not, so do a first lookup without
locking, and only acquire it if it misses. Then do a second lookup in case
another thread also established connection, and connect if it still misses.
parent 8afbdd53
...@@ -143,21 +143,27 @@ class ConnectionPool(object): ...@@ -143,21 +143,27 @@ class ConnectionPool(object):
If no connection exists, create a new one""" If no connection exists, create a new one"""
if node.isRunning(): if node.isRunning():
uuid = node.getUUID() uuid = node.getUUID()
self.connection_lock_acquire()
try: try:
# Already connected to node # Already connected to node
return self.connection_dict[uuid] return self.connection_dict[uuid]
except KeyError: except KeyError:
if len(self.connection_dict) > self.max_pool_size: self.connection_lock_acquire()
# must drop some unused connections try:
self._dropConnections() # Second lookup, if another thread initiated connection
# Create new connection to node # while we were waiting for connection lock.
conn = self._initNodeConnection(node) try:
if conn is not None: return self.connection_dict[uuid]
self.connection_dict[uuid] = conn except KeyError:
return conn if len(self.connection_dict) > self.max_pool_size:
finally: # must drop some unused connections
self.connection_lock_release() self._dropConnections()
# Create new connection to node
conn = self._initNodeConnection(node)
if conn is not None:
self.connection_dict[uuid] = conn
return conn
finally:
self.connection_lock_release()
@profiler_decorator @profiler_decorator
def removeConnection(self, node): def removeConnection(self, node):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment