Commit b2ce833d authored by Julien Muchembled's avatar Julien Muchembled

client: small optimizations

parent 990d6337
...@@ -25,14 +25,8 @@ class BaseHandler(EventHandler): ...@@ -25,14 +25,8 @@ class BaseHandler(EventHandler):
self.dispatcher = app.dispatcher self.dispatcher = app.dispatcher
def dispatch(self, conn, packet, kw={}): def dispatch(self, conn, packet, kw={}):
# Before calling superclass's dispatch method, lock the connection. assert conn._lock._is_owned()
# This covers the case where handler sends a response to received super(BaseHandler, self).dispatch(conn, packet, kw)
# packet.
conn.lock()
try:
super(BaseHandler, self).dispatch(conn, packet, kw)
finally:
conn.release()
def packetReceived(self, conn, packet, kw={}): def packetReceived(self, conn, packet, kw={}):
"""Redirect all received packet to dispatcher thread.""" """Redirect all received packet to dispatcher thread."""
......
...@@ -133,7 +133,6 @@ class PrimaryNotificationsHandler(BaseHandler): ...@@ -133,7 +133,6 @@ class PrimaryNotificationsHandler(BaseHandler):
if node and node.isConnected(): if node and node.isConnected():
node.getConnection().close() node.getConnection().close()
class PrimaryAnswersHandler(AnswerBaseHandler): class PrimaryAnswersHandler(AnswerBaseHandler):
""" Handle that process expected packets from the primary master """ """ Handle that process expected packets from the primary master """
...@@ -141,7 +140,7 @@ class PrimaryAnswersHandler(AnswerBaseHandler): ...@@ -141,7 +140,7 @@ class PrimaryAnswersHandler(AnswerBaseHandler):
self.app.setHandlerData(ttid) self.app.setHandlerData(ttid)
def answerNewOIDs(self, conn, oid_list): def answerNewOIDs(self, conn, oid_list):
self.app.new_oid_list = list(oid_list) self.app.new_oid_list = oid_list
def answerTransactionFinished(self, conn, _, tid): def answerTransactionFinished(self, conn, _, tid):
self.app.setHandlerData(tid) self.app.setHandlerData(tid)
......
...@@ -728,36 +728,21 @@ class ServerConnection(Connection): ...@@ -728,36 +728,21 @@ class ServerConnection(Connection):
class MTClientConnection(ClientConnection): class MTClientConnection(ClientConnection):
"""A Multithread-safe version of ClientConnection.""" """A Multithread-safe version of ClientConnection."""
def __metaclass__(name, base, d):
for k in ('analyse', 'answer', 'checkTimeout',
'process', 'readable', 'writable'):
d[k] = lockCheckWrapper(getattr(base[0], k).im_func)
return type(name, base, d)
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
# _lock is only here for lock debugging purposes. Do not use. # _lock is only here for lock debugging purposes. Do not use.
self._lock = lock = RLock() self._lock = lock = RLock()
self.acquire = lock.acquire self.lock = lock.acquire
self.release = lock.release self.unlock = lock.release
self.dispatcher = kwargs.pop('dispatcher') self.dispatcher = kwargs.pop('dispatcher')
self.dispatcher.needPollThread() self.dispatcher.needPollThread()
self.lock() with lock:
try:
super(MTClientConnection, self).__init__(*args, **kwargs) super(MTClientConnection, self).__init__(*args, **kwargs)
finally:
self.unlock()
def lock(self, blocking = 1):
return self.acquire(blocking = blocking)
def unlock(self):
self.release()
@lockCheckWrapper
def writable(self, *args, **kw):
return super(MTClientConnection, self).writable(*args, **kw)
@lockCheckWrapper
def readable(self, *args, **kw):
return super(MTClientConnection, self).readable(*args, **kw)
@lockCheckWrapper
def analyse(self, *args, **kw):
return super(MTClientConnection, self).analyse(*args, **kw)
def notify(self, *args, **kw): def notify(self, *args, **kw):
self.lock() self.lock()
...@@ -793,22 +778,9 @@ class MTClientConnection(ClientConnection): ...@@ -793,22 +778,9 @@ class MTClientConnection(ClientConnection):
finally: finally:
self.unlock() self.unlock()
@lockCheckWrapper
def answer(self, *args, **kw):
return super(MTClientConnection, self).answer(*args, **kw)
@lockCheckWrapper
def checkTimeout(self, *args, **kw):
return super(MTClientConnection, self).checkTimeout(*args, **kw)
def close(self): def close(self):
self.lock() self.lock()
try: try:
super(MTClientConnection, self).close() super(MTClientConnection, self).close()
finally: finally:
self.release() self.unlock()
@lockCheckWrapper
def process(self, *args, **kw):
return super(MTClientConnection, self).process(*args, **kw)
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