Commit e438f864 authored by Julien Muchembled's avatar Julien Muchembled

client: a simple lock is enough for the connection pool

parent c319b065
...@@ -18,7 +18,7 @@ import time ...@@ -18,7 +18,7 @@ import time
from random import shuffle from random import shuffle
from neo.lib import logging from neo.lib import logging
from neo.lib.locking import RLock from neo.lib.locking import Lock
from neo.lib.protocol import NodeTypes, Packets from neo.lib.protocol import NodeTypes, Packets
from neo.lib.connection import MTClientConnection, ConnectionClosed from neo.lib.connection import MTClientConnection, ConnectionClosed
from neo.lib.exception import NodeNotReady from neo.lib.exception import NodeNotReady
...@@ -46,9 +46,7 @@ class ConnectionPool(object): ...@@ -46,9 +46,7 @@ class ConnectionPool(object):
# Define a lock in order to create one connection to # Define a lock in order to create one connection to
# a storage node at a time to avoid multiple connections # a storage node at a time to avoid multiple connections
# to the same node. # to the same node.
l = RLock() self._lock = Lock()
self.connection_lock_acquire = l.acquire
self.connection_lock_release = l.release
self.node_failure_dict = {} self.node_failure_dict = {}
def _initNodeConnection(self, node): def _initNodeConnection(self, node):
...@@ -142,8 +140,7 @@ class ConnectionPool(object): ...@@ -142,8 +140,7 @@ class ConnectionPool(object):
# Already connected to node # Already connected to node
return self.connection_dict[uuid] return self.connection_dict[uuid]
except KeyError: except KeyError:
self.connection_lock_acquire() with self._lock:
try:
# Second lookup, if another thread initiated connection # Second lookup, if another thread initiated connection
# while we were waiting for connection lock. # while we were waiting for connection lock.
try: try:
...@@ -156,9 +153,7 @@ class ConnectionPool(object): ...@@ -156,9 +153,7 @@ class ConnectionPool(object):
conn = self._initNodeConnection(node) conn = self._initNodeConnection(node)
if conn is not None: if conn is not None:
self.connection_dict[uuid] = conn self.connection_dict[uuid] = conn
return conn return conn
finally:
self.connection_lock_release()
def removeConnection(self, node): def removeConnection(self, node):
"""Explicitly remove connection when a node is broken.""" """Explicitly remove connection when a node is broken."""
......
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